blob: 7481d92618abf2213526149b41b5e7b6130fed40 [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"
Andreas Gampe513061a2017-06-01 09:17:34 -070022#include "debugger.h"
Ian Rogers848871b2013-08-05 10:56:33 -070023#include "dex_file-inl.h"
Andreas Gampee2abbc62017-09-15 11:59:26 -070024#include "dex_file_types.h"
Ian Rogers848871b2013-08-05 10:56:33 -070025#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070026#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070027#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070028#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070029#include "imt_conflict_table.h"
30#include "imtable-inl.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070031#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070032#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000033#include "linear_alloc.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010034#include "method_bss_mapping.h"
Orion Hodsonac141392017-01-13 11:53:47 +000035#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080036#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070037#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070038#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070039#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000040#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070041#include "mirror/object-inl.h"
42#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010043#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010044#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070045#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070046#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070047#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070048#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070049#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000050#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070051
52namespace art {
53
Andreas Gampe8228cdf2017-05-30 15:03:54 -070054// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070055class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080056 // Number of bytes for each out register in the caller method's frame.
57 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070058 // Frame size in bytes of a callee-save frame for RefsAndArgs.
59 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070060 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070061#if defined(__arm__)
62 // The callee save frame is pointed to by SP.
63 // | argN | |
64 // | ... | |
65 // | arg4 | |
66 // | arg3 spill | | Caller's frame
67 // | arg2 spill | |
68 // | arg1 spill | |
69 // | Method* | ---
70 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080071 // | ... | 4x6 bytes callee saves
72 // | R3 |
73 // | R2 |
74 // | R1 |
75 // | S15 |
76 // | : |
77 // | S0 |
78 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070079 // | Method* | <- sp
Andreas Gampe217d6d32017-09-18 12:48:20 -070080 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
81 static constexpr bool kAlignPairRegister = true;
82 static constexpr bool kQuickSoftFloatAbi = false;
83 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = true;
Goran Jakovljevicff734982015-08-24 12:58:55 +000084 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080085 static constexpr size_t kNumQuickGprArgs = 3;
Andreas Gampe217d6d32017-09-18 12:48:20 -070086 static constexpr size_t kNumQuickFprArgs = 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080087 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080088 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070089 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080090 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070091 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080092 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070093 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080094 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000095 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080096 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000097#elif defined(__aarch64__)
98 // The callee save frame is pointed to by SP.
99 // | argN | |
100 // | ... | |
101 // | arg4 | |
102 // | arg3 spill | | Caller's frame
103 // | arg2 spill | |
104 // | arg1 spill | |
105 // | Method* | ---
106 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800107 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100109 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000110 // | X7 |
111 // | : |
112 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800113 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000114 // | : |
115 // | D0 |
116 // | | padding
117 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500118 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000119 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000120 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800121 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000122 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000123 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
124 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800125 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700126 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800127 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700128 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
129 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800130 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700131 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
132 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800133 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700134 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000135 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000136 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000137 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800138#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700139 // The callee save frame is pointed to by SP.
140 // | argN | |
141 // | ... | |
142 // | arg4 | |
143 // | arg3 spill | | Caller's frame
144 // | arg2 spill | |
145 // | arg1 spill | |
146 // | Method* | ---
147 // | RA |
148 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800149 // | T1 | arg5
150 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700151 // | A3 | arg3
152 // | A2 | arg2
153 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800154 // | F19 |
155 // | F18 | f_arg5
156 // | F17 |
157 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000158 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800159 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000160 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800161 // | F12 | f_arg2
162 // | F11 |
163 // | F10 | f_arg1
164 // | F9 |
165 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000166 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700167 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000168 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
169 static constexpr bool kAlignPairRegister = true;
170 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800171 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000172 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800173 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
174 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
175 // passed only in even numbered registers and each
176 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800177 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
180 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
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
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800222 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800223 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
224 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
225 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
226 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
227 }
Ian Rogers848871b2013-08-05 10:56:33 -0700228#elif defined(__i386__)
229 // The callee save frame is pointed to by SP.
230 // | argN | |
231 // | ... | |
232 // | arg4 | |
233 // | arg3 spill | | Caller's frame
234 // | arg2 spill | |
235 // | arg1 spill | |
236 // | Method* | ---
237 // | Return |
238 // | EBP,ESI,EDI | callee saves
239 // | EBX | arg3
240 // | EDX | arg2
241 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000242 // | XMM3 | float arg 4
243 // | XMM2 | float arg 3
244 // | XMM1 | float arg 2
245 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700246 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500247 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000248 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000249 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800250 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000251 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800252 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000253 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800254 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000255 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
256 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
257 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000259 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800261#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800262 // The callee save frame is pointed to by SP.
263 // | argN | |
264 // | ... | |
265 // | reg. arg spills | | Caller's frame
266 // | Method* | ---
267 // | Return |
268 // | R15 | callee save
269 // | R14 | callee save
270 // | R13 | callee save
271 // | R12 | callee save
272 // | R9 | arg5
273 // | R8 | arg4
274 // | RSI/R6 | arg1
275 // | RBP/R5 | callee save
276 // | RBX/R3 | callee save
277 // | RDX/R2 | arg2
278 // | RCX/R1 | arg3
279 // | XMM7 | float arg 8
280 // | XMM6 | float arg 7
281 // | XMM5 | float arg 6
282 // | XMM4 | float arg 5
283 // | XMM3 | float arg 4
284 // | XMM2 | float arg 3
285 // | XMM1 | float arg 2
286 // | XMM0 | float arg 1
287 // | Padding |
288 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500289 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000290 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800291 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800292 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000293 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700294 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700295 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800296 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800297 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700298 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
299 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800300 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
301 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000302 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
303 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
304 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
305 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
306 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800307 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700308 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
309 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800310 }
311 }
Ian Rogers848871b2013-08-05 10:56:33 -0700312#else
313#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700314#endif
315
Ian Rogers936b37f2014-02-14 00:52:24 -0800316 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100317 // Special handling for proxy methods. Proxy methods are instance methods so the
318 // 'this' object is the 1st argument. They also have the same frame layout as the
319 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
320 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700321 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700322 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000323 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100324 CHECK_GT(kNumQuickGprArgs, 0u);
325 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
326 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
327 GprIndexToGprOffset(kThisGprIndex);
328 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
329 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
330 }
331
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700332 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700333 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700334 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100335 }
336
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700337 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700338 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100339 uint8_t* previous_sp =
340 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700341 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100342 }
343
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700344 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700345 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700346 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
347 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700348 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
349 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100350 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100351 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
352 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100353
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100354 if (current_code->IsOptimized()) {
355 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000356 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100357 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100358 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800359 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100360 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800361 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
362 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100363 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800364 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100365 }
366 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100367 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100368 }
Ian Rogers848871b2013-08-05 10:56:33 -0700369 }
370
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800371 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
372 REQUIRES_SHARED(Locks::mutator_lock_) {
373 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700374 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
375 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800376 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
377 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
378 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
379 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
380 if (!current_code->IsOptimized()) {
381 return false;
382 }
383 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
384 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
385 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700386 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800387 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
388 if (invoke.IsValid()) {
389 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700390 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800391 return true;
392 }
393 return false;
394 }
395
Ian Rogers936b37f2014-02-14 00:52:24 -0800396 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700397 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700398 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700399 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700400 return *reinterpret_cast<uintptr_t*>(lr);
401 }
402
Mathieu Chartiere401d142015-04-22 13:56:20 -0700403 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700404 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700405 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700406 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
407 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
408 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700409 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800410 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
411 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800412 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
413 "Number of Quick FPR arguments unexpected");
414 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
415 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800416 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
417 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800418 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
419 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700420 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800421 }
Ian Rogers848871b2013-08-05 10:56:33 -0700422
423 virtual ~QuickArgumentVisitor() {}
424
425 virtual void Visit() = 0;
426
Ian Rogers936b37f2014-02-14 00:52:24 -0800427 Primitive::Type GetParamPrimitiveType() const {
428 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700429 }
430
Ian Rogers13735952014-10-08 12:43:28 -0700431 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800432 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800433 Primitive::Type type = GetParamPrimitiveType();
434 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800435 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
436 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
437 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
438 }
439 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000440 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800441 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700442 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800443 }
444 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800445 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800446 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
447 }
448 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700449 }
450
451 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700452 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
453 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800454 return is_split_long_or_double_;
455 } else {
456 return false; // An optimization for when GPR and FPRs are 64bit.
457 }
Ian Rogers848871b2013-08-05 10:56:33 -0700458 }
459
Ian Rogers936b37f2014-02-14 00:52:24 -0800460 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700461 return GetParamPrimitiveType() == Primitive::kPrimNot;
462 }
463
Ian Rogers936b37f2014-02-14 00:52:24 -0800464 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700465 Primitive::Type type = GetParamPrimitiveType();
466 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
467 }
468
469 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000470 // The splitted long is always available through the stack.
471 return *reinterpret_cast<uint64_t*>(stack_args_
472 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700473 }
474
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800475 void IncGprIndex() {
476 gpr_index_++;
477 if (kGprFprLockstep) {
478 fpr_index_++;
479 }
480 }
481
482 void IncFprIndex() {
483 fpr_index_++;
484 if (kGprFprLockstep) {
485 gpr_index_++;
486 }
487 }
488
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700489 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800490 // (a) 'stack_args_' should point to the first method's argument
491 // (b) whatever the argument type it is, the 'stack_index_' should
492 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800493 gpr_index_ = 0;
494 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800495 if (kQuickDoubleRegAlignedFloatBackFilled) {
496 fpr_double_index_ = 0;
497 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800498 stack_index_ = 0;
499 if (!is_static_) { // Handle this.
500 cur_type_ = Primitive::kPrimNot;
501 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700502 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800503 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800504 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800505 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800506 }
Ian Rogers848871b2013-08-05 10:56:33 -0700507 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800508 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
509 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
510 switch (cur_type_) {
511 case Primitive::kPrimNot:
512 case Primitive::kPrimBoolean:
513 case Primitive::kPrimByte:
514 case Primitive::kPrimChar:
515 case Primitive::kPrimShort:
516 case Primitive::kPrimInt:
517 is_split_long_or_double_ = false;
518 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800519 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800520 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800521 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800522 }
523 break;
524 case Primitive::kPrimFloat:
525 is_split_long_or_double_ = false;
526 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800527 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800528 if (kQuickSoftFloatAbi) {
529 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800530 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800531 }
532 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800533 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800534 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800535 if (kQuickDoubleRegAlignedFloatBackFilled) {
536 // Double should not overlap with float.
537 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
538 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
539 // Float should not overlap with double.
540 if (fpr_index_ % 2 == 0) {
541 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
542 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000543 } else if (kQuickSkipOddFpRegisters) {
544 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800545 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800546 }
547 }
548 break;
549 case Primitive::kPrimDouble:
550 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800551 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800552 if (cur_type_ == Primitive::kPrimLong &&
553#if defined(__mips__) && !defined(__LP64__)
554 (gpr_index_ == 0 || gpr_index_ == 2) &&
555#else
556 gpr_index_ == 0 &&
557#endif
558 kAlignPairRegister) {
559 // Currently, this is only for ARM and MIPS, where we align long parameters with
560 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
561 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800562 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000563 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000564 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800565 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500566 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
567 // We don't want to split this. Pass over this register.
568 gpr_index_++;
569 is_split_long_or_double_ = false;
570 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800571 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800572 if (kBytesStackArgLocation == 4) {
573 stack_index_+= 2;
574 } else {
575 CHECK_EQ(kBytesStackArgLocation, 8U);
576 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800577 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700578 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800579 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000580 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700581 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800582 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700583 }
584 }
585 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800586 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000587 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800588 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800589 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700590 if (kBytesStackArgLocation == 4) {
591 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800592 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700593 CHECK_EQ(kBytesStackArgLocation, 8U);
594 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800595 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800596 if (kQuickDoubleRegAlignedFloatBackFilled) {
597 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
598 fpr_double_index_ += 2;
599 // Float should not overlap with double.
600 if (fpr_index_ % 2 == 0) {
601 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
602 }
603 }
604 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800605 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800606 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
607 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800608 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800609 }
610 }
611 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800612 }
613 break;
614 default:
615 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
616 }
Ian Rogers848871b2013-08-05 10:56:33 -0700617 }
618 }
619
Andreas Gampec200a4a2014-06-16 18:39:09 -0700620 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700621 const bool is_static_;
622 const char* const shorty_;
623 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700624
625 private:
Ian Rogers13735952014-10-08 12:43:28 -0700626 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
627 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
628 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800629 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800630 // Index into spilled FPRs.
631 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
632 // holds a higher register number.
633 uint32_t fpr_index_;
634 // Index into spilled FPRs for aligned double.
635 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
636 // terms of singles, may be behind fpr_index.
637 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800638 uint32_t stack_index_; // Index into arguments on the stack.
639 // The current type of argument during VisitArguments.
640 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700641 // Does a 64bit parameter straddle the register and stack arguments?
642 bool is_split_long_or_double_;
643};
644
Sebastien Hertza836bc92014-11-25 16:30:53 +0100645// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
646// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700647extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700648 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100649 return QuickArgumentVisitor::GetProxyThisObject(sp);
650}
651
Ian Rogers848871b2013-08-05 10:56:33 -0700652// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800653class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700654 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700655 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
656 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700657 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700658
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700659 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700660
661 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800662 ShadowFrame* const sf_;
663 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700664
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700665 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700666};
667
Andreas Gampec200a4a2014-06-16 18:39:09 -0700668void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700669 Primitive::Type type = GetParamPrimitiveType();
670 switch (type) {
671 case Primitive::kPrimLong: // Fall-through.
672 case Primitive::kPrimDouble:
673 if (IsSplitLongOrDouble()) {
674 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
675 } else {
676 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
677 }
678 ++cur_reg_;
679 break;
680 case Primitive::kPrimNot: {
681 StackReference<mirror::Object>* stack_ref =
682 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
683 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
684 }
685 break;
686 case Primitive::kPrimBoolean: // Fall-through.
687 case Primitive::kPrimByte: // Fall-through.
688 case Primitive::kPrimChar: // Fall-through.
689 case Primitive::kPrimShort: // Fall-through.
690 case Primitive::kPrimInt: // Fall-through.
691 case Primitive::kPrimFloat:
692 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
693 break;
694 case Primitive::kPrimVoid:
695 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700696 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700697 }
698 ++cur_reg_;
699}
700
Mathieu Chartiere401d142015-04-22 13:56:20 -0700701extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700702 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700703 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
704 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700705 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700706
Alex Light9139e002015-10-09 15:59:48 -0700707 if (UNLIKELY(!method->IsInvokable())) {
708 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700709 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700710 }
711
712 JValue tmp_value;
713 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700714 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700715 ManagedStack fragment;
716
David Sehr709b0702016-10-13 09:12:37 -0700717 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700718 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700719 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700720 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700721 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700722 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
723
724 JValue result;
725
726 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700727 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700728
729 if (kIsDebugBuild) {
730 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
731 // of the call-stack) corresponds to the called method.
732 ShadowFrame* linked = deopt_frame;
733 while (linked->GetLink() != nullptr) {
734 linked = linked->GetLink();
735 }
David Sehr709b0702016-10-13 09:12:37 -0700736 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
737 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700738 }
739
740 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700741 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700742 LOG(INFO) << "Continue-ing from deopt. Stack is:";
743 QuickExceptionHandler::DumpFramesWithType(self, true);
744 }
745
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800746 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100747 bool from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700748 DeoptimizationMethodType method_type;
749 self->PopDeoptimizationContext(/* out */ &result,
750 /* out */ &pending_exception,
751 /* out */ &from_code,
752 /* out */ &method_type);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700753
754 // Push a transition back into managed code onto the linked list in thread.
755 self->PushManagedStackFragment(&fragment);
756
757 // Ensure that the stack is still in order.
758 if (kIsDebugBuild) {
759 class DummyStackVisitor : public StackVisitor {
760 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700761 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700762 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
763
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700764 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700765 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
766 // logic. Just always say we want to continue.
767 return true;
768 }
769 };
770 DummyStackVisitor dsv(self);
771 dsv.WalkStack();
772 }
773
774 // Restore the exception that was pending before deoptimization then interpret the
775 // deoptimized frames.
776 if (pending_exception != nullptr) {
777 self->SetException(pending_exception);
778 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700779 interpreter::EnterInterpreterFromDeoptimize(self,
780 deopt_frame,
781 &result,
782 from_code,
783 DeoptimizationMethodType::kDefault);
Ian Rogers848871b2013-08-05 10:56:33 -0700784 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700785 const char* old_cause = self->StartAssertNoThreadSuspension(
786 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700787 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700788 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700789 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700790 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700791 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700792 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700793 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800794 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700795 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800796 const bool needs_initialization =
797 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700798 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700799 self->PushManagedStackFragment(&fragment);
800 self->PushShadowFrame(shadow_frame);
801 self->EndAssertNoThreadSuspension(old_cause);
802
Ian Rogerse94652f2014-12-02 11:13:19 -0800803 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700804 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800805 StackHandleScope<1> hs(self);
806 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700807 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700808 DCHECK(Thread::Current()->IsExceptionPending())
809 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700810 self->PopManagedStackFragment(fragment);
811 return 0;
812 }
813 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200814
Andreas Gampe639bdd12015-06-03 11:22:45 -0700815 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700816 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700817
818 // Pop transition.
819 self->PopManagedStackFragment(fragment);
820
821 // Request a stack deoptimization if needed
822 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700823 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700824 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
825 // should be done and it knows the real return pc.
826 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000827 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
828 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
829 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
830 << caller->PrettyMethod();
831 } else {
832 // Push the context of the deoptimization stack so we can restore the return value and the
833 // exception before executing the deoptimized frames.
834 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700835 result,
836 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
837 self->GetException(),
838 false /* from_code */,
839 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700840
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000841 // Set special exception to cause deoptimization.
842 self->SetException(Thread::GetDeoptimizationException());
843 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700844 }
845
846 // No need to restore the args since the method has already been run by the interpreter.
847 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700848}
849
850// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
851// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800852class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700853 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700854 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700855 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700856 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700857
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700858 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700859
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700860 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800861
Ian Rogers848871b2013-08-05 10:56:33 -0700862 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700863 ScopedObjectAccessUnchecked* const soa_;
864 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800865 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700866 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700867
Ian Rogers848871b2013-08-05 10:56:33 -0700868 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
869};
870
Ian Rogers9758f792014-03-13 09:02:55 -0700871void BuildQuickArgumentVisitor::Visit() {
872 jvalue val;
873 Primitive::Type type = GetParamPrimitiveType();
874 switch (type) {
875 case Primitive::kPrimNot: {
876 StackReference<mirror::Object>* stack_ref =
877 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
878 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
879 references_.push_back(std::make_pair(val.l, stack_ref));
880 break;
881 }
882 case Primitive::kPrimLong: // Fall-through.
883 case Primitive::kPrimDouble:
884 if (IsSplitLongOrDouble()) {
885 val.j = ReadSplitLongParam();
886 } else {
887 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
888 }
889 break;
890 case Primitive::kPrimBoolean: // Fall-through.
891 case Primitive::kPrimByte: // Fall-through.
892 case Primitive::kPrimChar: // Fall-through.
893 case Primitive::kPrimShort: // Fall-through.
894 case Primitive::kPrimInt: // Fall-through.
895 case Primitive::kPrimFloat:
896 val.i = *reinterpret_cast<jint*>(GetParamAddress());
897 break;
898 case Primitive::kPrimVoid:
899 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700900 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700901 }
902 args_->push_back(val);
903}
904
905void BuildQuickArgumentVisitor::FixupReferences() {
906 // Fixup any references which may have changed.
907 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700908 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700909 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700910 }
911}
Ian Rogers848871b2013-08-05 10:56:33 -0700912// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
913// which is responsible for recording callee save registers. We explicitly place into jobjects the
914// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
915// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700916extern "C" uint64_t artQuickProxyInvokeHandler(
917 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700918 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700919 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
920 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700921 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
922 const char* old_cause =
923 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
924 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700925 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700926 self->VerifyStack();
927 // Start new JNI local reference state.
928 JNIEnvExt* env = self->GetJniEnv();
929 ScopedObjectAccessUnchecked soa(env);
930 ScopedJniEnvLocalRefState env_state(env);
931 // Create local ref. copies of proxy method and the receiver.
932 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
933
934 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700935 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700936 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
937 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700938 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700939 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700940 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700941 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700942
Ian Rogers848871b2013-08-05 10:56:33 -0700943 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700944 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700945 args.erase(args.begin());
946
947 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700948 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700949 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
950 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700951 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700952 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700953 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700954 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700955 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
956 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700957
958 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
959 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700960 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800961 // Restore references which might have moved.
962 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700963 return result.GetJ();
964}
965
966// Read object references held in arguments from quick frames and place in a JNI local references,
967// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800968class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700969 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700970 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
971 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700972 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700973
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700974 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700975
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700976 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700977
978 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700979 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800980 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700981 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
982
Mathieu Chartier590fee92013-09-13 13:46:47 -0700983 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700984};
985
Ian Rogers9758f792014-03-13 09:02:55 -0700986void RememberForGcArgumentVisitor::Visit() {
987 if (IsParamAReference()) {
988 StackReference<mirror::Object>* stack_ref =
989 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
990 jobject reference =
991 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
992 references_.push_back(std::make_pair(reference, stack_ref));
993 }
994}
995
996void RememberForGcArgumentVisitor::FixupReferences() {
997 // Fixup any references which may have changed.
998 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700999 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001000 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001001 }
1002}
1003
Alex Lightb7edcda2017-04-27 13:20:31 -07001004extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1005 mirror::Object* this_object,
1006 Thread* self,
1007 ArtMethod** sp)
1008 REQUIRES_SHARED(Locks::mutator_lock_) {
1009 const void* result;
1010 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1011 // that part.
1012 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1013 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1014 if (instrumentation->IsDeoptimized(method)) {
1015 result = GetQuickToInterpreterBridge();
1016 } else {
1017 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1018 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1019 }
1020
1021 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1022 bool is_static = method->IsStatic();
1023 uint32_t shorty_len;
1024 const char* shorty =
1025 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1026
1027 ScopedObjectAccessUnchecked soa(self);
1028 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1029 visitor.VisitArguments();
1030
1031 instrumentation->PushInstrumentationStackFrame(self,
1032 is_static ? nullptr : this_object,
1033 method,
1034 QuickArgumentVisitor::GetCallingPc(sp),
1035 interpreter_entry);
1036
1037 visitor.FixupReferences();
1038 if (UNLIKELY(self->IsExceptionPending())) {
1039 return nullptr;
1040 }
1041 CHECK(result != nullptr) << method->PrettyMethod();
1042 return result;
1043}
1044
1045extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1046 ArtMethod** sp,
1047 uint64_t* gpr_result,
1048 uint64_t* fpr_result)
1049 REQUIRES_SHARED(Locks::mutator_lock_) {
1050 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1051 CHECK(gpr_result != nullptr);
1052 CHECK(fpr_result != nullptr);
1053 // Instrumentation exit stub must not be entered with a pending exception.
1054 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1055 << self->GetException()->Dump();
1056 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001057 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1058 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001059 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1060 return_pc_offset);
1061 CHECK_EQ(*return_pc, 0U);
1062
1063 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1064 // deoptimization shouldn't be performed with the high-half having the return address. When
1065 // deoptimization should be performed the low half is zero and the high-half the address of the
1066 // deoptimization entry point.
1067 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1068 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1069 self, return_pc, gpr_result, fpr_result);
1070 if (self->IsExceptionPending()) {
1071 return GetTwoWordFailureValue();
1072 }
1073 return return_or_deoptimize_pc;
1074}
1075
Ian Rogers848871b2013-08-05 10:56:33 -07001076// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001077extern "C" const void* artQuickResolutionTrampoline(
1078 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001079 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001080 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1081 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1082 // does not have the same stack layout as the callee-save method).
1083 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001084 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001085 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001086 ScopedObjectAccessUnchecked soa(env);
1087 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001088 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001089
1090 // Compute details about the called method (avoid GCs)
1091 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001092 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001093 MethodReference called_method(nullptr, 0);
1094 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001095 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001096 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001097 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001098 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001099
1100 InvokeType stack_map_invoke_type;
1101 uint32_t stack_map_dex_method_idx;
1102 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1103 &stack_map_invoke_type,
1104 &stack_map_dex_method_idx);
1105 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1106 // code.
1107 if (!found_stack_map || kIsDebugBuild) {
1108 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1109 const DexFile::CodeItem* code;
1110 code = caller->GetCodeItem();
1111 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1112 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1113 Instruction::Code instr_code = instr->Opcode();
1114 bool is_range;
1115 switch (instr_code) {
1116 case Instruction::INVOKE_DIRECT:
1117 invoke_type = kDirect;
1118 is_range = false;
1119 break;
1120 case Instruction::INVOKE_DIRECT_RANGE:
1121 invoke_type = kDirect;
1122 is_range = true;
1123 break;
1124 case Instruction::INVOKE_STATIC:
1125 invoke_type = kStatic;
1126 is_range = false;
1127 break;
1128 case Instruction::INVOKE_STATIC_RANGE:
1129 invoke_type = kStatic;
1130 is_range = true;
1131 break;
1132 case Instruction::INVOKE_SUPER:
1133 invoke_type = kSuper;
1134 is_range = false;
1135 break;
1136 case Instruction::INVOKE_SUPER_RANGE:
1137 invoke_type = kSuper;
1138 is_range = true;
1139 break;
1140 case Instruction::INVOKE_VIRTUAL:
1141 invoke_type = kVirtual;
1142 is_range = false;
1143 break;
1144 case Instruction::INVOKE_VIRTUAL_RANGE:
1145 invoke_type = kVirtual;
1146 is_range = true;
1147 break;
1148 case Instruction::INVOKE_INTERFACE:
1149 invoke_type = kInterface;
1150 is_range = false;
1151 break;
1152 case Instruction::INVOKE_INTERFACE_RANGE:
1153 invoke_type = kInterface;
1154 is_range = true;
1155 break;
1156 default:
1157 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1158 UNREACHABLE();
1159 }
1160 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1161 // Check that the invoke matches what we expected, note that this path only happens for debug
1162 // builds.
1163 if (found_stack_map) {
1164 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1165 if (invoke_type != kSuper) {
1166 // Super may be sharpened.
1167 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1168 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1169 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1170 }
1171 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001172 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001173 << called_method.dex_method_index;
1174 }
1175 } else {
1176 invoke_type = stack_map_invoke_type;
1177 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001178 }
Ian Rogers848871b2013-08-05 10:56:33 -07001179 } else {
1180 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001181 called_method.dex_file = called->GetDexFile();
1182 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001183 }
1184 uint32_t shorty_len;
1185 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001186 called_method.dex_file->GetMethodShorty(
1187 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001188 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001189 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001190 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001191 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001192 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001193 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001194 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001195 mirror::Object* dummy = nullptr;
1196 HandleWrapper<mirror::Object> h_receiver(
1197 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001198 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001199 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001200 self, called_method.dex_method_index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001201
1202 // Update .bss entry in oat file if any.
1203 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1204 const MethodBssMapping* mapping =
1205 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1206 if (mapping != nullptr) {
1207 auto pp = std::partition_point(
1208 mapping->begin(),
1209 mapping->end(),
1210 [called_method](const MethodBssMappingEntry& entry) {
1211 return entry.method_index < called_method.dex_method_index;
1212 });
1213 if (pp != mapping->end() && pp->CoversIndex(called_method.dex_method_index)) {
1214 size_t bss_offset = pp->GetBssOffset(called_method.dex_method_index,
1215 static_cast<size_t>(kRuntimePointerSize));
1216 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1217 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1218 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1219 oat_file->BssBegin() + bss_offset));
1220 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1221 DCHECK_LT(method_entry,
1222 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1223 *method_entry = called;
1224 }
1225 }
1226 }
Ian Rogers848871b2013-08-05 10:56:33 -07001227 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001228 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001229 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001230 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001231 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001232 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001233 if (virtual_or_interface || invoke_type == kSuper) {
1234 // Refine called method based on receiver for kVirtual/kInterface, and
1235 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001236 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001237 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001238 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001239 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001240 } else if (invoke_type == kInterface) {
1241 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001242 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001243 } else {
1244 DCHECK_EQ(invoke_type, kSuper);
1245 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001246 StackHandleScope<2> hs(self);
1247 Handle<mirror::DexCache> dex_cache(
1248 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1249 Handle<mirror::ClassLoader> class_loader(
1250 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001251 // TODO Maybe put this into a mirror::Class function.
Vladimir Markoba118822017-06-12 15:41:56 +01001252 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
1253 *dex_cache->GetDexFile(),
1254 dex_cache->GetDexFile()->GetMethodId(called_method.dex_method_index).class_idx_,
1255 dex_cache.Get(),
1256 class_loader.Get());
Alex Lightfedd91d2016-01-07 14:49:16 -08001257 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001258 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001259 } else {
1260 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001261 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001262 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001263 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001264
David Sehr709b0702016-10-13 09:12:37 -07001265 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1266 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001267 << invoke_type << " " << orig_called->GetVtableIndex();
Ian Rogers83883d72013-10-21 21:07:24 -07001268 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001269
Ian Rogers848871b2013-08-05 10:56:33 -07001270 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001271 StackHandleScope<1> hs(soa.Self());
1272 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001273 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001274 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001275 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1276 // If we are single-stepping or the called method is deoptimized (by a
1277 // breakpoint, for example), then we have to execute the called method
1278 // with the interpreter.
1279 code = GetQuickToInterpreterBridge();
1280 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1281 // If the caller is deoptimized (by a breakpoint, for example), we have to
1282 // continue its execution with interpreter when returning from the called
1283 // method. Because we do not want to execute the called method with the
1284 // interpreter, we wrap its execution into the instrumentation stubs.
1285 // When the called method returns, it will execute the instrumentation
1286 // exit hook that will determine the need of the interpreter with a call
1287 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1288 // it is needed.
1289 code = GetQuickInstrumentationEntryPoint();
1290 } else {
1291 code = called->GetEntryPointFromQuickCompiledCode();
1292 }
Ian Rogers848871b2013-08-05 10:56:33 -07001293 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001294 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1295 // If we are single-stepping or the called method is deoptimized (by a
1296 // breakpoint, for example), then we have to execute the called method
1297 // with the interpreter.
1298 code = GetQuickToInterpreterBridge();
1299 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001300 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1301 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001302 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001303 } else {
1304 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001305 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001306 }
1307 } else {
1308 DCHECK(called_class->IsErroneous());
1309 }
1310 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001311 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001312 // Fixup any locally saved objects may have moved during a GC.
1313 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001314 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001315 *sp = called;
1316
Ian Rogers848871b2013-08-05 10:56:33 -07001317 return code;
1318}
1319
Andreas Gampec147b002014-03-06 18:11:06 -08001320/*
1321 * This class uses a couple of observations to unite the different calling conventions through
1322 * a few constants.
1323 *
1324 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1325 * possible alignment.
1326 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1327 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1328 * when we have to split things
1329 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1330 * and we can use Int handling directly.
1331 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1332 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1333 * extension should be compatible with Aarch64, which mandates copying the available bits
1334 * into LSB and leaving the rest unspecified.
1335 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1336 * the stack.
1337 * 6) There is only little endian.
1338 *
1339 *
1340 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1341 * follows:
1342 *
1343 * void PushGpr(uintptr_t): Add a value for the next GPR
1344 *
1345 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1346 * padding, that is, think the architecture is 32b and aligns 64b.
1347 *
1348 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1349 * split this if necessary. The current state will have aligned, if
1350 * necessary.
1351 *
1352 * void PushStack(uintptr_t): Push a value to the stack.
1353 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001354 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001355 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001356 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001357 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001358 *
1359 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001360template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001361 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001362#if defined(__arm__)
1363 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001364 static constexpr bool kNativeSoftFloatAbi = true;
1365 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001366 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1367
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001368 static constexpr size_t kRegistersNeededForLong = 2;
1369 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001370 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001371 static constexpr bool kMultiFPRegistersWidened = false;
1372 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001373 static constexpr bool kAlignLongOnStack = true;
1374 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001375#elif defined(__aarch64__)
1376 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1377 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1378 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1379
1380 static constexpr size_t kRegistersNeededForLong = 1;
1381 static constexpr size_t kRegistersNeededForDouble = 1;
1382 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001383 static constexpr bool kMultiFPRegistersWidened = false;
1384 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001385 static constexpr bool kAlignLongOnStack = false;
1386 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001387#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001388 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001389 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1390 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001391
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001392 static constexpr size_t kRegistersNeededForLong = 2;
1393 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001394 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001395 static constexpr bool kMultiFPRegistersWidened = true;
1396 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001397 static constexpr bool kAlignLongOnStack = true;
1398 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001399#elif defined(__mips__) && defined(__LP64__)
1400 // Let the code prepare GPRs only and we will load the FPRs with same data.
1401 static constexpr bool kNativeSoftFloatAbi = true;
1402 static constexpr size_t kNumNativeGprArgs = 8;
1403 static constexpr size_t kNumNativeFprArgs = 0;
1404
1405 static constexpr size_t kRegistersNeededForLong = 1;
1406 static constexpr size_t kRegistersNeededForDouble = 1;
1407 static constexpr bool kMultiRegistersAligned = false;
1408 static constexpr bool kMultiFPRegistersWidened = false;
1409 static constexpr bool kMultiGPRegistersWidened = true;
1410 static constexpr bool kAlignLongOnStack = false;
1411 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001412#elif defined(__i386__)
1413 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001414 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001415 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1416 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1417
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001418 static constexpr size_t kRegistersNeededForLong = 2;
1419 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001420 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001421 static constexpr bool kMultiFPRegistersWidened = false;
1422 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001423 static constexpr bool kAlignLongOnStack = false;
1424 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001425#elif defined(__x86_64__)
1426 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1427 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1428 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1429
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001430 static constexpr size_t kRegistersNeededForLong = 1;
1431 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001432 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001433 static constexpr bool kMultiFPRegistersWidened = false;
1434 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001435 static constexpr bool kAlignLongOnStack = false;
1436 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001437#else
1438#error "Unsupported architecture"
1439#endif
1440
Andreas Gampec147b002014-03-06 18:11:06 -08001441 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001442 explicit BuildNativeCallFrameStateMachine(T* delegate)
1443 : gpr_index_(kNumNativeGprArgs),
1444 fpr_index_(kNumNativeFprArgs),
1445 stack_entries_(0),
1446 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001447 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1448 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001449 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1450 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001451 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001452
Andreas Gampec200a4a2014-06-16 18:39:09 -07001453 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001454
Ian Rogers1428dce2014-10-21 15:02:15 -07001455 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001456 return gpr_index_ > 0;
1457 }
1458
Andreas Gampec200a4a2014-06-16 18:39:09 -07001459 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001460 if (HavePointerGpr()) {
1461 gpr_index_--;
1462 PushGpr(reinterpret_cast<uintptr_t>(val));
1463 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001464 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001465 PushStack(reinterpret_cast<uintptr_t>(val));
1466 gpr_index_ = 0;
1467 }
1468 }
1469
Ian Rogers1428dce2014-10-21 15:02:15 -07001470 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001471 return gpr_index_ > 0;
1472 }
1473
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001474 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001475 uintptr_t handle = PushHandle(ptr);
1476 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001477 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001478 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001479 } else {
1480 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001481 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001482 gpr_index_ = 0;
1483 }
1484 }
1485
Ian Rogers1428dce2014-10-21 15:02:15 -07001486 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001487 return gpr_index_ > 0;
1488 }
1489
1490 void AdvanceInt(uint32_t val) {
1491 if (HaveIntGpr()) {
1492 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001493 if (kMultiGPRegistersWidened) {
1494 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001495 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001496 } else {
1497 PushGpr(val);
1498 }
Andreas Gampec147b002014-03-06 18:11:06 -08001499 } else {
1500 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001501 if (kMultiGPRegistersWidened) {
1502 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001503 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001504 } else {
1505 PushStack(val);
1506 }
Andreas Gampec147b002014-03-06 18:11:06 -08001507 gpr_index_ = 0;
1508 }
1509 }
1510
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001512 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1513 }
1514
Ian Rogers1428dce2014-10-21 15:02:15 -07001515 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001516 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1517 kAlignLongOnStack && // and when it needs alignment
1518 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1519 }
1520
Ian Rogers1428dce2014-10-21 15:02:15 -07001521 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001522 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1523 kAlignLongOnStack && // and when it needs 8B alignment
1524 (stack_entries_ & 1) == 1; // counter is odd
1525 }
1526
1527 void AdvanceLong(uint64_t val) {
1528 if (HaveLongGpr()) {
1529 if (LongGprNeedsPadding()) {
1530 PushGpr(0);
1531 gpr_index_--;
1532 }
1533 if (kRegistersNeededForLong == 1) {
1534 PushGpr(static_cast<uintptr_t>(val));
1535 } else {
1536 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1537 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1538 }
1539 gpr_index_ -= kRegistersNeededForLong;
1540 } else {
1541 if (LongStackNeedsPadding()) {
1542 PushStack(0);
1543 stack_entries_++;
1544 }
1545 if (kRegistersNeededForLong == 1) {
1546 PushStack(static_cast<uintptr_t>(val));
1547 stack_entries_++;
1548 } else {
1549 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1550 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1551 stack_entries_ += 2;
1552 }
1553 gpr_index_ = 0;
1554 }
1555 }
1556
Ian Rogers1428dce2014-10-21 15:02:15 -07001557 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001558 return fpr_index_ > 0;
1559 }
1560
Andreas Gampec147b002014-03-06 18:11:06 -08001561 void AdvanceFloat(float val) {
1562 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001563 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001564 } else {
1565 if (HaveFloatFpr()) {
1566 fpr_index_--;
1567 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001568 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001569 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001570 } else {
1571 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001572 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001573 }
1574 } else {
1575 PushFpr4(val);
1576 }
1577 } else {
1578 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001579 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001580 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001581 // Note: We need to jump through those hoops to make the compiler happy.
1582 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001583 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001584 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001585 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001586 }
1587 fpr_index_ = 0;
1588 }
1589 }
1590 }
1591
Ian Rogers1428dce2014-10-21 15:02:15 -07001592 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001593 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1594 }
1595
Ian Rogers1428dce2014-10-21 15:02:15 -07001596 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001597 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1598 kAlignDoubleOnStack && // and when it needs alignment
1599 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1600 }
1601
Ian Rogers1428dce2014-10-21 15:02:15 -07001602 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001603 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1604 kAlignDoubleOnStack && // and when it needs 8B alignment
1605 (stack_entries_ & 1) == 1; // counter is odd
1606 }
1607
1608 void AdvanceDouble(uint64_t val) {
1609 if (kNativeSoftFloatAbi) {
1610 AdvanceLong(val);
1611 } else {
1612 if (HaveDoubleFpr()) {
1613 if (DoubleFprNeedsPadding()) {
1614 PushFpr4(0);
1615 fpr_index_--;
1616 }
1617 PushFpr8(val);
1618 fpr_index_ -= kRegistersNeededForDouble;
1619 } else {
1620 if (DoubleStackNeedsPadding()) {
1621 PushStack(0);
1622 stack_entries_++;
1623 }
1624 if (kRegistersNeededForDouble == 1) {
1625 PushStack(static_cast<uintptr_t>(val));
1626 stack_entries_++;
1627 } else {
1628 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1629 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1630 stack_entries_ += 2;
1631 }
1632 fpr_index_ = 0;
1633 }
1634 }
1635 }
1636
Ian Rogers1428dce2014-10-21 15:02:15 -07001637 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001638 return stack_entries_;
1639 }
1640
Ian Rogers1428dce2014-10-21 15:02:15 -07001641 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001642 return kNumNativeGprArgs - gpr_index_;
1643 }
1644
Ian Rogers1428dce2014-10-21 15:02:15 -07001645 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001646 return kNumNativeFprArgs - fpr_index_;
1647 }
1648
1649 private:
1650 void PushGpr(uintptr_t val) {
1651 delegate_->PushGpr(val);
1652 }
1653 void PushFpr4(float val) {
1654 delegate_->PushFpr4(val);
1655 }
1656 void PushFpr8(uint64_t val) {
1657 delegate_->PushFpr8(val);
1658 }
1659 void PushStack(uintptr_t val) {
1660 delegate_->PushStack(val);
1661 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001662 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001663 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001664 }
1665
1666 uint32_t gpr_index_; // Number of free GPRs
1667 uint32_t fpr_index_; // Number of free FPRs
1668 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1669 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001670 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001671};
1672
Andreas Gampec200a4a2014-06-16 18:39:09 -07001673// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1674// in subclasses.
1675//
1676// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1677// them with handles.
1678class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001679 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001680 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1681
1682 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001683
Ian Rogers1428dce2014-10-21 15:02:15 -07001684 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001685 return num_stack_entries_ * sizeof(uintptr_t);
1686 }
1687
Ian Rogers1428dce2014-10-21 15:02:15 -07001688 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001689 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001690 // Align by kStackAlignment.
1691 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001692 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001693 }
1694
Ian Rogers1428dce2014-10-21 15:02:15 -07001695 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1696 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001697 // Assumption is OK right now, as we have soft-float arm
1698 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1699 sp8 -= fregs * sizeof(uintptr_t);
1700 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1701 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1702 sp8 -= iregs * sizeof(uintptr_t);
1703 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1704 return sp8;
1705 }
Andreas Gampec147b002014-03-06 18:11:06 -08001706
Andreas Gampec200a4a2014-06-16 18:39:09 -07001707 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001708 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001709 // Native call stack.
1710 sp8 = LayoutCallStack(sp8);
1711 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001712
Andreas Gampec200a4a2014-06-16 18:39:09 -07001713 // Put fprs and gprs below.
1714 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001715
Andreas Gampec200a4a2014-06-16 18:39:09 -07001716 // Return the new bottom.
1717 return sp8;
1718 }
1719
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001720 virtual void WalkHeader(
1721 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001722 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001723 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001724
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001725 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001726 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1727
1728 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001729
1730 for (uint32_t i = 1; i < shorty_len; ++i) {
1731 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1732 switch (cur_type_) {
1733 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001734 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001735 sm.AdvanceHandleScope(
1736 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001737 break;
1738
1739 case Primitive::kPrimBoolean:
1740 case Primitive::kPrimByte:
1741 case Primitive::kPrimChar:
1742 case Primitive::kPrimShort:
1743 case Primitive::kPrimInt:
1744 sm.AdvanceInt(0);
1745 break;
1746 case Primitive::kPrimFloat:
1747 sm.AdvanceFloat(0);
1748 break;
1749 case Primitive::kPrimDouble:
1750 sm.AdvanceDouble(0);
1751 break;
1752 case Primitive::kPrimLong:
1753 sm.AdvanceLong(0);
1754 break;
1755 default:
1756 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001757 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001758 }
1759 }
1760
Ian Rogers1428dce2014-10-21 15:02:15 -07001761 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001762 }
1763
1764 void PushGpr(uintptr_t /* val */) {
1765 // not optimizing registers, yet
1766 }
1767
1768 void PushFpr4(float /* val */) {
1769 // not optimizing registers, yet
1770 }
1771
1772 void PushFpr8(uint64_t /* val */) {
1773 // not optimizing registers, yet
1774 }
1775
1776 void PushStack(uintptr_t /* val */) {
1777 // counting is already done in the superclass
1778 }
1779
Andreas Gampec200a4a2014-06-16 18:39:09 -07001780 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001781 return reinterpret_cast<uintptr_t>(nullptr);
1782 }
1783
Andreas Gampec200a4a2014-06-16 18:39:09 -07001784 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001785 uint32_t num_stack_entries_;
1786};
1787
Andreas Gampec200a4a2014-06-16 18:39:09 -07001788class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001789 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001790 explicit ComputeGenericJniFrameSize(bool critical_native)
1791 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001792
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1794 // is at *m = sp. Will update to point to the bottom of the save frame.
1795 //
1796 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001797 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001798 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001799 ArtMethod* method = **m;
1800
Andreas Gampe542451c2016-07-26 09:02:02 -07001801 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001802
Andreas Gampec200a4a2014-06-16 18:39:09 -07001803 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1804
1805 // First, fix up the layout of the callee-save frame.
1806 // We have to squeeze in the HandleScope, and relocate the method pointer.
1807
1808 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001809 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001810
1811 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001812 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001813 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001814
1815 sp8 -= scope_and_method;
1816 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001817 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001818
Mathieu Chartiere401d142015-04-22 13:56:20 -07001819 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001820 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1821 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001822
1823 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1824 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001825 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1826 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001827 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001828 }
1829
Andreas Gampec200a4a2014-06-16 18:39:09 -07001830 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001831 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001832 // Reference cookie and padding
1833 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001834 }
1835
Andreas Gampec200a4a2014-06-16 18:39:09 -07001836 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1837 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001838 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001839 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001840 // First, fix up the layout of the callee-save frame.
1841 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001842 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001843
1844 // The bottom of the callee-save frame is now where the method is, *m.
1845 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1846
1847 // Add space for cookie.
1848 LayoutCookie(&sp8);
1849
1850 return sp8;
1851 }
1852
1853 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1855 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1856 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001857 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001858 Walk(shorty, shorty_len);
1859
1860 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001861 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001862
1863 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1864
1865 // Return the new bottom.
1866 return sp8;
1867 }
1868
1869 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1870
1871 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1872 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001873 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001874
1875 private:
1876 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001877 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001878};
1879
1880uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1881 num_handle_scope_references_++;
1882 return reinterpret_cast<uintptr_t>(nullptr);
1883}
1884
1885void ComputeGenericJniFrameSize::WalkHeader(
1886 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001887 // First 2 parameters are always excluded for @CriticalNative.
1888 if (UNLIKELY(critical_native_)) {
1889 return;
1890 }
1891
Andreas Gampec200a4a2014-06-16 18:39:09 -07001892 // JNIEnv
1893 sm->AdvancePointer(nullptr);
1894
1895 // Class object or this as first argument
1896 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1897}
1898
1899// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1900// the template requirements of BuildGenericJniFrameStateMachine.
1901class FillNativeCall {
1902 public:
1903 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1904 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1905
1906 virtual ~FillNativeCall() {}
1907
1908 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1909 cur_gpr_reg_ = gpr_regs;
1910 cur_fpr_reg_ = fpr_regs;
1911 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001912 }
1913
1914 void PushGpr(uintptr_t val) {
1915 *cur_gpr_reg_ = val;
1916 cur_gpr_reg_++;
1917 }
1918
1919 void PushFpr4(float val) {
1920 *cur_fpr_reg_ = val;
1921 cur_fpr_reg_++;
1922 }
1923
1924 void PushFpr8(uint64_t val) {
1925 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1926 *tmp = val;
1927 cur_fpr_reg_ += 2;
1928 }
1929
1930 void PushStack(uintptr_t val) {
1931 *cur_stack_arg_ = val;
1932 cur_stack_arg_++;
1933 }
1934
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001935 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001936 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001937 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001938 }
1939
1940 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001941 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001942 uint32_t* cur_fpr_reg_;
1943 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001944};
Andreas Gampec147b002014-03-06 18:11:06 -08001945
Andreas Gampec200a4a2014-06-16 18:39:09 -07001946// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1947// of transitioning into native code.
1948class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1949 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001950 BuildGenericJniFrameVisitor(Thread* self,
1951 bool is_static,
1952 bool critical_native,
1953 const char* shorty,
1954 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001955 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001956 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001957 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1958 sm_(&jni_call_) {
1959 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001960 uintptr_t* start_gpr_reg;
1961 uint32_t* start_fpr_reg;
1962 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001963 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001964 &handle_scope_,
1965 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001966 &start_gpr_reg, &start_fpr_reg);
1967
Andreas Gampec200a4a2014-06-16 18:39:09 -07001968 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1969
Igor Murashkin06a04e02016-09-13 15:57:37 -07001970 // First 2 parameters are always excluded for CriticalNative methods.
1971 if (LIKELY(!critical_native)) {
1972 // jni environment is always first argument
1973 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001974
Igor Murashkin06a04e02016-09-13 15:57:37 -07001975 if (is_static) {
1976 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1977 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001978 }
1979 }
1980
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001981 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001982
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001983 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001984
Vladimir Markof39745e2016-01-26 12:16:55 +00001985 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001986 return handle_scope_->GetHandle(0).GetReference();
1987 }
1988
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001989 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001990 return handle_scope_->GetHandle(0).ToJObject();
1991 }
1992
Ian Rogers1428dce2014-10-21 15:02:15 -07001993 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001994 return bottom_of_used_area_;
1995 }
1996
1997 private:
1998 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1999 class FillJniCall FINAL : public FillNativeCall {
2000 public:
2001 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002002 HandleScope* handle_scope, bool critical_native)
2003 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2004 handle_scope_(handle_scope),
2005 cur_entry_(0),
2006 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002008 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009
2010 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2011 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2012 handle_scope_ = scope;
2013 cur_entry_ = 0U;
2014 }
2015
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002016 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002017 // Initialize padding entries.
2018 size_t expected_slots = handle_scope_->NumberOfReferences();
2019 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002020 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002021 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002022
2023 if (!critical_native_) {
2024 // Non-critical natives have at least the self class (jclass) or this (jobject).
2025 DCHECK_NE(cur_entry_, 0U);
2026 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002027 }
2028
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002029 bool CriticalNative() const {
2030 return critical_native_;
2031 }
2032
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033 private:
2034 HandleScope* handle_scope_;
2035 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002036 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037 };
2038
2039 HandleScope* handle_scope_;
2040 FillJniCall jni_call_;
2041 void* bottom_of_used_area_;
2042
2043 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002044
2045 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2046};
2047
Andreas Gampec200a4a2014-06-16 18:39:09 -07002048uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2049 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002050 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002051 h.Assign(ref);
2052 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2053 cur_entry_++;
2054 return tmp;
2055}
2056
Ian Rogers9758f792014-03-13 09:02:55 -07002057void BuildGenericJniFrameVisitor::Visit() {
2058 Primitive::Type type = GetParamPrimitiveType();
2059 switch (type) {
2060 case Primitive::kPrimLong: {
2061 jlong long_arg;
2062 if (IsSplitLongOrDouble()) {
2063 long_arg = ReadSplitLongParam();
2064 } else {
2065 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2066 }
2067 sm_.AdvanceLong(long_arg);
2068 break;
2069 }
2070 case Primitive::kPrimDouble: {
2071 uint64_t double_arg;
2072 if (IsSplitLongOrDouble()) {
2073 // Read into union so that we don't case to a double.
2074 double_arg = ReadSplitLongParam();
2075 } else {
2076 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2077 }
2078 sm_.AdvanceDouble(double_arg);
2079 break;
2080 }
2081 case Primitive::kPrimNot: {
2082 StackReference<mirror::Object>* stack_ref =
2083 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002084 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002085 break;
2086 }
2087 case Primitive::kPrimFloat:
2088 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2089 break;
2090 case Primitive::kPrimBoolean: // Fall-through.
2091 case Primitive::kPrimByte: // Fall-through.
2092 case Primitive::kPrimChar: // Fall-through.
2093 case Primitive::kPrimShort: // Fall-through.
2094 case Primitive::kPrimInt: // Fall-through.
2095 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2096 break;
2097 case Primitive::kPrimVoid:
2098 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002099 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002100 }
2101}
2102
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002103void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002104 // Clear out rest of the scope.
2105 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002106 if (!jni_call_.CriticalNative()) {
2107 // Install HandleScope.
2108 self->PushHandleScope(handle_scope_);
2109 }
Ian Rogers9758f792014-03-13 09:02:55 -07002110}
2111
Ian Rogers04c31d22014-07-07 21:44:06 -07002112#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002113extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002114#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002115extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002116#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002117
Igor Murashkin06a04e02016-09-13 15:57:37 -07002118static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2119 uint32_t cookie,
2120 bool fast_native ATTRIBUTE_UNUSED,
2121 jobject l,
2122 jobject lock) {
2123 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002124 if (lock != nullptr) {
2125 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2126 } else {
2127 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2128 }
2129}
2130
Igor Murashkin06a04e02016-09-13 15:57:37 -07002131static void artQuickGenericJniEndJNINonRef(Thread* self,
2132 uint32_t cookie,
2133 bool fast_native,
2134 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002135 if (lock != nullptr) {
2136 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002137 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002138 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002139 if (UNLIKELY(fast_native)) {
2140 JniMethodFastEnd(cookie, self);
2141 } else {
2142 JniMethodEnd(cookie, self);
2143 }
Andreas Gampead615172014-04-04 16:20:13 -07002144 }
2145}
2146
Andreas Gampec147b002014-03-06 18:11:06 -08002147/*
2148 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002149 * 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 -08002150 * The final element on the stack is a pointer to the native code.
2151 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002152 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002153 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002154 *
Andreas Gampec147b002014-03-06 18:11:06 -08002155 * The return of this function denotes:
2156 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2157 * 2) An error, if the value is negative.
2158 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002159extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002160 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002161 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002162 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Roland Levillain0d2323e2017-06-26 18:14:39 +01002163 // Fix up a callee-save frame at the bottom of the stack (at `*sp`,
2164 // above the alloca region) while we check for optimization
2165 // annotations, thus allowing stack walking until the completion of
2166 // the JNI frame creation.
2167 //
2168 // Note however that the Generic JNI trampoline does not expect
2169 // exception being thrown at that stage.
2170 *sp = Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
2171 self->SetTopOfStack(sp);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002172 uint32_t shorty_len = 0;
2173 const char* shorty = called->GetShorty(&shorty_len);
Roland Levillain35e42f02017-06-26 18:14:39 +01002174 // Optimization annotations lookup does not try to resolve classes,
2175 // as this may throw an exception, which is not supported by the
2176 // Generic JNI trampoline at this stage; instead, method's
2177 // annotations' classes are looked up in the bootstrap class
2178 // loader's resolved types (which won't trigger an exception).
Igor Murashkin06a04e02016-09-13 15:57:37 -07002179 bool critical_native = called->IsAnnotatedWithCriticalNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002180 // ArtMethod::IsAnnotatedWithCriticalNative should not throw
2181 // an exception; clear it if it happened anyway.
2182 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2183 if (self->IsExceptionPending()) {
2184 self->ClearException();
2185 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002186 bool fast_native = called->IsAnnotatedWithFastNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002187 // ArtMethod::IsAnnotatedWithFastNative should not throw
2188 // an exception; clear it if it happened anyway.
2189 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2190 if (self->IsExceptionPending()) {
2191 self->ClearException();
2192 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002193 bool normal_native = !critical_native && !fast_native;
Roland Levillain0d2323e2017-06-26 18:14:39 +01002194 // Restore the initial ArtMethod pointer at `*sp`.
2195 *sp = called;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002196
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002197 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002198 BuildGenericJniFrameVisitor visitor(self,
2199 called->IsStatic(),
2200 critical_native,
2201 shorty,
2202 shorty_len,
2203 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002204 {
2205 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2206 visitor.VisitArguments();
2207 // FinalizeHandleScope pushes the handle scope on the thread.
2208 visitor.FinalizeHandleScope(self);
2209 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002210
Andreas Gampec200a4a2014-06-16 18:39:09 -07002211 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002212 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002213
Ian Rogerse0dcd462014-03-08 15:21:04 -08002214 self->VerifyStack();
2215
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002216 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002217 uint32_t* sp32;
2218 // Skip calling JniMethodStart for @CriticalNative.
2219 if (LIKELY(!critical_native)) {
2220 // Start JNI, save the cookie.
2221 if (called->IsSynchronized()) {
2222 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2223 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2224 if (self->IsExceptionPending()) {
2225 self->PopHandleScope();
2226 // A negative value denotes an error.
2227 return GetTwoWordFailureValue();
2228 }
2229 } else {
2230 if (fast_native) {
2231 cookie = JniMethodFastStart(self);
2232 } else {
2233 DCHECK(normal_native);
2234 cookie = JniMethodStart(self);
2235 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002236 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002237 sp32 = reinterpret_cast<uint32_t*>(sp);
2238 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002239 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002240
Andreas Gampe90546832014-03-12 18:07:19 -07002241 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002242 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002243
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002244 // There are two cases for the content of nativeCode:
2245 // 1) Pointer to the native function.
2246 // 2) Pointer to the trampoline for native code binding.
2247 // In the second case, we need to execute the binding and continue with the actual native function
2248 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002249 DCHECK(nativeCode != nullptr);
2250 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002251#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002252 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002253#else
2254 nativeCode = artFindNativeMethod(self);
2255#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002256
2257 if (nativeCode == nullptr) {
2258 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002259
Igor Murashkin06a04e02016-09-13 15:57:37 -07002260 // @CriticalNative calls do not need to call back into JniMethodEnd.
2261 if (LIKELY(!critical_native)) {
2262 // End JNI, as the assembly will move to deliver the exception.
2263 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2264 if (shorty[0] == 'L') {
2265 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2266 } else {
2267 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2268 }
Andreas Gampead615172014-04-04 16:20:13 -07002269 }
2270
Andreas Gampec200a4a2014-06-16 18:39:09 -07002271 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002272 }
2273 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002274 }
2275
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002276#if defined(__mips__) && !defined(__LP64__)
2277 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2278 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2279 // and load into floating-point registers.
2280 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2281 // view):
2282 // (1)
2283 // | DOUBLE | DOUBLE | other args, if any
2284 // | F12 | F13 | F14 | F15 |
2285 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2286 // (2)
2287 // | DOUBLE | FLOAT | (PAD) | other args, if any
2288 // | F12 | F13 | F14 | |
2289 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2290 // (3)
2291 // | FLOAT | (PAD) | DOUBLE | other args, if any
2292 // | F12 | | F14 | F15 |
2293 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2294 // (4)
2295 // | FLOAT | FLOAT | other args, if any
2296 // | F12 | F14 |
2297 // | SP+0 | SP+4 | SP+8
2298 // As you can see, only the last case (4) is special. In all others we can just
2299 // load F12/F13 and F14/F15 in the same manner.
2300 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2301 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2302 if (nativeCode != nullptr &&
2303 shorty != nullptr &&
2304 shorty_len >= 3 &&
2305 shorty[1] == 'F' &&
2306 shorty[2] == 'F') {
2307 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2308 }
2309#endif
2310
Andreas Gampec200a4a2014-06-16 18:39:09 -07002311 // Return native code addr(lo) and bottom of alloca address(hi).
2312 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2313 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002314}
2315
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002316// Defined in quick_jni_entrypoints.cc.
2317extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2318 jvalue result, uint64_t result_f, ArtMethod* called,
2319 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002320/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002321 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002322 * unlocking.
2323 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002324extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2325 jvalue result,
2326 uint64_t result_f) {
2327 // We're here just back from a native call. We don't have the shared mutator lock at this point
2328 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2329 // anything that requires a mutator lock before that would cause problems as GC may have the
2330 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002331 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002332 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002333 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002334 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002335 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2336 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002337}
2338
Andreas Gamped58342c2014-06-05 14:18:08 -07002339// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2340// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002341//
Andreas Gamped58342c2014-06-05 14:18:08 -07002342// 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 -07002343// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002344
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002345template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002346static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2347 ObjPtr<mirror::Object> this_object,
2348 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002349 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002350 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002351 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002352 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002353 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002354 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002355 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2356 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002357 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002358 {
2359 // Remember the args in case a GC happens in FindMethodFromCode.
2360 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2361 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2362 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002363 method = FindMethodFromCode<type, access_check>(method_idx,
2364 &this_object,
2365 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002366 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002367 visitor.FixupReferences();
2368 }
2369
Ian Rogerse0a02da2014-12-02 14:10:53 -08002370 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002371 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002372 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002373 }
2374 }
2375 DCHECK(!self->IsExceptionPending());
2376 const void* code = method->GetEntryPointFromQuickCompiledCode();
2377
2378 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002379 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002380 << " location: "
2381 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002382
Andreas Gamped58342c2014-06-05 14:18:08 -07002383 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2384 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002385}
2386
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002387// Explicit artInvokeCommon template function declarations to please analysis tool.
2388#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002389 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002391 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002392
2393EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2394EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2395EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2396EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2397EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2398EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2399EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2400EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2401EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2402EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2403#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2404
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002405// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002406extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002407 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002408 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002409 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002410}
2411
Andreas Gampec200a4a2014-06-16 18:39:09 -07002412extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002413 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002414 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002415 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002416}
2417
Andreas Gampec200a4a2014-06-16 18:39:09 -07002418extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002419 uint32_t method_idx,
2420 mirror::Object* this_object ATTRIBUTE_UNUSED,
2421 Thread* self,
2422 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2423 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2424 // it doesn't cause ObjPtr alignment failure check.
2425 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002426}
2427
Andreas Gampec200a4a2014-06-16 18:39:09 -07002428extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002429 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002430 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002431 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002432}
2433
Andreas Gampec200a4a2014-06-16 18:39:09 -07002434extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002435 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002436 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002437 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002438}
2439
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002440// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2441extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2442 REQUIRES_SHARED(Locks::mutator_lock_) {
2443 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2444 DCHECK(!referrer->IsProxyMethod());
2445 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2446 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2447 DCHECK(result == nullptr ||
2448 result->GetDeclaringClass()->IsInterface() ||
2449 result->GetDeclaringClass() ==
2450 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2451 << result->PrettyMethod();
2452 return result;
2453}
2454
Jeff Hao5667f562017-02-27 19:32:01 -08002455// Determine target of interface dispatch. The interface method and this object are known non-null.
2456// The interface method is the method returned by the dex cache in the conflict trampoline.
2457extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002458 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002459 Thread* self,
2460 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002461 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002462 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002463 StackHandleScope<2> hs(self);
2464 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2465 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002466
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002467 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002468 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002469 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002470
Vladimir Marko302f69c2017-07-25 15:27:15 +01002471 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002472 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002473 // Fetch the dex_method_idx of the target interface method from the caller.
2474 uint32_t dex_method_idx;
2475 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2476 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2477 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2478 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2479 Instruction::Code instr_code = instr->Opcode();
2480 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2481 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2482 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2483 if (instr_code == Instruction::INVOKE_INTERFACE) {
2484 dex_method_idx = instr->VRegB_35c();
2485 } else {
2486 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2487 dex_method_idx = instr->VRegB_3rc();
2488 }
2489
Vladimir Marko302f69c2017-07-25 15:27:15 +01002490 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002491 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002492 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2493 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002494 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002495 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002496 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2497 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2498 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002499 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2500 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2501 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002502 visitor.FixupReferences();
2503 }
2504
Vladimir Marko302f69c2017-07-25 15:27:15 +01002505 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002506 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002507 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002508 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002509 }
2510
2511 DCHECK(!interface_method->IsRuntimeMethod());
2512 // Look whether we have a match in the ImtConflictTable.
2513 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2514 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2515 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2516 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2517 DCHECK(current_table != nullptr);
2518 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2519 } else {
2520 // It seems we aren't really a conflict method!
2521 if (kIsDebugBuild) {
2522 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2523 CHECK_EQ(conflict_method, m)
2524 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2525 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2526 }
2527 method = conflict_method;
2528 }
2529 if (method != nullptr) {
2530 return GetTwoWordSuccessValue(
2531 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2532 reinterpret_cast<uintptr_t>(method));
2533 }
2534
2535 // No match, use the IfTable.
2536 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2537 if (UNLIKELY(method == nullptr)) {
2538 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2539 interface_method, this_object.Get(), caller_method);
2540 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002541 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002542
2543 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2544 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002545 DCHECK(conflict_method->IsRuntimeMethod());
2546 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2547 cls.Get(),
2548 conflict_method,
2549 interface_method,
2550 method,
2551 /*force_new_conflict_method*/false);
2552 if (new_conflict_method != conflict_method) {
2553 // Update the IMT if we create a new conflict method. No fence needed here, as the
2554 // data is consistent.
2555 imt->Set(imt_index,
2556 new_conflict_method,
2557 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002558 }
2559
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002560 const void* code = method->GetEntryPointFromQuickCompiledCode();
2561
2562 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002563 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002564 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002565
Andreas Gamped58342c2014-06-05 14:18:08 -07002566 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2567 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002568}
2569
Orion Hodsonac141392017-01-13 11:53:47 +00002570// Returns shorty type so the caller can determine how to put |result|
2571// into expected registers. The shorty type is static so the compiler
2572// could call different flavors of this code path depending on the
2573// shorty type though this would require different entry points for
2574// each type.
2575extern "C" uintptr_t artInvokePolymorphic(
2576 JValue* result,
2577 mirror::Object* raw_method_handle,
2578 Thread* self,
2579 ArtMethod** sp)
2580 REQUIRES_SHARED(Locks::mutator_lock_) {
2581 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002582 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002583
2584 // Start new JNI local reference state
2585 JNIEnvExt* env = self->GetJniEnv();
2586 ScopedObjectAccessUnchecked soa(env);
2587 ScopedJniEnvLocalRefState env_state(env);
2588 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2589
2590 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2591 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2592 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2593 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2594 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2595 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2596 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2597 const DexFile* dex_file = caller_method->GetDexFile();
2598 const uint32_t proto_idx = inst->VRegH();
2599 const char* shorty = dex_file->GetShorty(proto_idx);
2600 const size_t shorty_length = strlen(shorty);
2601 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2602 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002603 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002604
2605 // Wrap raw_method_handle in a Handle for safety.
2606 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002607 Handle<mirror::MethodHandle> method_handle(
2608 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002609 raw_method_handle = nullptr;
2610 self->EndAssertNoThreadSuspension(old_cause);
2611
2612 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2613 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002614 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
2615 self, inst->VRegB(), caller_method, kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +00002616 DCHECK((resolved_method ==
2617 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2618 (resolved_method ==
2619 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2620 if (UNLIKELY(method_handle.IsNull())) {
2621 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2622 return static_cast<uintptr_t>('V');
2623 }
2624
2625 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2626 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2627 *dex_file, proto_idx,
2628 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2629 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2630 // This implies we couldn't resolve one or more types in this method handle.
2631 if (UNLIKELY(method_type.IsNull())) {
2632 CHECK(self->IsExceptionPending());
2633 return static_cast<uintptr_t>('V');
2634 }
2635
2636 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2637 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2638
2639 // Fix references before constructing the shadow frame.
2640 gc_visitor.FixupReferences();
2641
2642 // Construct shadow frame placing arguments consecutively from |first_arg|.
2643 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2644 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2645 const size_t first_arg = 0;
2646 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2647 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2648 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2649 ScopedStackedShadowFramePusher
2650 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2651 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2652 kMethodIsStatic,
2653 shorty,
2654 strlen(shorty),
2655 shadow_frame,
2656 first_arg);
2657 shadow_frame_builder.VisitArguments();
2658
2659 // Push a transition back into managed code onto the linked list in thread.
2660 ManagedStack fragment;
2661 self->PushManagedStackFragment(&fragment);
2662
2663 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2664 // consecutive order.
2665 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2666 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002667 if (!DoInvokePolymorphic<true /* is_range */>(self,
2668 resolved_method,
2669 *shadow_frame,
2670 method_handle,
2671 method_type,
2672 unused_args,
2673 first_callee_arg,
2674 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002675 DCHECK(self->IsExceptionPending());
2676 }
2677
2678 // Pop transition record.
2679 self->PopManagedStackFragment(fragment);
2680
2681 return static_cast<uintptr_t>(shorty[0]);
2682}
2683
Ian Rogers848871b2013-08-05 10:56:33 -07002684} // namespace art