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