1 /*
2 * Copyright (C) 2016 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
17 /*
18 * Mterp entry point and support functions.
19 */
20 #include "mterp.h"
21
22 #include "base/quasi_atomic.h"
23 #include "debugger.h"
24 #include "entrypoints/entrypoint_utils-inl.h"
25 #include "interpreter/interpreter_common.h"
26 #include "interpreter/interpreter_intrinsics.h"
27 #include "interpreter/shadow_frame-inl.h"
28 #include "mirror/string-alloc-inl.h"
29
30 namespace art {
31 namespace interpreter {
32 /*
33 * Verify some constants used by the mterp interpreter.
34 */
CheckMterpAsmConstants()35 void CheckMterpAsmConstants() {
36 /*
37 * If we're using computed goto instruction transitions, make sure
38 * none of the handlers overflows the byte limit. This won't tell
39 * which one did, but if any one is too big the total size will
40 * overflow.
41 */
42 const int width = kMterpHandlerSize;
43 int interp_size = (uintptr_t) artMterpAsmInstructionEnd -
44 (uintptr_t) artMterpAsmInstructionStart;
45 if ((interp_size == 0) || (interp_size != (art::kNumPackedOpcodes * width))) {
46 LOG(FATAL) << "ERROR: unexpected asm interp size " << interp_size
47 << "(did an instruction handler exceed " << width << " bytes?)";
48 }
49 }
50
InitMterpTls(Thread * self)51 void InitMterpTls(Thread* self) {
52 self->SetMterpCurrentIBase(artMterpAsmInstructionStart);
53 }
54
55 /*
56 * Find the matching case. Returns the offset to the handler instructions.
57 *
58 * Returns 3 if we don't find a match (it's the size of the sparse-switch
59 * instruction).
60 */
MterpDoSparseSwitch(const uint16_t * switchData,int32_t testVal)61 extern "C" ssize_t MterpDoSparseSwitch(const uint16_t* switchData, int32_t testVal) {
62 const int kInstrLen = 3;
63 uint16_t size;
64 const int32_t* keys;
65 const int32_t* entries;
66
67 /*
68 * Sparse switch data format:
69 * ushort ident = 0x0200 magic value
70 * ushort size number of entries in the table; > 0
71 * int keys[size] keys, sorted low-to-high; 32-bit aligned
72 * int targets[size] branch targets, relative to switch opcode
73 *
74 * Total size is (2+size*4) 16-bit code units.
75 */
76
77 uint16_t signature = *switchData++;
78 DCHECK_EQ(signature, static_cast<uint16_t>(art::Instruction::kSparseSwitchSignature));
79
80 size = *switchData++;
81
82 /* The keys are guaranteed to be aligned on a 32-bit boundary;
83 * we can treat them as a native int array.
84 */
85 keys = reinterpret_cast<const int32_t*>(switchData);
86
87 /* The entries are guaranteed to be aligned on a 32-bit boundary;
88 * we can treat them as a native int array.
89 */
90 entries = keys + size;
91
92 /*
93 * Binary-search through the array of keys, which are guaranteed to
94 * be sorted low-to-high.
95 */
96 int lo = 0;
97 int hi = size - 1;
98 while (lo <= hi) {
99 int mid = (lo + hi) >> 1;
100
101 int32_t foundVal = keys[mid];
102 if (testVal < foundVal) {
103 hi = mid - 1;
104 } else if (testVal > foundVal) {
105 lo = mid + 1;
106 } else {
107 return entries[mid];
108 }
109 }
110 return kInstrLen;
111 }
112
MterpDoPackedSwitch(const uint16_t * switchData,int32_t testVal)113 extern "C" ssize_t MterpDoPackedSwitch(const uint16_t* switchData, int32_t testVal) {
114 const int kInstrLen = 3;
115
116 /*
117 * Packed switch data format:
118 * ushort ident = 0x0100 magic value
119 * ushort size number of entries in the table
120 * int first_key first (and lowest) switch case value
121 * int targets[size] branch targets, relative to switch opcode
122 *
123 * Total size is (4+size*2) 16-bit code units.
124 */
125 uint16_t signature = *switchData++;
126 DCHECK_EQ(signature, static_cast<uint16_t>(art::Instruction::kPackedSwitchSignature));
127
128 uint16_t size = *switchData++;
129
130 int32_t firstKey = *switchData++;
131 firstKey |= (*switchData++) << 16;
132
133 int index = testVal - firstKey;
134 if (index < 0 || index >= size) {
135 return kInstrLen;
136 }
137
138 /*
139 * The entries are guaranteed to be aligned on a 32-bit boundary;
140 * we can treat them as a native int array.
141 */
142 const int32_t* entries = reinterpret_cast<const int32_t*>(switchData);
143 return entries[index];
144 }
145
CanUseMterp()146 bool CanUseMterp()
147 REQUIRES_SHARED(Locks::mutator_lock_) {
148 const Runtime* const runtime = Runtime::Current();
149 return
150 !runtime->IsAotCompiler() &&
151 !runtime->GetInstrumentation()->IsActive() &&
152 // mterp only knows how to deal with the normal exits. It cannot handle any of the
153 // non-standard force-returns.
154 !runtime->AreNonStandardExitsEnabled() &&
155 // An async exception has been thrown. We need to go to the switch interpreter. MTerp doesn't
156 // know how to deal with these so we could end up never dealing with it if we are in an
157 // infinite loop.
158 !runtime->AreAsyncExceptionsThrown() &&
159 (runtime->GetJit() == nullptr || !runtime->GetJit()->JitAtFirstUse());
160 }
161
162 #define MTERP_INVOKE(Name) \
163 extern "C" size_t MterpInvoke##Name(Thread* self, \
164 ShadowFrame* shadow_frame, \
165 uint16_t* dex_pc_ptr, \
166 uint16_t inst_data) \
167 REQUIRES_SHARED(Locks::mutator_lock_) { \
168 JValue* result_register = shadow_frame->GetResultRegister(); \
169 const Instruction* inst = Instruction::At(dex_pc_ptr); \
170 if (shadow_frame->GetMethod()->SkipAccessChecks()) { \
171 return DoInvoke<k##Name, /*is_range=*/ false, /*do_access_check=*/ false, /*is_mterp=*/ true>( \
172 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u; \
173 } else { \
174 return DoInvoke<k##Name, /*is_range=*/ false, /*do_access_check=*/ true, /*is_mterp=*/ true>( \
175 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u; \
176 } \
177 } \
178 extern "C" size_t MterpInvoke##Name##Range(Thread* self, \
179 ShadowFrame* shadow_frame, \
180 uint16_t* dex_pc_ptr, \
181 uint16_t inst_data) \
182 REQUIRES_SHARED(Locks::mutator_lock_) { \
183 JValue* result_register = shadow_frame->GetResultRegister(); \
184 const Instruction* inst = Instruction::At(dex_pc_ptr); \
185 if (shadow_frame->GetMethod()->SkipAccessChecks()) { \
186 return DoInvoke<k##Name, /*is_range=*/ true, /*do_access_check=*/ false, /*is_mterp=*/ true>( \
187 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u; \
188 } else { \
189 return DoInvoke<k##Name, /*is_range=*/ true, /*do_access_check=*/ true, /*is_mterp=*/ true>( \
190 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u; \
191 } \
192 }
193
194 MTERP_INVOKE(Virtual)
MTERP_INVOKE(Super)195 MTERP_INVOKE(Super)
196 MTERP_INVOKE(Interface)
197 MTERP_INVOKE(Direct)
198 MTERP_INVOKE(Static)
199
200 #undef MTERP_INVOKE
201
202 extern "C" size_t MterpInvokeCustom(Thread* self,
203 ShadowFrame* shadow_frame,
204 uint16_t* dex_pc_ptr,
205 uint16_t inst_data)
206 REQUIRES_SHARED(Locks::mutator_lock_) {
207 JValue* result_register = shadow_frame->GetResultRegister();
208 const Instruction* inst = Instruction::At(dex_pc_ptr);
209 return DoInvokeCustom</* is_range= */ false>(
210 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
211 }
212
MterpInvokePolymorphic(Thread * self,ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,uint16_t inst_data)213 extern "C" size_t MterpInvokePolymorphic(Thread* self,
214 ShadowFrame* shadow_frame,
215 uint16_t* dex_pc_ptr,
216 uint16_t inst_data)
217 REQUIRES_SHARED(Locks::mutator_lock_) {
218 JValue* result_register = shadow_frame->GetResultRegister();
219 const Instruction* inst = Instruction::At(dex_pc_ptr);
220 return DoInvokePolymorphic</* is_range= */ false>(
221 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
222 }
223
MterpInvokeCustomRange(Thread * self,ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,uint16_t inst_data)224 extern "C" size_t MterpInvokeCustomRange(Thread* self,
225 ShadowFrame* shadow_frame,
226 uint16_t* dex_pc_ptr,
227 uint16_t inst_data)
228 REQUIRES_SHARED(Locks::mutator_lock_) {
229 JValue* result_register = shadow_frame->GetResultRegister();
230 const Instruction* inst = Instruction::At(dex_pc_ptr);
231 return DoInvokeCustom</*is_range=*/ true>(
232 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
233 }
234
MterpInvokePolymorphicRange(Thread * self,ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,uint16_t inst_data)235 extern "C" size_t MterpInvokePolymorphicRange(Thread* self,
236 ShadowFrame* shadow_frame,
237 uint16_t* dex_pc_ptr,
238 uint16_t inst_data)
239 REQUIRES_SHARED(Locks::mutator_lock_) {
240 JValue* result_register = shadow_frame->GetResultRegister();
241 const Instruction* inst = Instruction::At(dex_pc_ptr);
242 return DoInvokePolymorphic</* is_range= */ true>(
243 self, *shadow_frame, inst, inst_data, result_register) ? 1u : 0u;
244 }
245
MterpThreadFenceForConstructor()246 extern "C" void MterpThreadFenceForConstructor() {
247 QuasiAtomic::ThreadFenceForConstructor();
248 }
249
MterpConstString(uint32_t index,uint32_t tgt_vreg,ShadowFrame * shadow_frame,Thread * self)250 extern "C" size_t MterpConstString(uint32_t index,
251 uint32_t tgt_vreg,
252 ShadowFrame* shadow_frame,
253 Thread* self)
254 REQUIRES_SHARED(Locks::mutator_lock_) {
255 ObjPtr<mirror::String> s = ResolveString(self, *shadow_frame, dex::StringIndex(index));
256 if (UNLIKELY(s == nullptr)) {
257 return 1u;
258 }
259 shadow_frame->SetVRegReference(tgt_vreg, s);
260 return 0u;
261 }
262
MterpConstClass(uint32_t index,uint32_t tgt_vreg,ShadowFrame * shadow_frame,Thread * self)263 extern "C" size_t MterpConstClass(uint32_t index,
264 uint32_t tgt_vreg,
265 ShadowFrame* shadow_frame,
266 Thread* self)
267 REQUIRES_SHARED(Locks::mutator_lock_) {
268 ObjPtr<mirror::Class> c =
269 ResolveVerifyAndClinit(dex::TypeIndex(index),
270 shadow_frame->GetMethod(),
271 self,
272 /* can_run_clinit= */ false,
273 !shadow_frame->GetMethod()->SkipAccessChecks());
274 if (UNLIKELY(c == nullptr)) {
275 return 1u;
276 }
277 shadow_frame->SetVRegReference(tgt_vreg, c);
278 return 0u;
279 }
280
MterpConstMethodHandle(uint32_t index,uint32_t tgt_vreg,ShadowFrame * shadow_frame,Thread * self)281 extern "C" size_t MterpConstMethodHandle(uint32_t index,
282 uint32_t tgt_vreg,
283 ShadowFrame* shadow_frame,
284 Thread* self)
285 REQUIRES_SHARED(Locks::mutator_lock_) {
286 ObjPtr<mirror::MethodHandle> mh = ResolveMethodHandle(self, index, shadow_frame->GetMethod());
287 if (UNLIKELY(mh == nullptr)) {
288 return 1u;
289 }
290 shadow_frame->SetVRegReference(tgt_vreg, mh);
291 return 0u;
292 }
293
MterpConstMethodType(uint32_t index,uint32_t tgt_vreg,ShadowFrame * shadow_frame,Thread * self)294 extern "C" size_t MterpConstMethodType(uint32_t index,
295 uint32_t tgt_vreg,
296 ShadowFrame* shadow_frame,
297 Thread* self)
298 REQUIRES_SHARED(Locks::mutator_lock_) {
299 ObjPtr<mirror::MethodType> mt =
300 ResolveMethodType(self, dex::ProtoIndex(index), shadow_frame->GetMethod());
301 if (UNLIKELY(mt == nullptr)) {
302 return 1u;
303 }
304 shadow_frame->SetVRegReference(tgt_vreg, mt);
305 return 0u;
306 }
307
MterpCheckCast(uint32_t index,StackReference<mirror::Object> * vreg_addr,art::ArtMethod * method,Thread * self)308 extern "C" size_t MterpCheckCast(uint32_t index,
309 StackReference<mirror::Object>* vreg_addr,
310 art::ArtMethod* method,
311 Thread* self)
312 REQUIRES_SHARED(Locks::mutator_lock_) {
313 ObjPtr<mirror::Class> c =
314 ResolveVerifyAndClinit(dex::TypeIndex(index),
315 method,
316 self,
317 /* can_run_clinit= */ false,
318 !method->SkipAccessChecks());
319 if (UNLIKELY(c == nullptr)) {
320 return 1u;
321 }
322 // Must load obj from vreg following ResolveVerifyAndClinit due to moving gc.
323 ObjPtr<mirror::Object> obj = vreg_addr->AsMirrorPtr();
324 if (UNLIKELY(obj != nullptr && !obj->InstanceOf(c))) {
325 ThrowClassCastException(c, obj->GetClass());
326 return 1u;
327 }
328 return 0u;
329 }
330
MterpInstanceOf(uint32_t index,StackReference<mirror::Object> * vreg_addr,art::ArtMethod * method,Thread * self)331 extern "C" size_t MterpInstanceOf(uint32_t index,
332 StackReference<mirror::Object>* vreg_addr,
333 art::ArtMethod* method,
334 Thread* self)
335 REQUIRES_SHARED(Locks::mutator_lock_) {
336 ObjPtr<mirror::Class> c = ResolveVerifyAndClinit(dex::TypeIndex(index),
337 method,
338 self,
339 /* can_run_clinit= */ false,
340 !method->SkipAccessChecks());
341 if (UNLIKELY(c == nullptr)) {
342 return 0u; // Caller will check for pending exception. Return value unimportant.
343 }
344 // Must load obj from vreg following ResolveVerifyAndClinit due to moving gc.
345 ObjPtr<mirror::Object> obj = vreg_addr->AsMirrorPtr();
346 return (obj != nullptr) && obj->InstanceOf(c) ? 1u : 0u;
347 }
348
MterpFillArrayData(mirror::Object * obj,const Instruction::ArrayDataPayload * payload)349 extern "C" size_t MterpFillArrayData(mirror::Object* obj,
350 const Instruction::ArrayDataPayload* payload)
351 REQUIRES_SHARED(Locks::mutator_lock_) {
352 return FillArrayData(obj, payload) ? 1u : 0u;
353 }
354
MterpNewInstance(ShadowFrame * shadow_frame,Thread * self,uint32_t inst_data)355 extern "C" size_t MterpNewInstance(ShadowFrame* shadow_frame, Thread* self, uint32_t inst_data)
356 REQUIRES_SHARED(Locks::mutator_lock_) {
357 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
358 ObjPtr<mirror::Object> obj = nullptr;
359 ObjPtr<mirror::Class> c =
360 ResolveVerifyAndClinit(dex::TypeIndex(inst->VRegB_21c()),
361 shadow_frame->GetMethod(),
362 self,
363 /* can_run_clinit= */ false,
364 !shadow_frame->GetMethod()->SkipAccessChecks());
365 if (LIKELY(c != nullptr)) {
366 if (UNLIKELY(c->IsStringClass())) {
367 gc::AllocatorType allocator_type = Runtime::Current()->GetHeap()->GetCurrentAllocator();
368 obj = mirror::String::AllocEmptyString(self, allocator_type);
369 } else {
370 obj = AllocObjectFromCode(c, self, Runtime::Current()->GetHeap()->GetCurrentAllocator());
371 }
372 }
373 if (UNLIKELY(obj == nullptr)) {
374 return 0u;
375 }
376 obj->GetClass()->AssertInitializedOrInitializingInThread(self);
377 shadow_frame->SetVRegReference(inst->VRegA_21c(inst_data), obj);
378 return 1u;
379 }
380
MterpAputObject(ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,uint32_t inst_data)381 extern "C" size_t MterpAputObject(ShadowFrame* shadow_frame,
382 uint16_t* dex_pc_ptr,
383 uint32_t inst_data)
384 REQUIRES_SHARED(Locks::mutator_lock_) {
385 const Instruction* inst = Instruction::At(dex_pc_ptr);
386 ObjPtr<mirror::Object> a = shadow_frame->GetVRegReference(inst->VRegB_23x());
387 if (UNLIKELY(a == nullptr)) {
388 return 0u;
389 }
390 int32_t index = shadow_frame->GetVReg(inst->VRegC_23x());
391 ObjPtr<mirror::Object> val = shadow_frame->GetVRegReference(inst->VRegA_23x(inst_data));
392 ObjPtr<mirror::ObjectArray<mirror::Object>> array = a->AsObjectArray<mirror::Object>();
393 if (array->CheckIsValidIndex(index) && array->CheckAssignable(val)) {
394 array->SetWithoutChecks<false>(index, val);
395 return 1u;
396 }
397 return 0u;
398 }
399
MterpFilledNewArray(ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,Thread * self)400 extern "C" size_t MterpFilledNewArray(ShadowFrame* shadow_frame,
401 uint16_t* dex_pc_ptr,
402 Thread* self)
403 REQUIRES_SHARED(Locks::mutator_lock_) {
404 const Instruction* inst = Instruction::At(dex_pc_ptr);
405 JValue* result_register = shadow_frame->GetResultRegister();
406 bool res = false;
407 if (shadow_frame->GetMethod()->SkipAccessChecks()) {
408 res = DoFilledNewArray</*is_range=*/false,
409 /*do_access_check=*/false,
410 /*transaction_active=*/false>(inst,
411 *shadow_frame,
412 self,
413 result_register);
414 } else {
415 res = DoFilledNewArray</*is_range=*/false,
416 /*do_access_check=*/true,
417 /*transaction_active=*/false>(inst,
418 *shadow_frame,
419 self,
420 result_register);
421 }
422 return res ? 1u : 0u;
423 }
424
MterpFilledNewArrayRange(ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,Thread * self)425 extern "C" size_t MterpFilledNewArrayRange(ShadowFrame* shadow_frame,
426 uint16_t* dex_pc_ptr,
427 Thread* self)
428 REQUIRES_SHARED(Locks::mutator_lock_) {
429 const Instruction* inst = Instruction::At(dex_pc_ptr);
430 JValue* result_register = shadow_frame->GetResultRegister();
431 bool res = false;
432 if (shadow_frame->GetMethod()->SkipAccessChecks()) {
433 res = DoFilledNewArray</*is_range=*/true,
434 /*do_access_check=*/false,
435 /*transaction_active=*/false>(inst,
436 *shadow_frame,
437 self,
438 result_register);
439 } else {
440 res = DoFilledNewArray</*is_range=*/true,
441 /*do_access_check=*/true,
442 /*transaction_active=*/false>(inst,
443 *shadow_frame,
444 self,
445 result_register);
446 }
447 return res ? 1u : 0u;
448 }
449
MterpNewArray(ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr,uint32_t inst_data,Thread * self)450 extern "C" size_t MterpNewArray(ShadowFrame* shadow_frame,
451 uint16_t* dex_pc_ptr,
452 uint32_t inst_data, Thread* self)
453 REQUIRES_SHARED(Locks::mutator_lock_) {
454 const Instruction* inst = Instruction::At(dex_pc_ptr);
455 int32_t length = shadow_frame->GetVReg(inst->VRegB_22c(inst_data));
456 gc::AllocatorType allocator = Runtime::Current()->GetHeap()->GetCurrentAllocator();
457 ObjPtr<mirror::Object> obj;
458 if (shadow_frame->GetMethod()->SkipAccessChecks()) {
459 obj = AllocArrayFromCode</*kAccessCheck=*/ false>(dex::TypeIndex(inst->VRegC_22c()),
460 length,
461 shadow_frame->GetMethod(),
462 self,
463 allocator);
464 } else {
465 obj = AllocArrayFromCode</*kAccessCheck=*/ true>(dex::TypeIndex(inst->VRegC_22c()),
466 length,
467 shadow_frame->GetMethod(),
468 self,
469 allocator);
470 }
471 if (UNLIKELY(obj == nullptr)) {
472 return 0u;
473 }
474 shadow_frame->SetVRegReference(inst->VRegA_22c(inst_data), obj);
475 return 1u;
476 }
477
MterpHandleException(Thread * self,ShadowFrame * shadow_frame)478 extern "C" size_t MterpHandleException(Thread* self, ShadowFrame* shadow_frame)
479 REQUIRES_SHARED(Locks::mutator_lock_) {
480 DCHECK(self->IsExceptionPending());
481 const instrumentation::Instrumentation* const instrumentation =
482 Runtime::Current()->GetInstrumentation();
483 return MoveToExceptionHandler(self, *shadow_frame, instrumentation) ? 1u : 0u;
484 }
485
486 struct MterpCheckHelper {
487 DECLARE_RUNTIME_DEBUG_FLAG(kSlowMode);
488 };
489 DEFINE_RUNTIME_DEBUG_FLAG(MterpCheckHelper, kSlowMode);
490
MterpCheckBefore(Thread * self,ShadowFrame * shadow_frame,uint16_t * dex_pc_ptr)491 extern "C" void MterpCheckBefore(Thread* self, ShadowFrame* shadow_frame, uint16_t* dex_pc_ptr)
492 REQUIRES_SHARED(Locks::mutator_lock_) {
493 // Check that we are using the right interpreter.
494 if (kIsDebugBuild && self->UseMterp() != CanUseMterp()) {
495 // The flag might be currently being updated on all threads. Retry with lock.
496 MutexLock tll_mu(self, *Locks::thread_list_lock_);
497 DCHECK_EQ(self->UseMterp(), CanUseMterp());
498 }
499 DCHECK(!Runtime::Current()->IsActiveTransaction());
500 const Instruction* inst = Instruction::At(dex_pc_ptr);
501 uint16_t inst_data = inst->Fetch16(0);
502 if (inst->Opcode(inst_data) == Instruction::MOVE_EXCEPTION) {
503 self->AssertPendingException();
504 } else {
505 self->AssertNoPendingException();
506 }
507 if (kTraceExecutionEnabled) {
508 uint32_t dex_pc = dex_pc_ptr - shadow_frame->GetDexInstructions();
509 TraceExecution(*shadow_frame, inst, dex_pc);
510 }
511 if (kTestExportPC) {
512 // Save invalid dex pc to force segfault if improperly used.
513 shadow_frame->SetDexPCPtr(reinterpret_cast<uint16_t*>(kExportPCPoison));
514 }
515 if (MterpCheckHelper::kSlowMode) {
516 shadow_frame->CheckConsistentVRegs();
517 }
518 }
519
MterpLogDivideByZeroException(Thread * self,ShadowFrame * shadow_frame)520 extern "C" void MterpLogDivideByZeroException(Thread* self, ShadowFrame* shadow_frame)
521 REQUIRES_SHARED(Locks::mutator_lock_) {
522 UNUSED(self);
523 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
524 uint16_t inst_data = inst->Fetch16(0);
525 LOG(INFO) << "DivideByZero: " << inst->Opcode(inst_data);
526 }
527
MterpLogArrayIndexException(Thread * self,ShadowFrame * shadow_frame)528 extern "C" void MterpLogArrayIndexException(Thread* self, ShadowFrame* shadow_frame)
529 REQUIRES_SHARED(Locks::mutator_lock_) {
530 UNUSED(self);
531 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
532 uint16_t inst_data = inst->Fetch16(0);
533 LOG(INFO) << "ArrayIndex: " << inst->Opcode(inst_data);
534 }
535
MterpLogNegativeArraySizeException(Thread * self,ShadowFrame * shadow_frame)536 extern "C" void MterpLogNegativeArraySizeException(Thread* self, ShadowFrame* shadow_frame)
537 REQUIRES_SHARED(Locks::mutator_lock_) {
538 UNUSED(self);
539 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
540 uint16_t inst_data = inst->Fetch16(0);
541 LOG(INFO) << "NegativeArraySize: " << inst->Opcode(inst_data);
542 }
543
MterpLogNoSuchMethodException(Thread * self,ShadowFrame * shadow_frame)544 extern "C" void MterpLogNoSuchMethodException(Thread* self, ShadowFrame* shadow_frame)
545 REQUIRES_SHARED(Locks::mutator_lock_) {
546 UNUSED(self);
547 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
548 uint16_t inst_data = inst->Fetch16(0);
549 LOG(INFO) << "NoSuchMethod: " << inst->Opcode(inst_data);
550 }
551
MterpLogExceptionThrownException(Thread * self,ShadowFrame * shadow_frame)552 extern "C" void MterpLogExceptionThrownException(Thread* self, ShadowFrame* shadow_frame)
553 REQUIRES_SHARED(Locks::mutator_lock_) {
554 UNUSED(self);
555 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
556 uint16_t inst_data = inst->Fetch16(0);
557 LOG(INFO) << "ExceptionThrown: " << inst->Opcode(inst_data);
558 }
559
MterpLogNullObjectException(Thread * self,ShadowFrame * shadow_frame)560 extern "C" void MterpLogNullObjectException(Thread* self, ShadowFrame* shadow_frame)
561 REQUIRES_SHARED(Locks::mutator_lock_) {
562 UNUSED(self);
563 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
564 uint16_t inst_data = inst->Fetch16(0);
565 LOG(INFO) << "NullObject: " << inst->Opcode(inst_data);
566 }
567
MterpLogFallback(Thread * self,ShadowFrame * shadow_frame)568 extern "C" void MterpLogFallback(Thread* self, ShadowFrame* shadow_frame)
569 REQUIRES_SHARED(Locks::mutator_lock_) {
570 UNUSED(self);
571 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
572 uint16_t inst_data = inst->Fetch16(0);
573 LOG(INFO) << "Fallback: " << inst->Opcode(inst_data) << ", Suspend Pending?: "
574 << self->IsExceptionPending();
575 }
576
MterpLogOSR(Thread * self,ShadowFrame * shadow_frame,int32_t offset)577 extern "C" void MterpLogOSR(Thread* self, ShadowFrame* shadow_frame, int32_t offset)
578 REQUIRES_SHARED(Locks::mutator_lock_) {
579 UNUSED(self);
580 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
581 uint16_t inst_data = inst->Fetch16(0);
582 LOG(INFO) << "OSR: " << inst->Opcode(inst_data) << ", offset = " << offset;
583 }
584
MterpLogSuspendFallback(Thread * self,ShadowFrame * shadow_frame,uint32_t flags)585 extern "C" void MterpLogSuspendFallback(Thread* self, ShadowFrame* shadow_frame, uint32_t flags)
586 REQUIRES_SHARED(Locks::mutator_lock_) {
587 UNUSED(self);
588 const Instruction* inst = Instruction::At(shadow_frame->GetDexPCPtr());
589 uint16_t inst_data = inst->Fetch16(0);
590 if (flags & kCheckpointRequest) {
591 LOG(INFO) << "Checkpoint fallback: " << inst->Opcode(inst_data);
592 } else if (flags & kSuspendRequest) {
593 LOG(INFO) << "Suspend fallback: " << inst->Opcode(inst_data);
594 } else if (flags & kEmptyCheckpointRequest) {
595 LOG(INFO) << "Empty checkpoint fallback: " << inst->Opcode(inst_data);
596 }
597 }
598
MterpSuspendCheck(Thread * self)599 extern "C" size_t MterpSuspendCheck(Thread* self)
600 REQUIRES_SHARED(Locks::mutator_lock_) {
601 self->AllowThreadSuspension();
602 return !self->UseMterp();
603 }
604
605 // Execute single field access instruction (get/put, static/instance).
606 // The template arguments reduce this to fairly small amount of code.
607 // It requires the target object and field to be already resolved.
608 template<typename PrimType, FindFieldType kAccessType>
MterpFieldAccess(Instruction * inst,uint16_t inst_data,ShadowFrame * shadow_frame,ObjPtr<mirror::Object> obj,MemberOffset offset,bool is_volatile)609 ALWAYS_INLINE void MterpFieldAccess(Instruction* inst,
610 uint16_t inst_data,
611 ShadowFrame* shadow_frame,
612 ObjPtr<mirror::Object> obj,
613 MemberOffset offset,
614 bool is_volatile)
615 REQUIRES_SHARED(Locks::mutator_lock_) {
616 static_assert(std::is_integral<PrimType>::value, "Unexpected primitive type");
617 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
618 constexpr bool kIsPrimitive = (kAccessType & FindFieldFlags::PrimitiveBit) != 0;
619 constexpr bool kIsRead = (kAccessType & FindFieldFlags::ReadBit) != 0;
620
621 uint16_t vRegA = kIsStatic ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
622 if (kIsPrimitive) {
623 if (kIsRead) {
624 PrimType value = UNLIKELY(is_volatile)
625 ? obj->GetFieldPrimitive<PrimType, /*kIsVolatile=*/ true>(offset)
626 : obj->GetFieldPrimitive<PrimType, /*kIsVolatile=*/ false>(offset);
627 if (sizeof(PrimType) == sizeof(uint64_t)) {
628 shadow_frame->SetVRegLong(vRegA, value); // Set two consecutive registers.
629 } else {
630 shadow_frame->SetVReg(vRegA, static_cast<int32_t>(value)); // Sign/zero extend.
631 }
632 } else { // Write.
633 uint64_t value = (sizeof(PrimType) == sizeof(uint64_t))
634 ? shadow_frame->GetVRegLong(vRegA)
635 : shadow_frame->GetVReg(vRegA);
636 if (UNLIKELY(is_volatile)) {
637 obj->SetFieldPrimitive<PrimType, /*kIsVolatile=*/ true>(offset, value);
638 } else {
639 obj->SetFieldPrimitive<PrimType, /*kIsVolatile=*/ false>(offset, value);
640 }
641 }
642 } else { // Object.
643 if (kIsRead) {
644 ObjPtr<mirror::Object> value = UNLIKELY(is_volatile)
645 ? obj->GetFieldObjectVolatile<mirror::Object>(offset)
646 : obj->GetFieldObject<mirror::Object>(offset);
647 shadow_frame->SetVRegReference(vRegA, value);
648 } else { // Write.
649 ObjPtr<mirror::Object> value = shadow_frame->GetVRegReference(vRegA);
650 if (UNLIKELY(is_volatile)) {
651 obj->SetFieldObjectVolatile</*kTransactionActive=*/ false>(offset, value);
652 } else {
653 obj->SetFieldObject</*kTransactionActive=*/ false>(offset, value);
654 }
655 }
656 }
657 }
658
659 template<typename PrimType, FindFieldType kAccessType, bool do_access_checks>
MterpFieldAccessSlow(Instruction * inst,uint16_t inst_data,ShadowFrame * shadow_frame,Thread * self)660 NO_INLINE bool MterpFieldAccessSlow(Instruction* inst,
661 uint16_t inst_data,
662 ShadowFrame* shadow_frame,
663 Thread* self)
664 REQUIRES_SHARED(Locks::mutator_lock_) {
665 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
666 constexpr bool kIsRead = (kAccessType & FindFieldFlags::ReadBit) != 0;
667
668 // Update the dex pc in shadow frame, just in case anything throws.
669 shadow_frame->SetDexPCPtr(reinterpret_cast<uint16_t*>(inst));
670 ArtMethod* referrer = shadow_frame->GetMethod();
671 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
672 ArtField* field = FindFieldFromCode<kAccessType, do_access_checks>(
673 field_idx, referrer, self, sizeof(PrimType));
674 if (UNLIKELY(field == nullptr)) {
675 DCHECK(self->IsExceptionPending());
676 return false;
677 }
678 constexpr bool kIsPrimitive = (kAccessType & FindFieldFlags::PrimitiveBit) != 0;
679 if (!kIsPrimitive && !kIsRead) {
680 uint16_t vRegA = kIsStatic ? inst->VRegA_21c(inst_data) : inst->VRegA_22c(inst_data);
681 ObjPtr<mirror::Object> value = shadow_frame->GetVRegReference(vRegA);
682 if (value != nullptr && field->ResolveType() == nullptr) {
683 DCHECK(self->IsExceptionPending());
684 return false;
685 }
686 }
687 ObjPtr<mirror::Object> obj = kIsStatic
688 ? field->GetDeclaringClass().Ptr()
689 : shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data));
690 if (UNLIKELY(obj == nullptr)) {
691 ThrowNullPointerExceptionForFieldAccess(field, kIsRead);
692 return false;
693 }
694 MterpFieldAccess<PrimType, kAccessType>(
695 inst, inst_data, shadow_frame, obj, field->GetOffset(), field->IsVolatile());
696 return true;
697 }
698
699 // This methods is called from assembly to handle field access instructions.
700 template<typename PrimType, FindFieldType kAccessType, bool do_access_checks>
MterpFieldAccessFast(Instruction * inst,uint16_t inst_data,ShadowFrame * shadow_frame,Thread * self)701 ALWAYS_INLINE bool MterpFieldAccessFast(Instruction* inst,
702 uint16_t inst_data,
703 ShadowFrame* shadow_frame,
704 Thread* self)
705 REQUIRES_SHARED(Locks::mutator_lock_) {
706 constexpr bool kIsStatic = (kAccessType & FindFieldFlags::StaticBit) != 0;
707
708 // Try to find the field in small thread-local cache first.
709 InterpreterCache* tls_cache = self->GetInterpreterCache();
710 size_t tls_value;
711 if (LIKELY(tls_cache->Get(inst, &tls_value))) {
712 // The meaning of the cache value is opcode-specific.
713 // It is ArtFiled* for static fields and the raw offset for instance fields.
714 size_t offset = kIsStatic
715 ? reinterpret_cast<ArtField*>(tls_value)->GetOffset().SizeValue()
716 : tls_value;
717 if (kIsDebugBuild) {
718 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
719 ArtField* field = FindFieldFromCode<kAccessType, do_access_checks>(
720 field_idx, shadow_frame->GetMethod(), self, sizeof(PrimType));
721 DCHECK_EQ(offset, field->GetOffset().SizeValue());
722 }
723 ObjPtr<mirror::Object> obj = kIsStatic
724 ? reinterpret_cast<ArtField*>(tls_value)->GetDeclaringClass()
725 : ObjPtr<mirror::Object>(shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data)));
726 if (LIKELY(obj != nullptr)) {
727 MterpFieldAccess<PrimType, kAccessType>(
728 inst, inst_data, shadow_frame, obj, MemberOffset(offset), /* is_volatile= */ false);
729 return true;
730 }
731 }
732
733 // This effectively inlines the fast path from ArtMethod::GetDexCache.
734 ArtMethod* referrer = shadow_frame->GetMethod();
735 if (LIKELY(!referrer->IsObsolete() && !do_access_checks)) {
736 // Avoid read barriers, since we need only the pointer to the native (non-movable)
737 // DexCache field array which we can get even through from-space objects.
738 ObjPtr<mirror::Class> klass = referrer->GetDeclaringClass<kWithoutReadBarrier>();
739 ObjPtr<mirror::DexCache> dex_cache =
740 klass->GetDexCache<kDefaultVerifyFlags, kWithoutReadBarrier>();
741
742 // Try to find the desired field in DexCache.
743 uint32_t field_idx = kIsStatic ? inst->VRegB_21c() : inst->VRegC_22c();
744 ArtField* field = dex_cache->GetResolvedField(field_idx);
745 if (LIKELY(field != nullptr)) {
746 bool visibly_initialized = !kIsStatic || field->GetDeclaringClass()->IsVisiblyInitialized();
747 if (LIKELY(visibly_initialized)) {
748 DCHECK_EQ(field, (FindFieldFromCode<kAccessType, do_access_checks>(
749 field_idx, referrer, self, sizeof(PrimType))));
750 ObjPtr<mirror::Object> obj = kIsStatic
751 ? field->GetDeclaringClass().Ptr()
752 : shadow_frame->GetVRegReference(inst->VRegB_22c(inst_data));
753 // We check if nterp is supported as nterp and mterp use the cache in an
754 // incompatible way.
755 if (!IsNterpSupported() && LIKELY(kIsStatic || obj != nullptr)) {
756 // Only non-volatile fields are allowed in the thread-local cache.
757 if (LIKELY(!field->IsVolatile())) {
758 if (kIsStatic) {
759 tls_cache->Set(inst, reinterpret_cast<uintptr_t>(field));
760 } else {
761 tls_cache->Set(inst, field->GetOffset().SizeValue());
762 }
763 }
764 MterpFieldAccess<PrimType, kAccessType>(
765 inst, inst_data, shadow_frame, obj, field->GetOffset(), field->IsVolatile());
766 return true;
767 }
768 }
769 }
770 }
771
772 // Slow path. Last and with identical arguments so that it becomes single instruction tail call.
773 return MterpFieldAccessSlow<PrimType, kAccessType, do_access_checks>(
774 inst, inst_data, shadow_frame, self);
775 }
776
777 #define MTERP_FIELD_ACCESSOR(Name, PrimType, AccessType) \
778 extern "C" bool Name(Instruction* inst, uint16_t inst_data, ShadowFrame* sf, Thread* self) \
779 REQUIRES_SHARED(Locks::mutator_lock_) { \
780 if (sf->GetMethod()->SkipAccessChecks()) { \
781 return MterpFieldAccessFast<PrimType, AccessType, false>(inst, inst_data, sf, self); \
782 } else { \
783 return MterpFieldAccessFast<PrimType, AccessType, true>(inst, inst_data, sf, self); \
784 } \
785 }
786
787 #define MTERP_FIELD_ACCESSORS_FOR_TYPE(Sufix, PrimType, Kind) \
788 MTERP_FIELD_ACCESSOR(MterpIGet##Sufix, PrimType, Instance##Kind##Read) \
789 MTERP_FIELD_ACCESSOR(MterpIPut##Sufix, PrimType, Instance##Kind##Write) \
790 MTERP_FIELD_ACCESSOR(MterpSGet##Sufix, PrimType, Static##Kind##Read) \
791 MTERP_FIELD_ACCESSOR(MterpSPut##Sufix, PrimType, Static##Kind##Write)
792
793 MTERP_FIELD_ACCESSORS_FOR_TYPE(I8, int8_t, Primitive)
794 MTERP_FIELD_ACCESSORS_FOR_TYPE(U8, uint8_t, Primitive)
795 MTERP_FIELD_ACCESSORS_FOR_TYPE(I16, int16_t, Primitive)
796 MTERP_FIELD_ACCESSORS_FOR_TYPE(U16, uint16_t, Primitive)
797 MTERP_FIELD_ACCESSORS_FOR_TYPE(U32, uint32_t, Primitive)
798 MTERP_FIELD_ACCESSORS_FOR_TYPE(U64, uint64_t, Primitive)
799 MTERP_FIELD_ACCESSORS_FOR_TYPE(Obj, uint32_t, Object)
800
801 // Check that the primitive type for Obj variant above is correct.
802 // It really must be primitive type for the templates to compile.
803 // In the case of objects, it is only used to get the field size.
804 static_assert(kHeapReferenceSize == sizeof(uint32_t), "Unexpected kHeapReferenceSize");
805
806 #undef MTERP_FIELD_ACCESSORS_FOR_TYPE
807 #undef MTERP_FIELD_ACCESSOR
808
artAGetObjectFromMterp(mirror::Object * arr,int32_t index)809 extern "C" mirror::Object* artAGetObjectFromMterp(mirror::Object* arr,
810 int32_t index)
811 REQUIRES_SHARED(Locks::mutator_lock_) {
812 if (UNLIKELY(arr == nullptr)) {
813 ThrowNullPointerExceptionFromInterpreter();
814 return nullptr;
815 }
816 ObjPtr<mirror::ObjectArray<mirror::Object>> array = arr->AsObjectArray<mirror::Object>();
817 if (LIKELY(array->CheckIsValidIndex(index))) {
818 return array->GetWithoutChecks(index).Ptr();
819 } else {
820 return nullptr;
821 }
822 }
823
artIGetObjectFromMterp(mirror::Object * obj,uint32_t field_offset)824 extern "C" mirror::Object* artIGetObjectFromMterp(mirror::Object* obj,
825 uint32_t field_offset)
826 REQUIRES_SHARED(Locks::mutator_lock_) {
827 if (UNLIKELY(obj == nullptr)) {
828 ThrowNullPointerExceptionFromInterpreter();
829 return nullptr;
830 }
831 return obj->GetFieldObject<mirror::Object>(MemberOffset(field_offset));
832 }
833
834 /*
835 * Create a hotness_countdown based on the current method hotness_count and profiling
836 * mode. In short, determine how many hotness events we hit before reporting back
837 * to the full instrumentation via MterpAddHotnessBatch. Called once on entry to the method,
838 * and regenerated following batch updates.
839 */
MterpSetUpHotnessCountdown(ArtMethod * method,ShadowFrame * shadow_frame,Thread * self)840 extern "C" ssize_t MterpSetUpHotnessCountdown(ArtMethod* method,
841 ShadowFrame* shadow_frame,
842 Thread* self)
843 REQUIRES_SHARED(Locks::mutator_lock_) {
844 uint16_t hotness_count = method->GetCounter();
845 int32_t countdown_value = jit::kJitHotnessDisabled;
846 jit::Jit* jit = Runtime::Current()->GetJit();
847 if (jit != nullptr) {
848 int32_t warm_threshold = jit->WarmMethodThreshold();
849 int32_t hot_threshold = jit->HotMethodThreshold();
850 int32_t osr_threshold = jit->OSRMethodThreshold();
851 if (hotness_count < warm_threshold) {
852 countdown_value = warm_threshold - hotness_count;
853 } else if (hotness_count < hot_threshold) {
854 countdown_value = hot_threshold - hotness_count;
855 } else if (hotness_count < osr_threshold) {
856 countdown_value = osr_threshold - hotness_count;
857 } else {
858 countdown_value = jit::kJitCheckForOSR;
859 }
860 if (jit::Jit::ShouldUsePriorityThreadWeight(self)) {
861 int32_t priority_thread_weight = jit->PriorityThreadWeight();
862 countdown_value = std::min(countdown_value, countdown_value / priority_thread_weight);
863 }
864 }
865 /*
866 * The actual hotness threshold may exceed the range of our int16_t countdown value. This is
867 * not a problem, though. We can just break it down into smaller chunks.
868 */
869 countdown_value = std::min(countdown_value,
870 static_cast<int32_t>(std::numeric_limits<int16_t>::max()));
871 shadow_frame->SetCachedHotnessCountdown(countdown_value);
872 shadow_frame->SetHotnessCountdown(countdown_value);
873 return countdown_value;
874 }
875
876 /*
877 * Report a batch of hotness events to the instrumentation and then return the new
878 * countdown value to the next time we should report.
879 */
MterpAddHotnessBatch(ArtMethod * method,ShadowFrame * shadow_frame,Thread * self)880 extern "C" ssize_t MterpAddHotnessBatch(ArtMethod* method,
881 ShadowFrame* shadow_frame,
882 Thread* self)
883 REQUIRES_SHARED(Locks::mutator_lock_) {
884 jit::Jit* jit = Runtime::Current()->GetJit();
885 if (jit != nullptr) {
886 int16_t count = shadow_frame->GetCachedHotnessCountdown() - shadow_frame->GetHotnessCountdown();
887 jit->AddSamples(self, method, count, /*with_backedges=*/ true);
888 }
889 return MterpSetUpHotnessCountdown(method, shadow_frame, self);
890 }
891
MterpMaybeDoOnStackReplacement(Thread * self,ShadowFrame * shadow_frame,int32_t offset)892 extern "C" size_t MterpMaybeDoOnStackReplacement(Thread* self,
893 ShadowFrame* shadow_frame,
894 int32_t offset)
895 REQUIRES_SHARED(Locks::mutator_lock_) {
896 int16_t osr_countdown = shadow_frame->GetCachedHotnessCountdown() - 1;
897 bool did_osr = false;
898 /*
899 * To reduce the cost of polling the compiler to determine whether the requested OSR
900 * compilation has completed, only check every Nth time. NOTE: the "osr_countdown <= 0"
901 * condition is satisfied either by the decrement below or the initial setting of
902 * the cached countdown field to kJitCheckForOSR, which elsewhere is asserted to be -1.
903 */
904 if (osr_countdown <= 0) {
905 ArtMethod* method = shadow_frame->GetMethod();
906 JValue* result = shadow_frame->GetResultRegister();
907 uint32_t dex_pc = shadow_frame->GetDexPC();
908 jit::Jit* jit = Runtime::Current()->GetJit();
909 osr_countdown = jit::Jit::kJitRecheckOSRThreshold;
910 if (offset <= 0) {
911 // Keep updating hotness in case a compilation request was dropped. Eventually it will retry.
912 jit->AddSamples(self, method, osr_countdown, /*with_backedges=*/ true);
913 }
914 did_osr = jit::Jit::MaybeDoOnStackReplacement(self, method, dex_pc, offset, result);
915 }
916 shadow_frame->SetCachedHotnessCountdown(osr_countdown);
917 return did_osr ? 1u : 0u;
918 }
919
920 } // namespace interpreter
921 } // namespace art
922