blob: 6fcb71171669b61985804f1fa98cedaf5f81a19b [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"
Ian Rogers848871b2013-08-05 10:56:33 -070030#include "interpreter/interpreter.h"
Nicolas Geoffray796d6302016-03-13 22:22:31 +000031#include "linear_alloc.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010032#include "method_bss_mapping.h"
Orion Hodsonac141392017-01-13 11:53:47 +000033#include "method_handles.h"
Ian Rogerse0a02da2014-12-02 14:10:53 -080034#include "method_reference.h"
Ian Rogers848871b2013-08-05 10:56:33 -070035#include "mirror/class-inl.h"
Mathieu Chartier5f3ded42014-04-03 15:25:30 -070036#include "mirror/dex_cache-inl.h"
Mathieu Chartierfc58af42015-04-16 18:00:39 -070037#include "mirror/method.h"
Orion Hodsonac141392017-01-13 11:53:47 +000038#include "mirror/method_handle_impl.h"
Ian Rogers848871b2013-08-05 10:56:33 -070039#include "mirror/object-inl.h"
40#include "mirror/object_array-inl.h"
Vladimir Marko0eb882b2017-05-15 13:39:18 +010041#include "oat_file.h"
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +010042#include "oat_quick_method_header.h"
Andreas Gampe639bdd12015-06-03 11:22:45 -070043#include "quick_exception_handler.h"
Ian Rogers848871b2013-08-05 10:56:33 -070044#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070045#include "scoped_thread_state_change-inl.h"
Andreas Gampeb3025922015-09-01 14:45:00 -070046#include "stack.h"
Andreas Gampe513061a2017-06-01 09:17:34 -070047#include "thread-inl.h"
Orion Hodsonac141392017-01-13 11:53:47 +000048#include "well_known_classes.h"
Ian Rogers848871b2013-08-05 10:56:33 -070049
50namespace art {
51
Andreas Gampe8228cdf2017-05-30 15:03:54 -070052// Visits the arguments as saved to the stack by a CalleeSaveType::kRefAndArgs callee save frame.
Ian Rogers848871b2013-08-05 10:56:33 -070053class QuickArgumentVisitor {
Ian Rogers936b37f2014-02-14 00:52:24 -080054 // Number of bytes for each out register in the caller method's frame.
55 static constexpr size_t kBytesStackArgLocation = 4;
Alexei Zavjalov41c507a2014-05-15 16:02:46 +070056 // Frame size in bytes of a callee-save frame for RefsAndArgs.
57 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_FrameSize =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070058 GetCalleeSaveFrameSize(kRuntimeISA, CalleeSaveType::kSaveRefsAndArgs);
Ian Rogers848871b2013-08-05 10:56:33 -070059#if defined(__arm__)
60 // The callee save frame is pointed to by SP.
61 // | argN | |
62 // | ... | |
63 // | arg4 | |
64 // | arg3 spill | | Caller's frame
65 // | arg2 spill | |
66 // | arg1 spill | |
67 // | Method* | ---
68 // | LR |
Zheng Xu5667fdb2014-10-23 18:29:55 +080069 // | ... | 4x6 bytes callee saves
70 // | R3 |
71 // | R2 |
72 // | R1 |
73 // | S15 |
74 // | : |
75 // | S0 |
76 // | | 4x2 bytes padding
Ian Rogers848871b2013-08-05 10:56:33 -070077 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -050078 static constexpr bool kSplitPairAcrossRegisterAndStack = kArm32QuickCodeUseSoftFloat;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +000079 static constexpr bool kAlignPairRegister = !kArm32QuickCodeUseSoftFloat;
Zheng Xu5667fdb2014-10-23 18:29:55 +080080 static constexpr bool kQuickSoftFloatAbi = kArm32QuickCodeUseSoftFloat;
81 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = !kArm32QuickCodeUseSoftFloat;
Goran Jakovljevicff734982015-08-24 12:58:55 +000082 static constexpr bool kQuickSkipOddFpRegisters = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +080083 static constexpr size_t kNumQuickGprArgs = 3;
84 static constexpr size_t kNumQuickFprArgs = kArm32QuickCodeUseSoftFloat ? 0 : 16;
Andreas Gampe1a5c4062015-01-15 12:10:47 -080085 static constexpr bool kGprFprLockstep = false;
Zheng Xub551fdc2014-07-25 11:49:42 +080086 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070087 arm::ArmCalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080088 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070089 arm::ArmCalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs); // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +080090 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -070091 arm::ArmCalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs); // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -080092 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +000093 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -080094 }
Stuart Monteithb95a5342014-03-12 13:32:32 +000095#elif defined(__aarch64__)
96 // The callee save frame is pointed to by SP.
97 // | argN | |
98 // | ... | |
99 // | arg4 | |
100 // | arg3 spill | | Caller's frame
101 // | arg2 spill | |
102 // | arg1 spill | |
103 // | Method* | ---
104 // | LR |
Zheng Xub551fdc2014-07-25 11:49:42 +0800105 // | X29 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000106 // | : |
Serban Constantinescu9bd88b02015-04-22 16:24:46 +0100107 // | X20 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000108 // | X7 |
109 // | : |
110 // | X1 |
Zheng Xub551fdc2014-07-25 11:49:42 +0800111 // | D7 |
Stuart Monteithb95a5342014-03-12 13:32:32 +0000112 // | : |
113 // | D0 |
114 // | | padding
115 // | Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500116 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000117 static constexpr bool kAlignPairRegister = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000118 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800119 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000120 static constexpr bool kQuickSkipOddFpRegisters = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +0000121 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
122 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800123 static constexpr bool kGprFprLockstep = false;
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700124 // Offset of first FPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800125 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700126 arm64::Arm64CalleeSaveFpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
127 // Offset of first GPR arg.
Zheng Xub551fdc2014-07-25 11:49:42 +0800128 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700129 arm64::Arm64CalleeSaveGpr1Offset(CalleeSaveType::kSaveRefsAndArgs);
130 // Offset of return address.
Zheng Xub551fdc2014-07-25 11:49:42 +0800131 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset =
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700132 arm64::Arm64CalleeSaveLrOffset(CalleeSaveType::kSaveRefsAndArgs);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000133 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000134 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Stuart Monteithb95a5342014-03-12 13:32:32 +0000135 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800136#elif defined(__mips__) && !defined(__LP64__)
Ian Rogers848871b2013-08-05 10:56:33 -0700137 // The callee save frame is pointed to by SP.
138 // | argN | |
139 // | ... | |
140 // | arg4 | |
141 // | arg3 spill | | Caller's frame
142 // | arg2 spill | |
143 // | arg1 spill | |
144 // | Method* | ---
145 // | RA |
146 // | ... | callee saves
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800147 // | T1 | arg5
148 // | T0 | arg4
Ian Rogers848871b2013-08-05 10:56:33 -0700149 // | A3 | arg3
150 // | A2 | arg2
151 // | A1 | arg1
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800152 // | F19 |
153 // | F18 | f_arg5
154 // | F17 |
155 // | F16 | f_arg4
Goran Jakovljevicff734982015-08-24 12:58:55 +0000156 // | F15 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800157 // | F14 | f_arg3
Goran Jakovljevicff734982015-08-24 12:58:55 +0000158 // | F13 |
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800159 // | F12 | f_arg2
160 // | F11 |
161 // | F10 | f_arg1
162 // | F9 |
163 // | F8 | f_arg0
Goran Jakovljevicff734982015-08-24 12:58:55 +0000164 // | | padding
Ian Rogers848871b2013-08-05 10:56:33 -0700165 // | A0/Method* | <- sp
Goran Jakovljevicff734982015-08-24 12:58:55 +0000166 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
167 static constexpr bool kAlignPairRegister = true;
168 static constexpr bool kQuickSoftFloatAbi = false;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800169 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000170 static constexpr bool kQuickSkipOddFpRegisters = true;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800171 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
172 static constexpr size_t kNumQuickFprArgs = 12; // 6 arguments passed in FPRs. Floats can be
173 // passed only in even numbered registers and each
174 // double occupies two registers.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800175 static constexpr bool kGprFprLockstep = false;
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800176 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 8; // Offset of first FPR arg.
177 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 56; // Offset of first GPR arg.
178 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 108; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800179 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000180 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800181 }
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800182#elif defined(__mips__) && defined(__LP64__)
183 // The callee save frame is pointed to by SP.
184 // | argN | |
185 // | ... | |
186 // | arg4 | |
187 // | arg3 spill | | Caller's frame
188 // | arg2 spill | |
189 // | arg1 spill | |
190 // | Method* | ---
191 // | RA |
192 // | ... | callee saves
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800193 // | A7 | arg7
194 // | A6 | arg6
195 // | A5 | arg5
196 // | A4 | arg4
197 // | A3 | arg3
198 // | A2 | arg2
199 // | A1 | arg1
Goran Jakovljevicff734982015-08-24 12:58:55 +0000200 // | F19 | f_arg7
201 // | F18 | f_arg6
202 // | F17 | f_arg5
203 // | F16 | f_arg4
204 // | F15 | f_arg3
205 // | F14 | f_arg2
206 // | F13 | f_arg1
207 // | F12 | f_arg0
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800208 // | | padding
209 // | A0/Method* | <- sp
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800210 // NOTE: for Mip64, when A0 is skipped, F12 is also skipped.
Douglas Leungd18e0832015-02-09 15:22:26 -0800211 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800212 static constexpr bool kAlignPairRegister = false;
213 static constexpr bool kQuickSoftFloatAbi = false;
214 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000215 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800216 static constexpr size_t kNumQuickGprArgs = 7; // 7 arguments passed in GPRs.
217 static constexpr size_t kNumQuickFprArgs = 7; // 7 arguments passed in FPRs.
218 static constexpr bool kGprFprLockstep = true;
219
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800220 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 24; // Offset of first FPR arg (F13).
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800221 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80; // Offset of first GPR arg (A1).
222 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 200; // Offset of return address.
223 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
224 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
225 }
Ian Rogers848871b2013-08-05 10:56:33 -0700226#elif defined(__i386__)
227 // The callee save frame is pointed to by SP.
228 // | argN | |
229 // | ... | |
230 // | arg4 | |
231 // | arg3 spill | | Caller's frame
232 // | arg2 spill | |
233 // | arg1 spill | |
234 // | Method* | ---
235 // | Return |
236 // | EBP,ESI,EDI | callee saves
237 // | EBX | arg3
238 // | EDX | arg2
239 // | ECX | arg1
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000240 // | XMM3 | float arg 4
241 // | XMM2 | float arg 3
242 // | XMM1 | float arg 2
243 // | XMM0 | float arg 1
Ian Rogers848871b2013-08-05 10:56:33 -0700244 // | EAX/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500245 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000246 static constexpr bool kAlignPairRegister = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000247 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800248 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000249 static constexpr bool kQuickSkipOddFpRegisters = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800250 static constexpr size_t kNumQuickGprArgs = 3; // 3 arguments passed in GPRs.
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000251 static constexpr size_t kNumQuickFprArgs = 4; // 4 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800252 static constexpr bool kGprFprLockstep = false;
Mark P Mendell966c3ae2015-01-27 15:45:27 +0000253 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 4; // Offset of first FPR arg.
254 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 4 + 4*8; // Offset of first GPR arg.
255 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 28 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800256 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000257 return gpr_index * GetBytesPerGprSpillLocation(kRuntimeISA);
Ian Rogers936b37f2014-02-14 00:52:24 -0800258 }
Ian Rogersef7d42f2014-01-06 12:55:46 -0800259#elif defined(__x86_64__)
Ian Rogers936b37f2014-02-14 00:52:24 -0800260 // The callee save frame is pointed to by SP.
261 // | argN | |
262 // | ... | |
263 // | reg. arg spills | | Caller's frame
264 // | Method* | ---
265 // | Return |
266 // | R15 | callee save
267 // | R14 | callee save
268 // | R13 | callee save
269 // | R12 | callee save
270 // | R9 | arg5
271 // | R8 | arg4
272 // | RSI/R6 | arg1
273 // | RBP/R5 | callee save
274 // | RBX/R3 | callee save
275 // | RDX/R2 | arg2
276 // | RCX/R1 | arg3
277 // | XMM7 | float arg 8
278 // | XMM6 | float arg 7
279 // | XMM5 | float arg 6
280 // | XMM4 | float arg 5
281 // | XMM3 | float arg 4
282 // | XMM2 | float arg 3
283 // | XMM1 | float arg 2
284 // | XMM0 | float arg 1
285 // | Padding |
286 // | RDI/Method* | <- sp
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500287 static constexpr bool kSplitPairAcrossRegisterAndStack = false;
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000288 static constexpr bool kAlignPairRegister = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800289 static constexpr bool kQuickSoftFloatAbi = false; // This is a hard float ABI.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800290 static constexpr bool kQuickDoubleRegAlignedFloatBackFilled = false;
Goran Jakovljevicff734982015-08-24 12:58:55 +0000291 static constexpr bool kQuickSkipOddFpRegisters = false;
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700292 static constexpr size_t kNumQuickGprArgs = 5; // 5 arguments passed in GPRs.
Dmitry Petrochenko58994cd2014-05-17 01:02:18 +0700293 static constexpr size_t kNumQuickFprArgs = 8; // 8 arguments passed in FPRs.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800294 static constexpr bool kGprFprLockstep = false;
Ian Rogers936b37f2014-02-14 00:52:24 -0800295 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset = 16; // Offset of first FPR arg.
Serguei Katkovc3801912014-07-08 17:21:53 +0700296 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset = 80 + 4*8; // Offset of first GPR arg.
297 static constexpr size_t kQuickCalleeSaveFrame_RefAndArgs_LrOffset = 168 + 4*8; // Offset of return address.
Ian Rogers936b37f2014-02-14 00:52:24 -0800298 static size_t GprIndexToGprOffset(uint32_t gpr_index) {
299 switch (gpr_index) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000300 case 0: return (4 * GetBytesPerGprSpillLocation(kRuntimeISA));
301 case 1: return (1 * GetBytesPerGprSpillLocation(kRuntimeISA));
302 case 2: return (0 * GetBytesPerGprSpillLocation(kRuntimeISA));
303 case 3: return (5 * GetBytesPerGprSpillLocation(kRuntimeISA));
304 case 4: return (6 * GetBytesPerGprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800305 default:
Andreas Gampec200a4a2014-06-16 18:39:09 -0700306 LOG(FATAL) << "Unexpected GPR index: " << gpr_index;
307 return 0;
Ian Rogers936b37f2014-02-14 00:52:24 -0800308 }
309 }
Ian Rogers848871b2013-08-05 10:56:33 -0700310#else
311#error "Unsupported architecture"
Ian Rogers848871b2013-08-05 10:56:33 -0700312#endif
313
Ian Rogers936b37f2014-02-14 00:52:24 -0800314 public:
Sebastien Hertza836bc92014-11-25 16:30:53 +0100315 // Special handling for proxy methods. Proxy methods are instance methods so the
316 // 'this' object is the 1st argument. They also have the same frame layout as the
317 // kRefAndArgs runtime method. Since 'this' is a reference, it is located in the
318 // 1st GPR.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700319 static mirror::Object* GetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700320 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray3a090922015-11-24 09:17:30 +0000321 CHECK((*sp)->IsProxyMethod());
Sebastien Hertza836bc92014-11-25 16:30:53 +0100322 CHECK_GT(kNumQuickGprArgs, 0u);
323 constexpr uint32_t kThisGprIndex = 0u; // 'this' is in the 1st GPR.
324 size_t this_arg_offset = kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset +
325 GprIndexToGprOffset(kThisGprIndex);
326 uint8_t* this_arg_address = reinterpret_cast<uint8_t*>(sp) + this_arg_offset;
327 return reinterpret_cast<StackReference<mirror::Object>*>(this_arg_address)->AsMirrorPtr();
328 }
329
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700330 static ArtMethod* GetCallingMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700331 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700332 return GetCalleeSaveMethodCaller(sp, CalleeSaveType::kSaveRefsAndArgs);
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +0100333 }
334
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700335 static ArtMethod* GetOuterMethod(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700336 DCHECK((*sp)->IsCalleeSaveMethod());
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100337 uint8_t* previous_sp =
338 reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700339 return *reinterpret_cast<ArtMethod**>(previous_sp);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100340 }
341
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700342 static uint32_t GetCallingDexPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700343 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700344 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
345 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartiere401d142015-04-22 13:56:20 -0700346 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
347 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100348 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100349 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
350 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100351
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100352 if (current_code->IsOptimized()) {
353 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
David Srbecky09ed0982016-02-12 21:58:43 +0000354 CodeInfoEncoding encoding = code_info.ExtractEncoding();
David Brazdilf677ebf2015-05-29 16:29:43 +0100355 StackMap stack_map = code_info.GetStackMapForNativePcOffset(outer_pc_offset, encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100356 DCHECK(stack_map.IsValid());
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800357 if (stack_map.HasInlineInfo(encoding.stack_map.encoding)) {
David Brazdilf677ebf2015-05-29 16:29:43 +0100358 InlineInfo inline_info = code_info.GetInlineInfoOf(stack_map, encoding);
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800359 return inline_info.GetDexPcAtDepth(encoding.inline_info.encoding,
360 inline_info.GetDepth(encoding.inline_info.encoding)-1);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100361 } else {
Mathieu Chartier575d3e62017-02-06 11:00:40 -0800362 return stack_map.GetDexPc(encoding.stack_map.encoding);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100363 }
364 } else {
Nicolas Geoffray524e7ea2015-10-16 17:13:34 +0100365 return current_code->ToDexPc(*caller_sp, outer_pc);
Nicolas Geoffrayd23eeef2015-05-18 22:31:29 +0100366 }
Ian Rogers848871b2013-08-05 10:56:33 -0700367 }
368
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800369 static bool GetInvokeType(ArtMethod** sp, InvokeType* invoke_type, uint32_t* dex_method_index)
370 REQUIRES_SHARED(Locks::mutator_lock_) {
371 DCHECK((*sp)->IsCalleeSaveMethod());
Andreas Gampe8228cdf2017-05-30 15:03:54 -0700372 const size_t callee_frame_size = GetCalleeSaveFrameSize(kRuntimeISA,
373 CalleeSaveType::kSaveRefsAndArgs);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800374 ArtMethod** caller_sp = reinterpret_cast<ArtMethod**>(
375 reinterpret_cast<uintptr_t>(sp) + callee_frame_size);
376 uintptr_t outer_pc = QuickArgumentVisitor::GetCallingPc(sp);
377 const OatQuickMethodHeader* current_code = (*caller_sp)->GetOatQuickMethodHeader(outer_pc);
378 if (!current_code->IsOptimized()) {
379 return false;
380 }
381 uintptr_t outer_pc_offset = current_code->NativeQuickPcOffset(outer_pc);
382 CodeInfo code_info = current_code->GetOptimizedCodeInfo();
383 CodeInfoEncoding encoding = code_info.ExtractEncoding();
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700384 MethodInfo method_info = current_code->GetOptimizedMethodInfo();
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800385 InvokeInfo invoke(code_info.GetInvokeInfoForNativePcOffset(outer_pc_offset, encoding));
386 if (invoke.IsValid()) {
387 *invoke_type = static_cast<InvokeType>(invoke.GetInvokeType(encoding.invoke_info.encoding));
Mathieu Chartiercbcedbf2017-03-12 22:24:50 -0700388 *dex_method_index = invoke.GetMethodIndex(encoding.invoke_info.encoding, method_info);
Mathieu Chartierd776ff02017-01-17 09:32:18 -0800389 return true;
390 }
391 return false;
392 }
393
Ian Rogers936b37f2014-02-14 00:52:24 -0800394 // For the given quick ref and args quick frame, return the caller's PC.
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700395 static uintptr_t GetCallingPc(ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700396 DCHECK((*sp)->IsCalleeSaveMethod());
Ian Rogers13735952014-10-08 12:43:28 -0700397 uint8_t* lr = reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_LrOffset;
Ian Rogers848871b2013-08-05 10:56:33 -0700398 return *reinterpret_cast<uintptr_t*>(lr);
399 }
400
Mathieu Chartiere401d142015-04-22 13:56:20 -0700401 QuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700402 uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700403 is_static_(is_static), shorty_(shorty), shorty_len_(shorty_len),
Ian Rogers13735952014-10-08 12:43:28 -0700404 gpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Gpr1Offset),
405 fpr_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_Fpr1Offset),
406 stack_args_(reinterpret_cast<uint8_t*>(sp) + kQuickCalleeSaveFrame_RefAndArgs_FrameSize
Mathieu Chartiere401d142015-04-22 13:56:20 -0700407 + sizeof(ArtMethod*)), // Skip ArtMethod*.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800408 gpr_index_(0), fpr_index_(0), fpr_double_index_(0), stack_index_(0),
409 cur_type_(Primitive::kPrimVoid), is_split_long_or_double_(false) {
Andreas Gampe575e78c2014-11-03 23:41:03 -0800410 static_assert(kQuickSoftFloatAbi == (kNumQuickFprArgs == 0),
411 "Number of Quick FPR arguments unexpected");
412 static_assert(!(kQuickSoftFloatAbi && kQuickDoubleRegAlignedFloatBackFilled),
413 "Double alignment unexpected");
Zheng Xu5667fdb2014-10-23 18:29:55 +0800414 // For register alignment, we want to assume that counters(fpr_double_index_) are even if the
415 // next register is even.
Andreas Gampe575e78c2014-11-03 23:41:03 -0800416 static_assert(!kQuickDoubleRegAlignedFloatBackFilled || kNumQuickFprArgs % 2 == 0,
417 "Number of Quick FPR arguments not even");
Andreas Gampe542451c2016-07-26 09:02:02 -0700418 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Zheng Xu5667fdb2014-10-23 18:29:55 +0800419 }
Ian Rogers848871b2013-08-05 10:56:33 -0700420
421 virtual ~QuickArgumentVisitor() {}
422
423 virtual void Visit() = 0;
424
Ian Rogers936b37f2014-02-14 00:52:24 -0800425 Primitive::Type GetParamPrimitiveType() const {
426 return cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700427 }
428
Ian Rogers13735952014-10-08 12:43:28 -0700429 uint8_t* GetParamAddress() const {
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800430 if (!kQuickSoftFloatAbi) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800431 Primitive::Type type = GetParamPrimitiveType();
432 if (UNLIKELY((type == Primitive::kPrimDouble) || (type == Primitive::kPrimFloat))) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800433 if (type == Primitive::kPrimDouble && kQuickDoubleRegAlignedFloatBackFilled) {
434 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
435 return fpr_args_ + (fpr_double_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
436 }
437 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000438 return fpr_args_ + (fpr_index_ * GetBytesPerFprSpillLocation(kRuntimeISA));
Ian Rogers936b37f2014-02-14 00:52:24 -0800439 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700440 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers936b37f2014-02-14 00:52:24 -0800441 }
442 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800443 if (gpr_index_ < kNumQuickGprArgs) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800444 return gpr_args_ + GprIndexToGprOffset(gpr_index_);
445 }
446 return stack_args_ + (stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700447 }
448
449 bool IsSplitLongOrDouble() const {
Mathieu Chartiere401d142015-04-22 13:56:20 -0700450 if ((GetBytesPerGprSpillLocation(kRuntimeISA) == 4) ||
451 (GetBytesPerFprSpillLocation(kRuntimeISA) == 4)) {
Ian Rogers936b37f2014-02-14 00:52:24 -0800452 return is_split_long_or_double_;
453 } else {
454 return false; // An optimization for when GPR and FPRs are 64bit.
455 }
Ian Rogers848871b2013-08-05 10:56:33 -0700456 }
457
Ian Rogers936b37f2014-02-14 00:52:24 -0800458 bool IsParamAReference() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700459 return GetParamPrimitiveType() == Primitive::kPrimNot;
460 }
461
Ian Rogers936b37f2014-02-14 00:52:24 -0800462 bool IsParamALongOrDouble() const {
Ian Rogers848871b2013-08-05 10:56:33 -0700463 Primitive::Type type = GetParamPrimitiveType();
464 return type == Primitive::kPrimLong || type == Primitive::kPrimDouble;
465 }
466
467 uint64_t ReadSplitLongParam() const {
Nicolas Geoffray425f2392015-01-08 14:52:29 +0000468 // The splitted long is always available through the stack.
469 return *reinterpret_cast<uint64_t*>(stack_args_
470 + stack_index_ * kBytesStackArgLocation);
Ian Rogers848871b2013-08-05 10:56:33 -0700471 }
472
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800473 void IncGprIndex() {
474 gpr_index_++;
475 if (kGprFprLockstep) {
476 fpr_index_++;
477 }
478 }
479
480 void IncFprIndex() {
481 fpr_index_++;
482 if (kGprFprLockstep) {
483 gpr_index_++;
484 }
485 }
486
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700487 void VisitArguments() REQUIRES_SHARED(Locks::mutator_lock_) {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800488 // (a) 'stack_args_' should point to the first method's argument
489 // (b) whatever the argument type it is, the 'stack_index_' should
490 // be moved forward along with every visiting.
Ian Rogers936b37f2014-02-14 00:52:24 -0800491 gpr_index_ = 0;
492 fpr_index_ = 0;
Zheng Xu5667fdb2014-10-23 18:29:55 +0800493 if (kQuickDoubleRegAlignedFloatBackFilled) {
494 fpr_double_index_ = 0;
495 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800496 stack_index_ = 0;
497 if (!is_static_) { // Handle this.
498 cur_type_ = Primitive::kPrimNot;
499 is_split_long_or_double_ = false;
Ian Rogers848871b2013-08-05 10:56:33 -0700500 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800501 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800502 if (kNumQuickGprArgs > 0) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800503 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800504 }
Ian Rogers848871b2013-08-05 10:56:33 -0700505 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800506 for (uint32_t shorty_index = 1; shorty_index < shorty_len_; ++shorty_index) {
507 cur_type_ = Primitive::GetType(shorty_[shorty_index]);
508 switch (cur_type_) {
509 case Primitive::kPrimNot:
510 case Primitive::kPrimBoolean:
511 case Primitive::kPrimByte:
512 case Primitive::kPrimChar:
513 case Primitive::kPrimShort:
514 case Primitive::kPrimInt:
515 is_split_long_or_double_ = false;
516 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800517 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800518 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800519 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800520 }
521 break;
522 case Primitive::kPrimFloat:
523 is_split_long_or_double_ = false;
524 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800525 stack_index_++;
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800526 if (kQuickSoftFloatAbi) {
527 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800528 IncGprIndex();
Ian Rogers936b37f2014-02-14 00:52:24 -0800529 }
530 } else {
Zheng Xu5667fdb2014-10-23 18:29:55 +0800531 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800532 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800533 if (kQuickDoubleRegAlignedFloatBackFilled) {
534 // Double should not overlap with float.
535 // For example, if fpr_index_ = 3, fpr_double_index_ should be at least 4.
536 fpr_double_index_ = std::max(fpr_double_index_, RoundUp(fpr_index_, 2));
537 // Float should not overlap with double.
538 if (fpr_index_ % 2 == 0) {
539 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
540 }
Goran Jakovljevicff734982015-08-24 12:58:55 +0000541 } else if (kQuickSkipOddFpRegisters) {
542 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800543 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800544 }
545 }
546 break;
547 case Primitive::kPrimDouble:
548 case Primitive::kPrimLong:
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800549 if (kQuickSoftFloatAbi || (cur_type_ == Primitive::kPrimLong)) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800550 if (cur_type_ == Primitive::kPrimLong &&
551#if defined(__mips__) && !defined(__LP64__)
552 (gpr_index_ == 0 || gpr_index_ == 2) &&
553#else
554 gpr_index_ == 0 &&
555#endif
556 kAlignPairRegister) {
557 // Currently, this is only for ARM and MIPS, where we align long parameters with
558 // even-numbered registers by skipping R1 (on ARM) or A1(A3) (on MIPS) and using
559 // R2 (on ARM) or A2(T0) (on MIPS) instead.
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800560 IncGprIndex();
Nicolas Geoffray69c15d32015-01-13 11:42:13 +0000561 }
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000562 is_split_long_or_double_ = (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) &&
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800563 ((gpr_index_ + 1) == kNumQuickGprArgs);
Mark Mendell3e6a3bf2015-01-19 14:09:22 -0500564 if (!kSplitPairAcrossRegisterAndStack && is_split_long_or_double_) {
565 // We don't want to split this. Pass over this register.
566 gpr_index_++;
567 is_split_long_or_double_ = false;
568 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800569 Visit();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800570 if (kBytesStackArgLocation == 4) {
571 stack_index_+= 2;
572 } else {
573 CHECK_EQ(kBytesStackArgLocation, 8U);
574 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800575 }
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700576 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800577 IncGprIndex();
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000578 if (GetBytesPerGprSpillLocation(kRuntimeISA) == 4) {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700579 if (gpr_index_ < kNumQuickGprArgs) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800580 IncGprIndex();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700581 }
582 }
583 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800584 } else {
Nicolas Geoffray42fcd982014-04-22 11:03:52 +0000585 is_split_long_or_double_ = (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) &&
Zheng Xu5667fdb2014-10-23 18:29:55 +0800586 ((fpr_index_ + 1) == kNumQuickFprArgs) && !kQuickDoubleRegAlignedFloatBackFilled;
Ian Rogers936b37f2014-02-14 00:52:24 -0800587 Visit();
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700588 if (kBytesStackArgLocation == 4) {
589 stack_index_+= 2;
Ian Rogers936b37f2014-02-14 00:52:24 -0800590 } else {
Vladimir Kostyukov1dd61ba2014-04-02 18:42:20 +0700591 CHECK_EQ(kBytesStackArgLocation, 8U);
592 stack_index_++;
Ian Rogers936b37f2014-02-14 00:52:24 -0800593 }
Zheng Xu5667fdb2014-10-23 18:29:55 +0800594 if (kQuickDoubleRegAlignedFloatBackFilled) {
595 if (fpr_double_index_ + 2 < kNumQuickFprArgs + 1) {
596 fpr_double_index_ += 2;
597 // Float should not overlap with double.
598 if (fpr_index_ % 2 == 0) {
599 fpr_index_ = std::max(fpr_double_index_, fpr_index_);
600 }
601 }
602 } else if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800603 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800604 if (GetBytesPerFprSpillLocation(kRuntimeISA) == 4) {
605 if (fpr_index_ + 1 < kNumQuickFprArgs + 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -0800606 IncFprIndex();
Zheng Xu5667fdb2014-10-23 18:29:55 +0800607 }
608 }
609 }
Ian Rogers936b37f2014-02-14 00:52:24 -0800610 }
611 break;
612 default:
613 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty_;
614 }
Ian Rogers848871b2013-08-05 10:56:33 -0700615 }
616 }
617
Andreas Gampec200a4a2014-06-16 18:39:09 -0700618 protected:
Ian Rogers848871b2013-08-05 10:56:33 -0700619 const bool is_static_;
620 const char* const shorty_;
621 const uint32_t shorty_len_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700622
623 private:
Ian Rogers13735952014-10-08 12:43:28 -0700624 uint8_t* const gpr_args_; // Address of GPR arguments in callee save frame.
625 uint8_t* const fpr_args_; // Address of FPR arguments in callee save frame.
626 uint8_t* const stack_args_; // Address of stack arguments in caller's frame.
Ian Rogers936b37f2014-02-14 00:52:24 -0800627 uint32_t gpr_index_; // Index into spilled GPRs.
Zheng Xu5667fdb2014-10-23 18:29:55 +0800628 // Index into spilled FPRs.
629 // In case kQuickDoubleRegAlignedFloatBackFilled, it may index a hole while fpr_double_index_
630 // holds a higher register number.
631 uint32_t fpr_index_;
632 // Index into spilled FPRs for aligned double.
633 // Only used when kQuickDoubleRegAlignedFloatBackFilled. Next available double register indexed in
634 // terms of singles, may be behind fpr_index.
635 uint32_t fpr_double_index_;
Ian Rogers936b37f2014-02-14 00:52:24 -0800636 uint32_t stack_index_; // Index into arguments on the stack.
637 // The current type of argument during VisitArguments.
638 Primitive::Type cur_type_;
Ian Rogers848871b2013-08-05 10:56:33 -0700639 // Does a 64bit parameter straddle the register and stack arguments?
640 bool is_split_long_or_double_;
641};
642
Sebastien Hertza836bc92014-11-25 16:30:53 +0100643// Returns the 'this' object of a proxy method. This function is only used by StackVisitor. It
644// allows to use the QuickArgumentVisitor constants without moving all the code in its own module.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700645extern "C" mirror::Object* artQuickGetProxyThisObject(ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700646 REQUIRES_SHARED(Locks::mutator_lock_) {
Sebastien Hertza836bc92014-11-25 16:30:53 +0100647 return QuickArgumentVisitor::GetProxyThisObject(sp);
648}
649
Ian Rogers848871b2013-08-05 10:56:33 -0700650// Visits arguments on the stack placing them into the shadow frame.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800651class BuildQuickShadowFrameVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700652 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700653 BuildQuickShadowFrameVisitor(ArtMethod** sp, bool is_static, const char* shorty,
654 uint32_t shorty_len, ShadowFrame* sf, size_t first_arg_reg) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700655 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), sf_(sf), cur_reg_(first_arg_reg) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700656
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700657 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700658
659 private:
Ian Rogers936b37f2014-02-14 00:52:24 -0800660 ShadowFrame* const sf_;
661 uint32_t cur_reg_;
Ian Rogers848871b2013-08-05 10:56:33 -0700662
Dragos Sbirleabd136a22013-08-13 18:07:04 -0700663 DISALLOW_COPY_AND_ASSIGN(BuildQuickShadowFrameVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700664};
665
Andreas Gampec200a4a2014-06-16 18:39:09 -0700666void BuildQuickShadowFrameVisitor::Visit() {
Ian Rogers9758f792014-03-13 09:02:55 -0700667 Primitive::Type type = GetParamPrimitiveType();
668 switch (type) {
669 case Primitive::kPrimLong: // Fall-through.
670 case Primitive::kPrimDouble:
671 if (IsSplitLongOrDouble()) {
672 sf_->SetVRegLong(cur_reg_, ReadSplitLongParam());
673 } else {
674 sf_->SetVRegLong(cur_reg_, *reinterpret_cast<jlong*>(GetParamAddress()));
675 }
676 ++cur_reg_;
677 break;
678 case Primitive::kPrimNot: {
679 StackReference<mirror::Object>* stack_ref =
680 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
681 sf_->SetVRegReference(cur_reg_, stack_ref->AsMirrorPtr());
682 }
683 break;
684 case Primitive::kPrimBoolean: // Fall-through.
685 case Primitive::kPrimByte: // Fall-through.
686 case Primitive::kPrimChar: // Fall-through.
687 case Primitive::kPrimShort: // Fall-through.
688 case Primitive::kPrimInt: // Fall-through.
689 case Primitive::kPrimFloat:
690 sf_->SetVReg(cur_reg_, *reinterpret_cast<jint*>(GetParamAddress()));
691 break;
692 case Primitive::kPrimVoid:
693 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700694 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700695 }
696 ++cur_reg_;
697}
698
Mathieu Chartiere401d142015-04-22 13:56:20 -0700699extern "C" uint64_t artQuickToInterpreterBridge(ArtMethod* method, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700700 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers848871b2013-08-05 10:56:33 -0700701 // Ensure we don't get thread suspension until the object arguments are safely in the shadow
702 // frame.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -0700703 ScopedQuickEntrypointChecks sqec(self);
Ian Rogers848871b2013-08-05 10:56:33 -0700704
Alex Light9139e002015-10-09 15:59:48 -0700705 if (UNLIKELY(!method->IsInvokable())) {
706 method->ThrowInvocationTimeError();
Ian Rogers848871b2013-08-05 10:56:33 -0700707 return 0;
Andreas Gampe639bdd12015-06-03 11:22:45 -0700708 }
709
710 JValue tmp_value;
711 ShadowFrame* deopt_frame = self->PopStackedShadowFrame(
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700712 StackedShadowFrameType::kDeoptimizationShadowFrame, false);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700713 ManagedStack fragment;
714
David Sehr709b0702016-10-13 09:12:37 -0700715 DCHECK(!method->IsNative()) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700716 uint32_t shorty_len = 0;
Andreas Gampe542451c2016-07-26 09:02:02 -0700717 ArtMethod* non_proxy_method = method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
Alex Lighte9dd04f2016-03-16 16:09:45 -0700718 const DexFile::CodeItem* code_item = non_proxy_method->GetCodeItem();
David Sehr709b0702016-10-13 09:12:37 -0700719 DCHECK(code_item != nullptr) << method->PrettyMethod();
Andreas Gampe639bdd12015-06-03 11:22:45 -0700720 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
721
722 JValue result;
723
724 if (deopt_frame != nullptr) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700725 // Coming from partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700726
727 if (kIsDebugBuild) {
728 // Sanity-check: are the methods as expected? We check that the last shadow frame (the bottom
729 // of the call-stack) corresponds to the called method.
730 ShadowFrame* linked = deopt_frame;
731 while (linked->GetLink() != nullptr) {
732 linked = linked->GetLink();
733 }
David Sehr709b0702016-10-13 09:12:37 -0700734 CHECK_EQ(method, linked->GetMethod()) << method->PrettyMethod() << " "
735 << ArtMethod::PrettyMethod(linked->GetMethod());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700736 }
737
738 if (VLOG_IS_ON(deopt)) {
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700739 // Print out the stack to verify that it was a partial-fragment deopt.
Andreas Gampe639bdd12015-06-03 11:22:45 -0700740 LOG(INFO) << "Continue-ing from deopt. Stack is:";
741 QuickExceptionHandler::DumpFramesWithType(self, true);
742 }
743
Mathieu Chartierf5769e12017-01-10 15:54:41 -0800744 ObjPtr<mirror::Throwable> pending_exception;
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100745 bool from_code = false;
746 self->PopDeoptimizationContext(&result, &pending_exception, /* out */ &from_code);
Andreas Gampe639bdd12015-06-03 11:22:45 -0700747
748 // Push a transition back into managed code onto the linked list in thread.
749 self->PushManagedStackFragment(&fragment);
750
751 // Ensure that the stack is still in order.
752 if (kIsDebugBuild) {
753 class DummyStackVisitor : public StackVisitor {
754 public:
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700755 explicit DummyStackVisitor(Thread* self_in) REQUIRES_SHARED(Locks::mutator_lock_)
Andreas Gampe639bdd12015-06-03 11:22:45 -0700756 : StackVisitor(self_in, nullptr, StackVisitor::StackWalkKind::kIncludeInlinedFrames) {}
757
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700758 bool VisitFrame() OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe639bdd12015-06-03 11:22:45 -0700759 // Nothing to do here. In a debug build, SanityCheckFrame will do the work in the walking
760 // logic. Just always say we want to continue.
761 return true;
762 }
763 };
764 DummyStackVisitor dsv(self);
765 dsv.WalkStack();
766 }
767
768 // Restore the exception that was pending before deoptimization then interpret the
769 // deoptimized frames.
770 if (pending_exception != nullptr) {
771 self->SetException(pending_exception);
772 }
Nicolas Geoffray73be1e82015-09-17 15:22:56 +0100773 interpreter::EnterInterpreterFromDeoptimize(self, deopt_frame, from_code, &result);
Ian Rogers848871b2013-08-05 10:56:33 -0700774 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -0700775 const char* old_cause = self->StartAssertNoThreadSuspension(
776 "Building interpreter shadow frame");
Ian Rogers848871b2013-08-05 10:56:33 -0700777 uint16_t num_regs = code_item->registers_size_;
Andreas Gampec200a4a2014-06-16 18:39:09 -0700778 // No last shadow coming from quick.
Andreas Gampeb3025922015-09-01 14:45:00 -0700779 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
Andreas Gampe03ec9302015-08-27 17:41:47 -0700780 CREATE_SHADOW_FRAME(num_regs, /* link */ nullptr, method, /* dex pc */ 0);
Andreas Gampeb3025922015-09-01 14:45:00 -0700781 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
Ian Rogers848871b2013-08-05 10:56:33 -0700782 size_t first_arg_reg = code_item->registers_size_ - code_item->ins_size_;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700783 BuildQuickShadowFrameVisitor shadow_frame_builder(sp, method->IsStatic(), shorty, shorty_len,
Ian Rogers936b37f2014-02-14 00:52:24 -0800784 shadow_frame, first_arg_reg);
Ian Rogers848871b2013-08-05 10:56:33 -0700785 shadow_frame_builder.VisitArguments();
Ian Rogerse94652f2014-12-02 11:13:19 -0800786 const bool needs_initialization =
787 method->IsStatic() && !method->GetDeclaringClass()->IsInitialized();
Ian Rogers848871b2013-08-05 10:56:33 -0700788 // Push a transition back into managed code onto the linked list in thread.
Ian Rogers848871b2013-08-05 10:56:33 -0700789 self->PushManagedStackFragment(&fragment);
790 self->PushShadowFrame(shadow_frame);
791 self->EndAssertNoThreadSuspension(old_cause);
792
Ian Rogerse94652f2014-12-02 11:13:19 -0800793 if (needs_initialization) {
Ian Rogers848871b2013-08-05 10:56:33 -0700794 // Ensure static method's class is initialized.
Ian Rogerse94652f2014-12-02 11:13:19 -0800795 StackHandleScope<1> hs(self);
796 Handle<mirror::Class> h_class(hs.NewHandle(shadow_frame->GetMethod()->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -0700797 if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
David Sehr709b0702016-10-13 09:12:37 -0700798 DCHECK(Thread::Current()->IsExceptionPending())
799 << shadow_frame->GetMethod()->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700800 self->PopManagedStackFragment(fragment);
801 return 0;
802 }
803 }
Daniel Mihalyieb076692014-08-22 17:33:31 +0200804
Andreas Gampe639bdd12015-06-03 11:22:45 -0700805 result = interpreter::EnterInterpreterFromEntryPoint(self, code_item, shadow_frame);
Ian Rogers848871b2013-08-05 10:56:33 -0700806 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700807
808 // Pop transition.
809 self->PopManagedStackFragment(fragment);
810
811 // Request a stack deoptimization if needed
812 ArtMethod* caller = QuickArgumentVisitor::GetCallingMethod(sp);
Mingyao Yangf711f2c2016-05-23 12:29:39 -0700813 uintptr_t caller_pc = QuickArgumentVisitor::GetCallingPc(sp);
Mingyao Yanga3549d22016-06-02 17:01:02 -0700814 // If caller_pc is the instrumentation exit stub, the stub will check to see if deoptimization
815 // should be done and it knows the real return pc.
816 if (UNLIKELY(caller_pc != reinterpret_cast<uintptr_t>(GetQuickInstrumentationExitPc()) &&
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000817 Dbg::IsForcedInterpreterNeededForUpcall(self, caller))) {
818 if (!Runtime::Current()->IsAsyncDeoptimizeable(caller_pc)) {
819 LOG(WARNING) << "Got a deoptimization request on un-deoptimizable method "
820 << caller->PrettyMethod();
821 } else {
822 // Push the context of the deoptimization stack so we can restore the return value and the
823 // exception before executing the deoptimized frames.
824 self->PushDeoptimizationContext(
825 result, shorty[0] == 'L', /* from_code */ false, self->GetException());
Andreas Gampe639bdd12015-06-03 11:22:45 -0700826
Nicolas Geoffray433b79a2017-01-30 20:54:45 +0000827 // Set special exception to cause deoptimization.
828 self->SetException(Thread::GetDeoptimizationException());
829 }
Andreas Gampe639bdd12015-06-03 11:22:45 -0700830 }
831
832 // No need to restore the args since the method has already been run by the interpreter.
833 return result.GetJ();
Ian Rogers848871b2013-08-05 10:56:33 -0700834}
835
836// Visits arguments on the stack placing them into the args vector, Object* arguments are converted
837// to jobjects.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800838class BuildQuickArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700839 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700840 BuildQuickArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty, uint32_t shorty_len,
Andreas Gampecf4035a2014-05-28 22:43:01 -0700841 ScopedObjectAccessUnchecked* soa, std::vector<jvalue>* args) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700842 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa), args_(args) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700843
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700844 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Ian Rogers848871b2013-08-05 10:56:33 -0700845
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700846 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800847
Ian Rogers848871b2013-08-05 10:56:33 -0700848 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700849 ScopedObjectAccessUnchecked* const soa_;
850 std::vector<jvalue>* const args_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800851 // References which we must update when exiting in case the GC moved the objects.
Ian Rogers700a4022014-05-19 16:49:03 -0700852 std::vector<std::pair<jobject, StackReference<mirror::Object>*>> references_;
Ian Rogers9758f792014-03-13 09:02:55 -0700853
Ian Rogers848871b2013-08-05 10:56:33 -0700854 DISALLOW_COPY_AND_ASSIGN(BuildQuickArgumentVisitor);
855};
856
Ian Rogers9758f792014-03-13 09:02:55 -0700857void BuildQuickArgumentVisitor::Visit() {
858 jvalue val;
859 Primitive::Type type = GetParamPrimitiveType();
860 switch (type) {
861 case Primitive::kPrimNot: {
862 StackReference<mirror::Object>* stack_ref =
863 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
864 val.l = soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
865 references_.push_back(std::make_pair(val.l, stack_ref));
866 break;
867 }
868 case Primitive::kPrimLong: // Fall-through.
869 case Primitive::kPrimDouble:
870 if (IsSplitLongOrDouble()) {
871 val.j = ReadSplitLongParam();
872 } else {
873 val.j = *reinterpret_cast<jlong*>(GetParamAddress());
874 }
875 break;
876 case Primitive::kPrimBoolean: // Fall-through.
877 case Primitive::kPrimByte: // Fall-through.
878 case Primitive::kPrimChar: // Fall-through.
879 case Primitive::kPrimShort: // Fall-through.
880 case Primitive::kPrimInt: // Fall-through.
881 case Primitive::kPrimFloat:
882 val.i = *reinterpret_cast<jint*>(GetParamAddress());
883 break;
884 case Primitive::kPrimVoid:
885 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -0700886 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -0700887 }
888 args_->push_back(val);
889}
890
891void BuildQuickArgumentVisitor::FixupReferences() {
892 // Fixup any references which may have changed.
893 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700894 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700895 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700896 }
897}
898
Ian Rogers848871b2013-08-05 10:56:33 -0700899// Handler for invocation on proxy methods. On entry a frame will exist for the proxy object method
900// which is responsible for recording callee save registers. We explicitly place into jobjects the
901// incoming reference arguments (so they survive GC). We invoke the invocation handler, which is a
902// field within the proxy object, which will box the primitive arguments and deal with error cases.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700903extern "C" uint64_t artQuickProxyInvokeHandler(
904 ArtMethod* proxy_method, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700905 REQUIRES_SHARED(Locks::mutator_lock_) {
David Sehr709b0702016-10-13 09:12:37 -0700906 DCHECK(proxy_method->IsProxyMethod()) << proxy_method->PrettyMethod();
907 DCHECK(receiver->GetClass()->IsProxyClass()) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700908 // Ensure we don't get thread suspension until the object arguments are safely in jobjects.
909 const char* old_cause =
910 self->StartAssertNoThreadSuspension("Adding to IRT proxy object arguments");
911 // Register the top of the managed stack, making stack crawlable.
David Sehr709b0702016-10-13 09:12:37 -0700912 DCHECK_EQ((*sp), proxy_method) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700913 self->VerifyStack();
914 // Start new JNI local reference state.
915 JNIEnvExt* env = self->GetJniEnv();
916 ScopedObjectAccessUnchecked soa(env);
917 ScopedJniEnvLocalRefState env_state(env);
918 // Create local ref. copies of proxy method and the receiver.
919 jobject rcvr_jobj = soa.AddLocalReference<jobject>(receiver);
920
921 // Placing arguments into args vector and remove the receiver.
Andreas Gampe542451c2016-07-26 09:02:02 -0700922 ArtMethod* non_proxy_method = proxy_method->GetInterfaceMethodIfProxy(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700923 CHECK(!non_proxy_method->IsStatic()) << proxy_method->PrettyMethod() << " "
924 << non_proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700925 std::vector<jvalue> args;
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700926 uint32_t shorty_len = 0;
Mathieu Chartiere401d142015-04-22 13:56:20 -0700927 const char* shorty = non_proxy_method->GetShorty(&shorty_len);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700928 BuildQuickArgumentVisitor local_ref_visitor(sp, false, shorty, shorty_len, &soa, &args);
Brian Carlstromd3633d52013-08-20 21:06:26 -0700929
Ian Rogers848871b2013-08-05 10:56:33 -0700930 local_ref_visitor.VisitArguments();
David Sehr709b0702016-10-13 09:12:37 -0700931 DCHECK_GT(args.size(), 0U) << proxy_method->PrettyMethod();
Ian Rogers848871b2013-08-05 10:56:33 -0700932 args.erase(args.begin());
933
934 // Convert proxy method into expected interface method.
Andreas Gampe542451c2016-07-26 09:02:02 -0700935 ArtMethod* interface_method = proxy_method->FindOverriddenMethod(kRuntimePointerSize);
David Sehr709b0702016-10-13 09:12:37 -0700936 DCHECK(interface_method != nullptr) << proxy_method->PrettyMethod();
937 DCHECK(!interface_method->IsProxyMethod()) << interface_method->PrettyMethod();
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700938 self->EndAssertNoThreadSuspension(old_cause);
Andreas Gampe542451c2016-07-26 09:02:02 -0700939 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampee01e3642016-07-25 13:06:04 -0700940 DCHECK(!Runtime::Current()->IsActiveTransaction());
Mathieu Chartierfc58af42015-04-16 18:00:39 -0700941 jobject interface_method_jobj = soa.AddLocalReference<jobject>(
Andreas Gampe542451c2016-07-26 09:02:02 -0700942 mirror::Method::CreateFromArtMethod<kRuntimePointerSize, false>(soa.Self(),
943 interface_method));
Ian Rogers848871b2013-08-05 10:56:33 -0700944
945 // All naked Object*s should now be in jobjects, so its safe to go into the main invoke code
946 // that performs allocations.
Mathieu Chartierbfd9a432014-05-21 17:43:44 -0700947 JValue result = InvokeProxyInvocationHandler(soa, shorty, rcvr_jobj, interface_method_jobj, args);
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800948 // Restore references which might have moved.
949 local_ref_visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -0700950 return result.GetJ();
951}
952
953// Read object references held in arguments from quick frames and place in a JNI local references,
954// so they don't get garbage collected.
Andreas Gampebf6b92a2014-03-05 16:11:04 -0800955class RememberForGcArgumentVisitor FINAL : public QuickArgumentVisitor {
Ian Rogers848871b2013-08-05 10:56:33 -0700956 public:
Mathieu Chartiere401d142015-04-22 13:56:20 -0700957 RememberForGcArgumentVisitor(ArtMethod** sp, bool is_static, const char* shorty,
958 uint32_t shorty_len, ScopedObjectAccessUnchecked* soa) :
Andreas Gampec200a4a2014-06-16 18:39:09 -0700959 QuickArgumentVisitor(sp, is_static, shorty, shorty_len), soa_(soa) {}
Ian Rogers848871b2013-08-05 10:56:33 -0700960
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700961 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Mathieu Chartier07d447b2013-09-26 11:57:43 -0700962
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700963 void FixupReferences() REQUIRES_SHARED(Locks::mutator_lock_);
Ian Rogers848871b2013-08-05 10:56:33 -0700964
965 private:
Ian Rogers9758f792014-03-13 09:02:55 -0700966 ScopedObjectAccessUnchecked* const soa_;
Mathieu Chartier5275bcb2014-02-20 17:16:42 -0800967 // References which we must update when exiting in case the GC moved the objects.
Andreas Gampec200a4a2014-06-16 18:39:09 -0700968 std::vector<std::pair<jobject, StackReference<mirror::Object>*> > references_;
969
Mathieu Chartier590fee92013-09-13 13:46:47 -0700970 DISALLOW_COPY_AND_ASSIGN(RememberForGcArgumentVisitor);
Ian Rogers848871b2013-08-05 10:56:33 -0700971};
972
Ian Rogers9758f792014-03-13 09:02:55 -0700973void RememberForGcArgumentVisitor::Visit() {
974 if (IsParamAReference()) {
975 StackReference<mirror::Object>* stack_ref =
976 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
977 jobject reference =
978 soa_->AddLocalReference<jobject>(stack_ref->AsMirrorPtr());
979 references_.push_back(std::make_pair(reference, stack_ref));
980 }
981}
982
983void RememberForGcArgumentVisitor::FixupReferences() {
984 // Fixup any references which may have changed.
985 for (const auto& pair : references_) {
Mathieu Chartier1a5337f2016-10-13 13:48:23 -0700986 pair.second->Assign(soa_->Decode<mirror::Object>(pair.first));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -0700987 soa_->Env()->DeleteLocalRef(pair.first);
Ian Rogers9758f792014-03-13 09:02:55 -0700988 }
989}
990
Ian Rogers848871b2013-08-05 10:56:33 -0700991// Lazily resolve a method for quick. Called by stub code.
Mathieu Chartiere401d142015-04-22 13:56:20 -0700992extern "C" const void* artQuickResolutionTrampoline(
993 ArtMethod* called, mirror::Object* receiver, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -0700994 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampe3b45ef22015-05-26 21:34:09 -0700995 // The resolution trampoline stashes the resolved method into the callee-save frame to transport
996 // it. Thus, when exiting, the stack cannot be verified (as the resolved method most likely
997 // does not have the same stack layout as the callee-save method).
998 ScopedQuickEntrypointChecks sqec(self, kIsDebugBuild, false);
Ian Rogers848871b2013-08-05 10:56:33 -0700999 // Start new JNI local reference state
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001000 JNIEnvExt* env = self->GetJniEnv();
Ian Rogers848871b2013-08-05 10:56:33 -07001001 ScopedObjectAccessUnchecked soa(env);
1002 ScopedJniEnvLocalRefState env_state(env);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001003 const char* old_cause = self->StartAssertNoThreadSuspension("Quick method resolution set up");
Ian Rogers848871b2013-08-05 10:56:33 -07001004
1005 // Compute details about the called method (avoid GCs)
1006 ClassLinker* linker = Runtime::Current()->GetClassLinker();
Ian Rogers848871b2013-08-05 10:56:33 -07001007 InvokeType invoke_type;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001008 MethodReference called_method(nullptr, 0);
1009 const bool called_method_known_on_entry = !called->IsRuntimeMethod();
Mathieu Chartiere401d142015-04-22 13:56:20 -07001010 ArtMethod* caller = nullptr;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001011 if (!called_method_known_on_entry) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01001012 caller = QuickArgumentVisitor::GetCallingMethod(sp);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001013 called_method.dex_file = caller->GetDexFile();
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001014
1015 InvokeType stack_map_invoke_type;
1016 uint32_t stack_map_dex_method_idx;
1017 const bool found_stack_map = QuickArgumentVisitor::GetInvokeType(sp,
1018 &stack_map_invoke_type,
1019 &stack_map_dex_method_idx);
1020 // For debug builds, we make sure both of the paths are consistent by also looking at the dex
1021 // code.
1022 if (!found_stack_map || kIsDebugBuild) {
1023 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
1024 const DexFile::CodeItem* code;
1025 code = caller->GetCodeItem();
1026 CHECK_LT(dex_pc, code->insns_size_in_code_units_);
1027 const Instruction* instr = Instruction::At(&code->insns_[dex_pc]);
1028 Instruction::Code instr_code = instr->Opcode();
1029 bool is_range;
1030 switch (instr_code) {
1031 case Instruction::INVOKE_DIRECT:
1032 invoke_type = kDirect;
1033 is_range = false;
1034 break;
1035 case Instruction::INVOKE_DIRECT_RANGE:
1036 invoke_type = kDirect;
1037 is_range = true;
1038 break;
1039 case Instruction::INVOKE_STATIC:
1040 invoke_type = kStatic;
1041 is_range = false;
1042 break;
1043 case Instruction::INVOKE_STATIC_RANGE:
1044 invoke_type = kStatic;
1045 is_range = true;
1046 break;
1047 case Instruction::INVOKE_SUPER:
1048 invoke_type = kSuper;
1049 is_range = false;
1050 break;
1051 case Instruction::INVOKE_SUPER_RANGE:
1052 invoke_type = kSuper;
1053 is_range = true;
1054 break;
1055 case Instruction::INVOKE_VIRTUAL:
1056 invoke_type = kVirtual;
1057 is_range = false;
1058 break;
1059 case Instruction::INVOKE_VIRTUAL_RANGE:
1060 invoke_type = kVirtual;
1061 is_range = true;
1062 break;
1063 case Instruction::INVOKE_INTERFACE:
1064 invoke_type = kInterface;
1065 is_range = false;
1066 break;
1067 case Instruction::INVOKE_INTERFACE_RANGE:
1068 invoke_type = kInterface;
1069 is_range = true;
1070 break;
1071 default:
1072 LOG(FATAL) << "Unexpected call into trampoline: " << instr->DumpString(nullptr);
1073 UNREACHABLE();
1074 }
1075 called_method.dex_method_index = (is_range) ? instr->VRegB_3rc() : instr->VRegB_35c();
1076 // Check that the invoke matches what we expected, note that this path only happens for debug
1077 // builds.
1078 if (found_stack_map) {
1079 DCHECK_EQ(stack_map_invoke_type, invoke_type);
1080 if (invoke_type != kSuper) {
1081 // Super may be sharpened.
1082 DCHECK_EQ(stack_map_dex_method_idx, called_method.dex_method_index)
1083 << called_method.dex_file->PrettyMethod(stack_map_dex_method_idx) << " "
1084 << called_method.dex_file->PrettyMethod(called_method.dex_method_index);
1085 }
1086 } else {
Andreas Gampe9e6dee22017-04-11 13:50:23 -07001087 VLOG(dex) << "Accessed dex file for invoke " << invoke_type << " "
Mathieu Chartierd776ff02017-01-17 09:32:18 -08001088 << called_method.dex_method_index;
1089 }
1090 } else {
1091 invoke_type = stack_map_invoke_type;
1092 called_method.dex_method_index = stack_map_dex_method_idx;
Ian Rogers848871b2013-08-05 10:56:33 -07001093 }
Ian Rogers848871b2013-08-05 10:56:33 -07001094 } else {
1095 invoke_type = kStatic;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001096 called_method.dex_file = called->GetDexFile();
1097 called_method.dex_method_index = called->GetDexMethodIndex();
Ian Rogers848871b2013-08-05 10:56:33 -07001098 }
1099 uint32_t shorty_len;
1100 const char* shorty =
Ian Rogerse0a02da2014-12-02 14:10:53 -08001101 called_method.dex_file->GetMethodShorty(
1102 called_method.dex_file->GetMethodId(called_method.dex_method_index), &shorty_len);
Mathieu Chartier590fee92013-09-13 13:46:47 -07001103 RememberForGcArgumentVisitor visitor(sp, invoke_type == kStatic, shorty, shorty_len, &soa);
Ian Rogers848871b2013-08-05 10:56:33 -07001104 visitor.VisitArguments();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001105 self->EndAssertNoThreadSuspension(old_cause);
Ian Rogerse0a02da2014-12-02 14:10:53 -08001106 const bool virtual_or_interface = invoke_type == kVirtual || invoke_type == kInterface;
Ian Rogers848871b2013-08-05 10:56:33 -07001107 // Resolve method filling in dex cache.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001108 if (!called_method_known_on_entry) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001109 StackHandleScope<1> hs(self);
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001110 mirror::Object* dummy = nullptr;
1111 HandleWrapper<mirror::Object> h_receiver(
1112 hs.NewHandleWrapper(virtual_or_interface ? &receiver : &dummy));
Ian Rogerse0a02da2014-12-02 14:10:53 -08001113 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
Andreas Gampe42ef8ab2015-12-03 17:27:32 -08001114 called = linker->ResolveMethod<ClassLinker::kForceICCECheck>(
1115 self, called_method.dex_method_index, caller, invoke_type);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001116
1117 // Update .bss entry in oat file if any.
1118 if (called != nullptr && called_method.dex_file->GetOatDexFile() != nullptr) {
1119 const MethodBssMapping* mapping =
1120 called_method.dex_file->GetOatDexFile()->GetMethodBssMapping();
1121 if (mapping != nullptr) {
1122 auto pp = std::partition_point(
1123 mapping->begin(),
1124 mapping->end(),
1125 [called_method](const MethodBssMappingEntry& entry) {
1126 return entry.method_index < called_method.dex_method_index;
1127 });
1128 if (pp != mapping->end() && pp->CoversIndex(called_method.dex_method_index)) {
1129 size_t bss_offset = pp->GetBssOffset(called_method.dex_method_index,
1130 static_cast<size_t>(kRuntimePointerSize));
1131 DCHECK_ALIGNED(bss_offset, static_cast<size_t>(kRuntimePointerSize));
1132 const OatFile* oat_file = called_method.dex_file->GetOatDexFile()->GetOatFile();
1133 ArtMethod** method_entry = reinterpret_cast<ArtMethod**>(const_cast<uint8_t*>(
1134 oat_file->BssBegin() + bss_offset));
1135 DCHECK_GE(method_entry, oat_file->GetBssMethods().data());
1136 DCHECK_LT(method_entry,
1137 oat_file->GetBssMethods().data() + oat_file->GetBssMethods().size());
1138 *method_entry = called;
1139 }
1140 }
1141 }
Ian Rogers848871b2013-08-05 10:56:33 -07001142 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001143 const void* code = nullptr;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001144 if (LIKELY(!self->IsExceptionPending())) {
Ian Rogers848871b2013-08-05 10:56:33 -07001145 // Incompatible class change should have been handled in resolve method.
Brian Carlstrom2ec65202014-03-03 15:16:37 -08001146 CHECK(!called->CheckIncompatibleClassChange(invoke_type))
David Sehr709b0702016-10-13 09:12:37 -07001147 << called->PrettyMethod() << " " << invoke_type;
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001148 if (virtual_or_interface || invoke_type == kSuper) {
1149 // Refine called method based on receiver for kVirtual/kInterface, and
1150 // caller for kSuper.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001151 ArtMethod* orig_called = called;
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001152 if (invoke_type == kVirtual) {
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001153 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001154 called = receiver->GetClass()->FindVirtualMethodForVirtual(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001155 } else if (invoke_type == kInterface) {
1156 CHECK(receiver != nullptr) << invoke_type;
Andreas Gampe542451c2016-07-26 09:02:02 -07001157 called = receiver->GetClass()->FindVirtualMethodForInterface(called, kRuntimePointerSize);
Nicolas Geoffraye5234232015-12-02 09:06:11 +00001158 } else {
1159 DCHECK_EQ(invoke_type, kSuper);
1160 CHECK(caller != nullptr) << invoke_type;
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001161 StackHandleScope<2> hs(self);
1162 Handle<mirror::DexCache> dex_cache(
1163 hs.NewHandle(caller->GetDeclaringClass()->GetDexCache()));
1164 Handle<mirror::ClassLoader> class_loader(
1165 hs.NewHandle(caller->GetDeclaringClass()->GetClassLoader()));
Alex Lightfedd91d2016-01-07 14:49:16 -08001166 // TODO Maybe put this into a mirror::Class function.
1167 mirror::Class* ref_class = linker->ResolveReferencedClassOfMethod(
Nicolas Geoffray393fdb82016-04-25 14:58:06 +01001168 called_method.dex_method_index, dex_cache, class_loader);
Alex Lightfedd91d2016-01-07 14:49:16 -08001169 if (ref_class->IsInterface()) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001170 called = ref_class->FindVirtualMethodForInterfaceSuper(called, kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001171 } else {
1172 called = caller->GetDeclaringClass()->GetSuperClass()->GetVTableEntry(
Andreas Gampe542451c2016-07-26 09:02:02 -07001173 called->GetMethodIndex(), kRuntimePointerSize);
Alex Lightfedd91d2016-01-07 14:49:16 -08001174 }
Mathieu Chartier55871bf2014-02-27 10:24:50 -08001175 }
Mingyao Yangf4867782014-05-05 11:55:02 -07001176
David Sehr709b0702016-10-13 09:12:37 -07001177 CHECK(called != nullptr) << orig_called->PrettyMethod() << " "
1178 << mirror::Object::PrettyTypeOf(receiver) << " "
Mingyao Yangf4867782014-05-05 11:55:02 -07001179 << invoke_type << " " << orig_called->GetVtableIndex();
1180
Ian Rogers83883d72013-10-21 21:07:24 -07001181 // 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 -08001182 // of the sharpened method avoiding dirtying the dex cache if possible.
Ian Rogers00f15272014-12-02 16:55:46 -08001183 // Note, called_method.dex_method_index references the dex method before the
1184 // FindVirtualMethodFor... This is ok for FindDexMethodIndexInOtherDexFile that only cares
1185 // about the name and signature.
1186 uint32_t update_dex_cache_method_index = called->GetDexMethodIndex();
Andreas Gampe542451c2016-07-26 09:02:02 -07001187 if (!called->HasSameDexCacheResolvedMethods(caller, kRuntimePointerSize)) {
Ian Rogers83883d72013-10-21 21:07:24 -07001188 // Calling from one dex file to another, need to compute the method index appropriate to
Vladimir Markobbcc0c02014-02-03 14:08:42 +00001189 // the caller's dex file. Since we get here only if the original called was a runtime
1190 // method, we've got the correct dex_file and a dex_method_idx from above.
Ian Rogerse0a02da2014-12-02 14:10:53 -08001191 DCHECK(!called_method_known_on_entry);
1192 DCHECK_EQ(caller->GetDexFile(), called_method.dex_file);
1193 const DexFile* caller_dex_file = called_method.dex_file;
1194 uint32_t caller_method_name_and_sig_index = called_method.dex_method_index;
1195 update_dex_cache_method_index =
1196 called->FindDexMethodIndexInOtherDexFile(*caller_dex_file,
1197 caller_method_name_and_sig_index);
1198 }
1199 if ((update_dex_cache_method_index != DexFile::kDexNoIndex) &&
Mathieu Chartiere401d142015-04-22 13:56:20 -07001200 (caller->GetDexCacheResolvedMethod(
Andreas Gampe542451c2016-07-26 09:02:02 -07001201 update_dex_cache_method_index, kRuntimePointerSize) != called)) {
1202 caller->SetDexCacheResolvedMethod(update_dex_cache_method_index,
1203 called,
1204 kRuntimePointerSize);
Ian Rogers83883d72013-10-21 21:07:24 -07001205 }
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001206 } else if (invoke_type == kStatic) {
1207 const auto called_dex_method_idx = called->GetDexMethodIndex();
1208 // For static invokes, we may dispatch to the static method in the superclass but resolve
1209 // using the subclass. To prevent getting slow paths on each invoke, we force set the
1210 // resolved method for the super class dex method index if we are in the same dex file.
1211 // b/19175856
1212 if (called->GetDexFile() == called_method.dex_file &&
1213 called_method.dex_method_index != called_dex_method_idx) {
Andreas Gampe542451c2016-07-26 09:02:02 -07001214 called->GetDexCache()->SetResolvedMethod(called_dex_method_idx,
1215 called,
1216 kRuntimePointerSize);
Mathieu Chartiere4a91bb2015-01-28 13:11:44 -08001217 }
Ian Rogers83883d72013-10-21 21:07:24 -07001218 }
Daniel Mihalyieb076692014-08-22 17:33:31 +02001219
Ian Rogers848871b2013-08-05 10:56:33 -07001220 // Ensure that the called method's class is initialized.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001221 StackHandleScope<1> hs(soa.Self());
1222 Handle<mirror::Class> called_class(hs.NewHandle(called->GetDeclaringClass()));
Ian Rogers7b078e82014-09-10 14:44:24 -07001223 linker->EnsureInitialized(soa.Self(), called_class, true, true);
Ian Rogers848871b2013-08-05 10:56:33 -07001224 if (LIKELY(called_class->IsInitialized())) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001225 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1226 // If we are single-stepping or the called method is deoptimized (by a
1227 // breakpoint, for example), then we have to execute the called method
1228 // with the interpreter.
1229 code = GetQuickToInterpreterBridge();
1230 } else if (UNLIKELY(Dbg::IsForcedInstrumentationNeededForResolution(self, caller))) {
1231 // If the caller is deoptimized (by a breakpoint, for example), we have to
1232 // continue its execution with interpreter when returning from the called
1233 // method. Because we do not want to execute the called method with the
1234 // interpreter, we wrap its execution into the instrumentation stubs.
1235 // When the called method returns, it will execute the instrumentation
1236 // exit hook that will determine the need of the interpreter with a call
1237 // to Dbg::IsForcedInterpreterNeededForUpcall and deoptimize the stack if
1238 // it is needed.
1239 code = GetQuickInstrumentationEntryPoint();
1240 } else {
1241 code = called->GetEntryPointFromQuickCompiledCode();
1242 }
Ian Rogers848871b2013-08-05 10:56:33 -07001243 } else if (called_class->IsInitializing()) {
Daniel Mihalyieb076692014-08-22 17:33:31 +02001244 if (UNLIKELY(Dbg::IsForcedInterpreterNeededForResolution(self, called))) {
1245 // If we are single-stepping or the called method is deoptimized (by a
1246 // breakpoint, for example), then we have to execute the called method
1247 // with the interpreter.
1248 code = GetQuickToInterpreterBridge();
1249 } else if (invoke_type == kStatic) {
Ian Rogers848871b2013-08-05 10:56:33 -07001250 // Class is still initializing, go to oat and grab code (trampoline must be left in place
1251 // until class is initialized to stop races between threads).
Ian Rogersef7d42f2014-01-06 12:55:46 -08001252 code = linker->GetQuickOatCodeFor(called);
Ian Rogers848871b2013-08-05 10:56:33 -07001253 } else {
1254 // No trampoline for non-static methods.
Ian Rogersef7d42f2014-01-06 12:55:46 -08001255 code = called->GetEntryPointFromQuickCompiledCode();
Ian Rogers848871b2013-08-05 10:56:33 -07001256 }
1257 } else {
1258 DCHECK(called_class->IsErroneous());
1259 }
1260 }
Ian Rogerse0a02da2014-12-02 14:10:53 -08001261 CHECK_EQ(code == nullptr, self->IsExceptionPending());
Mathieu Chartier07d447b2013-09-26 11:57:43 -07001262 // Fixup any locally saved objects may have moved during a GC.
1263 visitor.FixupReferences();
Ian Rogers848871b2013-08-05 10:56:33 -07001264 // Place called method in callee-save frame to be placed as first argument to quick method.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001265 *sp = called;
1266
Ian Rogers848871b2013-08-05 10:56:33 -07001267 return code;
1268}
1269
Andreas Gampec147b002014-03-06 18:11:06 -08001270/*
1271 * This class uses a couple of observations to unite the different calling conventions through
1272 * a few constants.
1273 *
1274 * 1) Number of registers used for passing is normally even, so counting down has no penalty for
1275 * possible alignment.
1276 * 2) Known 64b architectures store 8B units on the stack, both for integral and floating point
1277 * types, so using uintptr_t is OK. Also means that we can use kRegistersNeededX to denote
1278 * when we have to split things
1279 * 3) The only soft-float, Arm, is 32b, so no widening needs to be taken into account for floats
1280 * and we can use Int handling directly.
1281 * 4) Only 64b architectures widen, and their stack is aligned 8B anyways, so no padding code
1282 * necessary when widening. Also, widening of Ints will take place implicitly, and the
1283 * extension should be compatible with Aarch64, which mandates copying the available bits
1284 * into LSB and leaving the rest unspecified.
1285 * 5) Aligning longs and doubles is necessary on arm only, and it's the same in registers and on
1286 * the stack.
1287 * 6) There is only little endian.
1288 *
1289 *
1290 * Actual work is supposed to be done in a delegate of the template type. The interface is as
1291 * follows:
1292 *
1293 * void PushGpr(uintptr_t): Add a value for the next GPR
1294 *
1295 * void PushFpr4(float): Add a value for the next FPR of size 32b. Is only called if we need
1296 * padding, that is, think the architecture is 32b and aligns 64b.
1297 *
1298 * void PushFpr8(uint64_t): Push a double. We _will_ call this on 32b, it's the callee's job to
1299 * split this if necessary. The current state will have aligned, if
1300 * necessary.
1301 *
1302 * void PushStack(uintptr_t): Push a value to the stack.
1303 *
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001304 * uintptr_t PushHandleScope(mirror::Object* ref): Add a reference to the HandleScope. This _will_ have nullptr,
Andreas Gampe36fea8d2014-03-10 13:37:40 -07001305 * as this might be important for null initialization.
Andreas Gampec147b002014-03-06 18:11:06 -08001306 * Must return the jobject, that is, the reference to the
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001307 * entry in the HandleScope (nullptr if necessary).
Andreas Gampec147b002014-03-06 18:11:06 -08001308 *
1309 */
Andreas Gampec200a4a2014-06-16 18:39:09 -07001310template<class T> class BuildNativeCallFrameStateMachine {
Andreas Gampec147b002014-03-06 18:11:06 -08001311 public:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001312#if defined(__arm__)
1313 // TODO: These are all dummy values!
Andreas Gampec147b002014-03-06 18:11:06 -08001314 static constexpr bool kNativeSoftFloatAbi = true;
1315 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs, r0-r3
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001316 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
1317
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001318 static constexpr size_t kRegistersNeededForLong = 2;
1319 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001320 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001321 static constexpr bool kMultiFPRegistersWidened = false;
1322 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001323 static constexpr bool kAlignLongOnStack = true;
1324 static constexpr bool kAlignDoubleOnStack = true;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001325#elif defined(__aarch64__)
1326 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1327 static constexpr size_t kNumNativeGprArgs = 8; // 6 arguments passed in GPRs.
1328 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1329
1330 static constexpr size_t kRegistersNeededForLong = 1;
1331 static constexpr size_t kRegistersNeededForDouble = 1;
1332 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001333 static constexpr bool kMultiFPRegistersWidened = false;
1334 static constexpr bool kMultiGPRegistersWidened = false;
Stuart Monteithb95a5342014-03-12 13:32:32 +00001335 static constexpr bool kAlignLongOnStack = false;
1336 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001337#elif defined(__mips__) && !defined(__LP64__)
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001338 static constexpr bool kNativeSoftFloatAbi = true; // This is a hard float ABI.
Douglas Leung735b8552014-10-31 12:21:40 -07001339 static constexpr size_t kNumNativeGprArgs = 4; // 4 arguments passed in GPRs.
1340 static constexpr size_t kNumNativeFprArgs = 0; // 0 arguments passed in FPRs.
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001341
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001342 static constexpr size_t kRegistersNeededForLong = 2;
1343 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec147b002014-03-06 18:11:06 -08001344 static constexpr bool kMultiRegistersAligned = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001345 static constexpr bool kMultiFPRegistersWidened = true;
1346 static constexpr bool kMultiGPRegistersWidened = false;
Douglas Leung735b8552014-10-31 12:21:40 -07001347 static constexpr bool kAlignLongOnStack = true;
1348 static constexpr bool kAlignDoubleOnStack = true;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001349#elif defined(__mips__) && defined(__LP64__)
1350 // Let the code prepare GPRs only and we will load the FPRs with same data.
1351 static constexpr bool kNativeSoftFloatAbi = true;
1352 static constexpr size_t kNumNativeGprArgs = 8;
1353 static constexpr size_t kNumNativeFprArgs = 0;
1354
1355 static constexpr size_t kRegistersNeededForLong = 1;
1356 static constexpr size_t kRegistersNeededForDouble = 1;
1357 static constexpr bool kMultiRegistersAligned = false;
1358 static constexpr bool kMultiFPRegistersWidened = false;
1359 static constexpr bool kMultiGPRegistersWidened = true;
1360 static constexpr bool kAlignLongOnStack = false;
1361 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001362#elif defined(__i386__)
1363 // TODO: Check these!
Andreas Gampec147b002014-03-06 18:11:06 -08001364 static constexpr bool kNativeSoftFloatAbi = false; // Not using int registers for fp
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001365 static constexpr size_t kNumNativeGprArgs = 0; // 6 arguments passed in GPRs.
1366 static constexpr size_t kNumNativeFprArgs = 0; // 8 arguments passed in FPRs.
1367
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001368 static constexpr size_t kRegistersNeededForLong = 2;
1369 static constexpr size_t kRegistersNeededForDouble = 2;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001370 static constexpr bool kMultiRegistersAligned = false; // x86 not using regs, anyways
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001371 static constexpr bool kMultiFPRegistersWidened = false;
1372 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001373 static constexpr bool kAlignLongOnStack = false;
1374 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001375#elif defined(__x86_64__)
1376 static constexpr bool kNativeSoftFloatAbi = false; // This is a hard float ABI.
1377 static constexpr size_t kNumNativeGprArgs = 6; // 6 arguments passed in GPRs.
1378 static constexpr size_t kNumNativeFprArgs = 8; // 8 arguments passed in FPRs.
1379
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001380 static constexpr size_t kRegistersNeededForLong = 1;
1381 static constexpr size_t kRegistersNeededForDouble = 1;
Andreas Gampec147b002014-03-06 18:11:06 -08001382 static constexpr bool kMultiRegistersAligned = false;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001383 static constexpr bool kMultiFPRegistersWidened = false;
1384 static constexpr bool kMultiGPRegistersWidened = false;
Andreas Gampec147b002014-03-06 18:11:06 -08001385 static constexpr bool kAlignLongOnStack = false;
1386 static constexpr bool kAlignDoubleOnStack = false;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001387#else
1388#error "Unsupported architecture"
1389#endif
1390
Andreas Gampec147b002014-03-06 18:11:06 -08001391 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001392 explicit BuildNativeCallFrameStateMachine(T* delegate)
1393 : gpr_index_(kNumNativeGprArgs),
1394 fpr_index_(kNumNativeFprArgs),
1395 stack_entries_(0),
1396 delegate_(delegate) {
Andreas Gampec147b002014-03-06 18:11:06 -08001397 // For register alignment, we want to assume that counters (gpr_index_, fpr_index_) are even iff
1398 // the next register is even; counting down is just to make the compiler happy...
Andreas Gampe575e78c2014-11-03 23:41:03 -08001399 static_assert(kNumNativeGprArgs % 2 == 0U, "Number of native GPR arguments not even");
1400 static_assert(kNumNativeFprArgs % 2 == 0U, "Number of native FPR arguments not even");
Andreas Gampec147b002014-03-06 18:11:06 -08001401 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001402
Andreas Gampec200a4a2014-06-16 18:39:09 -07001403 virtual ~BuildNativeCallFrameStateMachine() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001404
Ian Rogers1428dce2014-10-21 15:02:15 -07001405 bool HavePointerGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001406 return gpr_index_ > 0;
1407 }
1408
Andreas Gampec200a4a2014-06-16 18:39:09 -07001409 void AdvancePointer(const void* val) {
Andreas Gampec147b002014-03-06 18:11:06 -08001410 if (HavePointerGpr()) {
1411 gpr_index_--;
1412 PushGpr(reinterpret_cast<uintptr_t>(val));
1413 } else {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001414 stack_entries_++; // TODO: have a field for pointer length as multiple of 32b
Andreas Gampec147b002014-03-06 18:11:06 -08001415 PushStack(reinterpret_cast<uintptr_t>(val));
1416 gpr_index_ = 0;
1417 }
1418 }
1419
Ian Rogers1428dce2014-10-21 15:02:15 -07001420 bool HaveHandleScopeGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001421 return gpr_index_ > 0;
1422 }
1423
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001424 void AdvanceHandleScope(mirror::Object* ptr) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001425 uintptr_t handle = PushHandle(ptr);
1426 if (HaveHandleScopeGpr()) {
Andreas Gampec147b002014-03-06 18:11:06 -08001427 gpr_index_--;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001428 PushGpr(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001429 } else {
1430 stack_entries_++;
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001431 PushStack(handle);
Andreas Gampec147b002014-03-06 18:11:06 -08001432 gpr_index_ = 0;
1433 }
1434 }
1435
Ian Rogers1428dce2014-10-21 15:02:15 -07001436 bool HaveIntGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001437 return gpr_index_ > 0;
1438 }
1439
1440 void AdvanceInt(uint32_t val) {
1441 if (HaveIntGpr()) {
1442 gpr_index_--;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001443 if (kMultiGPRegistersWidened) {
1444 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001445 PushGpr(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001446 } else {
1447 PushGpr(val);
1448 }
Andreas Gampec147b002014-03-06 18:11:06 -08001449 } else {
1450 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001451 if (kMultiGPRegistersWidened) {
1452 DCHECK_EQ(sizeof(uintptr_t), sizeof(int64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001453 PushStack(static_cast<int64_t>(bit_cast<int32_t, uint32_t>(val)));
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001454 } else {
1455 PushStack(val);
1456 }
Andreas Gampec147b002014-03-06 18:11:06 -08001457 gpr_index_ = 0;
1458 }
1459 }
1460
Ian Rogers1428dce2014-10-21 15:02:15 -07001461 bool HaveLongGpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001462 return gpr_index_ >= kRegistersNeededForLong + (LongGprNeedsPadding() ? 1 : 0);
1463 }
1464
Ian Rogers1428dce2014-10-21 15:02:15 -07001465 bool LongGprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001466 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1467 kAlignLongOnStack && // and when it needs alignment
1468 (gpr_index_ & 1) == 1; // counter is odd, see constructor
1469 }
1470
Ian Rogers1428dce2014-10-21 15:02:15 -07001471 bool LongStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001472 return kRegistersNeededForLong > 1 && // only pad when using multiple registers
1473 kAlignLongOnStack && // and when it needs 8B alignment
1474 (stack_entries_ & 1) == 1; // counter is odd
1475 }
1476
1477 void AdvanceLong(uint64_t val) {
1478 if (HaveLongGpr()) {
1479 if (LongGprNeedsPadding()) {
1480 PushGpr(0);
1481 gpr_index_--;
1482 }
1483 if (kRegistersNeededForLong == 1) {
1484 PushGpr(static_cast<uintptr_t>(val));
1485 } else {
1486 PushGpr(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1487 PushGpr(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1488 }
1489 gpr_index_ -= kRegistersNeededForLong;
1490 } else {
1491 if (LongStackNeedsPadding()) {
1492 PushStack(0);
1493 stack_entries_++;
1494 }
1495 if (kRegistersNeededForLong == 1) {
1496 PushStack(static_cast<uintptr_t>(val));
1497 stack_entries_++;
1498 } else {
1499 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1500 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1501 stack_entries_ += 2;
1502 }
1503 gpr_index_ = 0;
1504 }
1505 }
1506
Ian Rogers1428dce2014-10-21 15:02:15 -07001507 bool HaveFloatFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001508 return fpr_index_ > 0;
1509 }
1510
Andreas Gampec147b002014-03-06 18:11:06 -08001511 void AdvanceFloat(float val) {
1512 if (kNativeSoftFloatAbi) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001513 AdvanceInt(bit_cast<uint32_t, float>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001514 } else {
1515 if (HaveFloatFpr()) {
1516 fpr_index_--;
1517 if (kRegistersNeededForDouble == 1) {
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001518 if (kMultiFPRegistersWidened) {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001519 PushFpr8(bit_cast<uint64_t, double>(val));
Andreas Gampec147b002014-03-06 18:11:06 -08001520 } else {
1521 // No widening, just use the bits.
Roland Levillainda4d79b2015-03-24 14:36:11 +00001522 PushFpr8(static_cast<uint64_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001523 }
1524 } else {
1525 PushFpr4(val);
1526 }
1527 } else {
1528 stack_entries_++;
Andreas Gampe1a5c4062015-01-15 12:10:47 -08001529 if (kRegistersNeededForDouble == 1 && kMultiFPRegistersWidened) {
Andreas Gampec147b002014-03-06 18:11:06 -08001530 // Need to widen before storing: Note the "double" in the template instantiation.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001531 // Note: We need to jump through those hoops to make the compiler happy.
1532 DCHECK_EQ(sizeof(uintptr_t), sizeof(uint64_t));
Roland Levillainda4d79b2015-03-24 14:36:11 +00001533 PushStack(static_cast<uintptr_t>(bit_cast<uint64_t, double>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001534 } else {
Roland Levillainda4d79b2015-03-24 14:36:11 +00001535 PushStack(static_cast<uintptr_t>(bit_cast<uint32_t, float>(val)));
Andreas Gampec147b002014-03-06 18:11:06 -08001536 }
1537 fpr_index_ = 0;
1538 }
1539 }
1540 }
1541
Ian Rogers1428dce2014-10-21 15:02:15 -07001542 bool HaveDoubleFpr() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001543 return fpr_index_ >= kRegistersNeededForDouble + (DoubleFprNeedsPadding() ? 1 : 0);
1544 }
1545
Ian Rogers1428dce2014-10-21 15:02:15 -07001546 bool DoubleFprNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001547 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1548 kAlignDoubleOnStack && // and when it needs alignment
1549 (fpr_index_ & 1) == 1; // counter is odd, see constructor
1550 }
1551
Ian Rogers1428dce2014-10-21 15:02:15 -07001552 bool DoubleStackNeedsPadding() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001553 return kRegistersNeededForDouble > 1 && // only pad when using multiple registers
1554 kAlignDoubleOnStack && // and when it needs 8B alignment
1555 (stack_entries_ & 1) == 1; // counter is odd
1556 }
1557
1558 void AdvanceDouble(uint64_t val) {
1559 if (kNativeSoftFloatAbi) {
1560 AdvanceLong(val);
1561 } else {
1562 if (HaveDoubleFpr()) {
1563 if (DoubleFprNeedsPadding()) {
1564 PushFpr4(0);
1565 fpr_index_--;
1566 }
1567 PushFpr8(val);
1568 fpr_index_ -= kRegistersNeededForDouble;
1569 } else {
1570 if (DoubleStackNeedsPadding()) {
1571 PushStack(0);
1572 stack_entries_++;
1573 }
1574 if (kRegistersNeededForDouble == 1) {
1575 PushStack(static_cast<uintptr_t>(val));
1576 stack_entries_++;
1577 } else {
1578 PushStack(static_cast<uintptr_t>(val & 0xFFFFFFFF));
1579 PushStack(static_cast<uintptr_t>((val >> 32) & 0xFFFFFFFF));
1580 stack_entries_ += 2;
1581 }
1582 fpr_index_ = 0;
1583 }
1584 }
1585 }
1586
Ian Rogers1428dce2014-10-21 15:02:15 -07001587 uint32_t GetStackEntries() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001588 return stack_entries_;
1589 }
1590
Ian Rogers1428dce2014-10-21 15:02:15 -07001591 uint32_t GetNumberOfUsedGprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001592 return kNumNativeGprArgs - gpr_index_;
1593 }
1594
Ian Rogers1428dce2014-10-21 15:02:15 -07001595 uint32_t GetNumberOfUsedFprs() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001596 return kNumNativeFprArgs - fpr_index_;
1597 }
1598
1599 private:
1600 void PushGpr(uintptr_t val) {
1601 delegate_->PushGpr(val);
1602 }
1603 void PushFpr4(float val) {
1604 delegate_->PushFpr4(val);
1605 }
1606 void PushFpr8(uint64_t val) {
1607 delegate_->PushFpr8(val);
1608 }
1609 void PushStack(uintptr_t val) {
1610 delegate_->PushStack(val);
1611 }
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001612 uintptr_t PushHandle(mirror::Object* ref) REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07001613 return delegate_->PushHandle(ref);
Andreas Gampec147b002014-03-06 18:11:06 -08001614 }
1615
1616 uint32_t gpr_index_; // Number of free GPRs
1617 uint32_t fpr_index_; // Number of free FPRs
1618 uint32_t stack_entries_; // Stack entries are in multiples of 32b, as floats are usually not
1619 // extended
Ian Rogers1428dce2014-10-21 15:02:15 -07001620 T* const delegate_; // What Push implementation gets called
Andreas Gampec147b002014-03-06 18:11:06 -08001621};
1622
Andreas Gampec200a4a2014-06-16 18:39:09 -07001623// Computes the sizes of register stacks and call stack area. Handling of references can be extended
1624// in subclasses.
1625//
1626// To handle native pointers, use "L" in the shorty for an object reference, which simulates
1627// them with handles.
1628class ComputeNativeCallFrameSize {
Andreas Gampec147b002014-03-06 18:11:06 -08001629 public:
Andreas Gampec200a4a2014-06-16 18:39:09 -07001630 ComputeNativeCallFrameSize() : num_stack_entries_(0) {}
1631
1632 virtual ~ComputeNativeCallFrameSize() {}
Andreas Gampec147b002014-03-06 18:11:06 -08001633
Ian Rogers1428dce2014-10-21 15:02:15 -07001634 uint32_t GetStackSize() const {
Andreas Gampec147b002014-03-06 18:11:06 -08001635 return num_stack_entries_ * sizeof(uintptr_t);
1636 }
1637
Ian Rogers1428dce2014-10-21 15:02:15 -07001638 uint8_t* LayoutCallStack(uint8_t* sp8) const {
Andreas Gampec147b002014-03-06 18:11:06 -08001639 sp8 -= GetStackSize();
Andreas Gampe779f8c92014-06-09 18:29:38 -07001640 // Align by kStackAlignment.
1641 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001642 return sp8;
Andreas Gampec147b002014-03-06 18:11:06 -08001643 }
1644
Ian Rogers1428dce2014-10-21 15:02:15 -07001645 uint8_t* LayoutCallRegisterStacks(uint8_t* sp8, uintptr_t** start_gpr, uint32_t** start_fpr)
1646 const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001647 // Assumption is OK right now, as we have soft-float arm
1648 size_t fregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeFprArgs;
1649 sp8 -= fregs * sizeof(uintptr_t);
1650 *start_fpr = reinterpret_cast<uint32_t*>(sp8);
1651 size_t iregs = BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>::kNumNativeGprArgs;
1652 sp8 -= iregs * sizeof(uintptr_t);
1653 *start_gpr = reinterpret_cast<uintptr_t*>(sp8);
1654 return sp8;
1655 }
Andreas Gampec147b002014-03-06 18:11:06 -08001656
Andreas Gampec200a4a2014-06-16 18:39:09 -07001657 uint8_t* LayoutNativeCall(uint8_t* sp8, uintptr_t** start_stack, uintptr_t** start_gpr,
Ian Rogers1428dce2014-10-21 15:02:15 -07001658 uint32_t** start_fpr) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001659 // Native call stack.
1660 sp8 = LayoutCallStack(sp8);
1661 *start_stack = reinterpret_cast<uintptr_t*>(sp8);
Andreas Gampec147b002014-03-06 18:11:06 -08001662
Andreas Gampec200a4a2014-06-16 18:39:09 -07001663 // Put fprs and gprs below.
1664 sp8 = LayoutCallRegisterStacks(sp8, start_gpr, start_fpr);
Andreas Gampec147b002014-03-06 18:11:06 -08001665
Andreas Gampec200a4a2014-06-16 18:39:09 -07001666 // Return the new bottom.
1667 return sp8;
1668 }
1669
Roland Levillain4b8f1ec2015-08-26 18:34:03 +01001670 virtual void WalkHeader(
1671 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm ATTRIBUTE_UNUSED)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001672 REQUIRES_SHARED(Locks::mutator_lock_) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001673 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001674
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001675 void Walk(const char* shorty, uint32_t shorty_len) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001676 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize> sm(this);
1677
1678 WalkHeader(&sm);
Andreas Gampec147b002014-03-06 18:11:06 -08001679
1680 for (uint32_t i = 1; i < shorty_len; ++i) {
1681 Primitive::Type cur_type_ = Primitive::GetType(shorty[i]);
1682 switch (cur_type_) {
1683 case Primitive::kPrimNot:
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07001684 // TODO: fix abuse of mirror types.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001685 sm.AdvanceHandleScope(
1686 reinterpret_cast<mirror::Object*>(0x12345678));
Andreas Gampec147b002014-03-06 18:11:06 -08001687 break;
1688
1689 case Primitive::kPrimBoolean:
1690 case Primitive::kPrimByte:
1691 case Primitive::kPrimChar:
1692 case Primitive::kPrimShort:
1693 case Primitive::kPrimInt:
1694 sm.AdvanceInt(0);
1695 break;
1696 case Primitive::kPrimFloat:
1697 sm.AdvanceFloat(0);
1698 break;
1699 case Primitive::kPrimDouble:
1700 sm.AdvanceDouble(0);
1701 break;
1702 case Primitive::kPrimLong:
1703 sm.AdvanceLong(0);
1704 break;
1705 default:
1706 LOG(FATAL) << "Unexpected type: " << cur_type_ << " in " << shorty;
Ian Rogerse0a02da2014-12-02 14:10:53 -08001707 UNREACHABLE();
Andreas Gampec147b002014-03-06 18:11:06 -08001708 }
1709 }
1710
Ian Rogers1428dce2014-10-21 15:02:15 -07001711 num_stack_entries_ = sm.GetStackEntries();
Andreas Gampec147b002014-03-06 18:11:06 -08001712 }
1713
1714 void PushGpr(uintptr_t /* val */) {
1715 // not optimizing registers, yet
1716 }
1717
1718 void PushFpr4(float /* val */) {
1719 // not optimizing registers, yet
1720 }
1721
1722 void PushFpr8(uint64_t /* val */) {
1723 // not optimizing registers, yet
1724 }
1725
1726 void PushStack(uintptr_t /* val */) {
1727 // counting is already done in the superclass
1728 }
1729
Andreas Gampec200a4a2014-06-16 18:39:09 -07001730 virtual uintptr_t PushHandle(mirror::Object* /* ptr */) {
Andreas Gampec147b002014-03-06 18:11:06 -08001731 return reinterpret_cast<uintptr_t>(nullptr);
1732 }
1733
Andreas Gampec200a4a2014-06-16 18:39:09 -07001734 protected:
Andreas Gampec147b002014-03-06 18:11:06 -08001735 uint32_t num_stack_entries_;
1736};
1737
Andreas Gampec200a4a2014-06-16 18:39:09 -07001738class ComputeGenericJniFrameSize FINAL : public ComputeNativeCallFrameSize {
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001739 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001740 explicit ComputeGenericJniFrameSize(bool critical_native)
1741 : num_handle_scope_references_(0), critical_native_(critical_native) {}
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001742
Andreas Gampec200a4a2014-06-16 18:39:09 -07001743 // Lays out the callee-save frame. Assumes that the incorrect frame corresponding to RefsAndArgs
1744 // is at *m = sp. Will update to point to the bottom of the save frame.
1745 //
1746 // Note: assumes ComputeAll() has been run before.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001747 void LayoutCalleeSaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001748 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07001749 ArtMethod* method = **m;
1750
Andreas Gampe542451c2016-07-26 09:02:02 -07001751 DCHECK_EQ(Runtime::Current()->GetClassLinker()->GetImagePointerSize(), kRuntimePointerSize);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001752
Andreas Gampec200a4a2014-06-16 18:39:09 -07001753 uint8_t* sp8 = reinterpret_cast<uint8_t*>(sp);
1754
1755 // First, fix up the layout of the callee-save frame.
1756 // We have to squeeze in the HandleScope, and relocate the method pointer.
1757
1758 // "Free" the slot for the method.
Ian Rogers13735952014-10-08 12:43:28 -07001759 sp8 += sizeof(void*); // In the callee-save frame we use a full pointer.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001760
1761 // Under the callee saves put handle scope and new method stack reference.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001762 size_t handle_scope_size = HandleScope::SizeOf(num_handle_scope_references_);
Mathieu Chartiere401d142015-04-22 13:56:20 -07001763 size_t scope_and_method = handle_scope_size + sizeof(ArtMethod*);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001764
1765 sp8 -= scope_and_method;
1766 // Align by kStackAlignment.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001767 sp8 = reinterpret_cast<uint8_t*>(RoundDown(reinterpret_cast<uintptr_t>(sp8), kStackAlignment));
Andreas Gampec200a4a2014-06-16 18:39:09 -07001768
Mathieu Chartiere401d142015-04-22 13:56:20 -07001769 uint8_t* sp8_table = sp8 + sizeof(ArtMethod*);
Ian Rogers59c07062014-10-10 13:03:39 -07001770 *handle_scope = HandleScope::Create(sp8_table, self->GetTopHandleScope(),
1771 num_handle_scope_references_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001772
1773 // Add a slot for the method pointer, and fill it. Fix the pointer-pointer given to us.
1774 uint8_t* method_pointer = sp8;
Mathieu Chartiere401d142015-04-22 13:56:20 -07001775 auto** new_method_ref = reinterpret_cast<ArtMethod**>(method_pointer);
1776 *new_method_ref = method;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001777 *m = new_method_ref;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001778 }
1779
Andreas Gampec200a4a2014-06-16 18:39:09 -07001780 // Adds space for the cookie. Note: may leave stack unaligned.
Ian Rogers1428dce2014-10-21 15:02:15 -07001781 void LayoutCookie(uint8_t** sp) const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001782 // Reference cookie and padding
1783 *sp -= 8;
Mathieu Chartier0cd81352014-05-22 16:48:55 -07001784 }
1785
Andreas Gampec200a4a2014-06-16 18:39:09 -07001786 // Re-layout the callee-save frame (insert a handle-scope). Then add space for the cookie.
1787 // Returns the new bottom. Note: this may be unaligned.
Mathieu Chartiere401d142015-04-22 13:56:20 -07001788 uint8_t* LayoutJNISaveFrame(Thread* self, ArtMethod*** m, void* sp, HandleScope** handle_scope)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001789 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001790 // First, fix up the layout of the callee-save frame.
1791 // We have to squeeze in the HandleScope, and relocate the method pointer.
Ian Rogers59c07062014-10-10 13:03:39 -07001792 LayoutCalleeSaveFrame(self, m, sp, handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001793
1794 // The bottom of the callee-save frame is now where the method is, *m.
1795 uint8_t* sp8 = reinterpret_cast<uint8_t*>(*m);
1796
1797 // Add space for cookie.
1798 LayoutCookie(&sp8);
1799
1800 return sp8;
1801 }
1802
1803 // WARNING: After this, *sp won't be pointing to the method anymore!
Mathieu Chartiere401d142015-04-22 13:56:20 -07001804 uint8_t* ComputeLayout(Thread* self, ArtMethod*** m, const char* shorty, uint32_t shorty_len,
1805 HandleScope** handle_scope, uintptr_t** start_stack, uintptr_t** start_gpr,
1806 uint32_t** start_fpr)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001807 REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001808 Walk(shorty, shorty_len);
1809
1810 // JNI part.
Ian Rogers59c07062014-10-10 13:03:39 -07001811 uint8_t* sp8 = LayoutJNISaveFrame(self, m, reinterpret_cast<void*>(*m), handle_scope);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001812
1813 sp8 = LayoutNativeCall(sp8, start_stack, start_gpr, start_fpr);
1814
1815 // Return the new bottom.
1816 return sp8;
1817 }
1818
1819 uintptr_t PushHandle(mirror::Object* /* ptr */) OVERRIDE;
1820
1821 // Add JNIEnv* and jobj/jclass before the shorty-derived elements.
1822 void WalkHeader(BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) OVERRIDE
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001823 REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001824
1825 private:
1826 uint32_t num_handle_scope_references_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001827 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001828};
1829
1830uintptr_t ComputeGenericJniFrameSize::PushHandle(mirror::Object* /* ptr */) {
1831 num_handle_scope_references_++;
1832 return reinterpret_cast<uintptr_t>(nullptr);
1833}
1834
1835void ComputeGenericJniFrameSize::WalkHeader(
1836 BuildNativeCallFrameStateMachine<ComputeNativeCallFrameSize>* sm) {
Igor Murashkin06a04e02016-09-13 15:57:37 -07001837 // First 2 parameters are always excluded for @CriticalNative.
1838 if (UNLIKELY(critical_native_)) {
1839 return;
1840 }
1841
Andreas Gampec200a4a2014-06-16 18:39:09 -07001842 // JNIEnv
1843 sm->AdvancePointer(nullptr);
1844
1845 // Class object or this as first argument
1846 sm->AdvanceHandleScope(reinterpret_cast<mirror::Object*>(0x12345678));
1847}
1848
1849// Class to push values to three separate regions. Used to fill the native call part. Adheres to
1850// the template requirements of BuildGenericJniFrameStateMachine.
1851class FillNativeCall {
1852 public:
1853 FillNativeCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) :
1854 cur_gpr_reg_(gpr_regs), cur_fpr_reg_(fpr_regs), cur_stack_arg_(stack_args) {}
1855
1856 virtual ~FillNativeCall() {}
1857
1858 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args) {
1859 cur_gpr_reg_ = gpr_regs;
1860 cur_fpr_reg_ = fpr_regs;
1861 cur_stack_arg_ = stack_args;
Andreas Gampec147b002014-03-06 18:11:06 -08001862 }
1863
1864 void PushGpr(uintptr_t val) {
1865 *cur_gpr_reg_ = val;
1866 cur_gpr_reg_++;
1867 }
1868
1869 void PushFpr4(float val) {
1870 *cur_fpr_reg_ = val;
1871 cur_fpr_reg_++;
1872 }
1873
1874 void PushFpr8(uint64_t val) {
1875 uint64_t* tmp = reinterpret_cast<uint64_t*>(cur_fpr_reg_);
1876 *tmp = val;
1877 cur_fpr_reg_ += 2;
1878 }
1879
1880 void PushStack(uintptr_t val) {
1881 *cur_stack_arg_ = val;
1882 cur_stack_arg_++;
1883 }
1884
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001885 virtual uintptr_t PushHandle(mirror::Object*) REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001886 LOG(FATAL) << "(Non-JNI) Native call does not use handles.";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001887 UNREACHABLE();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001888 }
1889
1890 private:
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001891 uintptr_t* cur_gpr_reg_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001892 uint32_t* cur_fpr_reg_;
1893 uintptr_t* cur_stack_arg_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001894};
Andreas Gampec147b002014-03-06 18:11:06 -08001895
Andreas Gampec200a4a2014-06-16 18:39:09 -07001896// Visits arguments on the stack placing them into a region lower down the stack for the benefit
1897// of transitioning into native code.
1898class BuildGenericJniFrameVisitor FINAL : public QuickArgumentVisitor {
1899 public:
Igor Murashkin06a04e02016-09-13 15:57:37 -07001900 BuildGenericJniFrameVisitor(Thread* self,
1901 bool is_static,
1902 bool critical_native,
1903 const char* shorty,
1904 uint32_t shorty_len,
Mathieu Chartiere401d142015-04-22 13:56:20 -07001905 ArtMethod*** sp)
Andreas Gampec200a4a2014-06-16 18:39:09 -07001906 : QuickArgumentVisitor(*sp, is_static, shorty, shorty_len),
Igor Murashkin06a04e02016-09-13 15:57:37 -07001907 jni_call_(nullptr, nullptr, nullptr, nullptr, critical_native),
1908 sm_(&jni_call_) {
1909 ComputeGenericJniFrameSize fsc(critical_native);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001910 uintptr_t* start_gpr_reg;
1911 uint32_t* start_fpr_reg;
1912 uintptr_t* start_stack_arg;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -07001913 bottom_of_used_area_ = fsc.ComputeLayout(self, sp, shorty, shorty_len,
Ian Rogers59c07062014-10-10 13:03:39 -07001914 &handle_scope_,
1915 &start_stack_arg,
Andreas Gampec200a4a2014-06-16 18:39:09 -07001916 &start_gpr_reg, &start_fpr_reg);
1917
Andreas Gampec200a4a2014-06-16 18:39:09 -07001918 jni_call_.Reset(start_gpr_reg, start_fpr_reg, start_stack_arg, handle_scope_);
1919
Igor Murashkin06a04e02016-09-13 15:57:37 -07001920 // First 2 parameters are always excluded for CriticalNative methods.
1921 if (LIKELY(!critical_native)) {
1922 // jni environment is always first argument
1923 sm_.AdvancePointer(self->GetJniEnv());
Andreas Gampec200a4a2014-06-16 18:39:09 -07001924
Igor Murashkin06a04e02016-09-13 15:57:37 -07001925 if (is_static) {
1926 sm_.AdvanceHandleScope((**sp)->GetDeclaringClass());
1927 } // else "this" reference is already handled by QuickArgumentVisitor.
Andreas Gampec200a4a2014-06-16 18:39:09 -07001928 }
1929 }
1930
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001931 void Visit() REQUIRES_SHARED(Locks::mutator_lock_) OVERRIDE;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001932
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001933 void FinalizeHandleScope(Thread* self) REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001934
Vladimir Markof39745e2016-01-26 12:16:55 +00001935 StackReference<mirror::Object>* GetFirstHandleScopeEntry() {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001936 return handle_scope_->GetHandle(0).GetReference();
1937 }
1938
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001939 jobject GetFirstHandleScopeJObject() const REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001940 return handle_scope_->GetHandle(0).ToJObject();
1941 }
1942
Ian Rogers1428dce2014-10-21 15:02:15 -07001943 void* GetBottomOfUsedArea() const {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001944 return bottom_of_used_area_;
1945 }
1946
1947 private:
1948 // A class to fill a JNI call. Adds reference/handle-scope management to FillNativeCall.
1949 class FillJniCall FINAL : public FillNativeCall {
1950 public:
1951 FillJniCall(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args,
Igor Murashkin06a04e02016-09-13 15:57:37 -07001952 HandleScope* handle_scope, bool critical_native)
1953 : FillNativeCall(gpr_regs, fpr_regs, stack_args),
1954 handle_scope_(handle_scope),
1955 cur_entry_(0),
1956 critical_native_(critical_native) {}
Andreas Gampec200a4a2014-06-16 18:39:09 -07001957
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001958 uintptr_t PushHandle(mirror::Object* ref) OVERRIDE REQUIRES_SHARED(Locks::mutator_lock_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001959
1960 void Reset(uintptr_t* gpr_regs, uint32_t* fpr_regs, uintptr_t* stack_args, HandleScope* scope) {
1961 FillNativeCall::Reset(gpr_regs, fpr_regs, stack_args);
1962 handle_scope_ = scope;
1963 cur_entry_ = 0U;
1964 }
1965
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07001966 void ResetRemainingScopeSlots() REQUIRES_SHARED(Locks::mutator_lock_) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07001967 // Initialize padding entries.
1968 size_t expected_slots = handle_scope_->NumberOfReferences();
1969 while (cur_entry_ < expected_slots) {
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07001970 handle_scope_->GetMutableHandle(cur_entry_++).Assign(nullptr);
Andreas Gampec200a4a2014-06-16 18:39:09 -07001971 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07001972
1973 if (!critical_native_) {
1974 // Non-critical natives have at least the self class (jclass) or this (jobject).
1975 DCHECK_NE(cur_entry_, 0U);
1976 }
Andreas Gampec200a4a2014-06-16 18:39:09 -07001977 }
1978
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07001979 bool CriticalNative() const {
1980 return critical_native_;
1981 }
1982
Andreas Gampec200a4a2014-06-16 18:39:09 -07001983 private:
1984 HandleScope* handle_scope_;
1985 size_t cur_entry_;
Igor Murashkin06a04e02016-09-13 15:57:37 -07001986 const bool critical_native_;
Andreas Gampec200a4a2014-06-16 18:39:09 -07001987 };
1988
1989 HandleScope* handle_scope_;
1990 FillJniCall jni_call_;
1991 void* bottom_of_used_area_;
1992
1993 BuildNativeCallFrameStateMachine<FillJniCall> sm_;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08001994
1995 DISALLOW_COPY_AND_ASSIGN(BuildGenericJniFrameVisitor);
1996};
1997
Andreas Gampec200a4a2014-06-16 18:39:09 -07001998uintptr_t BuildGenericJniFrameVisitor::FillJniCall::PushHandle(mirror::Object* ref) {
1999 uintptr_t tmp;
Andreas Gampe5a4b8a22014-09-11 08:30:08 -07002000 MutableHandle<mirror::Object> h = handle_scope_->GetMutableHandle(cur_entry_);
Andreas Gampec200a4a2014-06-16 18:39:09 -07002001 h.Assign(ref);
2002 tmp = reinterpret_cast<uintptr_t>(h.ToJObject());
2003 cur_entry_++;
2004 return tmp;
2005}
2006
Ian Rogers9758f792014-03-13 09:02:55 -07002007void BuildGenericJniFrameVisitor::Visit() {
2008 Primitive::Type type = GetParamPrimitiveType();
2009 switch (type) {
2010 case Primitive::kPrimLong: {
2011 jlong long_arg;
2012 if (IsSplitLongOrDouble()) {
2013 long_arg = ReadSplitLongParam();
2014 } else {
2015 long_arg = *reinterpret_cast<jlong*>(GetParamAddress());
2016 }
2017 sm_.AdvanceLong(long_arg);
2018 break;
2019 }
2020 case Primitive::kPrimDouble: {
2021 uint64_t double_arg;
2022 if (IsSplitLongOrDouble()) {
2023 // Read into union so that we don't case to a double.
2024 double_arg = ReadSplitLongParam();
2025 } else {
2026 double_arg = *reinterpret_cast<uint64_t*>(GetParamAddress());
2027 }
2028 sm_.AdvanceDouble(double_arg);
2029 break;
2030 }
2031 case Primitive::kPrimNot: {
2032 StackReference<mirror::Object>* stack_ref =
2033 reinterpret_cast<StackReference<mirror::Object>*>(GetParamAddress());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002034 sm_.AdvanceHandleScope(stack_ref->AsMirrorPtr());
Ian Rogers9758f792014-03-13 09:02:55 -07002035 break;
2036 }
2037 case Primitive::kPrimFloat:
2038 sm_.AdvanceFloat(*reinterpret_cast<float*>(GetParamAddress()));
2039 break;
2040 case Primitive::kPrimBoolean: // Fall-through.
2041 case Primitive::kPrimByte: // Fall-through.
2042 case Primitive::kPrimChar: // Fall-through.
2043 case Primitive::kPrimShort: // Fall-through.
2044 case Primitive::kPrimInt: // Fall-through.
2045 sm_.AdvanceInt(*reinterpret_cast<jint*>(GetParamAddress()));
2046 break;
2047 case Primitive::kPrimVoid:
2048 LOG(FATAL) << "UNREACHABLE";
Ian Rogers2c4257b2014-10-24 14:20:06 -07002049 UNREACHABLE();
Ian Rogers9758f792014-03-13 09:02:55 -07002050 }
2051}
2052
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002053void BuildGenericJniFrameVisitor::FinalizeHandleScope(Thread* self) {
Andreas Gampec200a4a2014-06-16 18:39:09 -07002054 // Clear out rest of the scope.
2055 jni_call_.ResetRemainingScopeSlots();
Mathieu Chartier1432a5b2016-10-04 15:41:42 -07002056 if (!jni_call_.CriticalNative()) {
2057 // Install HandleScope.
2058 self->PushHandleScope(handle_scope_);
2059 }
Ian Rogers9758f792014-03-13 09:02:55 -07002060}
2061
Ian Rogers04c31d22014-07-07 21:44:06 -07002062#if defined(__arm__) || defined(__aarch64__)
Alex Lightd78ddec2017-04-18 15:20:38 -07002063extern "C" const void* artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002064#else
Alex Lightd78ddec2017-04-18 15:20:38 -07002065extern "C" const void* artFindNativeMethod(Thread* self);
Ian Rogers04c31d22014-07-07 21:44:06 -07002066#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002067
Igor Murashkin06a04e02016-09-13 15:57:37 -07002068static uint64_t artQuickGenericJniEndJNIRef(Thread* self,
2069 uint32_t cookie,
2070 bool fast_native ATTRIBUTE_UNUSED,
2071 jobject l,
2072 jobject lock) {
2073 // TODO: add entrypoints for @FastNative returning objects.
Andreas Gampead615172014-04-04 16:20:13 -07002074 if (lock != nullptr) {
2075 return reinterpret_cast<uint64_t>(JniMethodEndWithReferenceSynchronized(l, cookie, lock, self));
2076 } else {
2077 return reinterpret_cast<uint64_t>(JniMethodEndWithReference(l, cookie, self));
2078 }
2079}
2080
Igor Murashkin06a04e02016-09-13 15:57:37 -07002081static void artQuickGenericJniEndJNINonRef(Thread* self,
2082 uint32_t cookie,
2083 bool fast_native,
2084 jobject lock) {
Andreas Gampead615172014-04-04 16:20:13 -07002085 if (lock != nullptr) {
2086 JniMethodEndSynchronized(cookie, lock, self);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002087 // Ignore "fast_native" here because synchronized functions aren't very fast.
Andreas Gampead615172014-04-04 16:20:13 -07002088 } else {
Igor Murashkin06a04e02016-09-13 15:57:37 -07002089 if (UNLIKELY(fast_native)) {
2090 JniMethodFastEnd(cookie, self);
2091 } else {
2092 JniMethodEnd(cookie, self);
2093 }
Andreas Gampead615172014-04-04 16:20:13 -07002094 }
2095}
2096
Andreas Gampec147b002014-03-06 18:11:06 -08002097/*
2098 * Initializes an alloca region assumed to be directly below sp for a native call:
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002099 * 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 -08002100 * The final element on the stack is a pointer to the native code.
2101 *
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002102 * On entry, the stack has a standard callee-save frame above sp, and an alloca below it.
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002103 * We need to fix this, as the handle scope needs to go into the callee-save frame.
Andreas Gampe36fea8d2014-03-10 13:37:40 -07002104 *
Andreas Gampec147b002014-03-06 18:11:06 -08002105 * The return of this function denotes:
2106 * 1) How many bytes of the alloca can be released, if the value is non-negative.
2107 * 2) An error, if the value is negative.
2108 */
Mathieu Chartiere401d142015-04-22 13:56:20 -07002109extern "C" TwoWordReturn artQuickGenericJniTrampoline(Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002110 REQUIRES_SHARED(Locks::mutator_lock_) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002111 ArtMethod* called = *sp;
David Sehr709b0702016-10-13 09:12:37 -07002112 DCHECK(called->IsNative()) << called->PrettyMethod(true);
Mathieu Chartierbfd9a432014-05-21 17:43:44 -07002113 uint32_t shorty_len = 0;
2114 const char* shorty = called->GetShorty(&shorty_len);
Igor Murashkin06a04e02016-09-13 15:57:37 -07002115 bool critical_native = called->IsAnnotatedWithCriticalNative();
2116 bool fast_native = called->IsAnnotatedWithFastNative();
2117 bool normal_native = !critical_native && !fast_native;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002118
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002119 // Run the visitor and update sp.
Igor Murashkin06a04e02016-09-13 15:57:37 -07002120 BuildGenericJniFrameVisitor visitor(self,
2121 called->IsStatic(),
2122 critical_native,
2123 shorty,
2124 shorty_len,
2125 &sp);
Mathieu Chartierbe08cf52016-09-13 13:41:24 -07002126 {
2127 ScopedAssertNoThreadSuspension sants(__FUNCTION__);
2128 visitor.VisitArguments();
2129 // FinalizeHandleScope pushes the handle scope on the thread.
2130 visitor.FinalizeHandleScope(self);
2131 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002132
Andreas Gampec200a4a2014-06-16 18:39:09 -07002133 // Fix up managed-stack things in Thread.
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002134 self->SetTopOfStack(sp);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002135
Ian Rogerse0dcd462014-03-08 15:21:04 -08002136 self->VerifyStack();
2137
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002138 uint32_t cookie;
Igor Murashkin06a04e02016-09-13 15:57:37 -07002139 uint32_t* sp32;
2140 // Skip calling JniMethodStart for @CriticalNative.
2141 if (LIKELY(!critical_native)) {
2142 // Start JNI, save the cookie.
2143 if (called->IsSynchronized()) {
2144 DCHECK(normal_native) << " @FastNative and synchronize is not supported";
2145 cookie = JniMethodStartSynchronized(visitor.GetFirstHandleScopeJObject(), self);
2146 if (self->IsExceptionPending()) {
2147 self->PopHandleScope();
2148 // A negative value denotes an error.
2149 return GetTwoWordFailureValue();
2150 }
2151 } else {
2152 if (fast_native) {
2153 cookie = JniMethodFastStart(self);
2154 } else {
2155 DCHECK(normal_native);
2156 cookie = JniMethodStart(self);
2157 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002158 }
Igor Murashkin06a04e02016-09-13 15:57:37 -07002159 sp32 = reinterpret_cast<uint32_t*>(sp);
2160 *(sp32 - 1) = cookie;
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002161 }
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002162
Andreas Gampe90546832014-03-12 18:07:19 -07002163 // Retrieve the stored native code.
Alex Lightd78ddec2017-04-18 15:20:38 -07002164 void const* nativeCode = called->GetEntryPointFromJni();
Andreas Gampe90546832014-03-12 18:07:19 -07002165
Andreas Gampe9a6a99a2014-03-14 07:52:20 -07002166 // There are two cases for the content of nativeCode:
2167 // 1) Pointer to the native function.
2168 // 2) Pointer to the trampoline for native code binding.
2169 // In the second case, we need to execute the binding and continue with the actual native function
2170 // pointer.
Andreas Gampe90546832014-03-12 18:07:19 -07002171 DCHECK(nativeCode != nullptr);
2172 if (nativeCode == GetJniDlsymLookupStub()) {
Ian Rogers04c31d22014-07-07 21:44:06 -07002173#if defined(__arm__) || defined(__aarch64__)
Andreas Gampe90546832014-03-12 18:07:19 -07002174 nativeCode = artFindNativeMethod();
Ian Rogers04c31d22014-07-07 21:44:06 -07002175#else
2176 nativeCode = artFindNativeMethod(self);
2177#endif
Andreas Gampe90546832014-03-12 18:07:19 -07002178
2179 if (nativeCode == nullptr) {
2180 DCHECK(self->IsExceptionPending()); // There should be an exception pending now.
Andreas Gampead615172014-04-04 16:20:13 -07002181
Igor Murashkin06a04e02016-09-13 15:57:37 -07002182 // @CriticalNative calls do not need to call back into JniMethodEnd.
2183 if (LIKELY(!critical_native)) {
2184 // End JNI, as the assembly will move to deliver the exception.
2185 jobject lock = called->IsSynchronized() ? visitor.GetFirstHandleScopeJObject() : nullptr;
2186 if (shorty[0] == 'L') {
2187 artQuickGenericJniEndJNIRef(self, cookie, fast_native, nullptr, lock);
2188 } else {
2189 artQuickGenericJniEndJNINonRef(self, cookie, fast_native, lock);
2190 }
Andreas Gampead615172014-04-04 16:20:13 -07002191 }
2192
Andreas Gampec200a4a2014-06-16 18:39:09 -07002193 return GetTwoWordFailureValue();
Andreas Gampe90546832014-03-12 18:07:19 -07002194 }
2195 // Note that the native code pointer will be automatically set by artFindNativeMethod().
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002196 }
2197
Alexey Frunze1b8464d2016-11-12 17:22:05 -08002198#if defined(__mips__) && !defined(__LP64__)
2199 // On MIPS32 if the first two arguments are floating-point, we need to know their types
2200 // so that art_quick_generic_jni_trampoline can correctly extract them from the stack
2201 // and load into floating-point registers.
2202 // Possible arrangements of first two floating-point arguments on the stack (32-bit FPU
2203 // view):
2204 // (1)
2205 // | DOUBLE | DOUBLE | other args, if any
2206 // | F12 | F13 | F14 | F15 |
2207 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2208 // (2)
2209 // | DOUBLE | FLOAT | (PAD) | other args, if any
2210 // | F12 | F13 | F14 | |
2211 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2212 // (3)
2213 // | FLOAT | (PAD) | DOUBLE | other args, if any
2214 // | F12 | | F14 | F15 |
2215 // | SP+0 | SP+4 | SP+8 | SP+12 | SP+16
2216 // (4)
2217 // | FLOAT | FLOAT | other args, if any
2218 // | F12 | F14 |
2219 // | SP+0 | SP+4 | SP+8
2220 // As you can see, only the last case (4) is special. In all others we can just
2221 // load F12/F13 and F14/F15 in the same manner.
2222 // Set bit 0 of the native code address to 1 in this case (valid code addresses
2223 // are always a multiple of 4 on MIPS32, so we have 2 spare bits available).
2224 if (nativeCode != nullptr &&
2225 shorty != nullptr &&
2226 shorty_len >= 3 &&
2227 shorty[1] == 'F' &&
2228 shorty[2] == 'F') {
2229 nativeCode = reinterpret_cast<void*>(reinterpret_cast<uintptr_t>(nativeCode) | 1);
2230 }
2231#endif
2232
Andreas Gampec200a4a2014-06-16 18:39:09 -07002233 // Return native code addr(lo) and bottom of alloca address(hi).
2234 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(visitor.GetBottomOfUsedArea()),
2235 reinterpret_cast<uintptr_t>(nativeCode));
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002236}
2237
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002238// Defined in quick_jni_entrypoints.cc.
2239extern uint64_t GenericJniMethodEnd(Thread* self, uint32_t saved_local_ref_cookie,
2240 jvalue result, uint64_t result_f, ArtMethod* called,
2241 HandleScope* handle_scope);
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002242/*
Mathieu Chartiereb8167a2014-05-07 15:43:14 -07002243 * Is called after the native JNI code. Responsible for cleanup (handle scope, saved state) and
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002244 * unlocking.
2245 */
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002246extern "C" uint64_t artQuickGenericJniEndTrampoline(Thread* self,
2247 jvalue result,
2248 uint64_t result_f) {
2249 // We're here just back from a native call. We don't have the shared mutator lock at this point
2250 // yet until we call GoToRunnable() later in GenericJniMethodEnd(). Accessing objects or doing
2251 // anything that requires a mutator lock before that would cause problems as GC may have the
2252 // exclusive mutator lock and may be moving objects, etc.
Mathieu Chartiere401d142015-04-22 13:56:20 -07002253 ArtMethod** sp = self->GetManagedStack()->GetTopQuickFrame();
Andreas Gampebf6b92a2014-03-05 16:11:04 -08002254 uint32_t* sp32 = reinterpret_cast<uint32_t*>(sp);
Mathieu Chartiere401d142015-04-22 13:56:20 -07002255 ArtMethod* called = *sp;
Ian Rogerse0dcd462014-03-08 15:21:04 -08002256 uint32_t cookie = *(sp32 - 1);
Hiroshi Yamauchia23b4682015-09-28 17:47:32 -07002257 HandleScope* table = reinterpret_cast<HandleScope*>(reinterpret_cast<uint8_t*>(sp) + sizeof(*sp));
2258 return GenericJniMethodEnd(self, cookie, result, result_f, called, table);
Andreas Gampe2da88232014-02-27 12:26:20 -08002259}
2260
Andreas Gamped58342c2014-06-05 14:18:08 -07002261// We use TwoWordReturn to optimize scalar returns. We use the hi value for code, and the lo value
2262// for the method pointer.
Andreas Gampe51f76352014-05-21 08:28:48 -07002263//
Andreas Gamped58342c2014-06-05 14:18:08 -07002264// 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 -07002265// to hold the mutator lock (see REQUIRES_SHARED(Locks::mutator_lock_) annotations).
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002266
2267template<InvokeType type, bool access_check>
Mathieu Chartieref41db72016-10-25 15:08:01 -07002268static TwoWordReturn artInvokeCommon(uint32_t method_idx,
2269 ObjPtr<mirror::Object> this_object,
2270 Thread* self,
Mathieu Chartiere401d142015-04-22 13:56:20 -07002271 ArtMethod** sp) {
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002272 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002273 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Mathieu Chartiere401d142015-04-22 13:56:20 -07002274 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2275 ArtMethod* method = FindMethodFast(method_idx, this_object, caller_method, access_check, type);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002276 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002277 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()->GetDexFile();
2278 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002279 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(method_idx), &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002280 {
2281 // Remember the args in case a GC happens in FindMethodFromCode.
2282 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2283 RememberForGcArgumentVisitor visitor(sp, type == kStatic, shorty, shorty_len, &soa);
2284 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002285 method = FindMethodFromCode<type, access_check>(method_idx,
2286 &this_object,
2287 caller_method,
Mathieu Chartier0cd81352014-05-22 16:48:55 -07002288 self);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002289 visitor.FixupReferences();
2290 }
2291
Ian Rogerse0a02da2014-12-02 14:10:53 -08002292 if (UNLIKELY(method == nullptr)) {
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002293 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002294 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002295 }
2296 }
2297 DCHECK(!self->IsExceptionPending());
2298 const void* code = method->GetEntryPointFromQuickCompiledCode();
2299
2300 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002301 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002302 << " location: "
2303 << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002304
Andreas Gamped58342c2014-06-05 14:18:08 -07002305 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2306 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002307}
2308
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002309// Explicit artInvokeCommon template function declarations to please analysis tool.
2310#define EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(type, access_check) \
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002311 template REQUIRES_SHARED(Locks::mutator_lock_) \
Mathieu Chartiere401d142015-04-22 13:56:20 -07002312 TwoWordReturn artInvokeCommon<type, access_check>( \
Mathieu Chartieref41db72016-10-25 15:08:01 -07002313 uint32_t method_idx, ObjPtr<mirror::Object> his_object, Thread* self, ArtMethod** sp)
Nicolas Geoffray8689a0a2014-04-04 09:26:24 +01002314
2315EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, false);
2316EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kVirtual, true);
2317EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, false);
2318EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kInterface, true);
2319EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, false);
2320EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kDirect, true);
2321EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, false);
2322EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kStatic, true);
2323EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, false);
2324EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL(kSuper, true);
2325#undef EXPLICIT_INVOKE_COMMON_TEMPLATE_DECL
2326
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002327// See comments in runtime_support_asm.S
Andreas Gampec200a4a2014-06-16 18:39:09 -07002328extern "C" TwoWordReturn artInvokeInterfaceTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002329 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002330 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002331 return artInvokeCommon<kInterface, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002332}
2333
Andreas Gampec200a4a2014-06-16 18:39:09 -07002334extern "C" TwoWordReturn artInvokeDirectTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002335 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002336 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002337 return artInvokeCommon<kDirect, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002338}
2339
Andreas Gampec200a4a2014-06-16 18:39:09 -07002340extern "C" TwoWordReturn artInvokeStaticTrampolineWithAccessCheck(
Mathieu Chartieref41db72016-10-25 15:08:01 -07002341 uint32_t method_idx,
2342 mirror::Object* this_object ATTRIBUTE_UNUSED,
2343 Thread* self,
2344 ArtMethod** sp) REQUIRES_SHARED(Locks::mutator_lock_) {
2345 // For static, this_object is not required and may be random garbage. Don't pass it down so that
2346 // it doesn't cause ObjPtr alignment failure check.
2347 return artInvokeCommon<kStatic, true>(method_idx, nullptr, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002348}
2349
Andreas Gampec200a4a2014-06-16 18:39:09 -07002350extern "C" TwoWordReturn artInvokeSuperTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002351 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002352 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002353 return artInvokeCommon<kSuper, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002354}
2355
Andreas Gampec200a4a2014-06-16 18:39:09 -07002356extern "C" TwoWordReturn artInvokeVirtualTrampolineWithAccessCheck(
Mathieu Chartiere401d142015-04-22 13:56:20 -07002357 uint32_t method_idx, mirror::Object* this_object, Thread* self, ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002358 REQUIRES_SHARED(Locks::mutator_lock_) {
Nicolas Geoffray7ea6a172015-05-19 18:58:54 +01002359 return artInvokeCommon<kVirtual, true>(method_idx, this_object, self, sp);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002360}
2361
Jeff Hao5667f562017-02-27 19:32:01 -08002362// Determine target of interface dispatch. The interface method and this object are known non-null.
2363// The interface method is the method returned by the dex cache in the conflict trampoline.
2364extern "C" TwoWordReturn artInvokeInterfaceTrampoline(ArtMethod* interface_method,
Mathieu Chartieref41db72016-10-25 15:08:01 -07002365 mirror::Object* raw_this_object,
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002366 Thread* self,
2367 ArtMethod** sp)
Andreas Gampebdf7f1c2016-08-30 16:38:47 -07002368 REQUIRES_SHARED(Locks::mutator_lock_) {
Jeff Hao5667f562017-02-27 19:32:01 -08002369 CHECK(interface_method != nullptr);
Mathieu Chartieref41db72016-10-25 15:08:01 -07002370 ObjPtr<mirror::Object> this_object(raw_this_object);
Ian Rogers1d8cdbc2014-09-22 22:51:09 -07002371 ScopedQuickEntrypointChecks sqec(self);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002372 StackHandleScope<1> hs(self);
2373 Handle<mirror::Class> cls(hs.NewHandle(this_object->GetClass()));
2374
Nicolas Geoffray5bf7bac2016-07-06 14:18:23 +00002375 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002376 ArtMethod* method = nullptr;
Andreas Gampe542451c2016-07-26 09:02:02 -07002377 ImTable* imt = cls->GetImt(kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002378
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002379 if (LIKELY(interface_method->GetDexMethodIndex() != DexFile::kDexNoIndex)) {
Jeff Hao5667f562017-02-27 19:32:01 -08002380 // If the interface method is already resolved, look whether we have a match in the
2381 // ImtConflictTable.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002382 ArtMethod* conflict_method = imt->Get(ImTable::GetImtIndex(interface_method),
2383 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002384 if (LIKELY(conflict_method->IsRuntimeMethod())) {
Andreas Gampe542451c2016-07-26 09:02:02 -07002385 ImtConflictTable* current_table = conflict_method->GetImtConflictTable(kRuntimePointerSize);
Mathieu Chartiere42888f2016-04-14 10:49:19 -07002386 DCHECK(current_table != nullptr);
Andreas Gampe542451c2016-07-26 09:02:02 -07002387 method = current_table->Lookup(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002388 } else {
2389 // It seems we aren't really a conflict method!
Andreas Gampe542451c2016-07-26 09:02:02 -07002390 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002391 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002392 if (method != nullptr) {
2393 return GetTwoWordSuccessValue(
2394 reinterpret_cast<uintptr_t>(method->GetEntryPointFromQuickCompiledCode()),
2395 reinterpret_cast<uintptr_t>(method));
2396 }
2397
2398 // No match, use the IfTable.
Andreas Gampe542451c2016-07-26 09:02:02 -07002399 method = cls->FindVirtualMethodForInterface(interface_method, kRuntimePointerSize);
Ian Rogerse0a02da2014-12-02 14:10:53 -08002400 if (UNLIKELY(method == nullptr)) {
Mathieu Chartiere401d142015-04-22 13:56:20 -07002401 ThrowIncompatibleClassChangeErrorClassForInterfaceDispatch(
2402 interface_method, this_object, caller_method);
Andreas Gamped58342c2014-06-05 14:18:08 -07002403 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002404 }
2405 } else {
Jeff Hao5667f562017-02-27 19:32:01 -08002406 // The interface method is unresolved, so look it up in the dex file of the caller.
Mathieu Chartier4edd8472015-06-01 10:47:36 -07002407 DCHECK_EQ(interface_method, Runtime::Current()->GetResolutionMethod());
Jeff Hao5667f562017-02-27 19:32:01 -08002408
2409 // Fetch the dex_method_idx of the target interface method from the caller.
2410 uint32_t dex_method_idx;
2411 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2412 const DexFile::CodeItem* code_item = caller_method->GetCodeItem();
2413 DCHECK_LT(dex_pc, code_item->insns_size_in_code_units_);
2414 const Instruction* instr = Instruction::At(&code_item->insns_[dex_pc]);
2415 Instruction::Code instr_code = instr->Opcode();
2416 DCHECK(instr_code == Instruction::INVOKE_INTERFACE ||
2417 instr_code == Instruction::INVOKE_INTERFACE_RANGE)
2418 << "Unexpected call into interface trampoline: " << instr->DumpString(nullptr);
2419 if (instr_code == Instruction::INVOKE_INTERFACE) {
2420 dex_method_idx = instr->VRegB_35c();
2421 } else {
2422 DCHECK_EQ(instr_code, Instruction::INVOKE_INTERFACE_RANGE);
2423 dex_method_idx = instr->VRegB_3rc();
2424 }
2425
Andreas Gampec200a4a2014-06-16 18:39:09 -07002426 const DexFile* dex_file = caller_method->GetDeclaringClass()->GetDexCache()
2427 ->GetDexFile();
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002428 uint32_t shorty_len;
Andreas Gampec200a4a2014-06-16 18:39:09 -07002429 const char* shorty = dex_file->GetMethodShorty(dex_file->GetMethodId(dex_method_idx),
2430 &shorty_len);
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002431 {
2432 // Remember the args in case a GC happens in FindMethodFromCode.
2433 ScopedObjectAccessUnchecked soa(self->GetJniEnv());
2434 RememberForGcArgumentVisitor visitor(sp, false, shorty, shorty_len, &soa);
2435 visitor.VisitArguments();
Mathieu Chartieref41db72016-10-25 15:08:01 -07002436 method = FindMethodFromCode<kInterface, false>(dex_method_idx,
2437 &this_object,
2438 caller_method,
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002439 self);
2440 visitor.FixupReferences();
2441 }
2442
2443 if (UNLIKELY(method == nullptr)) {
2444 CHECK(self->IsExceptionPending());
Andreas Gamped58342c2014-06-05 14:18:08 -07002445 return GetTwoWordFailureValue(); // Failure.
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002446 }
Andreas Gampe542451c2016-07-26 09:02:02 -07002447 interface_method =
2448 caller_method->GetDexCacheResolvedMethod(dex_method_idx, kRuntimePointerSize);
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002449 DCHECK(!interface_method->IsRuntimeMethod());
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002450 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002451
2452 // We arrive here if we have found an implementation, and it is not in the ImtConflictTable.
2453 // We create a new table with the new pair { interface_method, method }.
Andreas Gampe75a7db62016-09-26 12:04:26 -07002454 uint32_t imt_index = ImTable::GetImtIndex(interface_method);
Andreas Gampe542451c2016-07-26 09:02:02 -07002455 ArtMethod* conflict_method = imt->Get(imt_index, kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002456 if (conflict_method->IsRuntimeMethod()) {
Mathieu Chartier7f98c9a2016-04-14 10:49:19 -07002457 ArtMethod* new_conflict_method = Runtime::Current()->GetClassLinker()->AddMethodToConflictTable(
2458 cls.Get(),
2459 conflict_method,
2460 interface_method,
2461 method,
2462 /*force_new_conflict_method*/false);
2463 if (new_conflict_method != conflict_method) {
Alex Light9fc547a2016-03-31 14:34:33 -07002464 // Update the IMT if we create a new conflict method. No fence needed here, as the
2465 // data is consistent.
Matthew Gharrity465ecc82016-07-19 21:32:52 +00002466 imt->Set(imt_index,
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00002467 new_conflict_method,
Andreas Gampe542451c2016-07-26 09:02:02 -07002468 kRuntimePointerSize);
Alex Light9fc547a2016-03-31 14:34:33 -07002469 }
Nicolas Geoffray796d6302016-03-13 22:22:31 +00002470 }
2471
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002472 const void* code = method->GetEntryPointFromQuickCompiledCode();
2473
2474 // When we return, the caller will branch to this address, so it had better not be 0!
David Sehr709b0702016-10-13 09:12:37 -07002475 DCHECK(code != nullptr) << "Code was null in method: " << method->PrettyMethod()
Andreas Gampec200a4a2014-06-16 18:39:09 -07002476 << " location: " << method->GetDexFile()->GetLocation();
Andreas Gampe51f76352014-05-21 08:28:48 -07002477
Andreas Gamped58342c2014-06-05 14:18:08 -07002478 return GetTwoWordSuccessValue(reinterpret_cast<uintptr_t>(code),
2479 reinterpret_cast<uintptr_t>(method));
Mathieu Chartier5f3ded42014-04-03 15:25:30 -07002480}
2481
Orion Hodsonac141392017-01-13 11:53:47 +00002482// Returns shorty type so the caller can determine how to put |result|
2483// into expected registers. The shorty type is static so the compiler
2484// could call different flavors of this code path depending on the
2485// shorty type though this would require different entry points for
2486// each type.
2487extern "C" uintptr_t artInvokePolymorphic(
2488 JValue* result,
2489 mirror::Object* raw_method_handle,
2490 Thread* self,
2491 ArtMethod** sp)
2492 REQUIRES_SHARED(Locks::mutator_lock_) {
2493 ScopedQuickEntrypointChecks sqec(self);
Andreas Gampe8228cdf2017-05-30 15:03:54 -07002494 DCHECK_EQ(*sp, Runtime::Current()->GetCalleeSaveMethod(CalleeSaveType::kSaveRefsAndArgs));
Orion Hodsonac141392017-01-13 11:53:47 +00002495
2496 // Start new JNI local reference state
2497 JNIEnvExt* env = self->GetJniEnv();
2498 ScopedObjectAccessUnchecked soa(env);
2499 ScopedJniEnvLocalRefState env_state(env);
2500 const char* old_cause = self->StartAssertNoThreadSuspension("Making stack arguments safe.");
2501
2502 // From the instruction, get the |callsite_shorty| and expose arguments on the stack to the GC.
2503 ArtMethod* caller_method = QuickArgumentVisitor::GetCallingMethod(sp);
2504 uint32_t dex_pc = QuickArgumentVisitor::GetCallingDexPc(sp);
2505 const DexFile::CodeItem* code = caller_method->GetCodeItem();
2506 const Instruction* inst = Instruction::At(&code->insns_[dex_pc]);
2507 DCHECK(inst->Opcode() == Instruction::INVOKE_POLYMORPHIC ||
2508 inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2509 const DexFile* dex_file = caller_method->GetDexFile();
2510 const uint32_t proto_idx = inst->VRegH();
2511 const char* shorty = dex_file->GetShorty(proto_idx);
2512 const size_t shorty_length = strlen(shorty);
2513 static const bool kMethodIsStatic = false; // invoke() and invokeExact() are not static.
2514 RememberForGcArgumentVisitor gc_visitor(sp, kMethodIsStatic, shorty, shorty_length, &soa);
Orion Hodsonfea84dd2017-01-16 13:52:20 +00002515 gc_visitor.VisitArguments();
Orion Hodsonac141392017-01-13 11:53:47 +00002516
2517 // Wrap raw_method_handle in a Handle for safety.
2518 StackHandleScope<5> hs(self);
Orion Hodsonc069a302017-01-18 09:23:12 +00002519 Handle<mirror::MethodHandle> method_handle(
2520 hs.NewHandle(ObjPtr<mirror::MethodHandle>::DownCast(MakeObjPtr(raw_method_handle))));
Orion Hodsonac141392017-01-13 11:53:47 +00002521 raw_method_handle = nullptr;
2522 self->EndAssertNoThreadSuspension(old_cause);
2523
2524 // Resolve method - it's either MethodHandle.invoke() or MethodHandle.invokeExact().
2525 ClassLinker* linker = Runtime::Current()->GetClassLinker();
2526 ArtMethod* resolved_method = linker->ResolveMethod<ClassLinker::kForceICCECheck>(self,
2527 inst->VRegB(),
2528 caller_method,
2529 kVirtual);
2530 DCHECK((resolved_method ==
2531 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invokeExact)) ||
2532 (resolved_method ==
2533 jni::DecodeArtMethod(WellKnownClasses::java_lang_invoke_MethodHandle_invoke)));
2534 if (UNLIKELY(method_handle.IsNull())) {
2535 ThrowNullPointerExceptionForMethodAccess(resolved_method, InvokeType::kVirtual);
2536 return static_cast<uintptr_t>('V');
2537 }
2538
2539 Handle<mirror::Class> caller_class(hs.NewHandle(caller_method->GetDeclaringClass()));
2540 Handle<mirror::MethodType> method_type(hs.NewHandle(linker->ResolveMethodType(
2541 *dex_file, proto_idx,
2542 hs.NewHandle<mirror::DexCache>(caller_class->GetDexCache()),
2543 hs.NewHandle<mirror::ClassLoader>(caller_class->GetClassLoader()))));
2544 // This implies we couldn't resolve one or more types in this method handle.
2545 if (UNLIKELY(method_type.IsNull())) {
2546 CHECK(self->IsExceptionPending());
2547 return static_cast<uintptr_t>('V');
2548 }
2549
2550 DCHECK_EQ(ArtMethod::NumArgRegisters(shorty) + 1u, (uint32_t)inst->VRegA());
2551 DCHECK_EQ(resolved_method->IsStatic(), kMethodIsStatic);
2552
2553 // Fix references before constructing the shadow frame.
2554 gc_visitor.FixupReferences();
2555
2556 // Construct shadow frame placing arguments consecutively from |first_arg|.
2557 const bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
2558 const size_t num_vregs = is_range ? inst->VRegA_4rcc() : inst->VRegA_45cc();
2559 const size_t first_arg = 0;
2560 ShadowFrameAllocaUniquePtr shadow_frame_unique_ptr =
2561 CREATE_SHADOW_FRAME(num_vregs, /* link */ nullptr, resolved_method, dex_pc);
2562 ShadowFrame* shadow_frame = shadow_frame_unique_ptr.get();
2563 ScopedStackedShadowFramePusher
2564 frame_pusher(self, shadow_frame, StackedShadowFrameType::kShadowFrameUnderConstruction);
2565 BuildQuickShadowFrameVisitor shadow_frame_builder(sp,
2566 kMethodIsStatic,
2567 shorty,
2568 strlen(shorty),
2569 shadow_frame,
2570 first_arg);
2571 shadow_frame_builder.VisitArguments();
2572
2573 // Push a transition back into managed code onto the linked list in thread.
2574 ManagedStack fragment;
2575 self->PushManagedStackFragment(&fragment);
2576
2577 // Call DoInvokePolymorphic with |is_range| = true, as shadow frame has argument registers in
2578 // consecutive order.
2579 uint32_t unused_args[Instruction::kMaxVarArgRegs] = {};
2580 uint32_t first_callee_arg = first_arg + 1;
Orion Hodsonc069a302017-01-18 09:23:12 +00002581 if (!DoInvokePolymorphic<true /* is_range */>(self,
2582 resolved_method,
2583 *shadow_frame,
2584 method_handle,
2585 method_type,
2586 unused_args,
2587 first_callee_arg,
2588 result)) {
Orion Hodsonac141392017-01-13 11:53:47 +00002589 DCHECK(self->IsExceptionPending());
2590 }
2591
2592 // Pop transition record.
2593 self->PopManagedStackFragment(fragment);
2594
2595 return static_cast<uintptr_t>(shorty[0]);
2596}
2597
Ian Rogers848871b2013-08-05 10:56:33 -07002598} // namespace art