blob: 7b83f204505b7b8356830cb5826804a9dba8d807 [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
Mathieu Chartiere401d142015-04-22 13:56:20 -0700700extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700701 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700702 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
703 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700704 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700705
Alex Light9139e002015-10-09 15:59:48 -0700706 if (UNLIKELY(!method->IsInvokable())) {
707 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700708 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700709 }
710
711 JValue tmp_value;
712 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700713 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700714 ManagedStack fragment;
715
David Sehr709b0702016-10-13 09:12:37 -0700716 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700717 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700718 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700719 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700720 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700721 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
722
723 JValue result;
724
725 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700726 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700727
728 if (kIsDebugBuild) {
729 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
730 // of the call-stack) corresponds to the called method.
731 ShadowFrame* linked = deopt_frame;
732 while (linked->GetLink() != nullptr) {
733 linked = linked->GetLink();
734 }
David Sehr709b0702016-10-13 09:12:37 -0700735 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
736 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700737 }
738
739 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700740 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700741 LOG(INFO) << "Continue-ing from deopt. Stack is:";
742 QuickExceptionHandler::DumpFramesWithType(self, true);
743 }
744
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800745 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100746 bool from_code = false;
Mingyao Yang2ee17902017-08-30 11:37:08 -0700747 DeoptimizationMethodType method_type;
748 self->PopDeoptimizationContext(/* out */ &result,
749 /* out */ &pending_exception,
750 /* out */ &from_code,
751 /* out */ &method_type);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700752
753 // Push a transition back into managed code onto the linked list in thread.
754 self->PushManagedStackFragment(&fragment);
755
756 // Ensure that the stack is still in order.
757 if (kIsDebugBuild) {
758 class DummyStackVisitor : public StackVisitor {
759 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700760 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700761 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
762
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700763 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700764 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
765 // logic. Just always say we want to continue.
766 return true;
767 }
768 };
769 DummyStackVisitor dsv(self);
770 dsv.WalkStack();
771 }
772
773 // Restore the exception that was pending before deoptimization then interpret the
774 // deoptimized frames.
775 if (pending_exception != nullptr) {
776 self->SetException(pending_exception);
777 }
Mingyao Yang2ee17902017-08-30 11:37:08 -0700778 interpreter::EnterInterpreterFromDeoptimize(self,
779 deopt_frame,
780 &result,
781 from_code,
782 DeoptimizationMethodType::kDefault);
Ian Rogers848871b2013-08-05 10:56:33 -0700783 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700784 const char* old_cause = self->StartAssertNoThreadSuspension(
785 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700786 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700787 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700788 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700789 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700790 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700791 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700792 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800793 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700794 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800795 const bool needs_initialization =
796 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700797 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700798 self->PushManagedStackFragment(&fragment);
799 self->PushShadowFrame(shadow_frame);
800 self->EndAssertNoThreadSuspension(old_cause);
801
Ian Rogerse94652f2014-12-02 11:13:19 -0800802 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700803 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800804 StackHandleScope<1> hs(self);
805 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700806 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700807 DCHECK(Thread::Current()->IsExceptionPending())
808 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700809 self->PopManagedStackFragment(fragment);
810 return 0;
811 }
812 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200813
Andreas Gampe639bdd12015-06-03 11:22:45 -0700814 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700815 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700816
817 // Pop transition.
818 self->PopManagedStackFragment(fragment);
819
820 // Request a stack deoptimization if needed
821 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700822 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700823 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
824 // should be done and it knows the real return pc.
825 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000826 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
827 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
828 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
829 << caller->PrettyMethod();
830 } else {
831 // Push the context of the deoptimization stack so we can restore the return value and the
832 // exception before executing the deoptimized frames.
833 self->PushDeoptimizationContext(
Mingyao Yang2ee17902017-08-30 11:37:08 -0700834 result,
835 shorty[0] == 'L' || shorty[0] == '[', /* class or array */
836 self->GetException(),
837 false /* from_code */,
838 DeoptimizationMethodType::kDefault);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700839
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000840 // Set special exception to cause deoptimization.
841 self->SetException(Thread::GetDeoptimizationException());
842 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700843 }
844
845 // No need to restore the args since the method has already been run by the interpreter.
846 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700847}
848
849// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
850// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800851class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700852 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700853 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700854 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700855 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700856
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700857 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700858
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700859 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800860
Ian Rogers848871b2013-08-05 10:56:33 -0700861 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700862 ScopedObjectAccessUnchecked* const soa_;
863 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800864 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700865 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700866
Ian Rogers848871b2013-08-05 10:56:33 -0700867 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
868};
869
Ian Rogers9758f792014-03-13 09:02:55 -0700870void BuildQuickArgumentVisitor::Visit() {
871 jvalue val;
872 Primitive::Type type = GetParamPrimitiveType();
873 switch (type) {
874 case Primitive::kPrimNot: {
875 StackReference<mirror::Object>* stack_ref =
876 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
877 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
878 references_.push_back(std::make_pair(val.l, stack_ref));
879 break;
880 }
881 case Primitive::kPrimLong: // Fall-through.
882 case Primitive::kPrimDouble:
883 if (IsSplitLongOrDouble()) {
884 val.j = ReadSplitLongParam();
885 } else {
886 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
887 }
888 break;
889 case Primitive::kPrimBoolean: // Fall-through.
890 case Primitive::kPrimByte: // Fall-through.
891 case Primitive::kPrimChar: // Fall-through.
892 case Primitive::kPrimShort: // Fall-through.
893 case Primitive::kPrimInt: // Fall-through.
894 case Primitive::kPrimFloat:
895 val.i = *reinterpret_cast<jint*>(GetParamAddress());
896 break;
897 case Primitive::kPrimVoid:
898 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700899 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700900 }
901 args_->push_back(val);
902}
903
904void BuildQuickArgumentVisitor::FixupReferences() {
905 // Fixup any references which may have changed.
906 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700907 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700908 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700909 }
910}
Ian Rogers848871b2013-08-05 10:56:33 -0700911// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
912// which is responsible for recording callee save registers. We explicitly place into jobjects the
913// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
914// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700915extern "C" uint64_t artQuickProxyInvokeHandler(
916 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700917 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700918 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
919 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700920 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
921 const char* old_cause =
922 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
923 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700924 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700925 self->VerifyStack();
926 // Start new JNI local reference state.
927 JNIEnvExt* env = self->GetJniEnv();
928 ScopedObjectAccessUnchecked soa(env);
929 ScopedJniEnvLocalRefState env_state(env);
930 // Create local ref. copies of proxy method and the receiver.
931 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
932
933 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700934 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700935 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
936 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700937 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700938 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700939 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700940 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700941
Ian Rogers848871b2013-08-05 10:56:33 -0700942 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700943 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700944 args.erase(args.begin());
945
946 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700947 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700948 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
949 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700950 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700951 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700952 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700953 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700954 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
955 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700956
957 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
958 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700959 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800960 // Restore references which might have moved.
961 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700962 return result.GetJ();
963}
964
965// Read object references held in arguments from quick frames and place in a JNI local references,
966// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800967class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700968 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700969 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
970 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700971 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700972
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700973 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700974
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700975 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700976
977 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700978 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800979 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700980 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
981
Mathieu Chartier590fee92013-09-13 13:46:47 -0700982 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700983};
984
Ian Rogers9758f792014-03-13 09:02:55 -0700985void RememberForGcArgumentVisitor::Visit() {
986 if (IsParamAReference()) {
987 StackReference<mirror::Object>* stack_ref =
988 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
989 jobject reference =
990 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
991 references_.push_back(std::make_pair(reference, stack_ref));
992 }
993}
994
995void RememberForGcArgumentVisitor::FixupReferences() {
996 // Fixup any references which may have changed.
997 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700998 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700999 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -07001000 }
1001}
1002
Alex Lightb7edcda2017-04-27 13:20:31 -07001003extern "C" const void* artInstrumentationMethodEntryFromCode(ArtMethod* method,
1004 mirror::Object* this_object,
1005 Thread* self,
1006 ArtMethod** sp)
1007 REQUIRES_SHARED(Locks::mutator_lock_) {
1008 const void* result;
1009 // Instrumentation changes the stack. Thus, when exiting, the stack cannot be verified, so skip
1010 // that part.
1011 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
1012 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1013 if (instrumentation->IsDeoptimized(method)) {
1014 result = GetQuickToInterpreterBridge();
1015 } else {
1016 result = instrumentation->GetQuickCodeFor(method, kRuntimePointerSize);
1017 DCHECK(!Runtime::Current()->GetClassLinker()->IsQuickToInterpreterBridge(result));
1018 }
1019
1020 bool interpreter_entry = (result == GetQuickToInterpreterBridge());
1021 bool is_static = method->IsStatic();
1022 uint32_t shorty_len;
1023 const char* shorty =
1024 method->GetInterfaceMethodIfProxy(kRuntimePointerSize)->GetShorty(&shorty_len);
1025
1026 ScopedObjectAccessUnchecked soa(self);
1027 RememberForGcArgumentVisitor visitor(sp, is_static, shorty, shorty_len, &soa);
1028 visitor.VisitArguments();
1029
1030 instrumentation->PushInstrumentationStackFrame(self,
1031 is_static ? nullptr : this_object,
1032 method,
1033 QuickArgumentVisitor::GetCallingPc(sp),
1034 interpreter_entry);
1035
1036 visitor.FixupReferences();
1037 if (UNLIKELY(self->IsExceptionPending())) {
1038 return nullptr;
1039 }
1040 CHECK(result != nullptr) << method->PrettyMethod();
1041 return result;
1042}
1043
1044extern "C" TwoWordReturn artInstrumentationMethodExitFromCode(Thread* self,
1045 ArtMethod** sp,
1046 uint64_t* gpr_result,
1047 uint64_t* fpr_result)
1048 REQUIRES_SHARED(Locks::mutator_lock_) {
1049 DCHECK_EQ(reinterpret_cast<uintptr_t>(self), reinterpret_cast<uintptr_t>(Thread::Current()));
1050 CHECK(gpr_result != nullptr);
1051 CHECK(fpr_result != nullptr);
1052 // Instrumentation exit stub must not be entered with a pending exception.
1053 CHECK(!self->IsExceptionPending()) << "Enter instrumentation exit stub with pending exception "
1054 << self->GetException()->Dump();
1055 // Compute address of return PC and sanity check that it currently holds 0.
Mingyao Yang2ee17902017-08-30 11:37:08 -07001056 size_t return_pc_offset = GetCalleeSaveReturnPcOffset(kRuntimeISA,
1057 CalleeSaveType::kSaveEverything);
Alex Lightb7edcda2017-04-27 13:20:31 -07001058 uintptr_t* return_pc = reinterpret_cast<uintptr_t*>(reinterpret_cast<uint8_t*>(sp) +
1059 return_pc_offset);
1060 CHECK_EQ(*return_pc, 0U);
1061
1062 // Pop the frame filling in the return pc. The low half of the return value is 0 when
1063 // deoptimization shouldn't be performed with the high-half having the return address. When
1064 // deoptimization should be performed the low half is zero and the high-half the address of the
1065 // deoptimization entry point.
1066 instrumentation::Instrumentation* instrumentation = Runtime::Current()->GetInstrumentation();
1067 TwoWordReturn return_or_deoptimize_pc = instrumentation->PopInstrumentationStackFrame(
1068 self, return_pc, gpr_result, fpr_result);
1069 if (self->IsExceptionPending()) {
1070 return GetTwoWordFailureValue();
1071 }
1072 return return_or_deoptimize_pc;
1073}
1074
Ian Rogers848871b2013-08-05 10:56:33 -07001075// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001076extern "C" const void* artQuickResolutionTrampoline(
1077 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001078 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -07001079 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
1080 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
1081 // does not have the same stack layout as the callee-save method).
1082 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -07001083 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001084 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001085 ScopedObjectAccessUnchecked soa(env);
1086 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001087 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001088
1089 // Compute details about the called method (avoid GCs)
1090 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001091 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001092 MethodReference called_method(nullptr, 0);
1093 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001094 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001095 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001096 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001097 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001098
1099 InvokeType stack_map_invoke_type;
1100 uint32_t stack_map_dex_method_idx;
1101 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1102 &stack_map_invoke_type,
1103 &stack_map_dex_method_idx);
1104 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1105 // code.
1106 if (!found_stack_map || kIsDebugBuild) {
1107 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1108 const DexFile::CodeItem* code;
1109 code = caller->GetCodeItem();
1110 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1111 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1112 Instruction::Code instr_code = instr->Opcode();
1113 bool is_range;
1114 switch (instr_code) {
1115 case Instruction::INVOKE_DIRECT:
1116 invoke_type = kDirect;
1117 is_range = false;
1118 break;
1119 case Instruction::INVOKE_DIRECT_RANGE:
1120 invoke_type = kDirect;
1121 is_range = true;
1122 break;
1123 case Instruction::INVOKE_STATIC:
1124 invoke_type = kStatic;
1125 is_range = false;
1126 break;
1127 case Instruction::INVOKE_STATIC_RANGE:
1128 invoke_type = kStatic;
1129 is_range = true;
1130 break;
1131 case Instruction::INVOKE_SUPER:
1132 invoke_type = kSuper;
1133 is_range = false;
1134 break;
1135 case Instruction::INVOKE_SUPER_RANGE:
1136 invoke_type = kSuper;
1137 is_range = true;
1138 break;
1139 case Instruction::INVOKE_VIRTUAL:
1140 invoke_type = kVirtual;
1141 is_range = false;
1142 break;
1143 case Instruction::INVOKE_VIRTUAL_RANGE:
1144 invoke_type = kVirtual;
1145 is_range = true;
1146 break;
1147 case Instruction::INVOKE_INTERFACE:
1148 invoke_type = kInterface;
1149 is_range = false;
1150 break;
1151 case Instruction::INVOKE_INTERFACE_RANGE:
1152 invoke_type = kInterface;
1153 is_range = true;
1154 break;
1155 default:
1156 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1157 UNREACHABLE();
1158 }
1159 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1160 // Check that the invoke matches what we expected, note that this path only happens for debug
1161 // builds.
1162 if (found_stack_map) {
1163 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1164 if (invoke_type != kSuper) {
1165 // Super may be sharpened.
1166 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1167 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1168 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1169 }
1170 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001171 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001172 << called_method.dex_method_index;
1173 }
1174 } else {
1175 invoke_type = stack_map_invoke_type;
1176 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001177 }
Ian Rogers848871b2013-08-05 10:56:33 -07001178 } else {
1179 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001180 called_method.dex_file = called->GetDexFile();
1181 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001182 }
1183 uint32_t shorty_len;
1184 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001185 called_method.dex_file->GetMethodShorty(
1186 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001187 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001188 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001189 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001190 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001191 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001192 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001193 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001194 mirror::Object* dummy = nullptr;
1195 HandleWrapper<mirror::Object> h_receiver(
1196 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001197 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Vladimir Markoba118822017-06-12 15:41:56 +01001198 called = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001199 self, called_method.dex_method_index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001200
1201 // Update .bss entry in oat file if any.
1202 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1203 const MethodBssMapping* mapping =
1204 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1205 if (mapping != nullptr) {
1206 auto pp = std::partition_point(
1207 mapping->begin(),
1208 mapping->end(),
1209 [called_method](const MethodBssMappingEntry& entry) {
1210 return entry.method_index < called_method.dex_method_index;
1211 });
1212 if (pp != mapping->end() && pp->CoversIndex(called_method.dex_method_index)) {
1213 size_t bss_offset = pp->GetBssOffset(called_method.dex_method_index,
1214 static_cast<size_t>(kRuntimePointerSize));
1215 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1216 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1217 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1218 oat_file->BssBegin() + bss_offset));
1219 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1220 DCHECK_LT(method_entry,
1221 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1222 *method_entry = called;
1223 }
1224 }
1225 }
Ian Rogers848871b2013-08-05 10:56:33 -07001226 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001227 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001228 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001229 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001230 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001231 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001232 if (virtual_or_interface || invoke_type == kSuper) {
1233 // Refine called method based on receiver for kVirtual/kInterface, and
1234 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001235 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001236 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001237 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001238 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001239 } else if (invoke_type == kInterface) {
1240 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001241 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001242 } else {
1243 DCHECK_EQ(invoke_type, kSuper);
1244 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001245 StackHandleScope<2> hs(self);
1246 Handle<mirror::DexCache> dex_cache(
1247 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1248 Handle<mirror::ClassLoader> class_loader(
1249 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001250 // TODO Maybe put this into a mirror::Class function.
Vladimir Markoba118822017-06-12 15:41:56 +01001251 ObjPtr<mirror::Class> ref_class = linker->LookupResolvedType(
1252 *dex_cache->GetDexFile(),
1253 dex_cache->GetDexFile()->GetMethodId(called_method.dex_method_index).class_idx_,
1254 dex_cache.Get(),
1255 class_loader.Get());
Alex Lightfedd91d2016-01-07 14:49:16 -08001256 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001257 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001258 } else {
1259 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001260 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001261 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001262 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001263
David Sehr709b0702016-10-13 09:12:37 -07001264 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1265 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001266 << invoke_type << " " << orig_called->GetVtableIndex();
1267
Ian Rogers83883d72013-10-21 21:07:24 -07001268 // 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 -08001269 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001270 // Note, called_method.dex_method_index references the dex method before the
1271 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1272 // about the name and signature.
1273 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001274 if (called->GetDexFile() != caller->GetDexFile()) {
Ian Rogers83883d72013-10-21 21:07:24 -07001275 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001276 // the caller's dex file. Since we get here only if the original called was a runtime
1277 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001278 DCHECK(!called_method_known_on_entry);
1279 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1280 const DexFile* caller_dex_file = called_method.dex_file;
1281 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1282 update_dex_cache_method_index =
1283 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1284 caller_method_name_and_sig_index);
1285 }
Vladimir Marko5122e6b2017-08-17 16:10:09 +01001286 if (update_dex_cache_method_index != DexFile::kDexNoIndex) {
1287 // Note: We do not need the read barrier for the dex cache as the SetResolvedMethod()
1288 // operates on native (non-moveable) data and constants (num_resolved_methods_).
1289 ObjPtr<mirror::DexCache> caller_dex_cache = caller->GetDexCache<kWithoutReadBarrier>();
1290 if (caller_dex_cache->GetResolvedMethod(
1291 update_dex_cache_method_index, kRuntimePointerSize) != called) {
1292 caller_dex_cache->SetResolvedMethod(update_dex_cache_method_index,
1293 called,
1294 kRuntimePointerSize);
1295 }
Ian Rogers83883d72013-10-21 21:07:24 -07001296 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001297 } else if (invoke_type == kStatic) {
1298 const auto called_dex_method_idx = called->GetDexMethodIndex();
1299 // For static invokes, we may dispatch to the static method in the superclass but resolve
1300 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1301 // resolved method for the super class dex method index if we are in the same dex file.
1302 // b/19175856
1303 if (called->GetDexFile() == called_method.dex_file &&
1304 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001305 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1306 called,
1307 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001308 }
Ian Rogers83883d72013-10-21 21:07:24 -07001309 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001310
Ian Rogers848871b2013-08-05 10:56:33 -07001311 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001312 StackHandleScope<1> hs(soa.Self());
1313 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001314 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001315 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001316 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1317 // If we are single-stepping or the called method is deoptimized (by a
1318 // breakpoint, for example), then we have to execute the called method
1319 // with the interpreter.
1320 code = GetQuickToInterpreterBridge();
1321 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1322 // If the caller is deoptimized (by a breakpoint, for example), we have to
1323 // continue its execution with interpreter when returning from the called
1324 // method. Because we do not want to execute the called method with the
1325 // interpreter, we wrap its execution into the instrumentation stubs.
1326 // When the called method returns, it will execute the instrumentation
1327 // exit hook that will determine the need of the interpreter with a call
1328 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1329 // it is needed.
1330 code = GetQuickInstrumentationEntryPoint();
1331 } else {
1332 code = called->GetEntryPointFromQuickCompiledCode();
1333 }
Ian Rogers848871b2013-08-05 10:56:33 -07001334 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001335 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1336 // If we are single-stepping or the called method is deoptimized (by a
1337 // breakpoint, for example), then we have to execute the called method
1338 // with the interpreter.
1339 code = GetQuickToInterpreterBridge();
1340 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001341 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1342 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001343 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001344 } else {
1345 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001346 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001347 }
1348 } else {
1349 DCHECK(called_class->IsErroneous());
1350 }
1351 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001352 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001353 // Fixup any locally saved objects may have moved during a GC.
1354 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001355 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001356 *sp = called;
1357
Ian Rogers848871b2013-08-05 10:56:33 -07001358 return code;
1359}
1360
Andreas Gampec147b002014-03-06 18:11:06 -08001361/*
1362 * This class uses a couple of observations to unite the different calling conventions through
1363 * a few constants.
1364 *
1365 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1366 * possible alignment.
1367 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1368 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1369 * when we have to split things
1370 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1371 * and we can use Int handling directly.
1372 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1373 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1374 * extension should be compatible with Aarch64, which mandates copying the available bits
1375 * into LSB and leaving the rest unspecified.
1376 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1377 * the stack.
1378 * 6) There is only little endian.
1379 *
1380 *
1381 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1382 * follows:
1383 *
1384 * void PushGpr(uintptr_t): Add a value for the next GPR
1385 *
1386 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1387 * padding, that is, think the architecture is 32b and aligns 64b.
1388 *
1389 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1390 * split this if necessary. The current state will have aligned, if
1391 * necessary.
1392 *
1393 * void PushStack(uintptr_t): Push a value to the stack.
1394 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001395 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001396 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001397 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001398 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001399 *
1400 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001401template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001402 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001403#if defined(__arm__)
1404 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001405 static constexpr bool kNativeSoftFloatAbi = true;
1406 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001407 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1408
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001409 static constexpr size_t kRegistersNeededForLong = 2;
1410 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001411 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001412 static constexpr bool kMultiFPRegistersWidened = false;
1413 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001414 static constexpr bool kAlignLongOnStack = true;
1415 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001416#elif defined(__aarch64__)
1417 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1418 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1419 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1420
1421 static constexpr size_t kRegistersNeededForLong = 1;
1422 static constexpr size_t kRegistersNeededForDouble = 1;
1423 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001424 static constexpr bool kMultiFPRegistersWidened = false;
1425 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001426 static constexpr bool kAlignLongOnStack = false;
1427 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001428#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001429 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001430 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1431 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001432
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001433 static constexpr size_t kRegistersNeededForLong = 2;
1434 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001435 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001436 static constexpr bool kMultiFPRegistersWidened = true;
1437 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001438 static constexpr bool kAlignLongOnStack = true;
1439 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001440#elif defined(__mips__) && defined(__LP64__)
1441 // Let the code prepare GPRs only and we will load the FPRs with same data.
1442 static constexpr bool kNativeSoftFloatAbi = true;
1443 static constexpr size_t kNumNativeGprArgs = 8;
1444 static constexpr size_t kNumNativeFprArgs = 0;
1445
1446 static constexpr size_t kRegistersNeededForLong = 1;
1447 static constexpr size_t kRegistersNeededForDouble = 1;
1448 static constexpr bool kMultiRegistersAligned = false;
1449 static constexpr bool kMultiFPRegistersWidened = false;
1450 static constexpr bool kMultiGPRegistersWidened = true;
1451 static constexpr bool kAlignLongOnStack = false;
1452 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001453#elif defined(__i386__)
1454 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001455 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001456 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1457 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1458
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001459 static constexpr size_t kRegistersNeededForLong = 2;
1460 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001461 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001462 static constexpr bool kMultiFPRegistersWidened = false;
1463 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001464 static constexpr bool kAlignLongOnStack = false;
1465 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001466#elif defined(__x86_64__)
1467 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1468 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1469 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1470
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001471 static constexpr size_t kRegistersNeededForLong = 1;
1472 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001473 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001474 static constexpr bool kMultiFPRegistersWidened = false;
1475 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001476 static constexpr bool kAlignLongOnStack = false;
1477 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001478#else
1479#error "Unsupported architecture"
1480#endif
1481
Andreas Gampec147b002014-03-06 18:11:06 -08001482 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001483 explicit BuildNativeCallFrameStateMachine(T* delegate)
1484 : gpr_index_(kNumNativeGprArgs),
1485 fpr_index_(kNumNativeFprArgs),
1486 stack_entries_(0),
1487 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001488 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1489 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001490 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1491 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001492 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001493
Andreas Gampec200a4a2014-06-16 18:39:09 -07001494 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001495
Ian Rogers1428dce2014-10-21 15:02:15 -07001496 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001497 return gpr_index_ > 0;
1498 }
1499
Andreas Gampec200a4a2014-06-16 18:39:09 -07001500 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001501 if (HavePointerGpr()) {
1502 gpr_index_--;
1503 PushGpr(reinterpret_cast<uintptr_t>(val));
1504 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001505 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001506 PushStack(reinterpret_cast<uintptr_t>(val));
1507 gpr_index_ = 0;
1508 }
1509 }
1510
Ian Rogers1428dce2014-10-21 15:02:15 -07001511 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001512 return gpr_index_ > 0;
1513 }
1514
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001515 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001516 uintptr_t handle = PushHandle(ptr);
1517 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001518 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001519 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001520 } else {
1521 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001522 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001523 gpr_index_ = 0;
1524 }
1525 }
1526
Ian Rogers1428dce2014-10-21 15:02:15 -07001527 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001528 return gpr_index_ > 0;
1529 }
1530
1531 void AdvanceInt(uint32_t val) {
1532 if (HaveIntGpr()) {
1533 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001534 if (kMultiGPRegistersWidened) {
1535 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001536 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001537 } else {
1538 PushGpr(val);
1539 }
Andreas Gampec147b002014-03-06 18:11:06 -08001540 } else {
1541 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001542 if (kMultiGPRegistersWidened) {
1543 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001544 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001545 } else {
1546 PushStack(val);
1547 }
Andreas Gampec147b002014-03-06 18:11:06 -08001548 gpr_index_ = 0;
1549 }
1550 }
1551
Ian Rogers1428dce2014-10-21 15:02:15 -07001552 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001553 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1554 }
1555
Ian Rogers1428dce2014-10-21 15:02:15 -07001556 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001557 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1558 kAlignLongOnStack && // and when it needs alignment
1559 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1560 }
1561
Ian Rogers1428dce2014-10-21 15:02:15 -07001562 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001563 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1564 kAlignLongOnStack && // and when it needs 8B alignment
1565 (stack_entries_ & 1) == 1; // counter is odd
1566 }
1567
1568 void AdvanceLong(uint64_t val) {
1569 if (HaveLongGpr()) {
1570 if (LongGprNeedsPadding()) {
1571 PushGpr(0);
1572 gpr_index_--;
1573 }
1574 if (kRegistersNeededForLong == 1) {
1575 PushGpr(static_cast<uintptr_t>(val));
1576 } else {
1577 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1578 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1579 }
1580 gpr_index_ -= kRegistersNeededForLong;
1581 } else {
1582 if (LongStackNeedsPadding()) {
1583 PushStack(0);
1584 stack_entries_++;
1585 }
1586 if (kRegistersNeededForLong == 1) {
1587 PushStack(static_cast<uintptr_t>(val));
1588 stack_entries_++;
1589 } else {
1590 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1591 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1592 stack_entries_ += 2;
1593 }
1594 gpr_index_ = 0;
1595 }
1596 }
1597
Ian Rogers1428dce2014-10-21 15:02:15 -07001598 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001599 return fpr_index_ > 0;
1600 }
1601
Andreas Gampec147b002014-03-06 18:11:06 -08001602 void AdvanceFloat(float val) {
1603 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001604 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001605 } else {
1606 if (HaveFloatFpr()) {
1607 fpr_index_--;
1608 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001609 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001610 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001611 } else {
1612 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001613 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001614 }
1615 } else {
1616 PushFpr4(val);
1617 }
1618 } else {
1619 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001620 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001621 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001622 // Note: We need to jump through those hoops to make the compiler happy.
1623 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001624 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001625 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001626 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001627 }
1628 fpr_index_ = 0;
1629 }
1630 }
1631 }
1632
Ian Rogers1428dce2014-10-21 15:02:15 -07001633 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001634 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1635 }
1636
Ian Rogers1428dce2014-10-21 15:02:15 -07001637 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001638 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1639 kAlignDoubleOnStack && // and when it needs alignment
1640 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1641 }
1642
Ian Rogers1428dce2014-10-21 15:02:15 -07001643 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001644 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1645 kAlignDoubleOnStack && // and when it needs 8B alignment
1646 (stack_entries_ & 1) == 1; // counter is odd
1647 }
1648
1649 void AdvanceDouble(uint64_t val) {
1650 if (kNativeSoftFloatAbi) {
1651 AdvanceLong(val);
1652 } else {
1653 if (HaveDoubleFpr()) {
1654 if (DoubleFprNeedsPadding()) {
1655 PushFpr4(0);
1656 fpr_index_--;
1657 }
1658 PushFpr8(val);
1659 fpr_index_ -= kRegistersNeededForDouble;
1660 } else {
1661 if (DoubleStackNeedsPadding()) {
1662 PushStack(0);
1663 stack_entries_++;
1664 }
1665 if (kRegistersNeededForDouble == 1) {
1666 PushStack(static_cast<uintptr_t>(val));
1667 stack_entries_++;
1668 } else {
1669 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1670 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1671 stack_entries_ += 2;
1672 }
1673 fpr_index_ = 0;
1674 }
1675 }
1676 }
1677
Ian Rogers1428dce2014-10-21 15:02:15 -07001678 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001679 return stack_entries_;
1680 }
1681
Ian Rogers1428dce2014-10-21 15:02:15 -07001682 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001683 return kNumNativeGprArgs - gpr_index_;
1684 }
1685
Ian Rogers1428dce2014-10-21 15:02:15 -07001686 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001687 return kNumNativeFprArgs - fpr_index_;
1688 }
1689
1690 private:
1691 void PushGpr(uintptr_t val) {
1692 delegate_->PushGpr(val);
1693 }
1694 void PushFpr4(float val) {
1695 delegate_->PushFpr4(val);
1696 }
1697 void PushFpr8(uint64_t val) {
1698 delegate_->PushFpr8(val);
1699 }
1700 void PushStack(uintptr_t val) {
1701 delegate_->PushStack(val);
1702 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001703 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001704 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001705 }
1706
1707 uint32_t gpr_index_; // Number of free GPRs
1708 uint32_t fpr_index_; // Number of free FPRs
1709 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1710 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001711 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001712};
1713
Andreas Gampec200a4a2014-06-16 18:39:09 -07001714// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1715// in subclasses.
1716//
1717// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1718// them with handles.
1719class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001720 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001721 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1722
1723 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001724
Ian Rogers1428dce2014-10-21 15:02:15 -07001725 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001726 return num_stack_entries_ * sizeof(uintptr_t);
1727 }
1728
Ian Rogers1428dce2014-10-21 15:02:15 -07001729 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001730 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001731 // Align by kStackAlignment.
1732 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001733 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001734 }
1735
Ian Rogers1428dce2014-10-21 15:02:15 -07001736 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1737 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001738 // Assumption is OK right now, as we have soft-float arm
1739 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1740 sp8 -= fregs * sizeof(uintptr_t);
1741 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1742 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1743 sp8 -= iregs * sizeof(uintptr_t);
1744 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1745 return sp8;
1746 }
Andreas Gampec147b002014-03-06 18:11:06 -08001747
Andreas Gampec200a4a2014-06-16 18:39:09 -07001748 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001749 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001750 // Native call stack.
1751 sp8 = LayoutCallStack(sp8);
1752 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001753
Andreas Gampec200a4a2014-06-16 18:39:09 -07001754 // Put fprs and gprs below.
1755 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001756
Andreas Gampec200a4a2014-06-16 18:39:09 -07001757 // Return the new bottom.
1758 return sp8;
1759 }
1760
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001761 virtual void WalkHeader(
1762 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001763 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001764 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001765
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001766 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001767 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1768
1769 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001770
1771 for (uint32_t i = 1; i < shorty_len; ++i) {
1772 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1773 switch (cur_type_) {
1774 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001775 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001776 sm.AdvanceHandleScope(
1777 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001778 break;
1779
1780 case Primitive::kPrimBoolean:
1781 case Primitive::kPrimByte:
1782 case Primitive::kPrimChar:
1783 case Primitive::kPrimShort:
1784 case Primitive::kPrimInt:
1785 sm.AdvanceInt(0);
1786 break;
1787 case Primitive::kPrimFloat:
1788 sm.AdvanceFloat(0);
1789 break;
1790 case Primitive::kPrimDouble:
1791 sm.AdvanceDouble(0);
1792 break;
1793 case Primitive::kPrimLong:
1794 sm.AdvanceLong(0);
1795 break;
1796 default:
1797 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001798 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001799 }
1800 }
1801
Ian Rogers1428dce2014-10-21 15:02:15 -07001802 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001803 }
1804
1805 void PushGpr(uintptr_t /* val */) {
1806 // not optimizing registers, yet
1807 }
1808
1809 void PushFpr4(float /* val */) {
1810 // not optimizing registers, yet
1811 }
1812
1813 void PushFpr8(uint64_t /* val */) {
1814 // not optimizing registers, yet
1815 }
1816
1817 void PushStack(uintptr_t /* val */) {
1818 // counting is already done in the superclass
1819 }
1820
Andreas Gampec200a4a2014-06-16 18:39:09 -07001821 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001822 return reinterpret_cast<uintptr_t>(nullptr);
1823 }
1824
Andreas Gampec200a4a2014-06-16 18:39:09 -07001825 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001826 uint32_t num_stack_entries_;
1827};
1828
Andreas Gampec200a4a2014-06-16 18:39:09 -07001829class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001830 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001831 explicit ComputeGenericJniFrameSize(bool critical_native)
1832 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001833
Andreas Gampec200a4a2014-06-16 18:39:09 -07001834 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1835 // is at *m = sp. Will update to point to the bottom of the save frame.
1836 //
1837 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001838 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001839 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001840 ArtMethod* method = **m;
1841
Andreas Gampe542451c2016-07-26 09:02:02 -07001842 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001843
Andreas Gampec200a4a2014-06-16 18:39:09 -07001844 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1845
1846 // First, fix up the layout of the callee-save frame.
1847 // We have to squeeze in the HandleScope, and relocate the method pointer.
1848
1849 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001850 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001851
1852 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001853 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001854 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001855
1856 sp8 -= scope_and_method;
1857 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001858 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001859
Mathieu Chartiere401d142015-04-22 13:56:20 -07001860 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001861 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1862 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001863
1864 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1865 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001866 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1867 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001868 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001869 }
1870
Andreas Gampec200a4a2014-06-16 18:39:09 -07001871 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001872 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001873 // Reference cookie and padding
1874 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001875 }
1876
Andreas Gampec200a4a2014-06-16 18:39:09 -07001877 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1878 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001879 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001880 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001881 // First, fix up the layout of the callee-save frame.
1882 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001883 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001884
1885 // The bottom of the callee-save frame is now where the method is, *m.
1886 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1887
1888 // Add space for cookie.
1889 LayoutCookie(&sp8);
1890
1891 return sp8;
1892 }
1893
1894 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001895 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1896 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1897 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001898 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001899 Walk(shorty, shorty_len);
1900
1901 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001902 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001903
1904 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1905
1906 // Return the new bottom.
1907 return sp8;
1908 }
1909
1910 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1911
1912 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1913 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001914 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001915
1916 private:
1917 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001918 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001919};
1920
1921uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1922 num_handle_scope_references_++;
1923 return reinterpret_cast<uintptr_t>(nullptr);
1924}
1925
1926void ComputeGenericJniFrameSize::WalkHeader(
1927 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001928 // First 2 parameters are always excluded for @CriticalNative.
1929 if (UNLIKELY(critical_native_)) {
1930 return;
1931 }
1932
Andreas Gampec200a4a2014-06-16 18:39:09 -07001933 // JNIEnv
1934 sm->AdvancePointer(nullptr);
1935
1936 // Class object or this as first argument
1937 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1938}
1939
1940// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1941// the template requirements of BuildGenericJniFrameStateMachine.
1942class FillNativeCall {
1943 public:
1944 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1945 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1946
1947 virtual ~FillNativeCall() {}
1948
1949 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1950 cur_gpr_reg_ = gpr_regs;
1951 cur_fpr_reg_ = fpr_regs;
1952 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001953 }
1954
1955 void PushGpr(uintptr_t val) {
1956 *cur_gpr_reg_ = val;
1957 cur_gpr_reg_++;
1958 }
1959
1960 void PushFpr4(float val) {
1961 *cur_fpr_reg_ = val;
1962 cur_fpr_reg_++;
1963 }
1964
1965 void PushFpr8(uint64_t val) {
1966 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1967 *tmp = val;
1968 cur_fpr_reg_ += 2;
1969 }
1970
1971 void PushStack(uintptr_t val) {
1972 *cur_stack_arg_ = val;
1973 cur_stack_arg_++;
1974 }
1975
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001976 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001977 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001978 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001979 }
1980
1981 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001982 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001983 uint32_t* cur_fpr_reg_;
1984 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001985};
Andreas Gampec147b002014-03-06 18:11:06 -08001986
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1988// of transitioning into native code.
1989class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1990 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001991 BuildGenericJniFrameVisitor(Thread* self,
1992 bool is_static,
1993 bool critical_native,
1994 const char* shorty,
1995 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001996 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001997 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001998 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1999 sm_(&jni_call_) {
2000 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002001 uintptr_t* start_gpr_reg;
2002 uint32_t* start_fpr_reg;
2003 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07002004 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07002005 &handle_scope_,
2006 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07002007 &start_gpr_reg, &start_fpr_reg);
2008
Andreas Gampec200a4a2014-06-16 18:39:09 -07002009 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
2010
Igor Murashkin06a04e02016-09-13 15:57:37 -07002011 // First 2 parameters are always excluded for CriticalNative methods.
2012 if (LIKELY(!critical_native)) {
2013 // jni environment is always first argument
2014 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07002015
Igor Murashkin06a04e02016-09-13 15:57:37 -07002016 if (is_static) {
2017 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
2018 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07002019 }
2020 }
2021
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002022 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002023
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002024 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002025
Vladimir Markof39745e2016-01-26 12:16:55 +00002026 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002027 return handle_scope_->GetHandle(0).GetReference();
2028 }
2029
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002030 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002031 return handle_scope_->GetHandle(0).ToJObject();
2032 }
2033
Ian Rogers1428dce2014-10-21 15:02:15 -07002034 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002035 return bottom_of_used_area_;
2036 }
2037
2038 private:
2039 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
2040 class FillJniCall FINAL : public FillNativeCall {
2041 public:
2042 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07002043 HandleScope* handle_scope, bool critical_native)
2044 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
2045 handle_scope_(handle_scope),
2046 cur_entry_(0),
2047 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07002048
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002049 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002050
2051 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
2052 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
2053 handle_scope_ = scope;
2054 cur_entry_ = 0U;
2055 }
2056
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002057 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002058 // Initialize padding entries.
2059 size_t expected_slots = handle_scope_->NumberOfReferences();
2060 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002061 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002062 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002063
2064 if (!critical_native_) {
2065 // Non-critical natives have at least the self class (jclass) or this (jobject).
2066 DCHECK_NE(cur_entry_, 0U);
2067 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07002068 }
2069
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002070 bool CriticalNative() const {
2071 return critical_native_;
2072 }
2073
Andreas Gampec200a4a2014-06-16 18:39:09 -07002074 private:
2075 HandleScope* handle_scope_;
2076 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002077 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002078 };
2079
2080 HandleScope* handle_scope_;
2081 FillJniCall jni_call_;
2082 void* bottom_of_used_area_;
2083
2084 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002085
2086 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
2087};
2088
Andreas Gampec200a4a2014-06-16 18:39:09 -07002089uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
2090 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002091 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002092 h.Assign(ref);
2093 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2094 cur_entry_++;
2095 return tmp;
2096}
2097
Ian Rogers9758f792014-03-13 09:02:55 -07002098void BuildGenericJniFrameVisitor::Visit() {
2099 Primitive::Type type = GetParamPrimitiveType();
2100 switch (type) {
2101 case Primitive::kPrimLong: {
2102 jlong long_arg;
2103 if (IsSplitLongOrDouble()) {
2104 long_arg = ReadSplitLongParam();
2105 } else {
2106 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2107 }
2108 sm_.AdvanceLong(long_arg);
2109 break;
2110 }
2111 case Primitive::kPrimDouble: {
2112 uint64_t double_arg;
2113 if (IsSplitLongOrDouble()) {
2114 // Read into union so that we don't case to a double.
2115 double_arg = ReadSplitLongParam();
2116 } else {
2117 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2118 }
2119 sm_.AdvanceDouble(double_arg);
2120 break;
2121 }
2122 case Primitive::kPrimNot: {
2123 StackReference<mirror::Object>* stack_ref =
2124 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002125 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002126 break;
2127 }
2128 case Primitive::kPrimFloat:
2129 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2130 break;
2131 case Primitive::kPrimBoolean: // Fall-through.
2132 case Primitive::kPrimByte: // Fall-through.
2133 case Primitive::kPrimChar: // Fall-through.
2134 case Primitive::kPrimShort: // Fall-through.
2135 case Primitive::kPrimInt: // Fall-through.
2136 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2137 break;
2138 case Primitive::kPrimVoid:
2139 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002140 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002141 }
2142}
2143
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002144void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002145 // Clear out rest of the scope.
2146 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002147 if (!jni_call_.CriticalNative()) {
2148 // Install HandleScope.
2149 self->PushHandleScope(handle_scope_);
2150 }
Ian Rogers9758f792014-03-13 09:02:55 -07002151}
2152
Ian Rogers04c31d22014-07-07 21:44:06 -07002153#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002154extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002155#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002156extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002157#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002158
Igor Murashkin06a04e02016-09-13 15:57:37 -07002159static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2160 uint32_t cookie,
2161 bool fast_native ATTRIBUTE_UNUSED,
2162 jobject l,
2163 jobject lock) {
2164 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002165 if (lock != nullptr) {
2166 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2167 } else {
2168 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2169 }
2170}
2171
Igor Murashkin06a04e02016-09-13 15:57:37 -07002172static void artQuickGenericJniEndJNINonRef(Thread* self,
2173 uint32_t cookie,
2174 bool fast_native,
2175 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002176 if (lock != nullptr) {
2177 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002178 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002179 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002180 if (UNLIKELY(fast_native)) {
2181 JniMethodFastEnd(cookie, self);
2182 } else {
2183 JniMethodEnd(cookie, self);
2184 }
Andreas Gampead615172014-04-04 16:20:13 -07002185 }
2186}
2187
Andreas Gampec147b002014-03-06 18:11:06 -08002188/*
2189 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002190 * 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 -08002191 * The final element on the stack is a pointer to the native code.
2192 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002193 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002194 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002195 *
Andreas Gampec147b002014-03-06 18:11:06 -08002196 * The return of this function denotes:
2197 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2198 * 2) An error, if the value is negative.
2199 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002200extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002201 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002202 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002203 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Roland Levillain0d2323e2017-06-26 18:14:39 +01002204 // Fix up a callee-save frame at the bottom of the stack (at `*sp`,
2205 // above the alloca region) while we check for optimization
2206 // annotations, thus allowing stack walking until the completion of
2207 // the JNI frame creation.
2208 //
2209 // Note however that the Generic JNI trampoline does not expect
2210 // exception being thrown at that stage.
2211 *sp = Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs);
2212 self->SetTopOfStack(sp);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002213 uint32_t shorty_len = 0;
2214 const char* shorty = called->GetShorty(&shorty_len);
Roland Levillain35e42f02017-06-26 18:14:39 +01002215 // Optimization annotations lookup does not try to resolve classes,
2216 // as this may throw an exception, which is not supported by the
2217 // Generic JNI trampoline at this stage; instead, method's
2218 // annotations' classes are looked up in the bootstrap class
2219 // loader's resolved types (which won't trigger an exception).
Igor Murashkin06a04e02016-09-13 15:57:37 -07002220 bool critical_native = called->IsAnnotatedWithCriticalNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002221 // ArtMethod::IsAnnotatedWithCriticalNative should not throw
2222 // an exception; clear it if it happened anyway.
2223 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2224 if (self->IsExceptionPending()) {
2225 self->ClearException();
2226 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002227 bool fast_native = called->IsAnnotatedWithFastNative();
Roland Levillain0d2323e2017-06-26 18:14:39 +01002228 // ArtMethod::IsAnnotatedWithFastNative should not throw
2229 // an exception; clear it if it happened anyway.
2230 // TODO: Revisit this code path and turn this into a CHECK(!self->IsExceptionPending()).
2231 if (self->IsExceptionPending()) {
2232 self->ClearException();
2233 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002234 bool normal_native = !critical_native && !fast_native;
Roland Levillain0d2323e2017-06-26 18:14:39 +01002235 // Restore the initial ArtMethod pointer at `*sp`.
2236 *sp = called;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002237
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002238 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002239 BuildGenericJniFrameVisitor visitor(self,
2240 called->IsStatic(),
2241 critical_native,
2242 shorty,
2243 shorty_len,
2244 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002245 {
2246 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2247 visitor.VisitArguments();
2248 // FinalizeHandleScope pushes the handle scope on the thread.
2249 visitor.FinalizeHandleScope(self);
2250 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002251
Andreas Gampec200a4a2014-06-16 18:39:09 -07002252 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002253 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002254
Ian Rogerse0dcd462014-03-08 15:21:04 -08002255 self->VerifyStack();
2256
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002257 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002258 uint32_t* sp32;
2259 // Skip calling JniMethodStart for @CriticalNative.
2260 if (LIKELY(!critical_native)) {
2261 // Start JNI, save the cookie.
2262 if (called->IsSynchronized()) {
2263 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2264 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2265 if (self->IsExceptionPending()) {
2266 self->PopHandleScope();
2267 // A negative value denotes an error.
2268 return GetTwoWordFailureValue();
2269 }
2270 } else {
2271 if (fast_native) {
2272 cookie = JniMethodFastStart(self);
2273 } else {
2274 DCHECK(normal_native);
2275 cookie = JniMethodStart(self);
2276 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002277 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002278 sp32 = reinterpret_cast<uint32_t*>(sp);
2279 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002280 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002281
Andreas Gampe90546832014-03-12 18:07:19 -07002282 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002283 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002284
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002285 // There are two cases for the content of nativeCode:
2286 // 1) Pointer to the native function.
2287 // 2) Pointer to the trampoline for native code binding.
2288 // In the second case, we need to execute the binding and continue with the actual native function
2289 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002290 DCHECK(nativeCode != nullptr);
2291 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002292#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002293 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002294#else
2295 nativeCode = artFindNativeMethod(self);
2296#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002297
2298 if (nativeCode == nullptr) {
2299 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002300
Igor Murashkin06a04e02016-09-13 15:57:37 -07002301 // @CriticalNative calls do not need to call back into JniMethodEnd.
2302 if (LIKELY(!critical_native)) {
2303 // End JNI, as the assembly will move to deliver the exception.
2304 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2305 if (shorty[0] == 'L') {
2306 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2307 } else {
2308 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2309 }
Andreas Gampead615172014-04-04 16:20:13 -07002310 }
2311
Andreas Gampec200a4a2014-06-16 18:39:09 -07002312 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002313 }
2314 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002315 }
2316
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002317#if defined(__mips__) && !defined(__LP64__)
2318 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2319 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2320 // and load into floating-point registers.
2321 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2322 // view):
2323 // (1)
2324 // | DOUBLE | DOUBLE | other args, if any
2325 // | F12 | F13 | F14 | F15 |
2326 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2327 // (2)
2328 // | DOUBLE | FLOAT | (PAD) | other args, if any
2329 // | F12 | F13 | F14 | |
2330 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2331 // (3)
2332 // | FLOAT | (PAD) | DOUBLE | other args, if any
2333 // | F12 | | F14 | F15 |
2334 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2335 // (4)
2336 // | FLOAT | FLOAT | other args, if any
2337 // | F12 | F14 |
2338 // | SP+0 | SP+4 | SP+8
2339 // As you can see, only the last case (4) is special. In all others we can just
2340 // load F12/F13 and F14/F15 in the same manner.
2341 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2342 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2343 if (nativeCode != nullptr &&
2344 shorty != nullptr &&
2345 shorty_len >= 3 &&
2346 shorty[1] == 'F' &&
2347 shorty[2] == 'F') {
2348 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2349 }
2350#endif
2351
Andreas Gampec200a4a2014-06-16 18:39:09 -07002352 // Return native code addr(lo) and bottom of alloca address(hi).
2353 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2354 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002355}
2356
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002357// Defined in quick_jni_entrypoints.cc.
2358extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2359 jvalue result, uint64_t result_f, ArtMethod* called,
2360 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002361/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002362 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002363 * unlocking.
2364 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002365extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2366 jvalue result,
2367 uint64_t result_f) {
2368 // We're here just back from a native call. We don't have the shared mutator lock at this point
2369 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2370 // anything that requires a mutator lock before that would cause problems as GC may have the
2371 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002372 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002373 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002374 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002375 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002376 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2377 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002378}
2379
Andreas Gamped58342c2014-06-05 14:18:08 -07002380// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2381// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002382//
Andreas Gamped58342c2014-06-05 14:18:08 -07002383// 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 -07002384// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002385
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002386template <InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002387static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2388 ObjPtr<mirror::Object> this_object,
2389 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002390 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002391 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002392 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002393 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Vladimir Markof79aa7f2017-07-04 16:58:55 +01002394 ArtMethod* method = FindMethodFast<type, access_check>(method_idx, this_object, caller_method);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002395 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002396 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2397 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002398 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002399 {
2400 // Remember the args in case a GC happens in FindMethodFromCode.
2401 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2402 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2403 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002404 method = FindMethodFromCode<type, access_check>(method_idx,
2405 &this_object,
2406 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002407 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002408 visitor.FixupReferences();
2409 }
2410
Ian Rogerse0a02da2014-12-02 14:10:53 -08002411 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002412 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002413 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002414 }
2415 }
2416 DCHECK(!self->IsExceptionPending());
2417 const void* code = method->GetEntryPointFromQuickCompiledCode();
2418
2419 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002420 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002421 << " location: "
2422 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002423
Andreas Gamped58342c2014-06-05 14:18:08 -07002424 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2425 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002426}
2427
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002428// Explicit artInvokeCommon template function declarations to please analysis tool.
2429#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002430 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002431 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002432 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002433
2434EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2435EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2436EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2437EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2438EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2439EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2440EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2441EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2442EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2443EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2444#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2445
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002446// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002447extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002448 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002449 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002450 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002451}
2452
Andreas Gampec200a4a2014-06-16 18:39:09 -07002453extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002454 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002455 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002456 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002457}
2458
Andreas Gampec200a4a2014-06-16 18:39:09 -07002459extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002460 uint32_t method_idx,
2461 mirror::Object* this_object ATTRIBUTE_UNUSED,
2462 Thread* self,
2463 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2464 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2465 // it doesn't cause ObjPtr alignment failure check.
2466 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002467}
2468
Andreas Gampec200a4a2014-06-16 18:39:09 -07002469extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002470 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002471 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002472 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002473}
2474
Andreas Gampec200a4a2014-06-16 18:39:09 -07002475extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002476 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002477 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002478 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002479}
2480
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002481// Helper function for art_quick_imt_conflict_trampoline to look up the interface method.
2482extern "C" ArtMethod* artLookupResolvedMethod(uint32_t method_index, ArtMethod* referrer)
2483 REQUIRES_SHARED(Locks::mutator_lock_) {
2484 ScopedAssertNoThreadSuspension ants(__FUNCTION__);
2485 DCHECK(!referrer->IsProxyMethod());
2486 ArtMethod* result = Runtime::Current()->GetClassLinker()->LookupResolvedMethod(
2487 method_index, referrer->GetDexCache(), referrer->GetClassLoader());
2488 DCHECK(result == nullptr ||
2489 result->GetDeclaringClass()->IsInterface() ||
2490 result->GetDeclaringClass() ==
2491 WellKnownClasses::ToClass(WellKnownClasses::java_lang_Object))
2492 << result->PrettyMethod();
2493 return result;
2494}
2495
Jeff Hao5667f562017-02-27 19:32:01 -08002496// Determine target of interface dispatch. The interface method and this object are known non-null.
2497// The interface method is the method returned by the dex cache in the conflict trampoline.
2498extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002499 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002500 Thread* self,
2501 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002502 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002503 ScopedQuickEntrypointChecks sqec(self);
Vladimir Marko302f69c2017-07-25 15:27:15 +01002504 StackHandleScope<2> hs(self);
2505 Handle<mirror::Object> this_object = hs.NewHandle(raw_this_object);
2506 Handle<mirror::Class> cls = hs.NewHandle(this_object->GetClass());
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002507
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002508 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002509 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002510 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002511
Vladimir Marko302f69c2017-07-25 15:27:15 +01002512 if (UNLIKELY(interface_method == nullptr)) {
Vladimir Marko07bfbac2017-07-06 14:55:02 +01002513 // The interface method is unresolved, so resolve it in the dex file of the caller.
Jeff Hao5667f562017-02-27 19:32:01 -08002514 // Fetch the dex_method_idx of the target interface method from the caller.
2515 uint32_t dex_method_idx;
2516 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2517 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2518 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2519 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2520 Instruction::Code instr_code = instr->Opcode();
2521 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2522 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2523 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2524 if (instr_code == Instruction::INVOKE_INTERFACE) {
2525 dex_method_idx = instr->VRegB_35c();
2526 } else {
2527 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2528 dex_method_idx = instr->VRegB_3rc();
2529 }
2530
Vladimir Marko302f69c2017-07-25 15:27:15 +01002531 const DexFile& dex_file = caller_method->GetDeclaringClass()->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002532 uint32_t shorty_len;
Vladimir Marko302f69c2017-07-25 15:27:15 +01002533 const char* shorty = dex_file.GetMethodShorty(dex_file.GetMethodId(dex_method_idx),
2534 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002535 {
Vladimir Marko302f69c2017-07-25 15:27:15 +01002536 // Remember the args in case a GC happens in ClassLinker::ResolveMethod().
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002537 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2538 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2539 visitor.VisitArguments();
Vladimir Marko302f69c2017-07-25 15:27:15 +01002540 ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
2541 interface_method = class_linker->ResolveMethod<ClassLinker::ResolveMode::kNoChecks>(
2542 self, dex_method_idx, caller_method, kInterface);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002543 visitor.FixupReferences();
2544 }
2545
Vladimir Marko302f69c2017-07-25 15:27:15 +01002546 if (UNLIKELY(interface_method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002547 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002548 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002549 }
Vladimir Marko302f69c2017-07-25 15:27:15 +01002550 }
2551
2552 DCHECK(!interface_method->IsRuntimeMethod());
2553 // Look whether we have a match in the ImtConflictTable.
2554 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
2555 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
2556 if (LIKELY(conflict_method->IsRuntimeMethod())) {
2557 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
2558 DCHECK(current_table != nullptr);
2559 method = current_table->Lookup(interface_method, kRuntimePointerSize);
2560 } else {
2561 // It seems we aren't really a conflict method!
2562 if (kIsDebugBuild) {
2563 ArtMethod* m = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2564 CHECK_EQ(conflict_method, m)
2565 << interface_method->PrettyMethod() << " / " << conflict_method->PrettyMethod() << " / "
2566 << " / " << ArtMethod::PrettyMethod(m) << " / " << cls->PrettyClass();
2567 }
2568 method = conflict_method;
2569 }
2570 if (method != nullptr) {
2571 return GetTwoWordSuccessValue(
2572 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2573 reinterpret_cast<uintptr_t>(method));
2574 }
2575
2576 // No match, use the IfTable.
2577 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
2578 if (UNLIKELY(method == nullptr)) {
2579 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2580 interface_method, this_object.Get(), caller_method);
2581 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002582 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002583
2584 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2585 // We create a new table with the new pair { interface_method, method }.
Vladimir Marko302f69c2017-07-25 15:27:15 +01002586 DCHECK(conflict_method->IsRuntimeMethod());
2587 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2588 cls.Get(),
2589 conflict_method,
2590 interface_method,
2591 method,
2592 /*force_new_conflict_method*/false);
2593 if (new_conflict_method != conflict_method) {
2594 // Update the IMT if we create a new conflict method. No fence needed here, as the
2595 // data is consistent.
2596 imt->Set(imt_index,
2597 new_conflict_method,
2598 kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002599 }
2600
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002601 const void* code = method->GetEntryPointFromQuickCompiledCode();
2602
2603 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002604 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002605 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002606
Andreas Gamped58342c2014-06-05 14:18:08 -07002607 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2608 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002609}
2610
Orion Hodsonac141392017-01-13 11:53:47 +00002611// Returns shorty type so the caller can determine how to put |result|
2612// into expected registers. The shorty type is static so the compiler
2613// could call different flavors of this code path depending on the
2614// shorty type though this would require different entry points for
2615// each type.
2616extern "C" uintptr_t artInvokePolymorphic(
2617 JValue* result,
2618 mirror::Object* raw_method_handle,
2619 Thread* self,
2620 ArtMethod** sp)
2621 REQUIRES_SHARED(Locks::mutator_lock_) {
2622 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002623 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002624
2625 // Start new JNI local reference state
2626 JNIEnvExt* env = self->GetJniEnv();
2627 ScopedObjectAccessUnchecked soa(env);
2628 ScopedJniEnvLocalRefState env_state(env);
2629 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2630
2631 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2632 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2633 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2634 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2635 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2636 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2637 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2638 const DexFile* dex_file = caller_method->GetDexFile();
2639 const uint32_t proto_idx = inst->VRegH();
2640 const char* shorty = dex_file->GetShorty(proto_idx);
2641 const size_t shorty_length = strlen(shorty);
2642 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2643 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002644 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002645
2646 // Wrap raw_method_handle in a Handle for safety.
2647 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002648 Handle<mirror::MethodHandle> method_handle(
2649 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002650 raw_method_handle = nullptr;
2651 self->EndAssertNoThreadSuspension(old_cause);
2652
2653 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2654 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Vladimir Markoba118822017-06-12 15:41:56 +01002655 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::ResolveMode::kCheckICCEAndIAE>(
2656 self, inst->VRegB(), caller_method, kVirtual);
Orion Hodsonac141392017-01-13 11:53:47 +00002657 DCHECK((resolved_method ==
2658 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2659 (resolved_method ==
2660 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2661 if (UNLIKELY(method_handle.IsNull())) {
2662 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2663 return static_cast<uintptr_t>('V');
2664 }
2665
2666 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2667 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2668 *dex_file, proto_idx,
2669 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2670 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2671 // This implies we couldn't resolve one or more types in this method handle.
2672 if (UNLIKELY(method_type.IsNull())) {
2673 CHECK(self->IsExceptionPending());
2674 return static_cast<uintptr_t>('V');
2675 }
2676
2677 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2678 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2679
2680 // Fix references before constructing the shadow frame.
2681 gc_visitor.FixupReferences();
2682
2683 // Construct shadow frame placing arguments consecutively from |first_arg|.
2684 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2685 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2686 const size_t first_arg = 0;
2687 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2688 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2689 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2690 ScopedStackedShadowFramePusher
2691 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2692 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2693 kMethodIsStatic,
2694 shorty,
2695 strlen(shorty),
2696 shadow_frame,
2697 first_arg);
2698 shadow_frame_builder.VisitArguments();
2699
2700 // Push a transition back into managed code onto the linked list in thread.
2701 ManagedStack fragment;
2702 self->PushManagedStackFragment(&fragment);
2703
2704 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2705 // consecutive order.
2706 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2707 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002708 if (!DoInvokePolymorphic<true /* is_range */>(self,
2709 resolved_method,
2710 *shadow_frame,
2711 method_handle,
2712 method_type,
2713 unused_args,
2714 first_callee_arg,
2715 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002716 DCHECK(self->IsExceptionPending());
2717 }
2718
2719 // Pop transition record.
2720 self->PopManagedStackFragment(fragment);
2721
2722 return static_cast<uintptr_t>(shorty[0]);
2723}
2724
Ian Rogers848871b2013-08-05 10:56:33 -07002725} // namespace art