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 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091
|
/*
* Copyright (C) 2015 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.
*/
#include "unstarted_runtime.h"
#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <cmath>
#include <initializer_list>
#include <limits>
#include <locale>
#include <unordered_map>
#include <android-base/logging.h>
#include <android-base/stringprintf.h>
#include "art_method-inl.h"
#include "base/casts.h"
#include "base/enums.h"
#include "base/macros.h"
#include "base/quasi_atomic.h"
#include "base/zip_archive.h"
#include "class_linker.h"
#include "common_throws.h"
#include "dex/descriptors_names.h"
#include "entrypoints/entrypoint_utils-inl.h"
#include "gc/reference_processor.h"
#include "handle_scope-inl.h"
#include "hidden_api.h"
#include "interpreter/interpreter_common.h"
#include "jvalue-inl.h"
#include "mirror/array-alloc-inl.h"
#include "mirror/array-inl.h"
#include "mirror/class-alloc-inl.h"
#include "mirror/executable-inl.h"
#include "mirror/field-inl.h"
#include "mirror/method.h"
#include "mirror/object-inl.h"
#include "mirror/object_array-alloc-inl.h"
#include "mirror/object_array-inl.h"
#include "mirror/string-alloc-inl.h"
#include "mirror/string-inl.h"
#include "nativehelper/scoped_local_ref.h"
#include "nth_caller_visitor.h"
#include "reflection.h"
#include "thread-inl.h"
#include "transaction.h"
#include "well_known_classes.h"
namespace art {
namespace interpreter {
using android::base::StringAppendV;
using android::base::StringPrintf;
static void AbortTransactionOrFail(Thread* self, const char* fmt, ...)
__attribute__((__format__(__printf__, 2, 3)))
REQUIRES_SHARED(Locks::mutator_lock_);
static void AbortTransactionOrFail(Thread* self, const char* fmt, ...) {
va_list args;
if (Runtime::Current()->IsActiveTransaction()) {
va_start(args, fmt);
AbortTransactionV(self, fmt, args);
va_end(args);
} else {
va_start(args, fmt);
std::string msg;
StringAppendV(&msg, fmt, args);
va_end(args);
LOG(FATAL) << "Trying to abort, but not in transaction mode: " << msg;
UNREACHABLE();
}
}
// Restricted support for character upper case / lower case. Only support ASCII, where
// it's easy. Abort the transaction otherwise.
static void CharacterLowerUpper(Thread* self,
ShadowFrame* shadow_frame,
JValue* result,
size_t arg_offset,
bool to_lower_case) REQUIRES_SHARED(Locks::mutator_lock_) {
uint32_t int_value = static_cast<uint32_t>(shadow_frame->GetVReg(arg_offset));
// Only ASCII (7-bit).
if (!isascii(int_value)) {
AbortTransactionOrFail(self,
"Only support ASCII characters for toLowerCase/toUpperCase: %u",
int_value);
return;
}
std::locale c_locale("C");
char char_value = static_cast<char>(int_value);
if (to_lower_case) {
result->SetI(std::tolower(char_value, c_locale));
} else {
result->SetI(std::toupper(char_value, c_locale));
}
}
void UnstartedRuntime::UnstartedCharacterToLowerCase(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
CharacterLowerUpper(self, shadow_frame, result, arg_offset, true);
}
void UnstartedRuntime::UnstartedCharacterToUpperCase(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
CharacterLowerUpper(self, shadow_frame, result, arg_offset, false);
}
// Helper function to deal with class loading in an unstarted runtime.
static void UnstartedRuntimeFindClass(Thread* self, Handle<mirror::String> className,
Handle<mirror::ClassLoader> class_loader, JValue* result,
const std::string& method_name, bool initialize_class,
bool abort_if_not_found)
REQUIRES_SHARED(Locks::mutator_lock_) {
CHECK(className != nullptr);
std::string descriptor(DotToDescriptor(className->ToModifiedUtf8().c_str()));
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
ObjPtr<mirror::Class> found = class_linker->FindClass(self, descriptor.c_str(), class_loader);
if (found == nullptr && abort_if_not_found) {
if (!self->IsExceptionPending()) {
AbortTransactionOrFail(self, "%s failed in un-started runtime for class: %s",
method_name.c_str(),
PrettyDescriptor(descriptor.c_str()).c_str());
}
return;
}
if (found != nullptr && initialize_class) {
StackHandleScope<1> hs(self);
HandleWrapperObjPtr<mirror::Class> h_class = hs.NewHandleWrapper(&found);
if (!class_linker->EnsureInitialized(self, h_class, true, true)) {
CHECK(self->IsExceptionPending());
return;
}
}
result->SetL(found);
}
// Common helper for class-loading cutouts in an unstarted runtime. We call Runtime methods that
// rely on Java code to wrap errors in the correct exception class (i.e., NoClassDefFoundError into
// ClassNotFoundException), so need to do the same. The only exception is if the exception is
// actually the transaction abort exception. This must not be wrapped, as it signals an
// initialization abort.
static void CheckExceptionGenerateClassNotFound(Thread* self)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (self->IsExceptionPending()) {
// If it is not the transaction abort exception, wrap it.
std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
if (type != Transaction::kAbortExceptionDescriptor) {
self->ThrowNewWrappedException("Ljava/lang/ClassNotFoundException;",
"ClassNotFoundException");
}
}
}
static ObjPtr<mirror::String> GetClassName(Thread* self,
ShadowFrame* shadow_frame,
size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
if (param == nullptr) {
AbortTransactionOrFail(self, "Null-pointer in Class.forName.");
return nullptr;
}
return param->AsString();
}
static std::function<hiddenapi::AccessContext()> GetHiddenapiAccessContextFunction(
ShadowFrame* frame) {
return [=]() REQUIRES_SHARED(Locks::mutator_lock_) {
return hiddenapi::AccessContext(frame->GetMethod()->GetDeclaringClass());
};
}
template<typename T>
static ALWAYS_INLINE bool ShouldDenyAccessToMember(T* member, ShadowFrame* frame)
REQUIRES_SHARED(Locks::mutator_lock_) {
// All uses in this file are from reflection
constexpr hiddenapi::AccessMethod kAccessMethod = hiddenapi::AccessMethod::kReflection;
return hiddenapi::ShouldDenyAccessToMember(member,
GetHiddenapiAccessContextFunction(frame),
kAccessMethod);
}
void UnstartedRuntime::UnstartedClassForNameCommon(Thread* self,
ShadowFrame* shadow_frame,
JValue* result,
size_t arg_offset,
bool long_form,
const char* caller) {
ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
if (class_name == nullptr) {
return;
}
bool initialize_class;
ObjPtr<mirror::ClassLoader> class_loader;
if (long_form) {
initialize_class = shadow_frame->GetVReg(arg_offset + 1) != 0;
class_loader =
ObjPtr<mirror::ClassLoader>::DownCast(shadow_frame->GetVRegReference(arg_offset + 2));
} else {
initialize_class = true;
// TODO: This is really only correct for the boot classpath, and for robustness we should
// check the caller.
class_loader = nullptr;
}
ScopedObjectAccessUnchecked soa(self);
if (class_loader != nullptr && !ClassLinker::IsBootClassLoader(soa, class_loader)) {
AbortTransactionOrFail(self,
"Only the boot classloader is supported: %s",
mirror::Object::PrettyTypeOf(class_loader).c_str());
return;
}
StackHandleScope<1> hs(self);
Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
UnstartedRuntimeFindClass(self,
h_class_name,
ScopedNullHandle<mirror::ClassLoader>(),
result,
caller,
initialize_class,
false);
CheckExceptionGenerateClassNotFound(self);
}
void UnstartedRuntime::UnstartedClassForName(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, false, "Class.forName");
}
void UnstartedRuntime::UnstartedClassForNameLong(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.forName");
}
void UnstartedRuntime::UnstartedClassGetPrimitiveClass(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
ObjPtr<mirror::String> class_name = GetClassName(self, shadow_frame, arg_offset);
ObjPtr<mirror::Class> klass = mirror::Class::GetPrimitiveClass(class_name);
if (UNLIKELY(klass == nullptr)) {
DCHECK(self->IsExceptionPending());
AbortTransactionOrFail(self,
"Class.getPrimitiveClass() failed: %s",
self->GetException()->GetDetailMessage()->ToModifiedUtf8().c_str());
return;
}
result->SetL(klass);
}
void UnstartedRuntime::UnstartedClassClassForName(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedClassForNameCommon(self, shadow_frame, result, arg_offset, true, "Class.classForName");
}
void UnstartedRuntime::UnstartedClassNewInstance(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<2> hs(self); // Class, constructor, object.
mirror::Object* param = shadow_frame->GetVRegReference(arg_offset);
if (param == nullptr) {
AbortTransactionOrFail(self, "Null-pointer in Class.newInstance.");
return;
}
Handle<mirror::Class> h_klass(hs.NewHandle(param->AsClass()));
// Check that it's not null.
if (h_klass == nullptr) {
AbortTransactionOrFail(self, "Class reference is null for newInstance");
return;
}
// If we're in a transaction, class must not be finalizable (it or a superclass has a finalizer).
if (Runtime::Current()->IsActiveTransaction()) {
if (h_klass->IsFinalizable()) {
AbortTransactionF(self, "Class for newInstance is finalizable: '%s'",
h_klass->PrettyClass().c_str());
return;
}
}
// There are two situations in which we'll abort this run.
// 1) If the class isn't yet initialized and initialization fails.
// 2) If we can't find the default constructor. We'll postpone the exception to runtime.
// Note that 2) could likely be handled here, but for safety abort the transaction.
bool ok = false;
auto* cl = Runtime::Current()->GetClassLinker();
if (cl->EnsureInitialized(self, h_klass, true, true)) {
ArtMethod* cons = h_klass->FindConstructor("()V", cl->GetImagePointerSize());
if (cons != nullptr && ShouldDenyAccessToMember(cons, shadow_frame)) {
cons = nullptr;
}
if (cons != nullptr) {
Handle<mirror::Object> h_obj(hs.NewHandle(h_klass->AllocObject(self)));
CHECK(h_obj != nullptr); // We don't expect OOM at compile-time.
EnterInterpreterFromInvoke(self, cons, h_obj.Get(), nullptr, nullptr);
if (!self->IsExceptionPending()) {
result->SetL(h_obj.Get());
ok = true;
}
} else {
self->ThrowNewExceptionF("Ljava/lang/InternalError;",
"Could not find default constructor for '%s'",
h_klass->PrettyClass().c_str());
}
}
if (!ok) {
AbortTransactionOrFail(self, "Failed in Class.newInstance for '%s' with %s",
h_klass->PrettyClass().c_str(),
mirror::Object::PrettyTypeOf(self->GetException()).c_str());
}
}
void UnstartedRuntime::UnstartedClassGetDeclaredField(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Special managed code cut-out to allow field lookup in a un-started runtime that'd fail
// going the reflective Dex way.
ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
ObjPtr<mirror::String> name2 = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
ArtField* found = nullptr;
for (ArtField& field : klass->GetIFields()) {
if (name2->Equals(field.GetName())) {
found = &field;
break;
}
}
if (found == nullptr) {
for (ArtField& field : klass->GetSFields()) {
if (name2->Equals(field.GetName())) {
found = &field;
break;
}
}
}
if (found != nullptr && ShouldDenyAccessToMember(found, shadow_frame)) {
found = nullptr;
}
if (found == nullptr) {
AbortTransactionOrFail(self, "Failed to find field in Class.getDeclaredField in un-started "
" runtime. name=%s class=%s", name2->ToModifiedUtf8().c_str(),
klass->PrettyDescriptor().c_str());
return;
}
Runtime* runtime = Runtime::Current();
PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
ObjPtr<mirror::Field> field;
if (runtime->IsActiveTransaction()) {
if (pointer_size == PointerSize::k64) {
field = mirror::Field::CreateFromArtField<PointerSize::k64, true>(
self, found, true);
} else {
field = mirror::Field::CreateFromArtField<PointerSize::k32, true>(
self, found, true);
}
} else {
if (pointer_size == PointerSize::k64) {
field = mirror::Field::CreateFromArtField<PointerSize::k64, false>(
self, found, true);
} else {
field = mirror::Field::CreateFromArtField<PointerSize::k32, false>(
self, found, true);
}
}
result->SetL(field);
}
// This is required for Enum(Set) code, as that uses reflection to inspect enum classes.
void UnstartedRuntime::UnstartedClassGetDeclaredMethod(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Special managed code cut-out to allow method lookup in a un-started runtime.
ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
if (klass == nullptr) {
ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
return;
}
ObjPtr<mirror::String> name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
ObjPtr<mirror::ObjectArray<mirror::Class>> args =
shadow_frame->GetVRegReference(arg_offset + 2)->AsObjectArray<mirror::Class>();
Runtime* runtime = Runtime::Current();
bool transaction = runtime->IsActiveTransaction();
PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
auto fn_hiddenapi_access_context = GetHiddenapiAccessContextFunction(shadow_frame);
ObjPtr<mirror::Method> method;
if (transaction) {
if (pointer_size == PointerSize::k64) {
method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, true>(
self, klass, name, args, fn_hiddenapi_access_context);
} else {
method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, true>(
self, klass, name, args, fn_hiddenapi_access_context);
}
} else {
if (pointer_size == PointerSize::k64) {
method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k64, false>(
self, klass, name, args, fn_hiddenapi_access_context);
} else {
method = mirror::Class::GetDeclaredMethodInternal<PointerSize::k32, false>(
self, klass, name, args, fn_hiddenapi_access_context);
}
}
if (method != nullptr && ShouldDenyAccessToMember(method->GetArtMethod(), shadow_frame)) {
method = nullptr;
}
result->SetL(method);
}
// Special managed code cut-out to allow constructor lookup in a un-started runtime.
void UnstartedRuntime::UnstartedClassGetDeclaredConstructor(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
ObjPtr<mirror::Class> klass = shadow_frame->GetVRegReference(arg_offset)->AsClass();
if (klass == nullptr) {
ThrowNullPointerExceptionForMethodAccess(shadow_frame->GetMethod(), InvokeType::kVirtual);
return;
}
ObjPtr<mirror::ObjectArray<mirror::Class>> args =
shadow_frame->GetVRegReference(arg_offset + 1)->AsObjectArray<mirror::Class>();
Runtime* runtime = Runtime::Current();
bool transaction = runtime->IsActiveTransaction();
PointerSize pointer_size = runtime->GetClassLinker()->GetImagePointerSize();
ObjPtr<mirror::Constructor> constructor;
if (transaction) {
if (pointer_size == PointerSize::k64) {
constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
true>(self, klass, args);
} else {
constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
true>(self, klass, args);
}
} else {
if (pointer_size == PointerSize::k64) {
constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k64,
false>(self, klass, args);
} else {
constructor = mirror::Class::GetDeclaredConstructorInternal<PointerSize::k32,
false>(self, klass, args);
}
}
if (constructor != nullptr &&
ShouldDenyAccessToMember(constructor->GetArtMethod(), shadow_frame)) {
constructor = nullptr;
}
result->SetL(constructor);
}
void UnstartedRuntime::UnstartedClassGetDeclaringClass(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> klass(hs.NewHandle(
reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
result->SetL(nullptr);
return;
}
// Return null for anonymous classes.
JValue is_anon_result;
UnstartedClassIsAnonymousClass(self, shadow_frame, &is_anon_result, arg_offset);
if (is_anon_result.GetZ() != 0) {
result->SetL(nullptr);
return;
}
result->SetL(annotations::GetDeclaringClass(klass));
}
void UnstartedRuntime::UnstartedClassGetEnclosingClass(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> klass(hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsClass()));
if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
result->SetL(nullptr);
}
result->SetL(annotations::GetEnclosingClass(klass));
}
void UnstartedRuntime::UnstartedClassGetInnerClassFlags(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> klass(hs.NewHandle(
reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
const int32_t default_value = shadow_frame->GetVReg(arg_offset + 1);
result->SetI(mirror::Class::GetInnerClassFlags(klass, default_value));
}
void UnstartedRuntime::UnstartedClassGetSignatureAnnotation(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> klass(hs.NewHandle(
reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
result->SetL(nullptr);
return;
}
result->SetL(annotations::GetSignatureAnnotationForClass(klass));
}
void UnstartedRuntime::UnstartedClassIsAnonymousClass(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
StackHandleScope<1> hs(self);
Handle<mirror::Class> klass(hs.NewHandle(
reinterpret_cast<mirror::Class*>(shadow_frame->GetVRegReference(arg_offset))));
if (klass->IsProxyClass() || klass->GetDexCache() == nullptr) {
result->SetZ(false);
return;
}
ObjPtr<mirror::String> class_name = nullptr;
if (!annotations::GetInnerClass(klass, &class_name)) {
result->SetZ(false);
return;
}
result->SetZ(class_name == nullptr);
}
static MemMap FindAndExtractEntry(const std::string& jar_file,
const char* entry_name,
size_t* size,
std::string* error_msg) {
CHECK(size != nullptr);
std::unique_ptr<ZipArchive> zip_archive(ZipArchive::Open(jar_file.c_str(), error_msg));
if (zip_archive == nullptr) {
return MemMap::Invalid();
}
std::unique_ptr<ZipEntry> zip_entry(zip_archive->Find(entry_name, error_msg));
if (zip_entry == nullptr) {
return MemMap::Invalid();
}
MemMap tmp_map = zip_entry->ExtractToMemMap(jar_file.c_str(), entry_name, error_msg);
if (!tmp_map.IsValid()) {
return MemMap::Invalid();
}
// OK, from here everything seems fine.
*size = zip_entry->GetUncompressedLength();
return tmp_map;
}
static void GetResourceAsStream(Thread* self,
ShadowFrame* shadow_frame,
JValue* result,
size_t arg_offset) REQUIRES_SHARED(Locks::mutator_lock_) {
mirror::Object* resource_obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (resource_obj == nullptr) {
AbortTransactionOrFail(self, "null name for getResourceAsStream");
return;
}
CHECK(resource_obj->IsString());
ObjPtr<mirror::String> resource_name = resource_obj->AsString();
std::string resource_name_str = resource_name->ToModifiedUtf8();
if (resource_name_str.empty() || resource_name_str == "/") {
AbortTransactionOrFail(self,
"Unsupported name %s for getResourceAsStream",
resource_name_str.c_str());
return;
}
const char* resource_cstr = resource_name_str.c_str();
if (resource_cstr[0] == '/') {
resource_cstr++;
}
Runtime* runtime = Runtime::Current();
const std::vector<std::string>& boot_class_path = Runtime::Current()->GetBootClassPath();
if (boot_class_path.empty()) {
AbortTransactionOrFail(self, "Boot classpath not set");
return;
}
MemMap mem_map;
size_t map_size;
std::string last_error_msg; // Only store the last message (we could concatenate).
for (const std::string& jar_file : boot_class_path) {
mem_map = FindAndExtractEntry(jar_file, resource_cstr, &map_size, &last_error_msg);
if (mem_map.IsValid()) {
break;
}
}
if (!mem_map.IsValid()) {
// Didn't find it. There's a good chance this will be the same at runtime, but still
// conservatively abort the transaction here.
AbortTransactionOrFail(self,
"Could not find resource %s. Last error was %s.",
resource_name_str.c_str(),
last_error_msg.c_str());
return;
}
StackHandleScope<3> hs(self);
// Create byte array for content.
Handle<mirror::ByteArray> h_array(hs.NewHandle(mirror::ByteArray::Alloc(self, map_size)));
if (h_array == nullptr) {
AbortTransactionOrFail(self, "Could not find/create byte array class");
return;
}
// Copy in content.
memcpy(h_array->GetData(), mem_map.Begin(), map_size);
// Be proactive releasing memory.
mem_map.Reset();
// Create a ByteArrayInputStream.
Handle<mirror::Class> h_class(hs.NewHandle(
runtime->GetClassLinker()->FindClass(self,
"Ljava/io/ByteArrayInputStream;",
ScopedNullHandle<mirror::ClassLoader>())));
if (h_class == nullptr) {
AbortTransactionOrFail(self, "Could not find ByteArrayInputStream class");
return;
}
if (!runtime->GetClassLinker()->EnsureInitialized(self, h_class, true, true)) {
AbortTransactionOrFail(self, "Could not initialize ByteArrayInputStream class");
return;
}
Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
if (h_obj == nullptr) {
AbortTransactionOrFail(self, "Could not allocate ByteArrayInputStream object");
return;
}
auto* cl = Runtime::Current()->GetClassLinker();
ArtMethod* constructor = h_class->FindConstructor("([B)V", cl->GetImagePointerSize());
if (constructor == nullptr) {
AbortTransactionOrFail(self, "Could not find ByteArrayInputStream constructor");
return;
}
uint32_t args[1];
args[0] = reinterpret_cast32<uint32_t>(h_array.Get());
EnterInterpreterFromInvoke(self, constructor, h_obj.Get(), args, nullptr);
if (self->IsExceptionPending()) {
AbortTransactionOrFail(self, "Could not run ByteArrayInputStream constructor");
return;
}
result->SetL(h_obj.Get());
}
void UnstartedRuntime::UnstartedClassLoaderGetResourceAsStream(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
{
mirror::Object* this_obj = shadow_frame->GetVRegReference(arg_offset);
CHECK(this_obj != nullptr);
CHECK(this_obj->IsClassLoader());
StackHandleScope<1> hs(self);
Handle<mirror::Class> this_classloader_class(hs.NewHandle(this_obj->GetClass()));
if (self->DecodeJObject(WellKnownClasses::java_lang_BootClassLoader) !=
this_classloader_class.Get()) {
AbortTransactionOrFail(self,
"Unsupported classloader type %s for getResourceAsStream",
mirror::Class::PrettyClass(this_classloader_class.Get()).c_str());
return;
}
}
GetResourceAsStream(self, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedConstructorNewInstance0(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// This is a cutdown version of java_lang_reflect_Constructor.cc's implementation.
StackHandleScope<4> hs(self);
Handle<mirror::Constructor> m = hs.NewHandle(
reinterpret_cast<mirror::Constructor*>(shadow_frame->GetVRegReference(arg_offset)));
Handle<mirror::ObjectArray<mirror::Object>> args = hs.NewHandle(
reinterpret_cast<mirror::ObjectArray<mirror::Object>*>(
shadow_frame->GetVRegReference(arg_offset + 1)));
Handle<mirror::Class> c(hs.NewHandle(m->GetDeclaringClass()));
if (UNLIKELY(c->IsAbstract())) {
AbortTransactionOrFail(self, "Cannot handle abstract classes");
return;
}
// Verify that we can access the class.
if (!m->IsAccessible() && !c->IsPublic()) {
// Go 2 frames back, this method is always called from newInstance0, which is called from
// Constructor.newInstance(Object... args).
ObjPtr<mirror::Class> caller = GetCallingClass(self, 2);
// If caller is null, then we called from JNI, just avoid the check since JNI avoids most
// access checks anyways. TODO: Investigate if this the correct behavior.
if (caller != nullptr && !caller->CanAccess(c.Get())) {
AbortTransactionOrFail(self, "Cannot access class");
return;
}
}
if (!Runtime::Current()->GetClassLinker()->EnsureInitialized(self, c, true, true)) {
DCHECK(self->IsExceptionPending());
return;
}
if (c->IsClassClass()) {
AbortTransactionOrFail(self, "new Class() is not supported");
return;
}
// String constructor is replaced by a StringFactory method in InvokeMethod.
if (c->IsStringClass()) {
// We don't support strings.
AbortTransactionOrFail(self, "String construction is not supported");
return;
}
Handle<mirror::Object> receiver = hs.NewHandle(c->AllocObject(self));
if (receiver == nullptr) {
AbortTransactionOrFail(self, "Could not allocate");
return;
}
// It's easier to use reflection to make the call, than create the uint32_t array.
{
ScopedObjectAccessUnchecked soa(self);
ScopedLocalRef<jobject> method_ref(self->GetJniEnv(),
soa.AddLocalReference<jobject>(m.Get()));
ScopedLocalRef<jobject> object_ref(self->GetJniEnv(),
soa.AddLocalReference<jobject>(receiver.Get()));
ScopedLocalRef<jobject> args_ref(self->GetJniEnv(),
soa.AddLocalReference<jobject>(args.Get()));
InvokeMethod(soa, method_ref.get(), object_ref.get(), args_ref.get(), 2);
}
if (self->IsExceptionPending()) {
AbortTransactionOrFail(self, "Failed running constructor");
} else {
result->SetL(receiver.Get());
}
}
void UnstartedRuntime::UnstartedVmClassLoaderFindLoadedClass(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
ObjPtr<mirror::String> class_name = shadow_frame->GetVRegReference(arg_offset + 1)->AsString();
ObjPtr<mirror::ClassLoader> class_loader =
ObjPtr<mirror::ClassLoader>::DownCast(shadow_frame->GetVRegReference(arg_offset));
StackHandleScope<2> hs(self);
Handle<mirror::String> h_class_name(hs.NewHandle(class_name));
Handle<mirror::ClassLoader> h_class_loader(hs.NewHandle(class_loader));
UnstartedRuntimeFindClass(self, h_class_name, h_class_loader, result,
"VMClassLoader.findLoadedClass", false, false);
// This might have an error pending. But semantics are to just return null.
if (self->IsExceptionPending()) {
// If it is an InternalError, keep it. See CheckExceptionGenerateClassNotFound.
std::string type(mirror::Object::PrettyTypeOf(self->GetException()));
if (type != "java.lang.InternalError") {
self->ClearException();
}
}
}
// Arraycopy emulation.
// Note: we can't use any fast copy functions, as they are not available under transaction.
template <typename T>
static void PrimitiveArrayCopy(Thread* self,
ObjPtr<mirror::Array> src_array,
int32_t src_pos,
ObjPtr<mirror::Array> dst_array,
int32_t dst_pos,
int32_t length)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (src_array->GetClass()->GetComponentType() != dst_array->GetClass()->GetComponentType()) {
AbortTransactionOrFail(self,
"Types mismatched in arraycopy: %s vs %s.",
mirror::Class::PrettyDescriptor(
src_array->GetClass()->GetComponentType()).c_str(),
mirror::Class::PrettyDescriptor(
dst_array->GetClass()->GetComponentType()).c_str());
return;
}
ObjPtr<mirror::PrimitiveArray<T>> src = ObjPtr<mirror::PrimitiveArray<T>>::DownCast(src_array);
ObjPtr<mirror::PrimitiveArray<T>> dst = ObjPtr<mirror::PrimitiveArray<T>>::DownCast(dst_array);
const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
if (copy_forward) {
for (int32_t i = 0; i < length; ++i) {
dst->Set(dst_pos + i, src->Get(src_pos + i));
}
} else {
for (int32_t i = 1; i <= length; ++i) {
dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
}
}
}
void UnstartedRuntime::UnstartedSystemArraycopy(
Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
// Special case array copying without initializing System.
jint src_pos = shadow_frame->GetVReg(arg_offset + 1);
jint dst_pos = shadow_frame->GetVReg(arg_offset + 3);
jint length = shadow_frame->GetVReg(arg_offset + 4);
mirror::Object* src_obj = shadow_frame->GetVRegReference(arg_offset);
mirror::Object* dst_obj = shadow_frame->GetVRegReference(arg_offset + 2);
// Null checking. For simplicity, abort transaction.
if (src_obj == nullptr) {
AbortTransactionOrFail(self, "src is null in arraycopy.");
return;
}
if (dst_obj == nullptr) {
AbortTransactionOrFail(self, "dst is null in arraycopy.");
return;
}
// Test for arrayness. Throw ArrayStoreException.
if (!src_obj->IsArrayInstance() || !dst_obj->IsArrayInstance()) {
self->ThrowNewException("Ljava/lang/ArrayStoreException;", "src or trg is not an array");
return;
}
ObjPtr<mirror::Array> src_array = src_obj->AsArray();
ObjPtr<mirror::Array> dst_array = dst_obj->AsArray();
// Bounds checking. Throw IndexOutOfBoundsException.
if (UNLIKELY(src_pos < 0) || UNLIKELY(dst_pos < 0) || UNLIKELY(length < 0) ||
UNLIKELY(src_pos > src_array->GetLength() - length) ||
UNLIKELY(dst_pos > dst_array->GetLength() - length)) {
self->ThrowNewExceptionF("Ljava/lang/IndexOutOfBoundsException;",
"src.length=%d srcPos=%d dst.length=%d dstPos=%d length=%d",
src_array->GetLength(), src_pos, dst_array->GetLength(), dst_pos,
length);
return;
}
if (Runtime::Current()->IsActiveTransaction() && !CheckWriteConstraint(self, dst_obj)) {
DCHECK(self->IsExceptionPending());
return;
}
// Type checking.
ObjPtr<mirror::Class> src_type = shadow_frame->GetVRegReference(arg_offset)->GetClass()->
GetComponentType();
if (!src_type->IsPrimitive()) {
// Check that the second type is not primitive.
ObjPtr<mirror::Class> trg_type = shadow_frame->GetVRegReference(arg_offset + 2)->GetClass()->
GetComponentType();
if (trg_type->IsPrimitiveInt()) {
AbortTransactionOrFail(self, "Type mismatch in arraycopy: %s vs %s",
mirror::Class::PrettyDescriptor(
src_array->GetClass()->GetComponentType()).c_str(),
mirror::Class::PrettyDescriptor(
dst_array->GetClass()->GetComponentType()).c_str());
return;
}
ObjPtr<mirror::ObjectArray<mirror::Object>> src = src_array->AsObjectArray<mirror::Object>();
ObjPtr<mirror::ObjectArray<mirror::Object>> dst = dst_array->AsObjectArray<mirror::Object>();
if (src == dst) {
// Can overlap, but not have type mismatches.
// We cannot use ObjectArray::MemMove here, as it doesn't support transactions.
const bool copy_forward = (dst_pos < src_pos) || (dst_pos - src_pos >= length);
if (copy_forward) {
for (int32_t i = 0; i < length; ++i) {
dst->Set(dst_pos + i, src->Get(src_pos + i));
}
} else {
for (int32_t i = 1; i <= length; ++i) {
dst->Set(dst_pos + length - i, src->Get(src_pos + length - i));
}
}
} else {
// We're being lazy here. Optimally this could be a memcpy (if component types are
// assignable), but the ObjectArray implementation doesn't support transactions. The
// checking version, however, does.
if (Runtime::Current()->IsActiveTransaction()) {
dst->AssignableCheckingMemcpy<true>(
dst_pos, src, src_pos, length, /* throw_exception= */ true);
} else {
dst->AssignableCheckingMemcpy<false>(
dst_pos, src, src_pos, length, /* throw_exception= */ true);
}
}
} else if (src_type->IsPrimitiveByte()) {
PrimitiveArrayCopy<uint8_t>(self, src_array, src_pos, dst_array, dst_pos, length);
} else if (src_type->IsPrimitiveChar()) {
PrimitiveArrayCopy<uint16_t>(self, src_array, src_pos, dst_array, dst_pos, length);
} else if (src_type->IsPrimitiveInt()) {
PrimitiveArrayCopy<int32_t>(self, src_array, src_pos, dst_array, dst_pos, length);
} else {
AbortTransactionOrFail(self, "Unimplemented System.arraycopy for type '%s'",
src_type->PrettyDescriptor().c_str());
}
}
void UnstartedRuntime::UnstartedSystemArraycopyByte(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Just forward.
UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedSystemArraycopyChar(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Just forward.
UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedSystemArraycopyInt(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Just forward.
UnstartedRuntime::UnstartedSystemArraycopy(self, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedSystemGetSecurityManager(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame ATTRIBUTE_UNUSED,
JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
result->SetL(nullptr);
}
static constexpr const char* kAndroidHardcodedSystemPropertiesFieldName = "STATIC_PROPERTIES";
static void GetSystemProperty(Thread* self,
ShadowFrame* shadow_frame,
JValue* result,
size_t arg_offset,
bool is_default_version)
REQUIRES_SHARED(Locks::mutator_lock_) {
StackHandleScope<4> hs(self);
Handle<mirror::String> h_key(
hs.NewHandle(reinterpret_cast<mirror::String*>(shadow_frame->GetVRegReference(arg_offset))));
if (h_key == nullptr) {
AbortTransactionOrFail(self, "getProperty key was null");
return;
}
// This is overall inefficient, but reflecting the values here is not great, either. So
// for simplicity, and with the assumption that the number of getProperty calls is not
// too great, just iterate each time.
// Get the storage class.
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
Handle<mirror::Class> h_props_class(hs.NewHandle(
class_linker->FindClass(self,
"Ljava/lang/AndroidHardcodedSystemProperties;",
ScopedNullHandle<mirror::ClassLoader>())));
if (h_props_class == nullptr) {
AbortTransactionOrFail(self, "Could not find AndroidHardcodedSystemProperties");
return;
}
if (!class_linker->EnsureInitialized(self, h_props_class, true, true)) {
AbortTransactionOrFail(self, "Could not initialize AndroidHardcodedSystemProperties");
return;
}
// Get the storage array.
ArtField* static_properties =
h_props_class->FindDeclaredStaticField(kAndroidHardcodedSystemPropertiesFieldName,
"[[Ljava/lang/String;");
if (static_properties == nullptr) {
AbortTransactionOrFail(self,
"Could not find %s field",
kAndroidHardcodedSystemPropertiesFieldName);
return;
}
ObjPtr<mirror::Object> props = static_properties->GetObject(h_props_class.Get());
Handle<mirror::ObjectArray<mirror::ObjectArray<mirror::String>>> h_2string_array(hs.NewHandle(
props->AsObjectArray<mirror::ObjectArray<mirror::String>>()));
if (h_2string_array == nullptr) {
AbortTransactionOrFail(self, "Field %s is null", kAndroidHardcodedSystemPropertiesFieldName);
return;
}
// Iterate over it.
const int32_t prop_count = h_2string_array->GetLength();
// Use the third handle as mutable.
MutableHandle<mirror::ObjectArray<mirror::String>> h_string_array(
hs.NewHandle<mirror::ObjectArray<mirror::String>>(nullptr));
for (int32_t i = 0; i < prop_count; ++i) {
h_string_array.Assign(h_2string_array->Get(i));
if (h_string_array == nullptr ||
h_string_array->GetLength() != 2 ||
h_string_array->Get(0) == nullptr) {
AbortTransactionOrFail(self,
"Unexpected content of %s",
kAndroidHardcodedSystemPropertiesFieldName);
return;
}
if (h_key->Equals(h_string_array->Get(0))) {
// Found a value.
if (h_string_array->Get(1) == nullptr && is_default_version) {
// Null is being delegated to the default map, and then resolved to the given default value.
// As there's no default map, return the given value.
result->SetL(shadow_frame->GetVRegReference(arg_offset + 1));
} else {
result->SetL(h_string_array->Get(1));
}
return;
}
}
// Key is not supported.
AbortTransactionOrFail(self, "getProperty key %s not supported", h_key->ToModifiedUtf8().c_str());
}
void UnstartedRuntime::UnstartedSystemGetProperty(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
GetSystemProperty(self, shadow_frame, result, arg_offset, false);
}
void UnstartedRuntime::UnstartedSystemGetPropertyWithDefault(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
GetSystemProperty(self, shadow_frame, result, arg_offset, true);
}
static std::string GetImmediateCaller(ShadowFrame* shadow_frame)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (shadow_frame->GetLink() == nullptr) {
return "<no caller>";
}
return ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
}
static bool CheckCallers(ShadowFrame* shadow_frame,
std::initializer_list<std::string> allowed_call_stack)
REQUIRES_SHARED(Locks::mutator_lock_) {
for (const std::string& allowed_caller : allowed_call_stack) {
if (shadow_frame->GetLink() == nullptr) {
return false;
}
std::string found_caller = ArtMethod::PrettyMethod(shadow_frame->GetLink()->GetMethod());
if (allowed_caller != found_caller) {
return false;
}
shadow_frame = shadow_frame->GetLink();
}
return true;
}
static ObjPtr<mirror::Object> CreateInstanceOf(Thread* self, const char* class_descriptor)
REQUIRES_SHARED(Locks::mutator_lock_) {
// Find the requested class.
ClassLinker* class_linker = Runtime::Current()->GetClassLinker();
ObjPtr<mirror::Class> klass =
class_linker->FindClass(self, class_descriptor, ScopedNullHandle<mirror::ClassLoader>());
if (klass == nullptr) {
AbortTransactionOrFail(self, "Could not load class %s", class_descriptor);
return nullptr;
}
StackHandleScope<2> hs(self);
Handle<mirror::Class> h_class(hs.NewHandle(klass));
Handle<mirror::Object> h_obj(hs.NewHandle(h_class->AllocObject(self)));
if (h_obj != nullptr) {
ArtMethod* init_method = h_class->FindConstructor("()V", class_linker->GetImagePointerSize());
if (init_method == nullptr) {
AbortTransactionOrFail(self, "Could not find <init> for %s", class_descriptor);
return nullptr;
} else {
JValue invoke_result;
EnterInterpreterFromInvoke(self, init_method, h_obj.Get(), nullptr, nullptr);
if (!self->IsExceptionPending()) {
return h_obj.Get();
}
AbortTransactionOrFail(self, "Could not run <init> for %s", class_descriptor);
}
}
AbortTransactionOrFail(self, "Could not allocate instance of %s", class_descriptor);
return nullptr;
}
void UnstartedRuntime::UnstartedThreadLocalGet(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
if (CheckCallers(shadow_frame, { "sun.misc.FloatingDecimal$BinaryToASCIIBuffer "
"sun.misc.FloatingDecimal.getBinaryToASCIIBuffer()" })) {
result->SetL(CreateInstanceOf(self, "Lsun/misc/FloatingDecimal$BinaryToASCIIBuffer;"));
} else {
AbortTransactionOrFail(self,
"ThreadLocal.get() does not support %s",
GetImmediateCaller(shadow_frame).c_str());
}
}
void UnstartedRuntime::UnstartedThreadCurrentThread(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
if (CheckCallers(shadow_frame,
{ "void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
"java.lang.String, long, java.security.AccessControlContext)",
"void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
"java.lang.String, long)",
"void java.lang.Thread.<init>()",
"void java.util.logging.LogManager$Cleaner.<init>("
"java.util.logging.LogManager)" })) {
// Whitelist LogManager$Cleaner, which is an unstarted Thread (for a shutdown hook). The
// Thread constructor only asks for the current thread to set up defaults and add the
// thread as unstarted to the ThreadGroup. A faked-up main thread peer is good enough for
// these purposes.
Runtime::Current()->InitThreadGroups(self);
jobject main_peer =
self->CreateCompileTimePeer(self->GetJniEnv(),
"main",
false,
Runtime::Current()->GetMainThreadGroup());
if (main_peer == nullptr) {
AbortTransactionOrFail(self, "Failed allocating peer");
return;
}
result->SetL(self->DecodeJObject(main_peer));
self->GetJniEnv()->DeleteLocalRef(main_peer);
} else {
AbortTransactionOrFail(self,
"Thread.currentThread() does not support %s",
GetImmediateCaller(shadow_frame).c_str());
}
}
void UnstartedRuntime::UnstartedThreadGetNativeState(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
if (CheckCallers(shadow_frame,
{ "java.lang.Thread$State java.lang.Thread.getState()",
"java.lang.ThreadGroup java.lang.Thread.getThreadGroup()",
"void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
"java.lang.String, long, java.security.AccessControlContext)",
"void java.lang.Thread.init(java.lang.ThreadGroup, java.lang.Runnable, "
"java.lang.String, long)",
"void java.lang.Thread.<init>()",
"void java.util.logging.LogManager$Cleaner.<init>("
"java.util.logging.LogManager)" })) {
// Whitelist reading the state of the "main" thread when creating another (unstarted) thread
// for LogManager. Report the thread as "new" (it really only counts that it isn't terminated).
constexpr int32_t kJavaRunnable = 1;
result->SetI(kJavaRunnable);
} else {
AbortTransactionOrFail(self,
"Thread.getNativeState() does not support %s",
GetImmediateCaller(shadow_frame).c_str());
}
}
void UnstartedRuntime::UnstartedMathCeil(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
result->SetD(ceil(shadow_frame->GetVRegDouble(arg_offset)));
}
void UnstartedRuntime::UnstartedMathFloor(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
result->SetD(floor(shadow_frame->GetVRegDouble(arg_offset)));
}
void UnstartedRuntime::UnstartedMathSin(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
result->SetD(sin(shadow_frame->GetVRegDouble(arg_offset)));
}
void UnstartedRuntime::UnstartedMathCos(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
result->SetD(cos(shadow_frame->GetVRegDouble(arg_offset)));
}
void UnstartedRuntime::UnstartedMathPow(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
result->SetD(pow(shadow_frame->GetVRegDouble(arg_offset),
shadow_frame->GetVRegDouble(arg_offset + 2)));
}
void UnstartedRuntime::UnstartedObjectHashCode(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
result->SetI(obj->IdentityHashCode());
}
void UnstartedRuntime::UnstartedDoubleDoubleToRawLongBits(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
double in = shadow_frame->GetVRegDouble(arg_offset);
result->SetJ(bit_cast<int64_t, double>(in));
}
static void UnstartedMemoryPeek(
Primitive::Type type, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
int64_t address = shadow_frame->GetVRegLong(arg_offset);
// TODO: Check that this is in the heap somewhere. Otherwise we will segfault instead of
// aborting the transaction.
switch (type) {
case Primitive::kPrimByte: {
result->SetB(*reinterpret_cast<int8_t*>(static_cast<intptr_t>(address)));
return;
}
case Primitive::kPrimShort: {
using unaligned_short __attribute__((__aligned__(1))) = int16_t;
result->SetS(*reinterpret_cast<unaligned_short*>(static_cast<intptr_t>(address)));
return;
}
case Primitive::kPrimInt: {
using unaligned_int __attribute__((__aligned__(1))) = int32_t;
result->SetI(*reinterpret_cast<unaligned_int*>(static_cast<intptr_t>(address)));
return;
}
case Primitive::kPrimLong: {
using unaligned_long __attribute__((__aligned__(1))) = int64_t;
result->SetJ(*reinterpret_cast<unaligned_long*>(static_cast<intptr_t>(address)));
return;
}
case Primitive::kPrimBoolean:
case Primitive::kPrimChar:
case Primitive::kPrimFloat:
case Primitive::kPrimDouble:
case Primitive::kPrimVoid:
case Primitive::kPrimNot:
LOG(FATAL) << "Not in the Memory API: " << type;
UNREACHABLE();
}
LOG(FATAL) << "Should not reach here";
UNREACHABLE();
}
void UnstartedRuntime::UnstartedMemoryPeekByte(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedMemoryPeek(Primitive::kPrimByte, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedMemoryPeekShort(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedMemoryPeek(Primitive::kPrimShort, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedMemoryPeekInt(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedMemoryPeek(Primitive::kPrimInt, shadow_frame, result, arg_offset);
}
void UnstartedRuntime::UnstartedMemoryPeekLong(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
UnstartedMemoryPeek(Primitive::kPrimLong, shadow_frame, result, arg_offset);
}
static void UnstartedMemoryPeekArray(
Primitive::Type type, Thread* self, ShadowFrame* shadow_frame, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
int64_t address_long = shadow_frame->GetVRegLong(arg_offset);
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 2);
if (obj == nullptr) {
Runtime::Current()->AbortTransactionAndThrowAbortError(self, "Null pointer in peekArray");
return;
}
ObjPtr<mirror::Array> array = obj->AsArray();
int offset = shadow_frame->GetVReg(arg_offset + 3);
int count = shadow_frame->GetVReg(arg_offset + 4);
if (offset < 0 || offset + count > array->GetLength()) {
std::string error_msg(StringPrintf("Array out of bounds in peekArray: %d/%d vs %d",
offset, count, array->GetLength()));
Runtime::Current()->AbortTransactionAndThrowAbortError(self, error_msg.c_str());
return;
}
switch (type) {
case Primitive::kPrimByte: {
int8_t* address = reinterpret_cast<int8_t*>(static_cast<intptr_t>(address_long));
ObjPtr<mirror::ByteArray> byte_array = array->AsByteArray();
for (int32_t i = 0; i < count; ++i, ++address) {
byte_array->SetWithoutChecks<true>(i + offset, *address);
}
return;
}
case Primitive::kPrimShort:
case Primitive::kPrimInt:
case Primitive::kPrimLong:
LOG(FATAL) << "Type unimplemented for Memory Array API, should not reach here: " << type;
UNREACHABLE();
case Primitive::kPrimBoolean:
case Primitive::kPrimChar:
case Primitive::kPrimFloat:
case Primitive::kPrimDouble:
case Primitive::kPrimVoid:
case Primitive::kPrimNot:
LOG(FATAL) << "Not in the Memory API: " << type;
UNREACHABLE();
}
LOG(FATAL) << "Should not reach here";
UNREACHABLE();
}
void UnstartedRuntime::UnstartedMemoryPeekByteArray(
Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
UnstartedMemoryPeekArray(Primitive::kPrimByte, self, shadow_frame, arg_offset);
}
// This allows reading the new style of String objects during compilation.
void UnstartedRuntime::UnstartedStringGetCharsNoCheck(
Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset) {
jint start = shadow_frame->GetVReg(arg_offset + 1);
jint end = shadow_frame->GetVReg(arg_offset + 2);
jint index = shadow_frame->GetVReg(arg_offset + 4);
ObjPtr<mirror::String> string = shadow_frame->GetVRegReference(arg_offset)->AsString();
if (string == nullptr) {
AbortTransactionOrFail(self, "String.getCharsNoCheck with null object");
return;
}
DCHECK_GE(start, 0);
DCHECK_LE(start, end);
DCHECK_LE(end, string->GetLength());
StackHandleScope<1> hs(self);
Handle<mirror::CharArray> h_char_array(
hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 3)->AsCharArray()));
DCHECK_GE(index, 0);
DCHECK_LE(index, h_char_array->GetLength());
DCHECK_LE(end - start, h_char_array->GetLength() - index);
string->GetChars(start, end, h_char_array, index);
}
// This allows reading chars from the new style of String objects during compilation.
void UnstartedRuntime::UnstartedStringCharAt(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
jint index = shadow_frame->GetVReg(arg_offset + 1);
ObjPtr<mirror::String> string = shadow_frame->GetVRegReference(arg_offset)->AsString();
if (string == nullptr) {
AbortTransactionOrFail(self, "String.charAt with null object");
return;
}
result->SetC(string->CharAt(index));
}
// This allows creating String objects with replaced characters during compilation.
// String.doReplace(char, char) is called from String.replace(char, char) when there is a match.
void UnstartedRuntime::UnstartedStringDoReplace(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
jchar old_c = shadow_frame->GetVReg(arg_offset + 1);
jchar new_c = shadow_frame->GetVReg(arg_offset + 2);
StackHandleScope<1> hs(self);
Handle<mirror::String> string =
hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
if (string == nullptr) {
AbortTransactionOrFail(self, "String.replaceWithMatch with null object");
return;
}
result->SetL(mirror::String::DoReplace(self, string, old_c, new_c));
}
// This allows creating the new style of String objects during compilation.
void UnstartedRuntime::UnstartedStringFactoryNewStringFromChars(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
jint offset = shadow_frame->GetVReg(arg_offset);
jint char_count = shadow_frame->GetVReg(arg_offset + 1);
DCHECK_GE(char_count, 0);
StackHandleScope<1> hs(self);
Handle<mirror::CharArray> h_char_array(
hs.NewHandle(shadow_frame->GetVRegReference(arg_offset + 2)->AsCharArray()));
Runtime* runtime = Runtime::Current();
gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
result->SetL(
mirror::String::AllocFromCharArray(self, char_count, h_char_array, offset, allocator));
}
// This allows creating the new style of String objects during compilation.
void UnstartedRuntime::UnstartedStringFactoryNewStringFromString(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
ObjPtr<mirror::String> to_copy = shadow_frame->GetVRegReference(arg_offset)->AsString();
if (to_copy == nullptr) {
AbortTransactionOrFail(self, "StringFactory.newStringFromString with null object");
return;
}
StackHandleScope<1> hs(self);
Handle<mirror::String> h_string(hs.NewHandle(to_copy));
Runtime* runtime = Runtime::Current();
gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
result->SetL(
mirror::String::AllocFromString(self, h_string->GetLength(), h_string, 0, allocator));
}
void UnstartedRuntime::UnstartedStringFastSubstring(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
jint start = shadow_frame->GetVReg(arg_offset + 1);
jint length = shadow_frame->GetVReg(arg_offset + 2);
DCHECK_GE(start, 0);
DCHECK_GE(length, 0);
StackHandleScope<1> hs(self);
Handle<mirror::String> h_string(
hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString()));
DCHECK_LE(start, h_string->GetLength());
DCHECK_LE(start + length, h_string->GetLength());
Runtime* runtime = Runtime::Current();
gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
result->SetL(mirror::String::AllocFromString(self, length, h_string, start, allocator));
}
// This allows getting the char array for new style of String objects during compilation.
void UnstartedRuntime::UnstartedStringToCharArray(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
StackHandleScope<1> hs(self);
Handle<mirror::String> string =
hs.NewHandle(shadow_frame->GetVRegReference(arg_offset)->AsString());
if (string == nullptr) {
AbortTransactionOrFail(self, "String.charAt with null object");
return;
}
result->SetL(mirror::String::ToCharArray(string, self));
}
// This allows statically initializing ConcurrentHashMap and SynchronousQueue.
void UnstartedRuntime::UnstartedReferenceGetReferent(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
const ObjPtr<mirror::Reference> ref = down_cast<mirror::Reference*>(
shadow_frame->GetVRegReference(arg_offset));
if (ref == nullptr) {
AbortTransactionOrFail(self, "Reference.getReferent() with null object");
return;
}
const ObjPtr<mirror::Object> referent =
Runtime::Current()->GetHeap()->GetReferenceProcessor()->GetReferent(self, ref);
result->SetL(referent);
}
// This allows statically initializing ConcurrentHashMap and SynchronousQueue. We use a somewhat
// conservative upper bound. We restrict the callers to SynchronousQueue and ConcurrentHashMap,
// where we can predict the behavior (somewhat).
// Note: this is required (instead of lazy initialization) as these classes are used in the static
// initialization of other classes, so will *use* the value.
void UnstartedRuntime::UnstartedRuntimeAvailableProcessors(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset ATTRIBUTE_UNUSED) {
if (CheckCallers(shadow_frame, { "void java.util.concurrent.SynchronousQueue.<clinit>()" })) {
// SynchronousQueue really only separates between single- and multiprocessor case. Return
// 8 as a conservative upper approximation.
result->SetI(8);
} else if (CheckCallers(shadow_frame,
{ "void java.util.concurrent.ConcurrentHashMap.<clinit>()" })) {
// ConcurrentHashMap uses it for striding. 8 still seems an OK general value, as it's likely
// a good upper bound.
// TODO: Consider resetting in the zygote?
result->SetI(8);
} else {
// Not supported.
AbortTransactionOrFail(self, "Accessing availableProcessors not allowed");
}
}
// This allows accessing ConcurrentHashMap/SynchronousQueue.
void UnstartedRuntime::UnstartedUnsafeCompareAndSwapLong(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Argument 0 is the Unsafe instance, skip.
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
return;
}
int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
int64_t expectedValue = shadow_frame->GetVRegLong(arg_offset + 4);
int64_t newValue = shadow_frame->GetVRegLong(arg_offset + 6);
bool success;
// Check whether we're in a transaction, call accordingly.
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj)) {
DCHECK(self->IsExceptionPending());
return;
}
success = obj->CasFieldStrongSequentiallyConsistent64<true>(MemberOffset(offset),
expectedValue,
newValue);
} else {
success = obj->CasFieldStrongSequentiallyConsistent64<false>(MemberOffset(offset),
expectedValue,
newValue);
}
result->SetZ(success ? 1 : 0);
}
void UnstartedRuntime::UnstartedUnsafeCompareAndSwapObject(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// Argument 0 is the Unsafe instance, skip.
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
return;
}
int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
mirror::Object* expected_value = shadow_frame->GetVRegReference(arg_offset + 4);
mirror::Object* new_value = shadow_frame->GetVRegReference(arg_offset + 5);
// Must use non transactional mode.
if (kUseReadBarrier) {
// Need to make sure the reference stored in the field is a to-space one before attempting the
// CAS or the CAS could fail incorrectly.
mirror::HeapReference<mirror::Object>* field_addr =
reinterpret_cast<mirror::HeapReference<mirror::Object>*>(
reinterpret_cast<uint8_t*>(obj) + static_cast<size_t>(offset));
ReadBarrier::Barrier<
mirror::Object,
/* kIsVolatile= */ false,
kWithReadBarrier,
/* kAlwaysUpdateField= */ true>(
obj,
MemberOffset(offset),
field_addr);
}
bool success;
// Check whether we're in a transaction, call accordingly.
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj) || !CheckWriteValueConstraint(self, new_value)) {
DCHECK(self->IsExceptionPending());
return;
}
success = obj->CasFieldObject<true>(MemberOffset(offset),
expected_value,
new_value,
CASMode::kStrong,
std::memory_order_seq_cst);
} else {
success = obj->CasFieldObject<false>(MemberOffset(offset),
expected_value,
new_value,
CASMode::kStrong,
std::memory_order_seq_cst);
}
result->SetZ(success ? 1 : 0);
}
void UnstartedRuntime::UnstartedUnsafeGetObjectVolatile(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
// Argument 0 is the Unsafe instance, skip.
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
return;
}
int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
ObjPtr<mirror::Object> value = obj->GetFieldObjectVolatile<mirror::Object>(MemberOffset(offset));
result->SetL(value);
}
void UnstartedRuntime::UnstartedUnsafePutObjectVolatile(
Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
// Argument 0 is the Unsafe instance, skip.
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
return;
}
int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
mirror::Object* value = shadow_frame->GetVRegReference(arg_offset + 4);
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj) || !CheckWriteValueConstraint(self, value)) {
DCHECK(self->IsExceptionPending());
return;
}
obj->SetFieldObjectVolatile<true>(MemberOffset(offset), value);
} else {
obj->SetFieldObjectVolatile<false>(MemberOffset(offset), value);
}
}
void UnstartedRuntime::UnstartedUnsafePutOrderedObject(
Thread* self, ShadowFrame* shadow_frame, JValue* result ATTRIBUTE_UNUSED, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
// Argument 0 is the Unsafe instance, skip.
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset + 1);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot access null object, retry at runtime.");
return;
}
int64_t offset = shadow_frame->GetVRegLong(arg_offset + 2);
mirror::Object* new_value = shadow_frame->GetVRegReference(arg_offset + 4);
std::atomic_thread_fence(std::memory_order_release);
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj) || !CheckWriteValueConstraint(self, new_value)) {
DCHECK(self->IsExceptionPending());
return;
}
obj->SetFieldObject<true>(MemberOffset(offset), new_value);
} else {
obj->SetFieldObject<false>(MemberOffset(offset), new_value);
}
}
// A cutout for Integer.parseInt(String). Note: this code is conservative and will bail instead
// of correctly handling the corner cases.
void UnstartedRuntime::UnstartedIntegerParseInt(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
return;
}
std::string string_value = obj->AsString()->ToModifiedUtf8();
if (string_value.empty()) {
AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
return;
}
const char* c_str = string_value.c_str();
char *end;
// Can we set errno to 0? Is this always a variable, and not a macro?
// Worst case, we'll incorrectly fail a transaction. Seems OK.
int64_t l = strtol(c_str, &end, 10);
if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
(errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
if (l == 0) {
// Check whether the string wasn't exactly zero.
if (string_value != "0") {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
} else if (*end != '\0') {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
result->SetI(static_cast<int32_t>(l));
}
// A cutout for Long.parseLong.
//
// Note: for now use code equivalent to Integer.parseInt, as the full range may not be supported
// well.
void UnstartedRuntime::UnstartedLongParseLong(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Cannot parse null string, retry at runtime.");
return;
}
std::string string_value = obj->AsString()->ToModifiedUtf8();
if (string_value.empty()) {
AbortTransactionOrFail(self, "Cannot parse empty string, retry at runtime.");
return;
}
const char* c_str = string_value.c_str();
char *end;
// Can we set errno to 0? Is this always a variable, and not a macro?
// Worst case, we'll incorrectly fail a transaction. Seems OK.
int64_t l = strtol(c_str, &end, 10);
// Note: comparing against int32_t min/max is intentional here.
if ((errno == ERANGE && l == LONG_MAX) || l > std::numeric_limits<int32_t>::max() ||
(errno == ERANGE && l == LONG_MIN) || l < std::numeric_limits<int32_t>::min()) {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
if (l == 0) {
// Check whether the string wasn't exactly zero.
if (string_value != "0") {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
} else if (*end != '\0') {
AbortTransactionOrFail(self, "Cannot parse string %s, retry at runtime.", c_str);
return;
}
result->SetJ(l);
}
void UnstartedRuntime::UnstartedMethodInvoke(
Thread* self, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
JNIEnvExt* env = self->GetJniEnv();
ScopedObjectAccessUnchecked soa(self);
ObjPtr<mirror::Object> java_method_obj = shadow_frame->GetVRegReference(arg_offset);
ScopedLocalRef<jobject> java_method(env,
java_method_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_method_obj));
ObjPtr<mirror::Object> java_receiver_obj = shadow_frame->GetVRegReference(arg_offset + 1);
ScopedLocalRef<jobject> java_receiver(env,
java_receiver_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_receiver_obj));
ObjPtr<mirror::Object> java_args_obj = shadow_frame->GetVRegReference(arg_offset + 2);
ScopedLocalRef<jobject> java_args(env,
java_args_obj == nullptr ? nullptr : env->AddLocalReference<jobject>(java_args_obj));
ScopedLocalRef<jobject> result_jobj(env,
InvokeMethod(soa, java_method.get(), java_receiver.get(), java_args.get()));
result->SetL(self->DecodeJObject(result_jobj.get()));
// Conservatively flag all exceptions as transaction aborts. This way we don't need to unwrap
// InvocationTargetExceptions.
if (self->IsExceptionPending()) {
AbortTransactionOrFail(self, "Failed Method.invoke");
}
}
void UnstartedRuntime::UnstartedSystemIdentityHashCode(
Thread* self ATTRIBUTE_UNUSED, ShadowFrame* shadow_frame, JValue* result, size_t arg_offset)
REQUIRES_SHARED(Locks::mutator_lock_) {
mirror::Object* obj = shadow_frame->GetVRegReference(arg_offset);
result->SetI((obj != nullptr) ? obj->IdentityHashCode() : 0);
}
// Checks whether the runtime is s64-bit. This is needed for the clinit of
// java.lang.invoke.VarHandle clinit. The clinit determines sets of
// available VarHandle accessors and these differ based on machine
// word size.
void UnstartedRuntime::UnstartedJNIVMRuntimeIs64Bit(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize();
jboolean is64bit = (pointer_size == PointerSize::k64) ? JNI_TRUE : JNI_FALSE;
result->SetZ(is64bit);
}
void UnstartedRuntime::UnstartedJNIVMRuntimeNewUnpaddedArray(
Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result) {
int32_t length = args[1];
DCHECK_GE(length, 0);
ObjPtr<mirror::Object> element_class = reinterpret_cast32<mirror::Object*>(args[0])->AsClass();
if (element_class == nullptr) {
AbortTransactionOrFail(self, "VMRuntime.newUnpaddedArray with null element_class.");
return;
}
Runtime* runtime = Runtime::Current();
ObjPtr<mirror::Class> array_class =
runtime->GetClassLinker()->FindArrayClass(self, element_class->AsClass());
DCHECK(array_class != nullptr);
gc::AllocatorType allocator = runtime->GetHeap()->GetCurrentAllocator();
result->SetL(mirror::Array::Alloc</*kIsInstrumented=*/ true, /*kFillUsable=*/ true>(
self, array_class, length, array_class->GetComponentSizeShift(), allocator));
}
void UnstartedRuntime::UnstartedJNIVMStackGetCallingClassLoader(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
result->SetL(nullptr);
}
void UnstartedRuntime::UnstartedJNIVMStackGetStackClass2(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
NthCallerVisitor visitor(self, 3);
visitor.WalkStack();
if (visitor.caller != nullptr) {
result->SetL(visitor.caller->GetDeclaringClass());
}
}
void UnstartedRuntime::UnstartedJNIMathLog(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
JValue value;
value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
result->SetD(log(value.GetD()));
}
void UnstartedRuntime::UnstartedJNIMathExp(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
JValue value;
value.SetJ((static_cast<uint64_t>(args[1]) << 32) | args[0]);
result->SetD(exp(value.GetD()));
}
void UnstartedRuntime::UnstartedJNIAtomicLongVMSupportsCS8(
Thread* self ATTRIBUTE_UNUSED,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args ATTRIBUTE_UNUSED,
JValue* result) {
result->SetZ(QuasiAtomic::LongAtomicsUseMutexes(Runtime::Current()->GetInstructionSet())
? 0
: 1);
}
void UnstartedRuntime::UnstartedJNIClassGetNameNative(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
StackHandleScope<1> hs(self);
result->SetL(mirror::Class::ComputeName(hs.NewHandle(receiver->AsClass())));
}
void UnstartedRuntime::UnstartedJNIDoubleLongBitsToDouble(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
uint64_t long_input = args[0] | (static_cast<uint64_t>(args[1]) << 32);
result->SetD(bit_cast<double>(long_input));
}
void UnstartedRuntime::UnstartedJNIFloatFloatToRawIntBits(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
result->SetI(args[0]);
}
void UnstartedRuntime::UnstartedJNIFloatIntBitsToFloat(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args, JValue* result) {
result->SetI(args[0]);
}
void UnstartedRuntime::UnstartedJNIObjectInternalClone(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
StackHandleScope<1> hs(self);
Handle<mirror::Object> h_receiver = hs.NewHandle(receiver);
result->SetL(mirror::Object::Clone(h_receiver, self));
}
void UnstartedRuntime::UnstartedJNIObjectNotifyAll(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result ATTRIBUTE_UNUSED) {
receiver->NotifyAll(self);
}
void UnstartedRuntime::UnstartedJNIStringCompareTo(Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver,
uint32_t* args,
JValue* result) {
ObjPtr<mirror::Object> rhs = reinterpret_cast32<mirror::Object*>(args[0]);
if (rhs == nullptr) {
AbortTransactionOrFail(self, "String.compareTo with null object.");
return;
}
result->SetI(receiver->AsString()->CompareTo(rhs->AsString()));
}
void UnstartedRuntime::UnstartedJNIStringIntern(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
result->SetL(receiver->AsString()->Intern());
}
void UnstartedRuntime::UnstartedJNIArrayCreateMultiArray(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args, JValue* result) {
StackHandleScope<2> hs(self);
auto h_class(hs.NewHandle(reinterpret_cast<mirror::Class*>(args[0])->AsClass()));
auto h_dimensions(hs.NewHandle(reinterpret_cast<mirror::IntArray*>(args[1])->AsIntArray()));
result->SetL(mirror::Array::CreateMultiArray(self, h_class, h_dimensions));
}
void UnstartedRuntime::UnstartedJNIArrayCreateObjectArray(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args, JValue* result) {
int32_t length = static_cast<int32_t>(args[1]);
if (length < 0) {
ThrowNegativeArraySizeException(length);
return;
}
ObjPtr<mirror::Class> element_class = reinterpret_cast<mirror::Class*>(args[0])->AsClass();
Runtime* runtime = Runtime::Current();
ClassLinker* class_linker = runtime->GetClassLinker();
ObjPtr<mirror::Class> array_class = class_linker->FindArrayClass(self, element_class);
if (UNLIKELY(array_class == nullptr)) {
CHECK(self->IsExceptionPending());
return;
}
DCHECK(array_class->IsObjectArrayClass());
ObjPtr<mirror::Array> new_array = mirror::ObjectArray<mirror::Object>::Alloc(
self, array_class, length, runtime->GetHeap()->GetCurrentAllocator());
result->SetL(new_array);
}
void UnstartedRuntime::UnstartedJNIThrowableNativeFillInStackTrace(
Thread* self, ArtMethod* method ATTRIBUTE_UNUSED, mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
ScopedObjectAccessUnchecked soa(self);
if (Runtime::Current()->IsActiveTransaction()) {
result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<true>(soa)));
} else {
result->SetL(soa.Decode<mirror::Object>(self->CreateInternalStackTrace<false>(soa)));
}
}
void UnstartedRuntime::UnstartedJNIByteOrderIsLittleEndian(
Thread* self ATTRIBUTE_UNUSED, ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED, uint32_t* args ATTRIBUTE_UNUSED, JValue* result) {
result->SetZ(JNI_TRUE);
}
void UnstartedRuntime::UnstartedJNIUnsafeCompareAndSwapInt(
Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result) {
ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Unsafe.compareAndSwapInt with null object.");
return;
}
jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
jint expectedValue = args[3];
jint newValue = args[4];
bool success;
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj)) {
DCHECK(self->IsExceptionPending());
return;
}
success = obj->CasField32<true>(MemberOffset(offset),
expectedValue,
newValue,
CASMode::kStrong,
std::memory_order_seq_cst);
} else {
success = obj->CasField32<false>(MemberOffset(offset),
expectedValue,
newValue,
CASMode::kStrong,
std::memory_order_seq_cst);
}
result->SetZ(success ? JNI_TRUE : JNI_FALSE);
}
void UnstartedRuntime::UnstartedJNIUnsafeGetIntVolatile(Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result) {
ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Unsafe.compareAndSwapIntVolatile with null object.");
return;
}
jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
result->SetI(obj->GetField32Volatile(MemberOffset(offset)));
}
void UnstartedRuntime::UnstartedJNIUnsafePutObject(Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result ATTRIBUTE_UNUSED) {
ObjPtr<mirror::Object> obj = reinterpret_cast32<mirror::Object*>(args[0]);
if (obj == nullptr) {
AbortTransactionOrFail(self, "Unsafe.putObject with null object.");
return;
}
jlong offset = (static_cast<uint64_t>(args[2]) << 32) | args[1];
ObjPtr<mirror::Object> new_value = reinterpret_cast32<mirror::Object*>(args[3]);
if (Runtime::Current()->IsActiveTransaction()) {
if (!CheckWriteConstraint(self, obj) || !CheckWriteValueConstraint(self, new_value)) {
DCHECK(self->IsExceptionPending());
return;
}
obj->SetFieldObject<true>(MemberOffset(offset), new_value);
} else {
obj->SetFieldObject<false>(MemberOffset(offset), new_value);
}
}
void UnstartedRuntime::UnstartedJNIUnsafeGetArrayBaseOffsetForComponentType(
Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result) {
ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
if (component == nullptr) {
AbortTransactionOrFail(self, "Unsafe.getArrayBaseOffsetForComponentType with null component.");
return;
}
Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
result->SetI(mirror::Array::DataOffset(Primitive::ComponentSize(primitive_type)).Int32Value());
}
void UnstartedRuntime::UnstartedJNIUnsafeGetArrayIndexScaleForComponentType(
Thread* self,
ArtMethod* method ATTRIBUTE_UNUSED,
mirror::Object* receiver ATTRIBUTE_UNUSED,
uint32_t* args,
JValue* result) {
ObjPtr<mirror::Object> component = reinterpret_cast32<mirror::Object*>(args[0]);
if (component == nullptr) {
AbortTransactionOrFail(self, "Unsafe.getArrayIndexScaleForComponentType with null component.");
return;
}
Primitive::Type primitive_type = component->AsClass()->GetPrimitiveType();
result->SetI(Primitive::ComponentSize(primitive_type));
}
using InvokeHandler = void(*)(Thread* self,
ShadowFrame* shadow_frame,
JValue* result,
size_t arg_size);
using JNIHandler = void(*)(Thread* self,
ArtMethod* method,
mirror::Object* receiver,
uint32_t* args,
JValue* result);
static bool tables_initialized_ = false;
static std::unordered_map<std::string, InvokeHandler> invoke_handlers_;
static std::unordered_map<std::string, JNIHandler> jni_handlers_;
void UnstartedRuntime::InitializeInvokeHandlers() {
#define UNSTARTED_DIRECT(ShortName, Sig) \
invoke_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::Unstarted ## ShortName));
#include "unstarted_runtime_list.h"
UNSTARTED_RUNTIME_DIRECT_LIST(UNSTARTED_DIRECT)
#undef UNSTARTED_RUNTIME_DIRECT_LIST
#undef UNSTARTED_RUNTIME_JNI_LIST
#undef UNSTARTED_DIRECT
}
void UnstartedRuntime::InitializeJNIHandlers() {
#define UNSTARTED_JNI(ShortName, Sig) \
jni_handlers_.insert(std::make_pair(Sig, & UnstartedRuntime::UnstartedJNI ## ShortName));
#include "unstarted_runtime_list.h"
UNSTARTED_RUNTIME_JNI_LIST(UNSTARTED_JNI)
#undef UNSTARTED_RUNTIME_DIRECT_LIST
#undef UNSTARTED_RUNTIME_JNI_LIST
#undef UNSTARTED_JNI
}
void UnstartedRuntime::Initialize() {
CHECK(!tables_initialized_);
InitializeInvokeHandlers();
InitializeJNIHandlers();
tables_initialized_ = true;
}
void UnstartedRuntime::Invoke(Thread* self, const CodeItemDataAccessor& accessor,
ShadowFrame* shadow_frame, JValue* result, size_t arg_offset) {
// In a runtime that's not started we intercept certain methods to avoid complicated dependency
// problems in core libraries.
CHECK(tables_initialized_);
std::string name(ArtMethod::PrettyMethod(shadow_frame->GetMethod()));
const auto& iter = invoke_handlers_.find(name);
if (iter != invoke_handlers_.end()) {
// Clear out the result in case it's not zeroed out.
result->SetL(nullptr);
// Push the shadow frame. This is so the failing method can be seen in abort dumps.
self->PushShadowFrame(shadow_frame);
(*iter->second)(self, shadow_frame, result, arg_offset);
self->PopShadowFrame();
} else {
// Not special, continue with regular interpreter execution.
ArtInterpreterToInterpreterBridge(self, accessor, shadow_frame, result);
}
}
// Hand select a number of methods to be run in a not yet started runtime without using JNI.
void UnstartedRuntime::Jni(Thread* self, ArtMethod* method, mirror::Object* receiver,
uint32_t* args, JValue* result) {
std::string name(ArtMethod::PrettyMethod(method));
const auto& iter = jni_handlers_.find(name);
if (iter != jni_handlers_.end()) {
// Clear out the result in case it's not zeroed out.
result->SetL(nullptr);
(*iter->second)(self, method, receiver, args, result);
} else if (Runtime::Current()->IsActiveTransaction()) {
AbortTransactionF(self, "Attempt to invoke native method in non-started runtime: %s",
name.c_str());
} else {
LOG(FATAL) << "Calling native method " << ArtMethod::PrettyMethod(method) << " in an unstarted "
"non-transactional runtime";
}
}
} // namespace interpreter
} // namespace art
|