blob: 597197da37c1e2292871f33c37e35eacb3697979 [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"
24#include "dex_instruction-inl.h"
Mingyao Yang98d1cc82014-05-15 17:02:16 -070025#include "entrypoints/entrypoint_utils-inl.h"
Ian Rogers6f3dbba2014-10-14 17:41:57 -070026#include "entrypoints/runtime_asm_entrypoints.h"
Ian Rogers83883d72013-10-21 21:07:24 -070027#include "gc/accounting/card_table-inl.h"
Andreas Gampe75a7db62016-09-26 12:04:26 -070028#include "imt_conflict_table.h"
29#include "imtable-inl.h"
Alex Lightb7edcda2017-04-27 13:20:31 -070030#include "instrumentation.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070031#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000032#include "linear_alloc.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010033#include "method_bss_mapping.h"
Orion Hodsonac141392017-01-13 11:53:47 +000034#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080035#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070036#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070037#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070038#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000039#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070040#include "mirror/object-inl.h"
41#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010042#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010043#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070044#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070045#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070046#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070047#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070048#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000049#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070050
51namespace art {
52
Andreas Gampe8228cdf2017-05-30 15:03:54 -070053// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070054class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080055 // Number of bytes for each out register in the caller method's frame.
56 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070057 // Frame size in bytes of a callee-save frame for RefsAndArgs.
58 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070059 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070060#if defined(__arm__)
61 // The callee save frame is pointed to by SP.
62 // | argN | |
63 // | ... | |
64 // | arg4 | |
65 // | arg3 spill | | Caller's frame
66 // | arg2 spill | |
67 // | arg1 spill | |
68 // | Method* | ---
69 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080070 // | ... | 4x6 bytes callee saves
71 // | R3 |
72 // | R2 |
73 // | R1 |
74 // | S15 |
75 // | : |
76 // | S0 |
77 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070078 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050079 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000080 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080081 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
82 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000083 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080084 static constexpr size_t kNumQuickGprArgs = 3;
85 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080086 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080087 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070088 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080089 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070090 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080091 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070092 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080093 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000094 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080095 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000096#elif defined(__aarch64__)
97 // The callee save frame is pointed to by SP.
98 // | argN | |
99 // | ... | |
100 // | arg4 | |
101 // | arg3 spill | | Caller's frame
102 // | arg2 spill | |
103 // | arg1 spill | |
104 // | Method* | ---
105 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800106 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000107 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100108 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000109 // | X7 |
110 // | : |
111 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800112 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000113 // | : |
114 // | D0 |
115 // | | padding
116 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500117 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000118 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000119 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800120 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000121 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000122 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
123 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800124 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700125 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800126 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700127 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
128 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800129 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700130 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
131 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800132 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700133 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000134 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000135 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000136 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800137#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700138 // The callee save frame is pointed to by SP.
139 // | argN | |
140 // | ... | |
141 // | arg4 | |
142 // | arg3 spill | | Caller's frame
143 // | arg2 spill | |
144 // | arg1 spill | |
145 // | Method* | ---
146 // | RA |
147 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800148 // | T1 | arg5
149 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700150 // | A3 | arg3
151 // | A2 | arg2
152 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800153 // | F19 |
154 // | F18 | f_arg5
155 // | F17 |
156 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000157 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800158 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000159 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800160 // | F12 | f_arg2
161 // | F11 |
162 // | F10 | f_arg1
163 // | F9 |
164 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000165 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700166 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000167 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
168 static constexpr bool kAlignPairRegister = true;
169 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800170 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000171 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800172 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
173 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
174 // passed only in even numbered registers and each
175 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800176 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
179 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800180 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000181 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800182 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800183#elif defined(__mips__) && defined(__LP64__)
184 // The callee save frame is pointed to by SP.
185 // | argN | |
186 // | ... | |
187 // | arg4 | |
188 // | arg3 spill | | Caller's frame
189 // | arg2 spill | |
190 // | arg1 spill | |
191 // | Method* | ---
192 // | RA |
193 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800194 // | A7 | arg7
195 // | A6 | arg6
196 // | A5 | arg5
197 // | A4 | arg4
198 // | A3 | arg3
199 // | A2 | arg2
200 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000201 // | F19 | f_arg7
202 // | F18 | f_arg6
203 // | F17 | f_arg5
204 // | F16 | f_arg4
205 // | F15 | f_arg3
206 // | F14 | f_arg2
207 // | F13 | f_arg1
208 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800209 // | | padding
210 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800211 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800212 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800213 static constexpr bool kAlignPairRegister = false;
214 static constexpr bool kQuickSoftFloatAbi = false;
215 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000216 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800217 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
218 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
219 static constexpr bool kGprFprLockstep = true;
220
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800221 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800222 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
223 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
224 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
225 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
226 }
Ian Rogers848871b2013-08-05 10:56:33 -0700227#elif defined(__i386__)
228 // The callee save frame is pointed to by SP.
229 // | argN | |
230 // | ... | |
231 // | arg4 | |
232 // | arg3 spill | | Caller's frame
233 // | arg2 spill | |
234 // | arg1 spill | |
235 // | Method* | ---
236 // | Return |
237 // | EBP,ESI,EDI | callee saves
238 // | EBX | arg3
239 // | EDX | arg2
240 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000241 // | XMM3 | float arg 4
242 // | XMM2 | float arg 3
243 // | XMM1 | float arg 2
244 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700245 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500246 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000247 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000248 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800249 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000250 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800251 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000252 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800253 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000254 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
255 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
256 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800257 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000258 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800259 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800260#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800261 // The callee save frame is pointed to by SP.
262 // | argN | |
263 // | ... | |
264 // | reg. arg spills | | Caller's frame
265 // | Method* | ---
266 // | Return |
267 // | R15 | callee save
268 // | R14 | callee save
269 // | R13 | callee save
270 // | R12 | callee save
271 // | R9 | arg5
272 // | R8 | arg4
273 // | RSI/R6 | arg1
274 // | RBP/R5 | callee save
275 // | RBX/R3 | callee save
276 // | RDX/R2 | arg2
277 // | RCX/R1 | arg3
278 // | XMM7 | float arg 8
279 // | XMM6 | float arg 7
280 // | XMM5 | float arg 6
281 // | XMM4 | float arg 5
282 // | XMM3 | float arg 4
283 // | XMM2 | float arg 3
284 // | XMM1 | float arg 2
285 // | XMM0 | float arg 1
286 // | Padding |
287 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500288 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000289 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800290 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800291 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000292 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700293 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700294 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800295 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800296 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700297 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
298 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800299 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
300 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000301 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
302 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
303 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
304 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
305 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800306 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700307 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
308 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800309 }
310 }
Ian Rogers848871b2013-08-05 10:56:33 -0700311#else
312#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700313#endif
314
Ian Rogers936b37f2014-02-14 00:52:24 -0800315 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100316 // Special handling for proxy methods. Proxy methods are instance methods so the
317 // 'this' object is the 1st argument. They also have the same frame layout as the
318 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
319 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700320 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700321 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000322 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100323 CHECK_GT(kNumQuickGprArgs, 0u);
324 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
325 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
326 GprIndexToGprOffset(kThisGprIndex);
327 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
328 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
329 }
330
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700331 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700332 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700333 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100334 }
335
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700336 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700337 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100338 uint8_t* previous_sp =
339 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700340 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100341 }
342
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700343 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700344 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700345 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
346 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700347 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
348 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100349 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100350 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
351 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100352
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100353 if (current_code->IsOptimized()) {
354 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000355 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100356 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100357 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800358 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100359 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800360 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
361 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100362 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800363 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100364 }
365 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100366 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100367 }
Ian Rogers848871b2013-08-05 10:56:33 -0700368 }
369
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800370 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
371 REQUIRES_SHARED(Locks::mutator_lock_) {
372 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700373 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
374 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800375 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
376 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
377 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
378 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
379 if (!current_code->IsOptimized()) {
380 return false;
381 }
382 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
383 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
384 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700385 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800386 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
387 if (invoke.IsValid()) {
388 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700389 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800390 return true;
391 }
392 return false;
393 }
394
Ian Rogers936b37f2014-02-14 00:52:24 -0800395 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700396 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700397 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700398 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700399 return *reinterpret_cast<uintptr_t*>(lr);
400 }
401
Mathieu Chartiere401d142015-04-22 13:56:20 -0700402 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700403 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700404 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700405 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
406 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
407 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700408 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800409 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
410 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800411 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
412 "Number of Quick FPR arguments unexpected");
413 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
414 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800415 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
416 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800417 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
418 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700419 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800420 }
Ian Rogers848871b2013-08-05 10:56:33 -0700421
422 virtual ~QuickArgumentVisitor() {}
423
424 virtual void Visit() = 0;
425
Ian Rogers936b37f2014-02-14 00:52:24 -0800426 Primitive::Type GetParamPrimitiveType() const {
427 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700428 }
429
Ian Rogers13735952014-10-08 12:43:28 -0700430 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800431 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800432 Primitive::Type type = GetParamPrimitiveType();
433 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800434 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
435 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
436 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
437 }
438 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000439 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800440 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700441 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800442 }
443 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800444 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800445 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
446 }
447 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700448 }
449
450 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700451 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
452 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800453 return is_split_long_or_double_;
454 } else {
455 return false; // An optimization for when GPR and FPRs are 64bit.
456 }
Ian Rogers848871b2013-08-05 10:56:33 -0700457 }
458
Ian Rogers936b37f2014-02-14 00:52:24 -0800459 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700460 return GetParamPrimitiveType() == Primitive::kPrimNot;
461 }
462
Ian Rogers936b37f2014-02-14 00:52:24 -0800463 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700464 Primitive::Type type = GetParamPrimitiveType();
465 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
466 }
467
468 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000469 // The splitted long is always available through the stack.
470 return *reinterpret_cast<uint64_t*>(stack_args_
471 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700472 }
473
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800474 void IncGprIndex() {
475 gpr_index_++;
476 if (kGprFprLockstep) {
477 fpr_index_++;
478 }
479 }
480
481 void IncFprIndex() {
482 fpr_index_++;
483 if (kGprFprLockstep) {
484 gpr_index_++;
485 }
486 }
487
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700488 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800489 // (a) 'stack_args_' should point to the first method's argument
490 // (b) whatever the argument type it is, the 'stack_index_' should
491 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800492 gpr_index_ = 0;
493 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800494 if (kQuickDoubleRegAlignedFloatBackFilled) {
495 fpr_double_index_ = 0;
496 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800497 stack_index_ = 0;
498 if (!is_static_) { // Handle this.
499 cur_type_ = Primitive::kPrimNot;
500 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700501 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800502 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800503 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800504 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800505 }
Ian Rogers848871b2013-08-05 10:56:33 -0700506 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800507 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
508 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
509 switch (cur_type_) {
510 case Primitive::kPrimNot:
511 case Primitive::kPrimBoolean:
512 case Primitive::kPrimByte:
513 case Primitive::kPrimChar:
514 case Primitive::kPrimShort:
515 case Primitive::kPrimInt:
516 is_split_long_or_double_ = false;
517 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800518 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800519 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800520 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800521 }
522 break;
523 case Primitive::kPrimFloat:
524 is_split_long_or_double_ = false;
525 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800526 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800527 if (kQuickSoftFloatAbi) {
528 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800529 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800530 }
531 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800532 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800533 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800534 if (kQuickDoubleRegAlignedFloatBackFilled) {
535 // Double should not overlap with float.
536 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
537 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
538 // Float should not overlap with double.
539 if (fpr_index_ % 2 == 0) {
540 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
541 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000542 } else if (kQuickSkipOddFpRegisters) {
543 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800544 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800545 }
546 }
547 break;
548 case Primitive::kPrimDouble:
549 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800550 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800551 if (cur_type_ == Primitive::kPrimLong &&
552#if defined(__mips__) && !defined(__LP64__)
553 (gpr_index_ == 0 || gpr_index_ == 2) &&
554#else
555 gpr_index_ == 0 &&
556#endif
557 kAlignPairRegister) {
558 // Currently, this is only for ARM and MIPS, where we align long parameters with
559 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
560 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800561 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000562 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000563 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800564 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500565 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
566 // We don't want to split this. Pass over this register.
567 gpr_index_++;
568 is_split_long_or_double_ = false;
569 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800570 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800571 if (kBytesStackArgLocation == 4) {
572 stack_index_+= 2;
573 } else {
574 CHECK_EQ(kBytesStackArgLocation, 8U);
575 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800576 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700577 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800578 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000579 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700580 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800581 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700582 }
583 }
584 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800585 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000586 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800587 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800588 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700589 if (kBytesStackArgLocation == 4) {
590 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800591 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700592 CHECK_EQ(kBytesStackArgLocation, 8U);
593 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800594 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800595 if (kQuickDoubleRegAlignedFloatBackFilled) {
596 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
597 fpr_double_index_ += 2;
598 // Float should not overlap with double.
599 if (fpr_index_ % 2 == 0) {
600 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
601 }
602 }
603 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800604 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800605 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
606 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800607 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800608 }
609 }
610 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800611 }
612 break;
613 default:
614 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
615 }
Ian Rogers848871b2013-08-05 10:56:33 -0700616 }
617 }
618
Andreas Gampec200a4a2014-06-16 18:39:09 -0700619 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700620 const bool is_static_;
621 const char* const shorty_;
622 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700623
624 private:
Ian Rogers13735952014-10-08 12:43:28 -0700625 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
626 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
627 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800628 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800629 // Index into spilled FPRs.
630 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
631 // holds a higher register number.
632 uint32_t fpr_index_;
633 // Index into spilled FPRs for aligned double.
634 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
635 // terms of singles, may be behind fpr_index.
636 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800637 uint32_t stack_index_; // Index into arguments on the stack.
638 // The current type of argument during VisitArguments.
639 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700640 // Does a 64bit parameter straddle the register and stack arguments?
641 bool is_split_long_or_double_;
642};
643
Sebastien Hertza836bc92014-11-25 16:30:53 +0100644// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
645// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700646extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700647 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100648 return QuickArgumentVisitor::GetProxyThisObject(sp);
649}
650
Ian Rogers848871b2013-08-05 10:56:33 -0700651// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800652class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700653 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700654 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
655 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700656 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700657
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700658 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700659
660 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800661 ShadowFrame* const sf_;
662 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700663
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700664 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700665};
666
Andreas Gampec200a4a2014-06-16 18:39:09 -0700667void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700668 Primitive::Type type = GetParamPrimitiveType();
669 switch (type) {
670 case Primitive::kPrimLong: // Fall-through.
671 case Primitive::kPrimDouble:
672 if (IsSplitLongOrDouble()) {
673 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
674 } else {
675 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
676 }
677 ++cur_reg_;
678 break;
679 case Primitive::kPrimNot: {
680 StackReference<mirror::Object>* stack_ref =
681 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
682 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
683 }
684 break;
685 case Primitive::kPrimBoolean: // Fall-through.
686 case Primitive::kPrimByte: // Fall-through.
687 case Primitive::kPrimChar: // Fall-through.
688 case Primitive::kPrimShort: // Fall-through.
689 case Primitive::kPrimInt: // Fall-through.
690 case Primitive::kPrimFloat:
691 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
692 break;
693 case Primitive::kPrimVoid:
694 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700695 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700696 }
697 ++cur_reg_;
698}
699
Mingyao Yang417528d2017-09-13 12:10:40 -0700700// Don't inline. See b/65159206.
701NO_INLINE
702static void HandleDeoptimization(JValue* result,
703 ArtMethod* method,
704 ShadowFrame* deopt_frame,
705 ManagedStack* fragment)
706 REQUIRES_SHARED(Locks::mutator_lock_) {
707 // Coming from partial-fragment deopt.
708 Thread* self = Thread::Current();
709 if (kIsDebugBuild) {
710 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
711 // of the call-stack) corresponds to the called method.
712 ShadowFrame* linked = deopt_frame;
713 while (linked->GetLink() != nullptr) {
714 linked = linked->GetLink();
715 }
716 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
717 << ArtMethod::PrettyMethod(linked->GetMethod());
718 }
719
720 if (VLOG_IS_ON(deopt)) {
721 // Print out the stack to verify that it was a partial-fragment deopt.
722 LOG(INFO) << "Continue-ing from deopt. Stack is:";
723 QuickExceptionHandler::DumpFramesWithType(self, true);
724 }
725
726 ObjPtr<mirror::Throwable> pending_exception;
727 bool from_code = false;
728 DeoptimizationMethodType method_type;
729 self->PopDeoptimizationContext(/* out */ result,
730 /* out */ &pending_exception,
731 /* out */ &from_code,
732 /* out */ &method_type);
733
734 // Push a transition back into managed code onto the linked list in thread.
735 self->PushManagedStackFragment(fragment);
736
737 // Ensure that the stack is still in order.
738 if (kIsDebugBuild) {
739 class DummyStackVisitor : public StackVisitor {
740 public:
741 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
742 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
743
744 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
745 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
746 // logic. Just always say we want to continue.
747 return true;
748 }
749 };
750 DummyStackVisitor dsv(self);
751 dsv.WalkStack();
752 }
753
754 // Restore the exception that was pending before deoptimization then interpret the
755 // deoptimized frames.
756 if (pending_exception != nullptr) {
757 self->SetException(pending_exception);
758 }
759 interpreter::EnterInterpreterFromDeoptimize(self,
760 deopt_frame,
761 result,
762 from_code,
763 DeoptimizationMethodType::kDefault);
764}
765
Mathieu Chartiere401d142015-04-22 13:56:20 -0700766extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700767 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700768 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
769 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700770 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700771
Alex Light9139e002015-10-09 15:59:48 -0700772 if (UNLIKELY(!method->IsInvokable())) {
773 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700774 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700775 }
776
777 JValue tmp_value;
778 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700779 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700780 ManagedStack fragment;
781
David Sehr709b0702016-10-13 09:12:37 -0700782 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700783 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700784 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700785 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700786 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700787 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
788
789 JValue result;
790
Mingyao Yang417528d2017-09-13 12:10:40 -0700791 if (UNLIKELY(deopt_frame != nullptr)) {
792 HandleDeoptimization(&result, method, deopt_frame, &fragment);
Ian Rogers848871b2013-08-05 10:56:33 -0700793 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700794 const char* old_cause = self->StartAssertNoThreadSuspension(
795 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700796 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700797 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700798 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700799 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700800 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700801 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700802 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800803 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700804 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800805 const bool needs_initialization =
806 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700807 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700808 self->PushManagedStackFragment(&fragment);
809 self->PushShadowFrame(shadow_frame);
810 self->EndAssertNoThreadSuspension(old_cause);
811
Ian Rogerse94652f2014-12-02 11:13:19 -0800812 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700813 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800814 StackHandleScope<1> hs(self);
815 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700816 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700817 DCHECK(Thread::Current()->IsExceptionPending())
818 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700819 self->PopManagedStackFragment(fragment);
820 return 0;
821 }
822 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200823
Andreas Gampe639bdd12015-06-03 11:22:45 -0700824 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700825 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700826
827 // Pop transition.
828 self->PopManagedStackFragment(fragment);
829
830 // Request a stack deoptimization if needed
831 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700832 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700833 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
834 // should be done and it knows the real return pc.
835 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000836 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
837 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
838 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
839 << caller->PrettyMethod();
840 } else {
841 // Push the context of the deoptimization stack so we can restore the return value and the
842 // exception before executing the deoptimized frames.
843 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700844 result,
845 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
846 self->GetException(),
847 false /* from_code */,
848 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700849
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000850 // Set special exception to cause deoptimization.
851 self->SetException(Thread::GetDeoptimizationException());
852 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700853 }
854
855 // No need to restore the args since the method has already been run by the interpreter.
856 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700857}
858
859// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
860// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800861class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700862 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700863 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700864 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700865 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700866
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700867 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700868
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700869 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800870
Ian Rogers848871b2013-08-05 10:56:33 -0700871 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700872 ScopedObjectAccessUnchecked* const soa_;
873 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800874 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700875 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700876
Ian Rogers848871b2013-08-05 10:56:33 -0700877 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
878};
879
Ian Rogers9758f792014-03-13 09:02:55 -0700880void BuildQuickArgumentVisitor::Visit() {
881 jvalue val;
882 Primitive::Type type = GetParamPrimitiveType();
883 switch (type) {
884 case Primitive::kPrimNot: {
885 StackReference<mirror::Object>* stack_ref =
886 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
887 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
888 references_.push_back(std::make_pair(val.l, stack_ref));
889 break;
890 }
891 case Primitive::kPrimLong: // Fall-through.
892 case Primitive::kPrimDouble:
893 if (IsSplitLongOrDouble()) {
894 val.j = ReadSplitLongParam();
895 } else {
896 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
897 }
898 break;
899 case Primitive::kPrimBoolean: // Fall-through.
900 case Primitive::kPrimByte: // Fall-through.
901 case Primitive::kPrimChar: // Fall-through.
902 case Primitive::kPrimShort: // Fall-through.
903 case Primitive::kPrimInt: // Fall-through.
904 case Primitive::kPrimFloat:
905 val.i = *reinterpret_cast<jint*>(GetParamAddress());
906 break;
907 case Primitive::kPrimVoid:
908 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700909 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700910 }
911 args_->push_back(val);
912}
913
914void BuildQuickArgumentVisitor::FixupReferences() {
915 // Fixup any references which may have changed.
916 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700917 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700918 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700919 }
920}
Ian Rogers848871b2013-08-05 10:56:33 -0700921// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
922// which is responsible for recording callee save registers. We explicitly place into jobjects the
923// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
924// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700925extern "C" uint64_t artQuickProxyInvokeHandler(
926 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700927 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700928 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
929 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700930 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
931 const char* old_cause =
932 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
933 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700934 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700935 self->VerifyStack();
936 // Start new JNI local reference state.
937 JNIEnvExt* env = self->GetJniEnv();
938 ScopedObjectAccessUnchecked soa(env);
939 ScopedJniEnvLocalRefState env_state(env);
940 // Create local ref. copies of proxy method and the receiver.
941 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
942
943 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700944 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700945 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
946 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700947 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700948 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700949 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700950 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700951
Ian Rogers848871b2013-08-05 10:56:33 -0700952 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700953 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700954 args.erase(args.begin());
955
956 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700957 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700958 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
959 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700960 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700961 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700962 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700963 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700964 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
965 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700966
967 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
968 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700969 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800970 // Restore references which might have moved.
971 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700972 return result.GetJ();
973}
974
975// Read object references held in arguments from quick frames and place in a JNI local references,
976// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800977class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700978 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700979 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
980 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700981 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700982
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700983 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700984
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700985 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700986
987 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700988 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800989 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700990 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
991
Mathieu Chartier590fee92013-09-13 13:46:47 -0700992 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700993};
994
Ian Rogers9758f792014-03-13 09:02:55 -0700995void RememberForGcArgumentVisitor::Visit() {
996 if (IsParamAReference()) {
997 StackReference<mirror::Object>* stack_ref =
998 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
999 jobject reference =
1000 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
1001 references_.push_back(std::make_pair(reference, stack_ref));
1002 }
1003}
1004
1005void RememberForGcArgumentVisitor::FixupReferences() {
1006 // Fixup any references which may have changed.
1007 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -07001008 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07001009 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001010 }
1011}
1012
Alex Lightb7edcda2017-04-27 13:20:31 -07001013extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1014 mirror::Object* this_object,
1015 Thread* self,
1016 ArtMethod** sp)
1017 REQUIRES_SHARED(Locks::mutator_lock_) {
1018 const void* result;
1019 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1020 // that part.
1021 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1022 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1023 if (instrumentation->IsDeoptimized(method)) {
1024 result = GetQuickToInterpreterBridge();
1025 } else {
1026 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1027 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1028 }
1029
1030 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1031 bool is_static = method->IsStatic();
1032 uint32_t shorty_len;
1033 const char* shorty =
1034 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1035
1036 ScopedObjectAccessUnchecked soa(self);
1037 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1038 visitor.VisitArguments();
1039
1040 instrumentation->PushInstrumentationStackFrame(self,
1041 is_static ? nullptr : this_object,
1042 method,
1043 QuickArgumentVisitor::GetCallingPc(sp),
1044 interpreter_entry);
1045
1046 visitor.FixupReferences();
1047 if (UNLIKELY(self->IsExceptionPending())) {
1048 return nullptr;
1049 }
1050 CHECK(result != nullptr) << method->PrettyMethod();
1051 return result;
1052}
1053
1054extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1055 ArtMethod** sp,
1056 uint64_t* gpr_result,
1057 uint64_t* fpr_result)
1058 REQUIRES_SHARED(Locks::mutator_lock_) {
1059 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1060 CHECK(gpr_result != nullptr);
1061 CHECK(fpr_result != nullptr);
1062 // Instrumentation exit stub must not be entered with a pending exception.
1063 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1064 << self->GetException()->Dump();
1065 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001066 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1067 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001068 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1069 return_pc_offset);
1070 CHECK_EQ(*return_pc, 0U);
1071
1072 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1073 // deoptimization shouldn't be performed with the high-half having the return address. When
1074 // deoptimization should be performed the low half is zero and the high-half the address of the
1075 // deoptimization entry point.
1076 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1077 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1078 self, return_pc, gpr_result, fpr_result);
1079 if (self->IsExceptionPending()) {
1080 return GetTwoWordFailureValue();
1081 }
1082 return return_or_deoptimize_pc;
1083}
1084
Ian Rogers848871b2013-08-05 10:56:33 -07001085// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001086extern "C" const void* artQuickResolutionTrampoline(
1087 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001088 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001089 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1090 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1091 // does not have the same stack layout as the callee-save method).
1092 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001093 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001094 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001095 ScopedObjectAccessUnchecked soa(env);
1096 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001097 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001098
1099 // Compute details about the called method (avoid GCs)
1100 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001101 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001102 MethodReference called_method(nullptr, 0);
1103 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001104 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001105 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001106 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001107 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001108
1109 InvokeType stack_map_invoke_type;
1110 uint32_t stack_map_dex_method_idx;
1111 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1112 &stack_map_invoke_type,
1113 &stack_map_dex_method_idx);
1114 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1115 // code.
1116 if (!found_stack_map || kIsDebugBuild) {
1117 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1118 const DexFile::CodeItem* code;
1119 code = caller->GetCodeItem();
1120 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1121 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1122 Instruction::Code instr_code = instr->Opcode();
1123 bool is_range;
1124 switch (instr_code) {
1125 case Instruction::INVOKE_DIRECT:
1126 invoke_type = kDirect;
1127 is_range = false;
1128 break;
1129 case Instruction::INVOKE_DIRECT_RANGE:
1130 invoke_type = kDirect;
1131 is_range = true;
1132 break;
1133 case Instruction::INVOKE_STATIC:
1134 invoke_type = kStatic;
1135 is_range = false;
1136 break;
1137 case Instruction::INVOKE_STATIC_RANGE:
1138 invoke_type = kStatic;
1139 is_range = true;
1140 break;
1141 case Instruction::INVOKE_SUPER:
1142 invoke_type = kSuper;
1143 is_range = false;
1144 break;
1145 case Instruction::INVOKE_SUPER_RANGE:
1146 invoke_type = kSuper;
1147 is_range = true;
1148 break;
1149 case Instruction::INVOKE_VIRTUAL:
1150 invoke_type = kVirtual;
1151 is_range = false;
1152 break;
1153 case Instruction::INVOKE_VIRTUAL_RANGE:
1154 invoke_type = kVirtual;
1155 is_range = true;
1156 break;
1157 case Instruction::INVOKE_INTERFACE:
1158 invoke_type = kInterface;
1159 is_range = false;
1160 break;
1161 case Instruction::INVOKE_INTERFACE_RANGE:
1162 invoke_type = kInterface;
1163 is_range = true;
1164 break;
1165 default:
1166 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1167 UNREACHABLE();
1168 }
1169 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1170 // Check that the invoke matches what we expected, note that this path only happens for debug
1171 // builds.
1172 if (found_stack_map) {
1173 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1174 if (invoke_type != kSuper) {
1175 // Super may be sharpened.
1176 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1177 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1178 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1179 }
1180 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001181 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001182 << called_method.dex_method_index;
1183 }
1184 } else {
1185 invoke_type = stack_map_invoke_type;
1186 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001187 }
Ian Rogers848871b2013-08-05 10:56:33 -07001188 } else {
1189 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001190 called_method.dex_file = called->GetDexFile();
1191 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001192 }
1193 uint32_t shorty_len;
1194 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001195 called_method.dex_file->GetMethodShorty(
1196 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001197 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001198 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001199 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001200 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001201 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001202 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001203 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001204 mirror::Object* dummy = nullptr;
1205 HandleWrapper<mirror::Object> h_receiver(
1206 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001207 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001208 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001209 self, called_method.dex_method_index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001210
1211 // Update .bss entry in oat file if any.
1212 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1213 const MethodBssMapping* mapping =
1214 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1215 if (mapping != nullptr) {
1216 auto pp = std::partition_point(
1217 mapping->begin(),
1218 mapping->end(),
1219 [called_method](const MethodBssMappingEntry& entry) {
1220 return entry.method_index < called_method.dex_method_index;
1221 });
1222 if (pp != mapping->end() && pp->CoversIndex(called_method.dex_method_index)) {
1223 size_t bss_offset = pp->GetBssOffset(called_method.dex_method_index,
1224 static_cast<size_t>(kRuntimePointerSize));
1225 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1226 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1227 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1228 oat_file->BssBegin() + bss_offset));
1229 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1230 DCHECK_LT(method_entry,
1231 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1232 *method_entry = called;
1233 }
1234 }
1235 }
Ian Rogers848871b2013-08-05 10:56:33 -07001236 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001237 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001238 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001239 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001240 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001241 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001242 if (virtual_or_interface || invoke_type == kSuper) {
1243 // Refine called method based on receiver for kVirtual/kInterface, and
1244 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001245 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001246 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001247 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001248 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001249 } else if (invoke_type == kInterface) {
1250 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001251 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001252 } else {
1253 DCHECK_EQ(invoke_type, kSuper);
1254 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001255 StackHandleScope<2> hs(self);
1256 Handle<mirror::DexCache> dex_cache(
1257 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1258 Handle<mirror::ClassLoader> class_loader(
1259 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001260 // TODO Maybe put this into a mirror::Class function.
Vladimir Markoba118822017-06-12 15:41:56 +01001261 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
1262 *dex_cache->GetDexFile(),
1263 dex_cache->GetDexFile()->GetMethodId(called_method.dex_method_index).class_idx_,
1264 dex_cache.Get(),
1265 class_loader.Get());
Alex Lightfedd91d2016-01-07 14:49:16 -08001266 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001267 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001268 } else {
1269 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001270 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001271 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001272 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001273
David Sehr709b0702016-10-13 09:12:37 -07001274 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1275 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001276 << invoke_type << " " << orig_called->GetVtableIndex();
1277
Ian Rogers83883d72013-10-21 21:07:24 -07001278 // We came here because of sharpening. Ensure the dex cache is up-to-date on the method index
Ian Rogerse0a02da2014-12-02 14:10:53 -08001279 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001280 // Note, called_method.dex_method_index references the dex method before the
1281 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1282 // about the name and signature.
1283 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001284 if (called->GetDexFile() != caller->GetDexFile()) {
Ian Rogers83883d72013-10-21 21:07:24 -07001285 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001286 // the caller's dex file. Since we get here only if the original called was a runtime
1287 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001288 DCHECK(!called_method_known_on_entry);
1289 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1290 const DexFile* caller_dex_file = called_method.dex_file;
1291 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1292 update_dex_cache_method_index =
1293 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1294 caller_method_name_and_sig_index);
1295 }
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001296 if (update_dex_cache_method_index != DexFile::kDexNoIndex) {
1297 // Note: We do not need the read barrier for the dex cache as the SetResolvedMethod()
1298 // operates on native (non-moveable) data and constants (num_resolved_methods_).
1299 ObjPtr<mirror::DexCache> caller_dex_cache = caller->GetDexCache<kWithoutReadBarrier>();
1300 if (caller_dex_cache->GetResolvedMethod(
1301 update_dex_cache_method_index, kRuntimePointerSize) != called) {
1302 caller_dex_cache->SetResolvedMethod(update_dex_cache_method_index,
1303 called,
1304 kRuntimePointerSize);
1305 }
Ian Rogers83883d72013-10-21 21:07:24 -07001306 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001307 } else if (invoke_type == kStatic) {
1308 const auto called_dex_method_idx = called->GetDexMethodIndex();
1309 // For static invokes, we may dispatch to the static method in the superclass but resolve
1310 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1311 // resolved method for the super class dex method index if we are in the same dex file.
1312 // b/19175856
1313 if (called->GetDexFile() == called_method.dex_file &&
1314 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001315 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1316 called,
1317 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001318 }
Ian Rogers83883d72013-10-21 21:07:24 -07001319 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001320
Ian Rogers848871b2013-08-05 10:56:33 -07001321 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001322 StackHandleScope<1> hs(soa.Self());
1323 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001324 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001325 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001326 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1327 // If we are single-stepping or the called method is deoptimized (by a
1328 // breakpoint, for example), then we have to execute the called method
1329 // with the interpreter.
1330 code = GetQuickToInterpreterBridge();
1331 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1332 // If the caller is deoptimized (by a breakpoint, for example), we have to
1333 // continue its execution with interpreter when returning from the called
1334 // method. Because we do not want to execute the called method with the
1335 // interpreter, we wrap its execution into the instrumentation stubs.
1336 // When the called method returns, it will execute the instrumentation
1337 // exit hook that will determine the need of the interpreter with a call
1338 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1339 // it is needed.
1340 code = GetQuickInstrumentationEntryPoint();
1341 } else {
1342 code = called->GetEntryPointFromQuickCompiledCode();
1343 }
Ian Rogers848871b2013-08-05 10:56:33 -07001344 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001345 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1346 // If we are single-stepping or the called method is deoptimized (by a
1347 // breakpoint, for example), then we have to execute the called method
1348 // with the interpreter.
1349 code = GetQuickToInterpreterBridge();
1350 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001351 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1352 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001353 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001354 } else {
1355 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001356 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001357 }
1358 } else {
1359 DCHECK(called_class->IsErroneous());
1360 }
1361 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001362 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001363 // Fixup any locally saved objects may have moved during a GC.
1364 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001365 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001366 *sp = called;
1367
Ian Rogers848871b2013-08-05 10:56:33 -07001368 return code;
1369}
1370
Andreas Gampec147b002014-03-06 18:11:06 -08001371/*
1372 * This class uses a couple of observations to unite the different calling conventions through
1373 * a few constants.
1374 *
1375 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1376 * possible alignment.
1377 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1378 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1379 * when we have to split things
1380 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1381 * and we can use Int handling directly.
1382 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1383 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1384 * extension should be compatible with Aarch64, which mandates copying the available bits
1385 * into LSB and leaving the rest unspecified.
1386 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1387 * the stack.
1388 * 6) There is only little endian.
1389 *
1390 *
1391 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1392 * follows:
1393 *
1394 * void PushGpr(uintptr_t): Add a value for the next GPR
1395 *
1396 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1397 * padding, that is, think the architecture is 32b and aligns 64b.
1398 *
1399 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1400 * split this if necessary. The current state will have aligned, if
1401 * necessary.
1402 *
1403 * void PushStack(uintptr_t): Push a value to the stack.
1404 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001405 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001406 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001407 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001408 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001409 *
1410 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001411template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001412 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001413#if defined(__arm__)
1414 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001415 static constexpr bool kNativeSoftFloatAbi = true;
1416 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001417 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1418
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001419 static constexpr size_t kRegistersNeededForLong = 2;
1420 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001421 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001422 static constexpr bool kMultiFPRegistersWidened = false;
1423 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001424 static constexpr bool kAlignLongOnStack = true;
1425 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001426#elif defined(__aarch64__)
1427 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1428 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1429 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1430
1431 static constexpr size_t kRegistersNeededForLong = 1;
1432 static constexpr size_t kRegistersNeededForDouble = 1;
1433 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001434 static constexpr bool kMultiFPRegistersWidened = false;
1435 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001436 static constexpr bool kAlignLongOnStack = false;
1437 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001438#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001439 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001440 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1441 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001442
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001443 static constexpr size_t kRegistersNeededForLong = 2;
1444 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001445 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001446 static constexpr bool kMultiFPRegistersWidened = true;
1447 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001448 static constexpr bool kAlignLongOnStack = true;
1449 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001450#elif defined(__mips__) && defined(__LP64__)
1451 // Let the code prepare GPRs only and we will load the FPRs with same data.
1452 static constexpr bool kNativeSoftFloatAbi = true;
1453 static constexpr size_t kNumNativeGprArgs = 8;
1454 static constexpr size_t kNumNativeFprArgs = 0;
1455
1456 static constexpr size_t kRegistersNeededForLong = 1;
1457 static constexpr size_t kRegistersNeededForDouble = 1;
1458 static constexpr bool kMultiRegistersAligned = false;
1459 static constexpr bool kMultiFPRegistersWidened = false;
1460 static constexpr bool kMultiGPRegistersWidened = true;
1461 static constexpr bool kAlignLongOnStack = false;
1462 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001463#elif defined(__i386__)
1464 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001465 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001466 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1467 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1468
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001469 static constexpr size_t kRegistersNeededForLong = 2;
1470 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001471 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001472 static constexpr bool kMultiFPRegistersWidened = false;
1473 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001474 static constexpr bool kAlignLongOnStack = false;
1475 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001476#elif defined(__x86_64__)
1477 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1478 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1479 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1480
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001481 static constexpr size_t kRegistersNeededForLong = 1;
1482 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001483 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001484 static constexpr bool kMultiFPRegistersWidened = false;
1485 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001486 static constexpr bool kAlignLongOnStack = false;
1487 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001488#else
1489#error "Unsupported architecture"
1490#endif
1491
Andreas Gampec147b002014-03-06 18:11:06 -08001492 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001493 explicit BuildNativeCallFrameStateMachine(T* delegate)
1494 : gpr_index_(kNumNativeGprArgs),
1495 fpr_index_(kNumNativeFprArgs),
1496 stack_entries_(0),
1497 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001498 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1499 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001500 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1501 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001502 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001503
Andreas Gampec200a4a2014-06-16 18:39:09 -07001504 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001505
Ian Rogers1428dce2014-10-21 15:02:15 -07001506 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001507 return gpr_index_ > 0;
1508 }
1509
Andreas Gampec200a4a2014-06-16 18:39:09 -07001510 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001511 if (HavePointerGpr()) {
1512 gpr_index_--;
1513 PushGpr(reinterpret_cast<uintptr_t>(val));
1514 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001515 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001516 PushStack(reinterpret_cast<uintptr_t>(val));
1517 gpr_index_ = 0;
1518 }
1519 }
1520
Ian Rogers1428dce2014-10-21 15:02:15 -07001521 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001522 return gpr_index_ > 0;
1523 }
1524
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001525 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001526 uintptr_t handle = PushHandle(ptr);
1527 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001528 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001529 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001530 } else {
1531 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001532 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001533 gpr_index_ = 0;
1534 }
1535 }
1536
Ian Rogers1428dce2014-10-21 15:02:15 -07001537 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001538 return gpr_index_ > 0;
1539 }
1540
1541 void AdvanceInt(uint32_t val) {
1542 if (HaveIntGpr()) {
1543 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001544 if (kMultiGPRegistersWidened) {
1545 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001546 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001547 } else {
1548 PushGpr(val);
1549 }
Andreas Gampec147b002014-03-06 18:11:06 -08001550 } else {
1551 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001552 if (kMultiGPRegistersWidened) {
1553 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001554 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001555 } else {
1556 PushStack(val);
1557 }
Andreas Gampec147b002014-03-06 18:11:06 -08001558 gpr_index_ = 0;
1559 }
1560 }
1561
Ian Rogers1428dce2014-10-21 15:02:15 -07001562 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001563 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1564 }
1565
Ian Rogers1428dce2014-10-21 15:02:15 -07001566 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001567 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1568 kAlignLongOnStack && // and when it needs alignment
1569 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1570 }
1571
Ian Rogers1428dce2014-10-21 15:02:15 -07001572 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001573 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1574 kAlignLongOnStack && // and when it needs 8B alignment
1575 (stack_entries_ & 1) == 1; // counter is odd
1576 }
1577
1578 void AdvanceLong(uint64_t val) {
1579 if (HaveLongGpr()) {
1580 if (LongGprNeedsPadding()) {
1581 PushGpr(0);
1582 gpr_index_--;
1583 }
1584 if (kRegistersNeededForLong == 1) {
1585 PushGpr(static_cast<uintptr_t>(val));
1586 } else {
1587 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1588 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1589 }
1590 gpr_index_ -= kRegistersNeededForLong;
1591 } else {
1592 if (LongStackNeedsPadding()) {
1593 PushStack(0);
1594 stack_entries_++;
1595 }
1596 if (kRegistersNeededForLong == 1) {
1597 PushStack(static_cast<uintptr_t>(val));
1598 stack_entries_++;
1599 } else {
1600 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1601 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1602 stack_entries_ += 2;
1603 }
1604 gpr_index_ = 0;
1605 }
1606 }
1607
Ian Rogers1428dce2014-10-21 15:02:15 -07001608 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001609 return fpr_index_ > 0;
1610 }
1611
Andreas Gampec147b002014-03-06 18:11:06 -08001612 void AdvanceFloat(float val) {
1613 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001614 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001615 } else {
1616 if (HaveFloatFpr()) {
1617 fpr_index_--;
1618 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001619 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001620 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001621 } else {
1622 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001623 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001624 }
1625 } else {
1626 PushFpr4(val);
1627 }
1628 } else {
1629 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001630 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001631 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001632 // Note: We need to jump through those hoops to make the compiler happy.
1633 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001634 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001635 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001636 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001637 }
1638 fpr_index_ = 0;
1639 }
1640 }
1641 }
1642
Ian Rogers1428dce2014-10-21 15:02:15 -07001643 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001644 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1645 }
1646
Ian Rogers1428dce2014-10-21 15:02:15 -07001647 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001648 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1649 kAlignDoubleOnStack && // and when it needs alignment
1650 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1651 }
1652
Ian Rogers1428dce2014-10-21 15:02:15 -07001653 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001654 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1655 kAlignDoubleOnStack && // and when it needs 8B alignment
1656 (stack_entries_ & 1) == 1; // counter is odd
1657 }
1658
1659 void AdvanceDouble(uint64_t val) {
1660 if (kNativeSoftFloatAbi) {
1661 AdvanceLong(val);
1662 } else {
1663 if (HaveDoubleFpr()) {
1664 if (DoubleFprNeedsPadding()) {
1665 PushFpr4(0);
1666 fpr_index_--;
1667 }
1668 PushFpr8(val);
1669 fpr_index_ -= kRegistersNeededForDouble;
1670 } else {
1671 if (DoubleStackNeedsPadding()) {
1672 PushStack(0);
1673 stack_entries_++;
1674 }
1675 if (kRegistersNeededForDouble == 1) {
1676 PushStack(static_cast<uintptr_t>(val));
1677 stack_entries_++;
1678 } else {
1679 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1680 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1681 stack_entries_ += 2;
1682 }
1683 fpr_index_ = 0;
1684 }
1685 }
1686 }
1687
Ian Rogers1428dce2014-10-21 15:02:15 -07001688 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001689 return stack_entries_;
1690 }
1691
Ian Rogers1428dce2014-10-21 15:02:15 -07001692 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001693 return kNumNativeGprArgs - gpr_index_;
1694 }
1695
Ian Rogers1428dce2014-10-21 15:02:15 -07001696 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001697 return kNumNativeFprArgs - fpr_index_;
1698 }
1699
1700 private:
1701 void PushGpr(uintptr_t val) {
1702 delegate_->PushGpr(val);
1703 }
1704 void PushFpr4(float val) {
1705 delegate_->PushFpr4(val);
1706 }
1707 void PushFpr8(uint64_t val) {
1708 delegate_->PushFpr8(val);
1709 }
1710 void PushStack(uintptr_t val) {
1711 delegate_->PushStack(val);
1712 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001713 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001714 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001715 }
1716
1717 uint32_t gpr_index_; // Number of free GPRs
1718 uint32_t fpr_index_; // Number of free FPRs
1719 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1720 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001721 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001722};
1723
Andreas Gampec200a4a2014-06-16 18:39:09 -07001724// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1725// in subclasses.
1726//
1727// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1728// them with handles.
1729class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001730 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001731 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1732
1733 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001734
Ian Rogers1428dce2014-10-21 15:02:15 -07001735 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001736 return num_stack_entries_ * sizeof(uintptr_t);
1737 }
1738
Ian Rogers1428dce2014-10-21 15:02:15 -07001739 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001740 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001741 // Align by kStackAlignment.
1742 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001743 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001744 }
1745
Ian Rogers1428dce2014-10-21 15:02:15 -07001746 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1747 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001748 // Assumption is OK right now, as we have soft-float arm
1749 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1750 sp8 -= fregs * sizeof(uintptr_t);
1751 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1752 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1753 sp8 -= iregs * sizeof(uintptr_t);
1754 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1755 return sp8;
1756 }
Andreas Gampec147b002014-03-06 18:11:06 -08001757
Andreas Gampec200a4a2014-06-16 18:39:09 -07001758 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001759 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001760 // Native call stack.
1761 sp8 = LayoutCallStack(sp8);
1762 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001763
Andreas Gampec200a4a2014-06-16 18:39:09 -07001764 // Put fprs and gprs below.
1765 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001766
Andreas Gampec200a4a2014-06-16 18:39:09 -07001767 // Return the new bottom.
1768 return sp8;
1769 }
1770
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001771 virtual void WalkHeader(
1772 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001773 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001774 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001775
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001776 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1778
1779 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001780
1781 for (uint32_t i = 1; i < shorty_len; ++i) {
1782 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1783 switch (cur_type_) {
1784 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001785 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001786 sm.AdvanceHandleScope(
1787 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001788 break;
1789
1790 case Primitive::kPrimBoolean:
1791 case Primitive::kPrimByte:
1792 case Primitive::kPrimChar:
1793 case Primitive::kPrimShort:
1794 case Primitive::kPrimInt:
1795 sm.AdvanceInt(0);
1796 break;
1797 case Primitive::kPrimFloat:
1798 sm.AdvanceFloat(0);
1799 break;
1800 case Primitive::kPrimDouble:
1801 sm.AdvanceDouble(0);
1802 break;
1803 case Primitive::kPrimLong:
1804 sm.AdvanceLong(0);
1805 break;
1806 default:
1807 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001808 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001809 }
1810 }
1811
Ian Rogers1428dce2014-10-21 15:02:15 -07001812 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001813 }
1814
1815 void PushGpr(uintptr_t /* val */) {
1816 // not optimizing registers, yet
1817 }
1818
1819 void PushFpr4(float /* val */) {
1820 // not optimizing registers, yet
1821 }
1822
1823 void PushFpr8(uint64_t /* val */) {
1824 // not optimizing registers, yet
1825 }
1826
1827 void PushStack(uintptr_t /* val */) {
1828 // counting is already done in the superclass
1829 }
1830
Andreas Gampec200a4a2014-06-16 18:39:09 -07001831 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001832 return reinterpret_cast<uintptr_t>(nullptr);
1833 }
1834
Andreas Gampec200a4a2014-06-16 18:39:09 -07001835 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001836 uint32_t num_stack_entries_;
1837};
1838
Andreas Gampec200a4a2014-06-16 18:39:09 -07001839class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001840 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001841 explicit ComputeGenericJniFrameSize(bool critical_native)
1842 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001843
Andreas Gampec200a4a2014-06-16 18:39:09 -07001844 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1845 // is at *m = sp. Will update to point to the bottom of the save frame.
1846 //
1847 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001848 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001849 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001850 ArtMethod* method = **m;
1851
Andreas Gampe542451c2016-07-26 09:02:02 -07001852 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001853
Andreas Gampec200a4a2014-06-16 18:39:09 -07001854 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1855
1856 // First, fix up the layout of the callee-save frame.
1857 // We have to squeeze in the HandleScope, and relocate the method pointer.
1858
1859 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001860 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001861
1862 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001863 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001864 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001865
1866 sp8 -= scope_and_method;
1867 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001868 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001869
Mathieu Chartiere401d142015-04-22 13:56:20 -07001870 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001871 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1872 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001873
1874 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1875 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001876 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1877 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001878 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001879 }
1880
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001882 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001883 // Reference cookie and padding
1884 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001885 }
1886
Andreas Gampec200a4a2014-06-16 18:39:09 -07001887 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1888 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001889 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001890 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001891 // First, fix up the layout of the callee-save frame.
1892 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001893 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894
1895 // The bottom of the callee-save frame is now where the method is, *m.
1896 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1897
1898 // Add space for cookie.
1899 LayoutCookie(&sp8);
1900
1901 return sp8;
1902 }
1903
1904 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1906 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1907 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001908 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001909 Walk(shorty, shorty_len);
1910
1911 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001912 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001913
1914 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1915
1916 // Return the new bottom.
1917 return sp8;
1918 }
1919
1920 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1921
1922 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1923 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001924 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001925
1926 private:
1927 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001928 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001929};
1930
1931uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1932 num_handle_scope_references_++;
1933 return reinterpret_cast<uintptr_t>(nullptr);
1934}
1935
1936void ComputeGenericJniFrameSize::WalkHeader(
1937 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001938 // First 2 parameters are always excluded for @CriticalNative.
1939 if (UNLIKELY(critical_native_)) {
1940 return;
1941 }
1942
Andreas Gampec200a4a2014-06-16 18:39:09 -07001943 // JNIEnv
1944 sm->AdvancePointer(nullptr);
1945
1946 // Class object or this as first argument
1947 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1948}
1949
1950// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1951// the template requirements of BuildGenericJniFrameStateMachine.
1952class FillNativeCall {
1953 public:
1954 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1955 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1956
1957 virtual ~FillNativeCall() {}
1958
1959 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1960 cur_gpr_reg_ = gpr_regs;
1961 cur_fpr_reg_ = fpr_regs;
1962 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001963 }
1964
1965 void PushGpr(uintptr_t val) {
1966 *cur_gpr_reg_ = val;
1967 cur_gpr_reg_++;
1968 }
1969
1970 void PushFpr4(float val) {
1971 *cur_fpr_reg_ = val;
1972 cur_fpr_reg_++;
1973 }
1974
1975 void PushFpr8(uint64_t val) {
1976 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1977 *tmp = val;
1978 cur_fpr_reg_ += 2;
1979 }
1980
1981 void PushStack(uintptr_t val) {
1982 *cur_stack_arg_ = val;
1983 cur_stack_arg_++;
1984 }
1985
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001986 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001988 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001989 }
1990
1991 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001992 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001993 uint32_t* cur_fpr_reg_;
1994 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001995};
Andreas Gampec147b002014-03-06 18:11:06 -08001996
Andreas Gampec200a4a2014-06-16 18:39:09 -07001997// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1998// of transitioning into native code.
1999class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
2000 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07002001 BuildGenericJniFrameVisitor(Thread* self,
2002 bool is_static,
2003 bool critical_native,
2004 const char* shorty,
2005 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002006 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07002008 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
2009 sm_(&jni_call_) {
2010 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002011 uintptr_t* start_gpr_reg;
2012 uint32_t* start_fpr_reg;
2013 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002014 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002015 &handle_scope_,
2016 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002017 &start_gpr_reg, &start_fpr_reg);
2018
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2020
Igor Murashkin06a04e02016-09-13 15:57:37 -07002021 // First 2 parameters are always excluded for CriticalNative methods.
2022 if (LIKELY(!critical_native)) {
2023 // jni environment is always first argument
2024 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002025
Igor Murashkin06a04e02016-09-13 15:57:37 -07002026 if (is_static) {
2027 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2028 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002029 }
2030 }
2031
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002032 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002033
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002034 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002035
Vladimir Markof39745e2016-01-26 12:16:55 +00002036 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002037 return handle_scope_->GetHandle(0).GetReference();
2038 }
2039
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002040 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002041 return handle_scope_->GetHandle(0).ToJObject();
2042 }
2043
Ian Rogers1428dce2014-10-21 15:02:15 -07002044 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002045 return bottom_of_used_area_;
2046 }
2047
2048 private:
2049 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2050 class FillJniCall FINAL : public FillNativeCall {
2051 public:
2052 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002053 HandleScope* handle_scope, bool critical_native)
2054 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2055 handle_scope_(handle_scope),
2056 cur_entry_(0),
2057 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002058
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002059 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002060
2061 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2062 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2063 handle_scope_ = scope;
2064 cur_entry_ = 0U;
2065 }
2066
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002067 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068 // Initialize padding entries.
2069 size_t expected_slots = handle_scope_->NumberOfReferences();
2070 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002071 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002072 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002073
2074 if (!critical_native_) {
2075 // Non-critical natives have at least the self class (jclass) or this (jobject).
2076 DCHECK_NE(cur_entry_, 0U);
2077 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002078 }
2079
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002080 bool CriticalNative() const {
2081 return critical_native_;
2082 }
2083
Andreas Gampec200a4a2014-06-16 18:39:09 -07002084 private:
2085 HandleScope* handle_scope_;
2086 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002087 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002088 };
2089
2090 HandleScope* handle_scope_;
2091 FillJniCall jni_call_;
2092 void* bottom_of_used_area_;
2093
2094 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002095
2096 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2097};
2098
Andreas Gampec200a4a2014-06-16 18:39:09 -07002099uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2100 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002101 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002102 h.Assign(ref);
2103 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2104 cur_entry_++;
2105 return tmp;
2106}
2107
Ian Rogers9758f792014-03-13 09:02:55 -07002108void BuildGenericJniFrameVisitor::Visit() {
2109 Primitive::Type type = GetParamPrimitiveType();
2110 switch (type) {
2111 case Primitive::kPrimLong: {
2112 jlong long_arg;
2113 if (IsSplitLongOrDouble()) {
2114 long_arg = ReadSplitLongParam();
2115 } else {
2116 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2117 }
2118 sm_.AdvanceLong(long_arg);
2119 break;
2120 }
2121 case Primitive::kPrimDouble: {
2122 uint64_t double_arg;
2123 if (IsSplitLongOrDouble()) {
2124 // Read into union so that we don't case to a double.
2125 double_arg = ReadSplitLongParam();
2126 } else {
2127 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2128 }
2129 sm_.AdvanceDouble(double_arg);
2130 break;
2131 }
2132 case Primitive::kPrimNot: {
2133 StackReference<mirror::Object>* stack_ref =
2134 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002135 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002136 break;
2137 }
2138 case Primitive::kPrimFloat:
2139 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2140 break;
2141 case Primitive::kPrimBoolean: // Fall-through.
2142 case Primitive::kPrimByte: // Fall-through.
2143 case Primitive::kPrimChar: // Fall-through.
2144 case Primitive::kPrimShort: // Fall-through.
2145 case Primitive::kPrimInt: // Fall-through.
2146 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2147 break;
2148 case Primitive::kPrimVoid:
2149 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002150 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002151 }
2152}
2153
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002154void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002155 // Clear out rest of the scope.
2156 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002157 if (!jni_call_.CriticalNative()) {
2158 // Install HandleScope.
2159 self->PushHandleScope(handle_scope_);
2160 }
Ian Rogers9758f792014-03-13 09:02:55 -07002161}
2162
Ian Rogers04c31d22014-07-07 21:44:06 -07002163#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002164extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002165#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002166extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002167#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002168
Igor Murashkin06a04e02016-09-13 15:57:37 -07002169static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2170 uint32_t cookie,
2171 bool fast_native ATTRIBUTE_UNUSED,
2172 jobject l,
2173 jobject lock) {
2174 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002175 if (lock != nullptr) {
2176 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2177 } else {
2178 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2179 }
2180}
2181
Igor Murashkin06a04e02016-09-13 15:57:37 -07002182static void artQuickGenericJniEndJNINonRef(Thread* self,
2183 uint32_t cookie,
2184 bool fast_native,
2185 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002186 if (lock != nullptr) {
2187 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002188 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002189 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002190 if (UNLIKELY(fast_native)) {
2191 JniMethodFastEnd(cookie, self);
2192 } else {
2193 JniMethodEnd(cookie, self);
2194 }
Andreas Gampead615172014-04-04 16:20:13 -07002195 }
2196}
2197
Andreas Gampec147b002014-03-06 18:11:06 -08002198/*
2199 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002200 * 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 -08002201 * The final element on the stack is a pointer to the native code.
2202 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002203 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002204 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002205 *
Andreas Gampec147b002014-03-06 18:11:06 -08002206 * The return of this function denotes:
2207 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2208 * 2) An error, if the value is negative.
2209 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002210extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002211 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002212 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002213 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Roland Levillain0d2323e2017-06-26 18:14:39 +01002214 // Fix up a callee-save frame at the bottom of the stack (at `*sp`,
2215 // above the alloca region) while we check for optimization
2216 // annotations, thus allowing stack walking until the completion of
2217 // the JNI frame creation.
2218 //
2219 // Note however that the Generic JNI trampoline does not expect
2220 // exception being thrown at that stage.
2221 *sp = Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
2222 self->SetTopOfStack(sp);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002223 uint32_t shorty_len = 0;
2224 const char* shorty = called->GetShorty(&shorty_len);
Roland Levillain35e42f02017-06-26 18:14:39 +01002225 // Optimization annotations lookup does not try to resolve classes,
2226 // as this may throw an exception, which is not supported by the
2227 // Generic JNI trampoline at this stage; instead, method's
2228 // annotations' classes are looked up in the bootstrap class
2229 // loader's resolved types (which won't trigger an exception).
Igor Murashkin06a04e02016-09-13 15:57:37 -07002230 bool critical_native = called->IsAnnotatedWithCriticalNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002231 // ArtMethod::IsAnnotatedWithCriticalNative should not throw
2232 // an exception; clear it if it happened anyway.
2233 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2234 if (self->IsExceptionPending()) {
2235 self->ClearException();
2236 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002237 bool fast_native = called->IsAnnotatedWithFastNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002238 // ArtMethod::IsAnnotatedWithFastNative should not throw
2239 // an exception; clear it if it happened anyway.
2240 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2241 if (self->IsExceptionPending()) {
2242 self->ClearException();
2243 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002244 bool normal_native = !critical_native && !fast_native;
Roland Levillain0d2323e2017-06-26 18:14:39 +01002245 // Restore the initial ArtMethod pointer at `*sp`.
2246 *sp = called;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002247
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002248 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002249 BuildGenericJniFrameVisitor visitor(self,
2250 called->IsStatic(),
2251 critical_native,
2252 shorty,
2253 shorty_len,
2254 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002255 {
2256 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2257 visitor.VisitArguments();
2258 // FinalizeHandleScope pushes the handle scope on the thread.
2259 visitor.FinalizeHandleScope(self);
2260 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002261
Andreas Gampec200a4a2014-06-16 18:39:09 -07002262 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002263 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002264
Ian Rogerse0dcd462014-03-08 15:21:04 -08002265 self->VerifyStack();
2266
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002267 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002268 uint32_t* sp32;
2269 // Skip calling JniMethodStart for @CriticalNative.
2270 if (LIKELY(!critical_native)) {
2271 // Start JNI, save the cookie.
2272 if (called->IsSynchronized()) {
2273 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2274 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2275 if (self->IsExceptionPending()) {
2276 self->PopHandleScope();
2277 // A negative value denotes an error.
2278 return GetTwoWordFailureValue();
2279 }
2280 } else {
2281 if (fast_native) {
2282 cookie = JniMethodFastStart(self);
2283 } else {
2284 DCHECK(normal_native);
2285 cookie = JniMethodStart(self);
2286 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002287 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002288 sp32 = reinterpret_cast<uint32_t*>(sp);
2289 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002290 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002291
Andreas Gampe90546832014-03-12 18:07:19 -07002292 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002293 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002294
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002295 // There are two cases for the content of nativeCode:
2296 // 1) Pointer to the native function.
2297 // 2) Pointer to the trampoline for native code binding.
2298 // In the second case, we need to execute the binding and continue with the actual native function
2299 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002300 DCHECK(nativeCode != nullptr);
2301 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002302#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002303 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002304#else
2305 nativeCode = artFindNativeMethod(self);
2306#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002307
2308 if (nativeCode == nullptr) {
2309 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002310
Igor Murashkin06a04e02016-09-13 15:57:37 -07002311 // @CriticalNative calls do not need to call back into JniMethodEnd.
2312 if (LIKELY(!critical_native)) {
2313 // End JNI, as the assembly will move to deliver the exception.
2314 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2315 if (shorty[0] == 'L') {
2316 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2317 } else {
2318 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2319 }
Andreas Gampead615172014-04-04 16:20:13 -07002320 }
2321
Andreas Gampec200a4a2014-06-16 18:39:09 -07002322 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002323 }
2324 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002325 }
2326
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002327#if defined(__mips__) && !defined(__LP64__)
2328 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2329 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2330 // and load into floating-point registers.
2331 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2332 // view):
2333 // (1)
2334 // | DOUBLE | DOUBLE | other args, if any
2335 // | F12 | F13 | F14 | F15 |
2336 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2337 // (2)
2338 // | DOUBLE | FLOAT | (PAD) | other args, if any
2339 // | F12 | F13 | F14 | |
2340 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2341 // (3)
2342 // | FLOAT | (PAD) | DOUBLE | other args, if any
2343 // | F12 | | F14 | F15 |
2344 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2345 // (4)
2346 // | FLOAT | FLOAT | other args, if any
2347 // | F12 | F14 |
2348 // | SP+0 | SP+4 | SP+8
2349 // As you can see, only the last case (4) is special. In all others we can just
2350 // load F12/F13 and F14/F15 in the same manner.
2351 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2352 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2353 if (nativeCode != nullptr &&
2354 shorty != nullptr &&
2355 shorty_len >= 3 &&
2356 shorty[1] == 'F' &&
2357 shorty[2] == 'F') {
2358 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2359 }
2360#endif
2361
Andreas Gampec200a4a2014-06-16 18:39:09 -07002362 // Return native code addr(lo) and bottom of alloca address(hi).
2363 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2364 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002365}
2366
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002367// Defined in quick_jni_entrypoints.cc.
2368extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2369 jvalue result, uint64_t result_f, ArtMethod* called,
2370 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002371/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002372 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002373 * unlocking.
2374 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002375extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2376 jvalue result,
2377 uint64_t result_f) {
2378 // We're here just back from a native call. We don't have the shared mutator lock at this point
2379 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2380 // anything that requires a mutator lock before that would cause problems as GC may have the
2381 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002382 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002383 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002384 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002385 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002386 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2387 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002388}
2389
Andreas Gamped58342c2014-06-05 14:18:08 -07002390// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2391// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002392//
Andreas Gamped58342c2014-06-05 14:18:08 -07002393// 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 -07002394// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002395
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002396template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002397static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2398 ObjPtr<mirror::Object> this_object,
2399 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002400 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002401 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002402 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002403 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002404 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002405 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002406 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2407 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002408 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002409 {
2410 // Remember the args in case a GC happens in FindMethodFromCode.
2411 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2412 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2413 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002414 method = FindMethodFromCode<type, access_check>(method_idx,
2415 &this_object,
2416 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002417 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002418 visitor.FixupReferences();
2419 }
2420
Ian Rogerse0a02da2014-12-02 14:10:53 -08002421 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002422 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002423 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002424 }
2425 }
2426 DCHECK(!self->IsExceptionPending());
2427 const void* code = method->GetEntryPointFromQuickCompiledCode();
2428
2429 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002430 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002431 << " location: "
2432 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002433
Andreas Gamped58342c2014-06-05 14:18:08 -07002434 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2435 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002436}
2437
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002438// Explicit artInvokeCommon template function declarations to please analysis tool.
2439#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002440 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002441 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002442 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002443
2444EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2445EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2446EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2447EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2448EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2449EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2450EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2451EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2452EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2453EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2454#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2455
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002456// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002457extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002458 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002459 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002460 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002461}
2462
Andreas Gampec200a4a2014-06-16 18:39:09 -07002463extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002464 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002465 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002466 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002467}
2468
Andreas Gampec200a4a2014-06-16 18:39:09 -07002469extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002470 uint32_t method_idx,
2471 mirror::Object* this_object ATTRIBUTE_UNUSED,
2472 Thread* self,
2473 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2474 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2475 // it doesn't cause ObjPtr alignment failure check.
2476 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002477}
2478
Andreas Gampec200a4a2014-06-16 18:39:09 -07002479extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002480 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002481 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002482 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002483}
2484
Andreas Gampec200a4a2014-06-16 18:39:09 -07002485extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002486 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002487 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002488 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002489}
2490
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002491// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2492extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2493 REQUIRES_SHARED(Locks::mutator_lock_) {
2494 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2495 DCHECK(!referrer->IsProxyMethod());
2496 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2497 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2498 DCHECK(result == nullptr ||
2499 result->GetDeclaringClass()->IsInterface() ||
2500 result->GetDeclaringClass() ==
2501 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2502 << result->PrettyMethod();
2503 return result;
2504}
2505
Jeff Hao5667f562017-02-27 19:32:01 -08002506// Determine target of interface dispatch. The interface method and this object are known non-null.
2507// The interface method is the method returned by the dex cache in the conflict trampoline.
2508extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002509 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002510 Thread* self,
2511 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002512 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002513 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002514 StackHandleScope<2> hs(self);
2515 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2516 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002517
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002518 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002519 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002520 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002521
Vladimir Marko302f69c2017-07-25 15:27:15 +01002522 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002523 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002524 // Fetch the dex_method_idx of the target interface method from the caller.
2525 uint32_t dex_method_idx;
2526 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2527 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2528 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2529 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2530 Instruction::Code instr_code = instr->Opcode();
2531 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2532 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2533 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2534 if (instr_code == Instruction::INVOKE_INTERFACE) {
2535 dex_method_idx = instr->VRegB_35c();
2536 } else {
2537 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2538 dex_method_idx = instr->VRegB_3rc();
2539 }
2540
Vladimir Marko302f69c2017-07-25 15:27:15 +01002541 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002542 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002543 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2544 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002545 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002546 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002547 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2548 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2549 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002550 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2551 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2552 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002553 visitor.FixupReferences();
2554 }
2555
Vladimir Marko302f69c2017-07-25 15:27:15 +01002556 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002557 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002558 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002559 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002560 }
2561
2562 DCHECK(!interface_method->IsRuntimeMethod());
2563 // Look whether we have a match in the ImtConflictTable.
2564 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2565 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2566 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2567 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2568 DCHECK(current_table != nullptr);
2569 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2570 } else {
2571 // It seems we aren't really a conflict method!
2572 if (kIsDebugBuild) {
2573 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2574 CHECK_EQ(conflict_method, m)
2575 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2576 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2577 }
2578 method = conflict_method;
2579 }
2580 if (method != nullptr) {
2581 return GetTwoWordSuccessValue(
2582 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2583 reinterpret_cast<uintptr_t>(method));
2584 }
2585
2586 // No match, use the IfTable.
2587 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2588 if (UNLIKELY(method == nullptr)) {
2589 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2590 interface_method, this_object.Get(), caller_method);
2591 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002592 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002593
2594 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2595 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002596 DCHECK(conflict_method->IsRuntimeMethod());
2597 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2598 cls.Get(),
2599 conflict_method,
2600 interface_method,
2601 method,
2602 /*force_new_conflict_method*/false);
2603 if (new_conflict_method != conflict_method) {
2604 // Update the IMT if we create a new conflict method. No fence needed here, as the
2605 // data is consistent.
2606 imt->Set(imt_index,
2607 new_conflict_method,
2608 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002609 }
2610
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002611 const void* code = method->GetEntryPointFromQuickCompiledCode();
2612
2613 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002614 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002615 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002616
Andreas Gamped58342c2014-06-05 14:18:08 -07002617 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2618 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002619}
2620
Orion Hodsonac141392017-01-13 11:53:47 +00002621// Returns shorty type so the caller can determine how to put |result|
2622// into expected registers. The shorty type is static so the compiler
2623// could call different flavors of this code path depending on the
2624// shorty type though this would require different entry points for
2625// each type.
2626extern "C" uintptr_t artInvokePolymorphic(
2627 JValue* result,
2628 mirror::Object* raw_method_handle,
2629 Thread* self,
2630 ArtMethod** sp)
2631 REQUIRES_SHARED(Locks::mutator_lock_) {
2632 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002633 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002634
2635 // Start new JNI local reference state
2636 JNIEnvExt* env = self->GetJniEnv();
2637 ScopedObjectAccessUnchecked soa(env);
2638 ScopedJniEnvLocalRefState env_state(env);
2639 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2640
2641 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2642 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2643 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2644 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2645 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2646 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2647 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2648 const DexFile* dex_file = caller_method->GetDexFile();
2649 const uint32_t proto_idx = inst->VRegH();
2650 const char* shorty = dex_file->GetShorty(proto_idx);
2651 const size_t shorty_length = strlen(shorty);
2652 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2653 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002654 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002655
2656 // Wrap raw_method_handle in a Handle for safety.
2657 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002658 Handle<mirror::MethodHandle> method_handle(
2659 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002660 raw_method_handle = nullptr;
2661 self->EndAssertNoThreadSuspension(old_cause);
2662
2663 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2664 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002665 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
2666 self, inst->VRegB(), caller_method, kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +00002667 DCHECK((resolved_method ==
2668 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2669 (resolved_method ==
2670 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2671 if (UNLIKELY(method_handle.IsNull())) {
2672 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2673 return static_cast<uintptr_t>('V');
2674 }
2675
2676 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2677 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2678 *dex_file, proto_idx,
2679 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2680 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2681 // This implies we couldn't resolve one or more types in this method handle.
2682 if (UNLIKELY(method_type.IsNull())) {
2683 CHECK(self->IsExceptionPending());
2684 return static_cast<uintptr_t>('V');
2685 }
2686
2687 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2688 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2689
2690 // Fix references before constructing the shadow frame.
2691 gc_visitor.FixupReferences();
2692
2693 // Construct shadow frame placing arguments consecutively from |first_arg|.
2694 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2695 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2696 const size_t first_arg = 0;
2697 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2698 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2699 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2700 ScopedStackedShadowFramePusher
2701 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2702 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2703 kMethodIsStatic,
2704 shorty,
2705 strlen(shorty),
2706 shadow_frame,
2707 first_arg);
2708 shadow_frame_builder.VisitArguments();
2709
2710 // Push a transition back into managed code onto the linked list in thread.
2711 ManagedStack fragment;
2712 self->PushManagedStackFragment(&fragment);
2713
2714 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2715 // consecutive order.
2716 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2717 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002718 if (!DoInvokePolymorphic<true /* is_range */>(self,
2719 resolved_method,
2720 *shadow_frame,
2721 method_handle,
2722 method_type,
2723 unused_args,
2724 first_callee_arg,
2725 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002726 DCHECK(self->IsExceptionPending());
2727 }
2728
2729 // Pop transition record.
2730 self->PopManagedStackFragment(fragment);
2731
2732 return static_cast<uintptr_t>(shorty[0]);
2733}
2734
Ian Rogers848871b2013-08-05 10:56:33 -07002735} // namespace art