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 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601
|
/*
* Copyright (C) 2011 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 "method_verifier-inl.h"
#include <ostream>
#include "android-base/stringprintf.h"
#include "art_field-inl.h"
#include "art_method-inl.h"
#include "base/aborting.h"
#include "base/enums.h"
#include "base/leb128.h"
#include "base/indenter.h"
#include "base/logging.h" // For VLOG.
#include "base/mutex-inl.h"
#include "base/sdk_version.h"
#include "base/stl_util.h"
#include "base/systrace.h"
#include "base/time_utils.h"
#include "base/utils.h"
#include "class_linker.h"
#include "class_root.h"
#include "compiler_callbacks.h"
#include "dex/class_accessor-inl.h"
#include "dex/descriptors_names.h"
#include "dex/dex_file-inl.h"
#include "dex/dex_file_exception_helpers.h"
#include "dex/dex_instruction-inl.h"
#include "dex/dex_instruction_utils.h"
#include "experimental_flags.h"
#include "gc/accounting/card_table-inl.h"
#include "handle_scope-inl.h"
#include "intern_table.h"
#include "mirror/class-inl.h"
#include "mirror/class.h"
#include "mirror/class_loader.h"
#include "mirror/dex_cache-inl.h"
#include "mirror/method_handle_impl.h"
#include "mirror/method_type.h"
#include "mirror/object-inl.h"
#include "mirror/object_array-inl.h"
#include "mirror/var_handle.h"
#include "obj_ptr-inl.h"
#include "reg_type-inl.h"
#include "register_line-inl.h"
#include "runtime.h"
#include "scoped_newline.h"
#include "scoped_thread_state_change-inl.h"
#include "stack.h"
#include "vdex_file.h"
#include "verifier/method_verifier.h"
#include "verifier_compiler_binding.h"
#include "verifier_deps.h"
namespace art {
namespace verifier {
using android::base::StringPrintf;
static constexpr bool kTimeVerifyMethod = !kIsDebugBuild;
PcToRegisterLineTable::PcToRegisterLineTable(ScopedArenaAllocator& allocator)
: register_lines_(allocator.Adapter(kArenaAllocVerifier)) {}
void PcToRegisterLineTable::Init(RegisterTrackingMode mode,
InstructionFlags* flags,
uint32_t insns_size,
uint16_t registers_size,
ScopedArenaAllocator& allocator,
RegTypeCache* reg_types) {
DCHECK_GT(insns_size, 0U);
register_lines_.resize(insns_size);
for (uint32_t i = 0; i < insns_size; i++) {
bool interesting = false;
switch (mode) {
case kTrackRegsAll:
interesting = flags[i].IsOpcode();
break;
case kTrackCompilerInterestPoints:
interesting = flags[i].IsCompileTimeInfoPoint() || flags[i].IsBranchTarget();
break;
case kTrackRegsBranches:
interesting = flags[i].IsBranchTarget();
break;
}
if (interesting) {
register_lines_[i].reset(RegisterLine::Create(registers_size, allocator, reg_types));
}
}
}
PcToRegisterLineTable::~PcToRegisterLineTable() {}
namespace impl {
namespace {
enum class CheckAccess {
kNo,
kOnResolvedClass,
kYes,
};
enum class FieldAccessType {
kAccGet,
kAccPut
};
// Instruction types that are not marked as throwing (because they normally would not), but for
// historical reasons may do so. These instructions cannot be marked kThrow as that would introduce
// a general flow that is unwanted.
//
// Note: Not implemented as Instruction::Flags value as that set is full and we'd need to increase
// the struct size (making it a non-power-of-two) for a single element.
//
// Note: This should eventually be removed.
constexpr bool IsCompatThrow(Instruction::Code opcode) {
return opcode == Instruction::Code::RETURN_OBJECT;
}
template <bool kVerifierDebug>
class MethodVerifier final : public ::art::verifier::MethodVerifier {
public:
bool IsInstanceConstructor() const {
return IsConstructor() && !IsStatic();
}
const RegType& ResolveCheckedClass(dex::TypeIndex class_idx) override
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(!HasFailures());
const RegType& result = ResolveClass<CheckAccess::kYes>(class_idx);
DCHECK(!HasFailures());
return result;
}
void FindLocksAtDexPc() REQUIRES_SHARED(Locks::mutator_lock_);
private:
MethodVerifier(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
const DexFile* dex_file,
const dex::CodeItem* code_item,
uint32_t method_idx,
bool can_load_classes,
bool allow_thread_suspension,
bool allow_soft_failures,
bool aot_mode,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
ArtMethod* method,
uint32_t access_flags,
bool need_precise_constants,
bool verify_to_dump,
bool fill_register_lines,
uint32_t api_level) REQUIRES_SHARED(Locks::mutator_lock_)
: art::verifier::MethodVerifier(self,
class_linker,
arena_pool,
dex_file,
code_item,
method_idx,
can_load_classes,
allow_thread_suspension,
allow_soft_failures,
aot_mode),
method_being_verified_(method),
method_access_flags_(access_flags),
return_type_(nullptr),
dex_cache_(dex_cache),
class_loader_(class_loader),
class_def_(class_def),
declaring_class_(nullptr),
interesting_dex_pc_(-1),
monitor_enter_dex_pcs_(nullptr),
need_precise_constants_(need_precise_constants),
verify_to_dump_(verify_to_dump),
allow_thread_suspension_(allow_thread_suspension),
is_constructor_(false),
fill_register_lines_(fill_register_lines),
api_level_(api_level == 0 ? std::numeric_limits<uint32_t>::max() : api_level) {
}
void UninstantiableError(const char* descriptor) {
Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
<< "non-instantiable klass " << descriptor;
}
static bool IsInstantiableOrPrimitive(ObjPtr<mirror::Class> klass)
REQUIRES_SHARED(Locks::mutator_lock_) {
return klass->IsInstantiable() || klass->IsPrimitive();
}
// Is the method being verified a constructor? See the comment on the field.
bool IsConstructor() const {
return is_constructor_;
}
// Is the method verified static?
bool IsStatic() const {
return (method_access_flags_ & kAccStatic) != 0;
}
// Adds the given string to the beginning of the last failure message.
void PrependToLastFailMessage(std::string prepend) {
size_t failure_num = failure_messages_.size();
DCHECK_NE(failure_num, 0U);
std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
prepend += last_fail_message->str();
failure_messages_[failure_num - 1] = new std::ostringstream(prepend, std::ostringstream::ate);
delete last_fail_message;
}
// Adds the given string to the end of the last failure message.
void AppendToLastFailMessage(const std::string& append) {
size_t failure_num = failure_messages_.size();
DCHECK_NE(failure_num, 0U);
std::ostringstream* last_fail_message = failure_messages_[failure_num - 1];
(*last_fail_message) << append;
}
/*
* Compute the width of the instruction at each address in the instruction stream, and store it in
* insn_flags_. Addresses that are in the middle of an instruction, or that are part of switch
* table data, are not touched (so the caller should probably initialize "insn_flags" to zero).
*
* The "new_instance_count_" and "monitor_enter_count_" fields in vdata are also set.
*
* Performs some static checks, notably:
* - opcode of first instruction begins at index 0
* - only documented instructions may appear
* - each instruction follows the last
* - last byte of last instruction is at (code_length-1)
*
* Logs an error and returns "false" on failure.
*/
bool ComputeWidthsAndCountOps();
/*
* Set the "in try" flags for all instructions protected by "try" statements. Also sets the
* "branch target" flags for exception handlers.
*
* Call this after widths have been set in "insn_flags".
*
* Returns "false" if something in the exception table looks fishy, but we're expecting the
* exception table to be somewhat sane.
*/
bool ScanTryCatchBlocks() REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Perform static verification on all instructions in a method.
*
* Walks through instructions in a method calling VerifyInstruction on each.
*/
template <bool kAllowRuntimeOnlyInstructions>
bool VerifyInstructions();
/*
* Perform static verification on an instruction.
*
* As a side effect, this sets the "branch target" flags in InsnFlags.
*
* "(CF)" items are handled during code-flow analysis.
*
* v3 4.10.1
* - target of each jump and branch instruction must be valid
* - targets of switch statements must be valid
* - operands referencing constant pool entries must be valid
* - (CF) operands of getfield, putfield, getstatic, putstatic must be valid
* - (CF) operands of method invocation instructions must be valid
* - (CF) only invoke-direct can call a method starting with '<'
* - (CF) <clinit> must never be called explicitly
* - operands of instanceof, checkcast, new (and variants) must be valid
* - new-array[-type] limited to 255 dimensions
* - can't use "new" on an array class
* - (?) limit dimensions in multi-array creation
* - local variable load/store register values must be in valid range
*
* v3 4.11.1.2
* - branches must be within the bounds of the code array
* - targets of all control-flow instructions are the start of an instruction
* - register accesses fall within range of allocated registers
* - (N/A) access to constant pool must be of appropriate type
* - code does not end in the middle of an instruction
* - execution cannot fall off the end of the code
* - (earlier) for each exception handler, the "try" area must begin and
* end at the start of an instruction (end can be at the end of the code)
* - (earlier) for each exception handler, the handler must start at a valid
* instruction
*/
template <bool kAllowRuntimeOnlyInstructions>
bool VerifyInstruction(const Instruction* inst, uint32_t code_offset);
/* Ensure that the register index is valid for this code item. */
bool CheckRegisterIndex(uint32_t idx) {
if (UNLIKELY(idx >= code_item_accessor_.RegistersSize())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register index out of range (" << idx << " >= "
<< code_item_accessor_.RegistersSize() << ")";
return false;
}
return true;
}
/* Ensure that the wide register index is valid for this code item. */
bool CheckWideRegisterIndex(uint32_t idx) {
if (UNLIKELY(idx + 1 >= code_item_accessor_.RegistersSize())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "wide register index out of range (" << idx
<< "+1 >= " << code_item_accessor_.RegistersSize() << ")";
return false;
}
return true;
}
// Perform static checks on an instruction referencing a CallSite. All we do here is ensure that
// the call site index is in the valid range.
bool CheckCallSiteIndex(uint32_t idx) {
uint32_t limit = dex_file_->NumCallSiteIds();
if (UNLIKELY(idx >= limit)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad call site index " << idx << " (max "
<< limit << ")";
return false;
}
return true;
}
// Perform static checks on a field Get or set instruction. All we do here is ensure that the
// field index is in the valid range.
bool CheckFieldIndex(uint32_t idx) {
if (UNLIKELY(idx >= dex_file_->GetHeader().field_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad field index " << idx << " (max "
<< dex_file_->GetHeader().field_ids_size_ << ")";
return false;
}
return true;
}
// Perform static checks on a method invocation instruction. All we do here is ensure that the
// method index is in the valid range.
bool CheckMethodIndex(uint32_t idx) {
if (UNLIKELY(idx >= dex_file_->GetHeader().method_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method index " << idx << " (max "
<< dex_file_->GetHeader().method_ids_size_ << ")";
return false;
}
return true;
}
// Perform static checks on an instruction referencing a constant method handle. All we do here
// is ensure that the method index is in the valid range.
bool CheckMethodHandleIndex(uint32_t idx) {
uint32_t limit = dex_file_->NumMethodHandles();
if (UNLIKELY(idx >= limit)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad method handle index " << idx << " (max "
<< limit << ")";
return false;
}
return true;
}
// Perform static checks on a "new-instance" instruction. Specifically, make sure the class
// reference isn't for an array class.
bool CheckNewInstance(dex::TypeIndex idx);
// Perform static checks on a prototype indexing instruction. All we do here is ensure that the
// prototype index is in the valid range.
bool CheckPrototypeIndex(uint32_t idx) {
if (UNLIKELY(idx >= dex_file_->GetHeader().proto_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad prototype index " << idx << " (max "
<< dex_file_->GetHeader().proto_ids_size_ << ")";
return false;
}
return true;
}
/* Ensure that the string index is in the valid range. */
bool CheckStringIndex(uint32_t idx) {
if (UNLIKELY(idx >= dex_file_->GetHeader().string_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad string index " << idx << " (max "
<< dex_file_->GetHeader().string_ids_size_ << ")";
return false;
}
return true;
}
// Perform static checks on an instruction that takes a class constant. Ensure that the class
// index is in the valid range.
bool CheckTypeIndex(dex::TypeIndex idx) {
if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
return true;
}
// Perform static checks on a "new-array" instruction. Specifically, make sure they aren't
// creating an array of arrays that causes the number of dimensions to exceed 255.
bool CheckNewArray(dex::TypeIndex idx);
// Verify an array data table. "cur_offset" is the offset of the fill-array-data instruction.
bool CheckArrayData(uint32_t cur_offset);
// Verify that the target of a branch instruction is valid. We don't expect code to jump directly
// into an exception handler, but it's valid to do so as long as the target isn't a
// "move-exception" instruction. We verify that in a later stage.
// The dex format forbids certain instructions from branching to themselves.
// Updates "insn_flags_", setting the "branch target" flag.
bool CheckBranchTarget(uint32_t cur_offset);
// Verify a switch table. "cur_offset" is the offset of the switch instruction.
// Updates "insn_flags_", setting the "branch target" flag.
bool CheckSwitchTargets(uint32_t cur_offset);
// Check the register indices used in a "vararg" instruction, such as invoke-virtual or
// filled-new-array.
// - vA holds word count (0-5), args[] have values.
// There are some tests we don't do here, e.g. we don't try to verify that invoking a method that
// takes a double is done with consecutive registers. This requires parsing the target method
// signature, which we will be doing later on during the code flow analysis.
bool CheckVarArgRegs(uint32_t vA, uint32_t arg[]) {
uint16_t registers_size = code_item_accessor_.RegistersSize();
for (uint32_t idx = 0; idx < vA; idx++) {
if (UNLIKELY(arg[idx] >= registers_size)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index (" << arg[idx]
<< ") in non-range invoke (>= " << registers_size << ")";
return false;
}
}
return true;
}
// Check the register indices used in a "vararg/range" instruction, such as invoke-virtual/range
// or filled-new-array/range.
// - vA holds word count, vC holds index of first reg.
bool CheckVarArgRangeRegs(uint32_t vA, uint32_t vC) {
uint16_t registers_size = code_item_accessor_.RegistersSize();
// vA/vC are unsigned 8-bit/16-bit quantities for /range instructions, so there's no risk of
// integer overflow when adding them here.
if (UNLIKELY(vA + vC > registers_size)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid reg index " << vA << "+" << vC
<< " in range invoke (> " << registers_size << ")";
return false;
}
return true;
}
// Checks the method matches the expectations required to be signature polymorphic.
bool CheckSignaturePolymorphicMethod(ArtMethod* method) REQUIRES_SHARED(Locks::mutator_lock_);
// Checks the invoked receiver matches the expectations for signature polymorphic methods.
bool CheckSignaturePolymorphicReceiver(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
// Extract the relative offset from a branch instruction.
// Returns "false" on failure (e.g. this isn't a branch instruction).
bool GetBranchOffset(uint32_t cur_offset, int32_t* pOffset, bool* pConditional,
bool* selfOkay);
/* Perform detailed code-flow analysis on a single method. */
bool VerifyCodeFlow() REQUIRES_SHARED(Locks::mutator_lock_);
// Set the register types for the first instruction in the method based on the method signature.
// This has the side-effect of validating the signature.
bool SetTypesFromSignature() REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Perform code flow on a method.
*
* The basic strategy is as outlined in v3 4.11.1.2: set the "changed" bit on the first
* instruction, process it (setting additional "changed" bits), and repeat until there are no
* more.
*
* v3 4.11.1.1
* - (N/A) operand stack is always the same size
* - operand stack [registers] contain the correct types of values
* - local variables [registers] contain the correct types of values
* - methods are invoked with the appropriate arguments
* - fields are assigned using values of appropriate types
* - opcodes have the correct type values in operand registers
* - there is never an uninitialized class instance in a local variable in code protected by an
* exception handler (operand stack is okay, because the operand stack is discarded when an
* exception is thrown) [can't know what's a local var w/o the debug info -- should fall out of
* register typing]
*
* v3 4.11.1.2
* - execution cannot fall off the end of the code
*
* (We also do many of the items described in the "static checks" sections, because it's easier to
* do them here.)
*
* We need an array of RegType values, one per register, for every instruction. If the method uses
* monitor-enter, we need extra data for every register, and a stack for every "interesting"
* instruction. In theory this could become quite large -- up to several megabytes for a monster
* function.
*
* NOTE:
* The spec forbids backward branches when there's an uninitialized reference in a register. The
* idea is to prevent something like this:
* loop:
* move r1, r0
* new-instance r0, MyClass
* ...
* if-eq rN, loop // once
* initialize r0
*
* This leaves us with two different instances, both allocated by the same instruction, but only
* one is initialized. The scheme outlined in v3 4.11.1.4 wouldn't catch this, so they work around
* it by preventing backward branches. We achieve identical results without restricting code
* reordering by specifying that you can't execute the new-instance instruction if a register
* contains an uninitialized instance created by that same instruction.
*/
template <bool kMonitorDexPCs>
bool CodeFlowVerifyMethod() REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Perform verification for a single instruction.
*
* This requires fully decoding the instruction to determine the effect it has on registers.
*
* Finds zero or more following instructions and sets the "changed" flag if execution at that
* point needs to be (re-)evaluated. Register changes are merged into "reg_types_" at the target
* addresses. Does not set or clear any other flags in "insn_flags_".
*/
bool CodeFlowVerifyInstruction(uint32_t* start_guess)
REQUIRES_SHARED(Locks::mutator_lock_);
// Perform verification of a new array instruction
void VerifyNewArray(const Instruction* inst, bool is_filled, bool is_range)
REQUIRES_SHARED(Locks::mutator_lock_);
// Helper to perform verification on puts of primitive type.
void VerifyPrimitivePut(const RegType& target_type, const RegType& insn_type,
const uint32_t vregA) REQUIRES_SHARED(Locks::mutator_lock_);
// Perform verification of an aget instruction. The destination register's type will be set to
// be that of component type of the array unless the array type is unknown, in which case a
// bottom type inferred from the type of instruction is used. is_primitive is false for an
// aget-object.
void VerifyAGet(const Instruction* inst, const RegType& insn_type,
bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
// Perform verification of an aput instruction.
void VerifyAPut(const Instruction* inst, const RegType& insn_type,
bool is_primitive) REQUIRES_SHARED(Locks::mutator_lock_);
// Lookup instance field and fail for resolution violations
ArtField* GetInstanceField(const RegType& obj_type, int field_idx)
REQUIRES_SHARED(Locks::mutator_lock_);
// Lookup static field and fail for resolution violations
ArtField* GetStaticField(int field_idx) REQUIRES_SHARED(Locks::mutator_lock_);
// Perform verification of an iget/sget/iput/sput instruction.
template <FieldAccessType kAccType>
void VerifyISFieldAccess(const Instruction* inst, const RegType& insn_type,
bool is_primitive, bool is_static)
REQUIRES_SHARED(Locks::mutator_lock_);
// Resolves a class based on an index and, if C is kYes, performs access checks to ensure
// the referrer can access the resolved class.
template <CheckAccess C>
const RegType& ResolveClass(dex::TypeIndex class_idx)
REQUIRES_SHARED(Locks::mutator_lock_);
/*
* For the "move-exception" instruction at "work_insn_idx_", which must be at an exception handler
* address, determine the Join of all exceptions that can land here. Fails if no matching
* exception handler can be found or if the Join of exception types fails.
*/
const RegType& GetCaughtExceptionType()
REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Resolves a method based on an index and performs access checks to ensure
* the referrer can access the resolved method.
* Does not throw exceptions.
*/
ArtMethod* ResolveMethodAndCheckAccess(uint32_t method_idx, MethodType method_type)
REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Verify the arguments to a method. We're executing in "method", making
* a call to the method reference in vB.
*
* If this is a "direct" invoke, we allow calls to <init>. For calls to
* <init>, the first argument may be an uninitialized reference. Otherwise,
* calls to anything starting with '<' will be rejected, as will any
* uninitialized reference arguments.
*
* For non-static method calls, this will verify that the method call is
* appropriate for the "this" argument.
*
* The method reference is in vBBBB. The "is_range" parameter determines
* whether we use 0-4 "args" values or a range of registers defined by
* vAA and vCCCC.
*
* Widening conversions on integers and references are allowed, but
* narrowing conversions are not.
*
* Returns the resolved method on success, null on failure (with *failure
* set appropriately).
*/
ArtMethod* VerifyInvocationArgs(const Instruction* inst, MethodType method_type, bool is_range)
REQUIRES_SHARED(Locks::mutator_lock_);
// Similar checks to the above, but on the proto. Will be used when the method cannot be
// resolved.
void VerifyInvocationArgsUnresolvedMethod(const Instruction* inst, MethodType method_type,
bool is_range)
REQUIRES_SHARED(Locks::mutator_lock_);
template <class T>
ArtMethod* VerifyInvocationArgsFromIterator(T* it, const Instruction* inst,
MethodType method_type, bool is_range,
ArtMethod* res_method)
REQUIRES_SHARED(Locks::mutator_lock_);
/*
* Verify the arguments present for a call site. Returns "true" if all is well, "false" otherwise.
*/
bool CheckCallSite(uint32_t call_site_idx);
/*
* Verify that the target instruction is not "move-exception". It's important that the only way
* to execute a move-exception is as the first instruction of an exception handler.
* Returns "true" if all is well, "false" if the target instruction is move-exception.
*/
bool CheckNotMoveException(const uint16_t* insns, int insn_idx) {
if ((insns[insn_idx] & 0xff) == Instruction::MOVE_EXCEPTION) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-exception";
return false;
}
return true;
}
/*
* Verify that the target instruction is not "move-result". It is important that we cannot
* branch to move-result instructions, but we have to make this a distinct check instead of
* adding it to CheckNotMoveException, because it is legal to continue into "move-result"
* instructions - as long as the previous instruction was an invoke, which is checked elsewhere.
*/
bool CheckNotMoveResult(const uint16_t* insns, int insn_idx) {
if (((insns[insn_idx] & 0xff) >= Instruction::MOVE_RESULT) &&
((insns[insn_idx] & 0xff) <= Instruction::MOVE_RESULT_OBJECT)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid use of move-result*";
return false;
}
return true;
}
/*
* Verify that the target instruction is not "move-result" or "move-exception". This is to
* be used when checking branch and switch instructions, but not instructions that can
* continue.
*/
bool CheckNotMoveExceptionOrMoveResult(const uint16_t* insns, int insn_idx) {
return (CheckNotMoveException(insns, insn_idx) && CheckNotMoveResult(insns, insn_idx));
}
/*
* Control can transfer to "next_insn". Merge the registers from merge_line into the table at
* next_insn, and set the changed flag on the target address if any of the registers were changed.
* In the case of fall-through, update the merge line on a change as its the working line for the
* next instruction.
* Returns "false" if an error is encountered.
*/
bool UpdateRegisters(uint32_t next_insn, RegisterLine* merge_line, bool update_merge_line)
REQUIRES_SHARED(Locks::mutator_lock_);
// Return the register type for the method.
const RegType& GetMethodReturnType() REQUIRES_SHARED(Locks::mutator_lock_);
// Get a type representing the declaring class of the method.
const RegType& GetDeclaringClass() REQUIRES_SHARED(Locks::mutator_lock_) {
if (declaring_class_ == nullptr) {
const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
const char* descriptor
= dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(method_id.class_idx_));
if (method_being_verified_ != nullptr) {
ObjPtr<mirror::Class> klass = method_being_verified_->GetDeclaringClass();
declaring_class_ = &FromClass(descriptor, klass, klass->CannotBeAssignedFromOtherTypes());
} else {
declaring_class_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
}
}
return *declaring_class_;
}
InstructionFlags* CurrentInsnFlags() {
return &GetModifiableInstructionFlags(work_insn_idx_);
}
const RegType& DetermineCat1Constant(int32_t value, bool precise)
REQUIRES_SHARED(Locks::mutator_lock_);
// Try to create a register type from the given class. In case a precise type is requested, but
// the class is not instantiable, a soft error (of type NO_CLASS) will be enqueued and a
// non-precise reference will be returned.
// Note: we reuse NO_CLASS as this will throw an exception at runtime, when the failing class is
// actually touched.
const RegType& FromClass(const char* descriptor, ObjPtr<mirror::Class> klass, bool precise)
REQUIRES_SHARED(Locks::mutator_lock_) {
DCHECK(klass != nullptr);
if (precise && !klass->IsInstantiable() && !klass->IsPrimitive()) {
Fail(VerifyError::VERIFY_ERROR_NO_CLASS) << "Could not create precise reference for "
<< "non-instantiable klass " << descriptor;
precise = false;
}
return reg_types_.FromClass(descriptor, klass, precise);
}
ALWAYS_INLINE bool FailOrAbort(bool condition, const char* error_msg, uint32_t work_insn_idx);
ALWAYS_INLINE InstructionFlags& GetModifiableInstructionFlags(size_t index) {
return insn_flags_[index];
}
// Returns the method index of an invoke instruction.
uint16_t GetMethodIdxOfInvoke(const Instruction* inst)
REQUIRES_SHARED(Locks::mutator_lock_) {
switch (inst->Opcode()) {
case Instruction::INVOKE_VIRTUAL_RANGE_QUICK:
case Instruction::INVOKE_VIRTUAL_QUICK: {
DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_)
<< dex_file_->PrettyMethod(dex_method_idx_, true) << "@" << work_insn_idx_;
DCHECK(method_being_verified_ != nullptr);
uint16_t method_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
CHECK_NE(method_idx, DexFile::kDexNoIndex16);
return method_idx;
}
default: {
return inst->VRegB();
}
}
}
// Returns the field index of a field access instruction.
uint16_t GetFieldIdxOfFieldAccess(const Instruction* inst, bool is_static)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (is_static) {
return inst->VRegB_21c();
} else if (inst->IsQuickened()) {
DCHECK(Runtime::Current()->IsStarted() || verify_to_dump_);
DCHECK(method_being_verified_ != nullptr);
uint16_t field_idx = method_being_verified_->GetIndexFromQuickening(work_insn_idx_);
CHECK_NE(field_idx, DexFile::kDexNoIndex16);
return field_idx;
} else {
return inst->VRegC_22c();
}
}
// Run verification on the method. Returns true if verification completes and false if the input
// has an irrecoverable corruption.
bool Verify() override REQUIRES_SHARED(Locks::mutator_lock_);
// Dump the failures encountered by the verifier.
std::ostream& DumpFailures(std::ostream& os) {
DCHECK_EQ(failures_.size(), failure_messages_.size());
for (const auto* stream : failure_messages_) {
os << stream->str() << "\n";
}
return os;
}
// Dump the state of the verifier, namely each instruction, what flags are set on it, register
// information
void Dump(std::ostream& os) REQUIRES_SHARED(Locks::mutator_lock_) {
VariableIndentationOutputStream vios(&os);
Dump(&vios);
}
void Dump(VariableIndentationOutputStream* vios) REQUIRES_SHARED(Locks::mutator_lock_);
bool HandleMoveException(const Instruction* inst) REQUIRES_SHARED(Locks::mutator_lock_);
ArtMethod* method_being_verified_; // Its ArtMethod representation if known.
const uint32_t method_access_flags_; // Method's access flags.
const RegType* return_type_; // Lazily computed return type of the method.
// The dex_cache for the declaring class of the method.
Handle<mirror::DexCache> dex_cache_ GUARDED_BY(Locks::mutator_lock_);
// The class loader for the declaring class of the method.
Handle<mirror::ClassLoader> class_loader_ GUARDED_BY(Locks::mutator_lock_);
const dex::ClassDef& class_def_; // The class def of the declaring class of the method.
const RegType* declaring_class_; // Lazily computed reg type of the method's declaring class.
// The dex PC of a FindLocksAtDexPc request, -1 otherwise.
uint32_t interesting_dex_pc_;
// The container into which FindLocksAtDexPc should write the registers containing held locks,
// null if we're not doing FindLocksAtDexPc.
std::vector<DexLockInfo>* monitor_enter_dex_pcs_;
// An optimization where instead of generating unique RegTypes for constants we use imprecise
// constants that cover a range of constants. This isn't good enough for deoptimization that
// avoids loading from registers in the case of a constant as the dex instruction set lost the
// notion of whether a value should be in a floating point or general purpose register file.
const bool need_precise_constants_;
// Indicates whether we verify to dump the info. In that case we accept quickened instructions
// even though we might detect to be a compiler. Should only be set when running
// VerifyMethodAndDump.
const bool verify_to_dump_;
// Whether or not we call AllowThreadSuspension periodically, we want a way to disable this for
// thread dumping checkpoints since we may get thread suspension at an inopportune time due to
// FindLocksAtDexPC, resulting in deadlocks.
const bool allow_thread_suspension_;
// Whether the method seems to be a constructor. Note that this field exists as we can't trust
// the flags in the dex file. Some older code does not mark methods named "<init>" and "<clinit>"
// correctly.
//
// Note: this flag is only valid once Verify() has started.
bool is_constructor_;
// Whether to attempt to fill all register lines for (ex) debugger use.
bool fill_register_lines_;
// API level, for dependent checks. Note: we do not use '0' for unset here, to simplify checks.
// Instead, unset level should correspond to max().
const uint32_t api_level_;
friend class ::art::verifier::MethodVerifier;
DISALLOW_COPY_AND_ASSIGN(MethodVerifier);
};
// Note: returns true on failure.
template <bool kVerifierDebug>
inline bool MethodVerifier<kVerifierDebug>::FailOrAbort(bool condition,
const char* error_msg,
uint32_t work_insn_idx) {
if (kIsDebugBuild) {
// In a debug build, abort if the error condition is wrong. Only warn if
// we are already aborting (as this verification is likely run to print
// lock information).
if (LIKELY(gAborting == 0)) {
DCHECK(condition) << error_msg << work_insn_idx << " "
<< dex_file_->PrettyMethod(dex_method_idx_);
} else {
if (!condition) {
LOG(ERROR) << error_msg << work_insn_idx;
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
return true;
}
}
} else {
// In a non-debug build, just fail the class.
if (!condition) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << error_msg << work_insn_idx;
return true;
}
}
return false;
}
static bool IsLargeMethod(const CodeItemDataAccessor& accessor) {
if (!accessor.HasCodeItem()) {
return false;
}
uint16_t registers_size = accessor.RegistersSize();
uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
return registers_size * insns_size > 4*1024*1024;
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::FindLocksAtDexPc() {
CHECK(monitor_enter_dex_pcs_ != nullptr);
CHECK(code_item_accessor_.HasCodeItem()); // This only makes sense for methods with code.
// Quick check whether there are any monitor_enter instructions before verifying.
for (const DexInstructionPcPair& inst : code_item_accessor_) {
if (inst->Opcode() == Instruction::MONITOR_ENTER) {
// Strictly speaking, we ought to be able to get away with doing a subset of the full method
// verification. In practice, the phase we want relies on data structures set up by all the
// earlier passes, so we just run the full method verification and bail out early when we've
// got what we wanted.
Verify();
return;
}
}
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::Verify() {
// Some older code doesn't correctly mark constructors as such. Test for this case by looking at
// the name.
const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
const char* method_name = dex_file_->StringDataByIdx(method_id.name_idx_);
bool instance_constructor_by_name = strcmp("<init>", method_name) == 0;
bool static_constructor_by_name = strcmp("<clinit>", method_name) == 0;
bool constructor_by_name = instance_constructor_by_name || static_constructor_by_name;
// Check that only constructors are tagged, and check for bad code that doesn't tag constructors.
if ((method_access_flags_ & kAccConstructor) != 0) {
if (!constructor_by_name) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "method is marked as constructor, but not named accordingly";
return false;
}
is_constructor_ = true;
} else if (constructor_by_name) {
LOG(WARNING) << "Method " << dex_file_->PrettyMethod(dex_method_idx_)
<< " not marked as constructor.";
is_constructor_ = true;
}
// If it's a constructor, check whether IsStatic() matches the name.
// This should have been rejected by the dex file verifier. Only do in debug build.
if (kIsDebugBuild) {
if (IsConstructor()) {
if (IsStatic() ^ static_constructor_by_name) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "constructor name doesn't match static flag";
return false;
}
}
}
// Methods may only have one of public/protected/private.
// This should have been rejected by the dex file verifier. Only do in debug build.
if (kIsDebugBuild) {
size_t access_mod_count =
(((method_access_flags_ & kAccPublic) == 0) ? 0 : 1) +
(((method_access_flags_ & kAccProtected) == 0) ? 0 : 1) +
(((method_access_flags_ & kAccPrivate) == 0) ? 0 : 1);
if (access_mod_count > 1) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "method has more than one of public/protected/private";
return false;
}
}
// If there aren't any instructions, make sure that's expected, then exit successfully.
if (!code_item_accessor_.HasCodeItem()) {
// Only native or abstract methods may not have code.
if ((method_access_flags_ & (kAccNative | kAccAbstract)) == 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "zero-length code in concrete non-native method";
return false;
}
// This should have been rejected by the dex file verifier. Only do in debug build.
// Note: the above will also be rejected in the dex file verifier, starting in dex version 37.
if (kIsDebugBuild) {
if ((method_access_flags_ & kAccAbstract) != 0) {
// Abstract methods are not allowed to have the following flags.
static constexpr uint32_t kForbidden =
kAccPrivate |
kAccStatic |
kAccFinal |
kAccNative |
kAccStrict |
kAccSynchronized;
if ((method_access_flags_ & kForbidden) != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "method can't be abstract and private/static/final/native/strict/synchronized";
return false;
}
}
if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
// Interface methods must be public and abstract (if default methods are disabled).
uint32_t kRequired = kAccPublic;
if ((method_access_flags_ & kRequired) != kRequired) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods must be public";
return false;
}
// In addition to the above, interface methods must not be protected.
static constexpr uint32_t kForbidden = kAccProtected;
if ((method_access_flags_ & kForbidden) != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface methods can't be protected";
return false;
}
}
// We also don't allow constructors to be abstract or native.
if (IsConstructor()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be abstract or native";
return false;
}
}
return true;
}
// This should have been rejected by the dex file verifier. Only do in debug build.
if (kIsDebugBuild) {
// When there's code, the method must not be native or abstract.
if ((method_access_flags_ & (kAccNative | kAccAbstract)) != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "non-zero-length code in abstract or native method";
return false;
}
if ((class_def_.GetJavaAccessFlags() & kAccInterface) != 0) {
// Interfaces may always have static initializers for their fields. If we are running with
// default methods enabled we also allow other public, static, non-final methods to have code.
// Otherwise that is the only type of method allowed.
if (!(IsConstructor() && IsStatic())) {
if (IsInstanceConstructor()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have non-static constructor";
return false;
} else if (method_access_flags_ & kAccFinal) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interfaces may not have final methods";
return false;
} else {
uint32_t access_flag_options = kAccPublic;
if (dex_file_->SupportsDefaultMethods()) {
access_flag_options |= kAccPrivate;
}
if (!(method_access_flags_ & access_flag_options)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "interfaces may not have protected or package-private members";
return false;
}
}
}
}
// Instance constructors must not be synchronized.
if (IsInstanceConstructor()) {
static constexpr uint32_t kForbidden = kAccSynchronized;
if ((method_access_flags_ & kForbidden) != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "constructors can't be synchronized";
return false;
}
}
}
// Sanity-check the register counts. ins + locals = registers, so make sure that ins <= registers.
if (code_item_accessor_.InsSize() > code_item_accessor_.RegistersSize()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad register counts (ins="
<< code_item_accessor_.InsSize()
<< " regs=" << code_item_accessor_.RegistersSize();
return false;
}
// Allocate and initialize an array to hold instruction data.
insn_flags_.reset(allocator_.AllocArray<InstructionFlags>(
code_item_accessor_.InsnsSizeInCodeUnits()));
DCHECK(insn_flags_ != nullptr);
std::uninitialized_fill_n(insn_flags_.get(),
code_item_accessor_.InsnsSizeInCodeUnits(),
InstructionFlags());
// Run through the instructions and see if the width checks out.
bool result = ComputeWidthsAndCountOps();
bool allow_runtime_only_instructions = !IsAotMode() || verify_to_dump_;
// Flag instructions guarded by a "try" block and check exception handlers.
result = result && ScanTryCatchBlocks();
// Perform static instruction verification.
result = result && (allow_runtime_only_instructions
? VerifyInstructions<true>()
: VerifyInstructions<false>());
// Perform code-flow analysis and return.
result = result && VerifyCodeFlow();
return result;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::ComputeWidthsAndCountOps() {
// We can't assume the instruction is well formed, handle the case where calculating the size
// goes past the end of the code item.
SafeDexInstructionIterator it(code_item_accessor_.begin(), code_item_accessor_.end());
for ( ; !it.IsErrorState() && it < code_item_accessor_.end(); ++it) {
// In case the instruction goes past the end of the code item, make sure to not process it.
SafeDexInstructionIterator next = it;
++next;
if (next.IsErrorState()) {
break;
}
Instruction::Code opcode = it->Opcode();
switch (opcode) {
case Instruction::APUT_OBJECT:
case Instruction::CHECK_CAST:
has_check_casts_ = true;
break;
default:
break;
}
GetModifiableInstructionFlags(it.DexPc()).SetIsOpcode();
}
if (it != code_item_accessor_.end()) {
const size_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "code did not end where expected ("
<< it.DexPc() << " vs. " << insns_size << ")";
return false;
}
DCHECK(GetInstructionFlags(0).IsOpcode());
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::ScanTryCatchBlocks() {
const uint32_t tries_size = code_item_accessor_.TriesSize();
if (tries_size == 0) {
return true;
}
const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
for (const dex::TryItem& try_item : code_item_accessor_.TryItems()) {
const uint32_t start = try_item.start_addr_;
const uint32_t end = start + try_item.insn_count_;
if ((start >= end) || (start >= insns_size) || (end > insns_size)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad exception entry: startAddr=" << start
<< " endAddr=" << end << " (size=" << insns_size << ")";
return false;
}
if (!GetInstructionFlags(start).IsOpcode()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "'try' block starts inside an instruction (" << start << ")";
return false;
}
DexInstructionIterator end_it(code_item_accessor_.Insns(), end);
for (DexInstructionIterator it(code_item_accessor_.Insns(), start); it < end_it; ++it) {
GetModifiableInstructionFlags(it.DexPc()).SetInTry();
}
}
// Iterate over each of the handlers to verify target addresses.
const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
const uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
ClassLinker* linker = GetClassLinker();
for (uint32_t idx = 0; idx < handlers_size; idx++) {
CatchHandlerIterator iterator(handlers_ptr);
for (; iterator.HasNext(); iterator.Next()) {
uint32_t dex_pc = iterator.GetHandlerAddress();
if (!GetInstructionFlags(dex_pc).IsOpcode()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "exception handler starts at bad address (" << dex_pc << ")";
return false;
}
if (!CheckNotMoveResult(code_item_accessor_.Insns(), dex_pc)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "exception handler begins with move-result* (" << dex_pc << ")";
return false;
}
GetModifiableInstructionFlags(dex_pc).SetBranchTarget();
// Ensure exception types are resolved so that they don't need resolution to be delivered,
// unresolved exception types will be ignored by exception delivery
if (iterator.GetHandlerTypeIndex().IsValid()) {
ObjPtr<mirror::Class> exception_type =
linker->ResolveType(iterator.GetHandlerTypeIndex(), dex_cache_, class_loader_);
if (exception_type == nullptr) {
DCHECK(self_->IsExceptionPending());
self_->ClearException();
}
}
}
handlers_ptr = iterator.EndDataPointer();
}
return true;
}
template <bool kVerifierDebug>
template <bool kAllowRuntimeOnlyInstructions>
bool MethodVerifier<kVerifierDebug>::VerifyInstructions() {
// Flag the start of the method as a branch target.
GetModifiableInstructionFlags(0).SetBranchTarget();
for (const DexInstructionPcPair& inst : code_item_accessor_) {
const uint32_t dex_pc = inst.DexPc();
if (!VerifyInstruction<kAllowRuntimeOnlyInstructions>(&inst.Inst(), dex_pc)) {
DCHECK_NE(failures_.size(), 0U);
return false;
}
// Flag some interesting instructions.
if (inst->IsReturn()) {
GetModifiableInstructionFlags(dex_pc).SetReturn();
} else if (inst->Opcode() == Instruction::CHECK_CAST) {
// The dex-to-dex compiler wants type information to elide check-casts.
GetModifiableInstructionFlags(dex_pc).SetCompileTimeInfoPoint();
}
}
return true;
}
template <bool kVerifierDebug>
template <bool kAllowRuntimeOnlyInstructions>
bool MethodVerifier<kVerifierDebug>::VerifyInstruction(const Instruction* inst,
uint32_t code_offset) {
if (Instruction::kHaveExperimentalInstructions && UNLIKELY(inst->IsExperimental())) {
// Experimental instructions don't yet have verifier support implementation.
// While it is possible to use them by themselves, when we try to use stable instructions
// with a virtual register that was created by an experimental instruction,
// the data flow analysis will fail.
Fail(VERIFY_ERROR_FORCE_INTERPRETER)
<< "experimental instruction is not supported by verifier; skipping verification";
flags_.have_pending_experimental_failure_ = true;
return false;
}
bool result = true;
switch (inst->GetVerifyTypeArgumentA()) {
case Instruction::kVerifyRegA:
result = result && CheckRegisterIndex(inst->VRegA());
break;
case Instruction::kVerifyRegAWide:
result = result && CheckWideRegisterIndex(inst->VRegA());
break;
}
switch (inst->GetVerifyTypeArgumentB()) {
case Instruction::kVerifyRegB:
result = result && CheckRegisterIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBField:
result = result && CheckFieldIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBMethod:
result = result && CheckMethodIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBNewInstance:
result = result && CheckNewInstance(dex::TypeIndex(inst->VRegB()));
break;
case Instruction::kVerifyRegBString:
result = result && CheckStringIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBType:
result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegB()));
break;
case Instruction::kVerifyRegBWide:
result = result && CheckWideRegisterIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBCallSite:
result = result && CheckCallSiteIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBMethodHandle:
result = result && CheckMethodHandleIndex(inst->VRegB());
break;
case Instruction::kVerifyRegBPrototype:
result = result && CheckPrototypeIndex(inst->VRegB());
break;
}
switch (inst->GetVerifyTypeArgumentC()) {
case Instruction::kVerifyRegC:
result = result && CheckRegisterIndex(inst->VRegC());
break;
case Instruction::kVerifyRegCField:
result = result && CheckFieldIndex(inst->VRegC());
break;
case Instruction::kVerifyRegCNewArray:
result = result && CheckNewArray(dex::TypeIndex(inst->VRegC()));
break;
case Instruction::kVerifyRegCType:
result = result && CheckTypeIndex(dex::TypeIndex(inst->VRegC()));
break;
case Instruction::kVerifyRegCWide:
result = result && CheckWideRegisterIndex(inst->VRegC());
break;
}
switch (inst->GetVerifyTypeArgumentH()) {
case Instruction::kVerifyRegHPrototype:
result = result && CheckPrototypeIndex(inst->VRegH());
break;
}
switch (inst->GetVerifyExtraFlags()) {
case Instruction::kVerifyArrayData:
result = result && CheckArrayData(code_offset);
break;
case Instruction::kVerifyBranchTarget:
result = result && CheckBranchTarget(code_offset);
break;
case Instruction::kVerifySwitchTargets:
result = result && CheckSwitchTargets(code_offset);
break;
case Instruction::kVerifyVarArgNonZero:
// Fall-through.
case Instruction::kVerifyVarArg: {
// Instructions that can actually return a negative value shouldn't have this flag.
uint32_t v_a = dchecked_integral_cast<uint32_t>(inst->VRegA());
if ((inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgNonZero && v_a == 0) ||
v_a > Instruction::kMaxVarArgRegs) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << v_a << ") in "
"non-range invoke";
return false;
}
uint32_t args[Instruction::kMaxVarArgRegs];
inst->GetVarArgs(args);
result = result && CheckVarArgRegs(v_a, args);
break;
}
case Instruction::kVerifyVarArgRangeNonZero:
// Fall-through.
case Instruction::kVerifyVarArgRange:
if (inst->GetVerifyExtraFlags() == Instruction::kVerifyVarArgRangeNonZero &&
inst->VRegA() <= 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid arg count (" << inst->VRegA() << ") in "
"range invoke";
return false;
}
result = result && CheckVarArgRangeRegs(inst->VRegA(), inst->VRegC());
break;
case Instruction::kVerifyError:
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected opcode " << inst->Name();
result = false;
break;
}
if (!kAllowRuntimeOnlyInstructions && inst->GetVerifyIsRuntimeOnly()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "opcode only expected at runtime " << inst->Name();
result = false;
}
return result;
}
template <bool kVerifierDebug>
inline bool MethodVerifier<kVerifierDebug>::CheckNewInstance(dex::TypeIndex idx) {
if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
// We don't need the actual class, just a pointer to the class name.
const char* descriptor = dex_file_->StringByTypeIdx(idx);
if (UNLIKELY(descriptor[0] != 'L')) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "can't call new-instance on type '" << descriptor << "'";
return false;
} else if (UNLIKELY(strcmp(descriptor, "Ljava/lang/Class;") == 0)) {
// An unlikely new instance on Class is not allowed. Fall back to interpreter to ensure an
// exception is thrown when this statement is executed (compiled code would not do that).
Fail(VERIFY_ERROR_INSTANTIATION);
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckNewArray(dex::TypeIndex idx) {
if (UNLIKELY(idx.index_ >= dex_file_->GetHeader().type_ids_size_)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad type index " << idx.index_ << " (max "
<< dex_file_->GetHeader().type_ids_size_ << ")";
return false;
}
int bracket_count = 0;
const char* descriptor = dex_file_->StringByTypeIdx(idx);
const char* cp = descriptor;
while (*cp++ == '[') {
bracket_count++;
}
if (UNLIKELY(bracket_count == 0)) {
/* The given class must be an array type. */
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "can't new-array class '" << descriptor << "' (not an array)";
return false;
} else if (UNLIKELY(bracket_count > 255)) {
/* It is illegal to create an array of more than 255 dimensions. */
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "can't new-array class '" << descriptor << "' (exceeds limit)";
return false;
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckArrayData(uint32_t cur_offset) {
const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
const uint16_t* array_data;
int32_t array_data_offset;
DCHECK_LT(cur_offset, insn_count);
/* make sure the start of the array data table is in range */
array_data_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
if (UNLIKELY(static_cast<int32_t>(cur_offset) + array_data_offset < 0 ||
cur_offset + array_data_offset + 2 >= insn_count)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data start: at " << cur_offset
<< ", data offset " << array_data_offset
<< ", count " << insn_count;
return false;
}
/* offset to array data table is a relative branch-style offset */
array_data = insns + array_data_offset;
// Make sure the table is at an even dex pc, that is, 32-bit aligned.
if (UNLIKELY(!IsAligned<4>(array_data))) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned array data table: at " << cur_offset
<< ", data offset " << array_data_offset;
return false;
}
// Make sure the array-data is marked as an opcode. This ensures that it was reached when
// traversing the code item linearly. It is an approximation for a by-spec padding value.
if (UNLIKELY(!GetInstructionFlags(cur_offset + array_data_offset).IsOpcode())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array data table at " << cur_offset
<< ", data offset " << array_data_offset
<< " not correctly visited, probably bad padding.";
return false;
}
uint32_t value_width = array_data[1];
uint32_t value_count = *reinterpret_cast<const uint32_t*>(&array_data[2]);
uint32_t table_size = 4 + (value_width * value_count + 1) / 2;
/* make sure the end of the switch is in range */
if (UNLIKELY(cur_offset + array_data_offset + table_size > insn_count)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid array data end: at " << cur_offset
<< ", data offset " << array_data_offset << ", end "
<< cur_offset + array_data_offset + table_size
<< ", count " << insn_count;
return false;
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckBranchTarget(uint32_t cur_offset) {
int32_t offset;
bool isConditional, selfOkay;
if (!GetBranchOffset(cur_offset, &offset, &isConditional, &selfOkay)) {
return false;
}
if (UNLIKELY(!selfOkay && offset == 0)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch offset of zero not allowed at"
<< reinterpret_cast<void*>(cur_offset);
return false;
}
// Check for 32-bit overflow. This isn't strictly necessary if we can depend on the runtime
// to have identical "wrap-around" behavior, but it's unwise to depend on that.
if (UNLIKELY(((int64_t) cur_offset + (int64_t) offset) != (int64_t) (cur_offset + offset))) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "branch target overflow "
<< reinterpret_cast<void*>(cur_offset) << " +" << offset;
return false;
}
int32_t abs_offset = cur_offset + offset;
if (UNLIKELY(abs_offset < 0 ||
(uint32_t) abs_offset >= code_item_accessor_.InsnsSizeInCodeUnits() ||
!GetInstructionFlags(abs_offset).IsOpcode())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid branch target " << offset << " (-> "
<< reinterpret_cast<void*>(abs_offset) << ") at "
<< reinterpret_cast<void*>(cur_offset);
return false;
}
GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::GetBranchOffset(uint32_t cur_offset,
int32_t* pOffset,
bool* pConditional,
bool* selfOkay) {
const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
*pConditional = false;
*selfOkay = false;
switch (*insns & 0xff) {
case Instruction::GOTO:
*pOffset = ((int16_t) *insns) >> 8;
break;
case Instruction::GOTO_32:
*pOffset = insns[1] | (((uint32_t) insns[2]) << 16);
*selfOkay = true;
break;
case Instruction::GOTO_16:
*pOffset = (int16_t) insns[1];
break;
case Instruction::IF_EQ:
case Instruction::IF_NE:
case Instruction::IF_LT:
case Instruction::IF_GE:
case Instruction::IF_GT:
case Instruction::IF_LE:
case Instruction::IF_EQZ:
case Instruction::IF_NEZ:
case Instruction::IF_LTZ:
case Instruction::IF_GEZ:
case Instruction::IF_GTZ:
case Instruction::IF_LEZ:
*pOffset = (int16_t) insns[1];
*pConditional = true;
break;
default:
return false;
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckSwitchTargets(uint32_t cur_offset) {
const uint32_t insn_count = code_item_accessor_.InsnsSizeInCodeUnits();
DCHECK_LT(cur_offset, insn_count);
const uint16_t* insns = code_item_accessor_.Insns() + cur_offset;
/* make sure the start of the switch is in range */
int32_t switch_offset = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
if (UNLIKELY(static_cast<int32_t>(cur_offset) + switch_offset < 0 ||
cur_offset + switch_offset + 2 > insn_count)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch start: at " << cur_offset
<< ", switch offset " << switch_offset
<< ", count " << insn_count;
return false;
}
/* offset to switch table is a relative branch-style offset */
const uint16_t* switch_insns = insns + switch_offset;
// Make sure the table is at an even dex pc, that is, 32-bit aligned.
if (UNLIKELY(!IsAligned<4>(switch_insns))) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unaligned switch table: at " << cur_offset
<< ", switch offset " << switch_offset;
return false;
}
// Make sure the switch data is marked as an opcode. This ensures that it was reached when
// traversing the code item linearly. It is an approximation for a by-spec padding value.
if (UNLIKELY(!GetInstructionFlags(cur_offset + switch_offset).IsOpcode())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "switch table at " << cur_offset
<< ", switch offset " << switch_offset
<< " not correctly visited, probably bad padding.";
return false;
}
bool is_packed_switch = (*insns & 0xff) == Instruction::PACKED_SWITCH;
uint32_t switch_count = switch_insns[1];
int32_t targets_offset;
uint16_t expected_signature;
if (is_packed_switch) {
/* 0=sig, 1=count, 2/3=firstKey */
targets_offset = 4;
expected_signature = Instruction::kPackedSwitchSignature;
} else {
/* 0=sig, 1=count, 2..count*2 = keys */
targets_offset = 2 + 2 * switch_count;
expected_signature = Instruction::kSparseSwitchSignature;
}
uint32_t table_size = targets_offset + switch_count * 2;
if (UNLIKELY(switch_insns[0] != expected_signature)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< StringPrintf("wrong signature for switch table (%x, wanted %x)",
switch_insns[0], expected_signature);
return false;
}
/* make sure the end of the switch is in range */
if (UNLIKELY(cur_offset + switch_offset + table_size > (uint32_t) insn_count)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch end: at " << cur_offset
<< ", switch offset " << switch_offset
<< ", end " << (cur_offset + switch_offset + table_size)
<< ", count " << insn_count;
return false;
}
constexpr int32_t keys_offset = 2;
if (switch_count > 1) {
if (is_packed_switch) {
/* for a packed switch, verify that keys do not overflow int32 */
int32_t first_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
int32_t max_first_key =
std::numeric_limits<int32_t>::max() - (static_cast<int32_t>(switch_count) - 1);
if (UNLIKELY(first_key > max_first_key)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid packed switch: first_key=" << first_key
<< ", switch_count=" << switch_count;
return false;
}
} else {
/* for a sparse switch, verify the keys are in ascending order */
int32_t last_key = switch_insns[keys_offset] | (switch_insns[keys_offset + 1] << 16);
for (uint32_t targ = 1; targ < switch_count; targ++) {
int32_t key =
static_cast<int32_t>(switch_insns[keys_offset + targ * 2]) |
static_cast<int32_t>(switch_insns[keys_offset + targ * 2 + 1] << 16);
if (UNLIKELY(key <= last_key)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid sparse switch: last key=" << last_key
<< ", this=" << key;
return false;
}
last_key = key;
}
}
}
/* verify each switch target */
for (uint32_t targ = 0; targ < switch_count; targ++) {
int32_t offset = static_cast<int32_t>(switch_insns[targets_offset + targ * 2]) |
static_cast<int32_t>(switch_insns[targets_offset + targ * 2 + 1] << 16);
int32_t abs_offset = cur_offset + offset;
if (UNLIKELY(abs_offset < 0 ||
abs_offset >= static_cast<int32_t>(insn_count) ||
!GetInstructionFlags(abs_offset).IsOpcode())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid switch target " << offset
<< " (-> " << reinterpret_cast<void*>(abs_offset) << ") at "
<< reinterpret_cast<void*>(cur_offset)
<< "[" << targ << "]";
return false;
}
GetModifiableInstructionFlags(abs_offset).SetBranchTarget();
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::VerifyCodeFlow() {
const uint16_t registers_size = code_item_accessor_.RegistersSize();
/* Create and initialize table holding register status */
RegisterTrackingMode base_mode = IsAotMode()
? kTrackCompilerInterestPoints
: kTrackRegsBranches;
reg_table_.Init(fill_register_lines_ ? kTrackRegsAll : base_mode,
insn_flags_.get(),
code_item_accessor_.InsnsSizeInCodeUnits(),
registers_size,
allocator_,
GetRegTypeCache());
work_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
saved_line_.reset(RegisterLine::Create(registers_size, allocator_, GetRegTypeCache()));
/* Initialize register types of method arguments. */
if (!SetTypesFromSignature()) {
DCHECK_NE(failures_.size(), 0U);
std::string prepend("Bad signature in ");
prepend += dex_file_->PrettyMethod(dex_method_idx_);
PrependToLastFailMessage(prepend);
return false;
}
// We may have a runtime failure here, clear.
flags_.have_pending_runtime_throw_failure_ = false;
/* Perform code flow verification. */
bool res = LIKELY(monitor_enter_dex_pcs_ == nullptr)
? CodeFlowVerifyMethod</*kMonitorDexPCs=*/ false>()
: CodeFlowVerifyMethod</*kMonitorDexPCs=*/ true>();
if (UNLIKELY(!res)) {
DCHECK_NE(failures_.size(), 0U);
return false;
}
return true;
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::Dump(VariableIndentationOutputStream* vios) {
if (!code_item_accessor_.HasCodeItem()) {
vios->Stream() << "Native method\n";
return;
}
{
vios->Stream() << "Register Types:\n";
ScopedIndentation indent1(vios);
reg_types_.Dump(vios->Stream());
}
vios->Stream() << "Dumping instructions and register lines:\n";
ScopedIndentation indent1(vios);
for (const DexInstructionPcPair& inst : code_item_accessor_) {
const size_t dex_pc = inst.DexPc();
// Might be asked to dump before the table is initialized.
if (reg_table_.IsInitialized()) {
RegisterLine* reg_line = reg_table_.GetLine(dex_pc);
if (reg_line != nullptr) {
vios->Stream() << reg_line->Dump(this) << "\n";
}
}
vios->Stream()
<< StringPrintf("0x%04zx", dex_pc) << ": " << GetInstructionFlags(dex_pc).ToString() << " ";
const bool kDumpHexOfInstruction = false;
if (kDumpHexOfInstruction) {
vios->Stream() << inst->DumpHex(5) << " ";
}
vios->Stream() << inst->DumpString(dex_file_) << "\n";
}
}
static bool IsPrimitiveDescriptor(char descriptor) {
switch (descriptor) {
case 'I':
case 'C':
case 'S':
case 'B':
case 'Z':
case 'F':
case 'D':
case 'J':
return true;
default:
return false;
}
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::SetTypesFromSignature() {
RegisterLine* reg_line = reg_table_.GetLine(0);
// Should have been verified earlier.
DCHECK_GE(code_item_accessor_.RegistersSize(), code_item_accessor_.InsSize());
uint32_t arg_start = code_item_accessor_.RegistersSize() - code_item_accessor_.InsSize();
size_t expected_args = code_item_accessor_.InsSize(); /* long/double count as two */
// Include the "this" pointer.
size_t cur_arg = 0;
if (!IsStatic()) {
if (expected_args == 0) {
// Expect at least a receiver.
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected 0 args, but method is not static";
return false;
}
// If this is a constructor for a class other than java.lang.Object, mark the first ("this")
// argument as uninitialized. This restricts field access until the superclass constructor is
// called.
const RegType& declaring_class = GetDeclaringClass();
if (IsConstructor()) {
if (declaring_class.IsJavaLangObject()) {
// "this" is implicitly initialized.
reg_line->SetThisInitialized();
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
} else {
reg_line->SetRegisterType<LockOp::kClear>(
this,
arg_start + cur_arg,
reg_types_.UninitializedThisArgument(declaring_class));
}
} else {
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, declaring_class);
}
cur_arg++;
}
const dex::ProtoId& proto_id =
dex_file_->GetMethodPrototype(dex_file_->GetMethodId(dex_method_idx_));
DexFileParameterIterator iterator(*dex_file_, proto_id);
for (; iterator.HasNext(); iterator.Next()) {
const char* descriptor = iterator.GetDescriptor();
if (descriptor == nullptr) {
LOG(FATAL) << "Null descriptor";
}
if (cur_arg >= expected_args) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
<< " args, found more (" << descriptor << ")";
return false;
}
switch (descriptor[0]) {
case 'L':
case '[':
// We assume that reference arguments are initialized. The only way it could be otherwise
// (assuming the caller was verified) is if the current method is <init>, but in that case
// it's effectively considered initialized the instant we reach here (in the sense that we
// can return without doing anything or call virtual methods).
{
// Note: don't check access. No error would be thrown for declaring or passing an
// inaccessible class. Only actual accesses to fields or methods will.
const RegType& reg_type = ResolveClass<CheckAccess::kNo>(iterator.GetTypeIdx());
if (!reg_type.IsNonZeroReferenceTypes()) {
DCHECK(HasFailures());
return false;
}
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_type);
}
break;
case 'Z':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Boolean());
break;
case 'C':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Char());
break;
case 'B':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Byte());
break;
case 'I':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Integer());
break;
case 'S':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Short());
break;
case 'F':
reg_line->SetRegisterType<LockOp::kClear>(this, arg_start + cur_arg, reg_types_.Float());
break;
case 'J':
case 'D': {
if (cur_arg + 1 >= expected_args) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
<< " args, found more (" << descriptor << ")";
return false;
}
const RegType* lo_half;
const RegType* hi_half;
if (descriptor[0] == 'J') {
lo_half = ®_types_.LongLo();
hi_half = ®_types_.LongHi();
} else {
lo_half = ®_types_.DoubleLo();
hi_half = ®_types_.DoubleHi();
}
reg_line->SetRegisterTypeWide(this, arg_start + cur_arg, *lo_half, *hi_half);
cur_arg++;
break;
}
default:
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected signature type char '"
<< descriptor << "'";
return false;
}
cur_arg++;
}
if (cur_arg != expected_args) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected " << expected_args
<< " arguments, found " << cur_arg;
return false;
}
const char* descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
// Validate return type. We don't do the type lookup; just want to make sure that it has the right
// format. Only major difference from the method argument format is that 'V' is supported.
bool result;
if (IsPrimitiveDescriptor(descriptor[0]) || descriptor[0] == 'V') {
result = descriptor[1] == '\0';
} else if (descriptor[0] == '[') { // single/multi-dimensional array of object/primitive
size_t i = 0;
do {
i++;
} while (descriptor[i] == '['); // process leading [
if (descriptor[i] == 'L') { // object array
do {
i++; // find closing ;
} while (descriptor[i] != ';' && descriptor[i] != '\0');
result = descriptor[i] == ';';
} else { // primitive array
result = IsPrimitiveDescriptor(descriptor[i]) && descriptor[i + 1] == '\0';
}
} else if (descriptor[0] == 'L') {
// could be more thorough here, but shouldn't be required
size_t i = 0;
do {
i++;
} while (descriptor[i] != ';' && descriptor[i] != '\0');
result = descriptor[i] == ';';
} else {
result = false;
}
if (!result) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected char in return type descriptor '"
<< descriptor << "'";
}
return result;
}
COLD_ATTR
void HandleMonitorDexPcsWorkLine(
std::vector<::art::verifier::MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
RegisterLine* work_line) {
monitor_enter_dex_pcs->clear(); // The new work line is more accurate than the previous one.
std::map<uint32_t, ::art::verifier::MethodVerifier::DexLockInfo> depth_to_lock_info;
auto collector = [&](uint32_t dex_reg, uint32_t depth) {
auto insert_pair = depth_to_lock_info.emplace(
depth, ::art::verifier::MethodVerifier::DexLockInfo(depth));
auto it = insert_pair.first;
auto set_insert_pair = it->second.dex_registers.insert(dex_reg);
DCHECK(set_insert_pair.second);
};
work_line->IterateRegToLockDepths(collector);
for (auto& pair : depth_to_lock_info) {
monitor_enter_dex_pcs->push_back(pair.second);
// Map depth to dex PC.
monitor_enter_dex_pcs->back().dex_pc = work_line->GetMonitorEnterDexPc(pair.second.dex_pc);
}
}
template <bool kVerifierDebug>
template <bool kMonitorDexPCs>
bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyMethod() {
const uint16_t* insns = code_item_accessor_.Insns();
const uint32_t insns_size = code_item_accessor_.InsnsSizeInCodeUnits();
/* Begin by marking the first instruction as "changed". */
GetModifiableInstructionFlags(0).SetChanged();
uint32_t start_guess = 0;
/* Continue until no instructions are marked "changed". */
while (true) {
if (allow_thread_suspension_) {
self_->AllowThreadSuspension();
}
// Find the first marked one. Use "start_guess" as a way to find one quickly.
uint32_t insn_idx = start_guess;
for (; insn_idx < insns_size; insn_idx++) {
if (GetInstructionFlags(insn_idx).IsChanged())
break;
}
if (insn_idx == insns_size) {
if (start_guess != 0) {
/* try again, starting from the top */
start_guess = 0;
continue;
} else {
/* all flags are clear */
break;
}
}
// We carry the working set of registers from instruction to instruction. If this address can
// be the target of a branch (or throw) instruction, or if we're skipping around chasing
// "changed" flags, we need to load the set of registers from the table.
// Because we always prefer to continue on to the next instruction, we should never have a
// situation where we have a stray "changed" flag set on an instruction that isn't a branch
// target.
work_insn_idx_ = insn_idx;
if (GetInstructionFlags(insn_idx).IsBranchTarget()) {
work_line_->CopyFromLine(reg_table_.GetLine(insn_idx));
} else if (kIsDebugBuild) {
/*
* Sanity check: retrieve the stored register line (assuming
* a full table) and make sure it actually matches.
*/
RegisterLine* register_line = reg_table_.GetLine(insn_idx);
if (register_line != nullptr) {
if (work_line_->CompareLine(register_line) != 0) {
Dump(LOG_STREAM(FATAL_WITHOUT_ABORT));
LOG(FATAL_WITHOUT_ABORT) << info_messages_.str();
LOG(FATAL) << "work_line diverged in " << dex_file_->PrettyMethod(dex_method_idx_)
<< "@" << reinterpret_cast<void*>(work_insn_idx_) << "\n"
<< " work_line=" << work_line_->Dump(this) << "\n"
<< " expected=" << register_line->Dump(this);
}
}
}
// If we're doing FindLocksAtDexPc, check whether we're at the dex pc we care about.
// We want the state _before_ the instruction, for the case where the dex pc we're
// interested in is itself a monitor-enter instruction (which is a likely place
// for a thread to be suspended).
if (kMonitorDexPCs && UNLIKELY(work_insn_idx_ == interesting_dex_pc_)) {
HandleMonitorDexPcsWorkLine(monitor_enter_dex_pcs_, work_line_.get());
}
if (!CodeFlowVerifyInstruction(&start_guess)) {
std::string prepend(dex_file_->PrettyMethod(dex_method_idx_));
prepend += " failed to verify: ";
PrependToLastFailMessage(prepend);
return false;
}
/* Clear "changed" and mark as visited. */
GetModifiableInstructionFlags(insn_idx).SetVisited();
GetModifiableInstructionFlags(insn_idx).ClearChanged();
}
if (kVerifierDebug) {
/*
* Scan for dead code. There's nothing "evil" about dead code
* (besides the wasted space), but it indicates a flaw somewhere
* down the line, possibly in the verifier.
*
* If we've substituted "always throw" instructions into the stream,
* we are almost certainly going to have some dead code.
*/
int dead_start = -1;
for (const DexInstructionPcPair& inst : code_item_accessor_) {
const uint32_t insn_idx = inst.DexPc();
/*
* Switch-statement data doesn't get "visited" by scanner. It
* may or may not be preceded by a padding NOP (for alignment).
*/
if (insns[insn_idx] == Instruction::kPackedSwitchSignature ||
insns[insn_idx] == Instruction::kSparseSwitchSignature ||
insns[insn_idx] == Instruction::kArrayDataSignature ||
(insns[insn_idx] == Instruction::NOP && (insn_idx + 1 < insns_size) &&
(insns[insn_idx + 1] == Instruction::kPackedSwitchSignature ||
insns[insn_idx + 1] == Instruction::kSparseSwitchSignature ||
insns[insn_idx + 1] == Instruction::kArrayDataSignature))) {
GetModifiableInstructionFlags(insn_idx).SetVisited();
}
if (!GetInstructionFlags(insn_idx).IsVisited()) {
if (dead_start < 0) {
dead_start = insn_idx;
}
} else if (dead_start >= 0) {
LogVerifyInfo() << "dead code " << reinterpret_cast<void*>(dead_start)
<< "-" << reinterpret_cast<void*>(insn_idx - 1);
dead_start = -1;
}
}
if (dead_start >= 0) {
LogVerifyInfo()
<< "dead code " << reinterpret_cast<void*>(dead_start)
<< "-" << reinterpret_cast<void*>(code_item_accessor_.InsnsSizeInCodeUnits() - 1);
}
// To dump the state of the verify after a method, do something like:
// if (dex_file_->PrettyMethod(dex_method_idx_) ==
// "boolean java.lang.String.equals(java.lang.Object)") {
// LOG(INFO) << info_messages_.str();
// }
}
return true;
}
// Returns the index of the first final instance field of the given class, or kDexNoIndex if there
// is no such field.
static uint32_t GetFirstFinalInstanceFieldIndex(const DexFile& dex_file, dex::TypeIndex type_idx) {
const dex::ClassDef* class_def = dex_file.FindClassDef(type_idx);
DCHECK(class_def != nullptr);
ClassAccessor accessor(dex_file, *class_def);
for (const ClassAccessor::Field& field : accessor.GetInstanceFields()) {
if (field.IsFinal()) {
return field.GetIndex();
}
}
return dex::kDexNoIndex;
}
// Setup a register line for the given return instruction.
template <bool kVerifierDebug>
static void AdjustReturnLine(MethodVerifier<kVerifierDebug>* verifier,
const Instruction* ret_inst,
RegisterLine* line) {
Instruction::Code opcode = ret_inst->Opcode();
switch (opcode) {
case Instruction::RETURN_VOID:
case Instruction::RETURN_VOID_NO_BARRIER:
if (verifier->IsInstanceConstructor()) {
// Before we mark all regs as conflicts, check that we don't have an uninitialized this.
line->CheckConstructorReturn(verifier);
}
line->MarkAllRegistersAsConflicts(verifier);
break;
case Instruction::RETURN:
case Instruction::RETURN_OBJECT:
line->MarkAllRegistersAsConflictsExcept(verifier, ret_inst->VRegA_11x());
break;
case Instruction::RETURN_WIDE:
line->MarkAllRegistersAsConflictsExceptWide(verifier, ret_inst->VRegA_11x());
break;
default:
LOG(FATAL) << "Unknown return opcode " << opcode;
UNREACHABLE();
}
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CodeFlowVerifyInstruction(uint32_t* start_guess) {
/*
* Once we finish decoding the instruction, we need to figure out where
* we can go from here. There are three possible ways to transfer
* control to another statement:
*
* (1) Continue to the next instruction. Applies to all but
* unconditional branches, method returns, and exception throws.
* (2) Branch to one or more possible locations. Applies to branches
* and switch statements.
* (3) Exception handlers. Applies to any instruction that can
* throw an exception that is handled by an encompassing "try"
* block.
*
* We can also return, in which case there is no successor instruction
* from this point.
*
* The behavior can be determined from the opcode flags.
*/
const uint16_t* insns = code_item_accessor_.Insns() + work_insn_idx_;
const Instruction* inst = Instruction::At(insns);
int opcode_flags = Instruction::FlagsOf(inst->Opcode());
int32_t branch_target = 0;
bool just_set_result = false;
if (kVerifierDebug) {
// Generate processing back trace to debug verifier
LogVerifyInfo() << "Processing " << inst->DumpString(dex_file_) << std::endl
<< work_line_->Dump(this);
}
/*
* Make a copy of the previous register state. If the instruction
* can throw an exception, we will copy/merge this into the "catch"
* address rather than work_line, because we don't want the result
* from the "successful" code path (e.g. a check-cast that "improves"
* a type) to be visible to the exception handler.
*/
if (((opcode_flags & Instruction::kThrow) != 0 || IsCompatThrow(inst->Opcode())) &&
CurrentInsnFlags()->IsInTry()) {
saved_line_->CopyFromLine(work_line_.get());
} else if (kIsDebugBuild) {
saved_line_->FillWithGarbage();
}
// Per-instruction flag, should not be set here.
DCHECK(!flags_.have_pending_runtime_throw_failure_);
bool exc_handler_unreachable = false;
// We need to ensure the work line is consistent while performing validation. When we spot a
// peephole pattern we compute a new line for either the fallthrough instruction or the
// branch target.
RegisterLineArenaUniquePtr branch_line;
RegisterLineArenaUniquePtr fallthrough_line;
switch (inst->Opcode()) {
case Instruction::NOP:
/*
* A "pure" NOP has no effect on anything. Data tables start with
* a signature that looks like a NOP; if we see one of these in
* the course of executing code then we have a problem.
*/
if (inst->VRegA_10x() != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "encountered data table in instruction stream";
}
break;
case Instruction::MOVE:
work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategory1nr);
break;
case Instruction::MOVE_FROM16:
work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategory1nr);
break;
case Instruction::MOVE_16:
work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategory1nr);
break;
case Instruction::MOVE_WIDE:
work_line_->CopyRegister2(this, inst->VRegA_12x(), inst->VRegB_12x());
break;
case Instruction::MOVE_WIDE_FROM16:
work_line_->CopyRegister2(this, inst->VRegA_22x(), inst->VRegB_22x());
break;
case Instruction::MOVE_WIDE_16:
work_line_->CopyRegister2(this, inst->VRegA_32x(), inst->VRegB_32x());
break;
case Instruction::MOVE_OBJECT:
work_line_->CopyRegister1(this, inst->VRegA_12x(), inst->VRegB_12x(), kTypeCategoryRef);
break;
case Instruction::MOVE_OBJECT_FROM16:
work_line_->CopyRegister1(this, inst->VRegA_22x(), inst->VRegB_22x(), kTypeCategoryRef);
break;
case Instruction::MOVE_OBJECT_16:
work_line_->CopyRegister1(this, inst->VRegA_32x(), inst->VRegB_32x(), kTypeCategoryRef);
break;
/*
* The move-result instructions copy data out of a "pseudo-register"
* with the results from the last method invocation. In practice we
* might want to hold the result in an actual CPU register, so the
* Dalvik spec requires that these only appear immediately after an
* invoke or filled-new-array.
*
* These calls invalidate the "result" register. (This is now
* redundant with the reset done below, but it can make the debug info
* easier to read in some cases.)
*/
case Instruction::MOVE_RESULT:
work_line_->CopyResultRegister1(this, inst->VRegA_11x(), false);
break;
case Instruction::MOVE_RESULT_WIDE:
work_line_->CopyResultRegister2(this, inst->VRegA_11x());
break;
case Instruction::MOVE_RESULT_OBJECT:
work_line_->CopyResultRegister1(this, inst->VRegA_11x(), true);
break;
case Instruction::MOVE_EXCEPTION:
if (!HandleMoveException(inst)) {
exc_handler_unreachable = true;
}
break;
case Instruction::RETURN_VOID:
if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
if (!GetMethodReturnType().IsConflict()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
}
}
break;
case Instruction::RETURN:
if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
/* check the method signature */
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsCategory1Types()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected non-category 1 return type "
<< return_type;
} else {
// Compilers may generate synthetic functions that write byte values into boolean fields.
// Also, it may use integer values for boolean, byte, short, and character return types.
const uint32_t vregA = inst->VRegA_11x();
const RegType& src_type = work_line_->GetRegisterType(this, vregA);
bool use_src = ((return_type.IsBoolean() && src_type.IsByte()) ||
((return_type.IsBoolean() || return_type.IsByte() ||
return_type.IsShort() || return_type.IsChar()) &&
src_type.IsInteger()));
/* check the register contents */
bool success =
work_line_->VerifyRegisterType(this, vregA, use_src ? src_type : return_type);
if (!success) {
AppendToLastFailMessage(StringPrintf(" return-1nr on invalid register v%d", vregA));
}
}
}
break;
case Instruction::RETURN_WIDE:
if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
/* check the method signature */
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsCategory2Types()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-wide not expected";
} else {
/* check the register contents */
const uint32_t vregA = inst->VRegA_11x();
bool success = work_line_->VerifyRegisterType(this, vregA, return_type);
if (!success) {
AppendToLastFailMessage(StringPrintf(" return-wide on invalid register v%d", vregA));
}
}
}
break;
case Instruction::RETURN_OBJECT:
if (!IsInstanceConstructor() || work_line_->CheckConstructorReturn(this)) {
const RegType& return_type = GetMethodReturnType();
if (!return_type.IsReferenceTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object not expected";
} else {
/* return_type is the *expected* return type, not register value */
DCHECK(!return_type.IsZeroOrNull());
DCHECK(!return_type.IsUninitializedReference());
const uint32_t vregA = inst->VRegA_11x();
const RegType& reg_type = work_line_->GetRegisterType(this, vregA);
// Disallow returning undefined, conflict & uninitialized values and verify that the
// reference in vAA is an instance of the "return_type."
if (reg_type.IsUndefined()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning undefined register";
} else if (reg_type.IsConflict()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning register with conflict";
} else if (reg_type.IsUninitializedTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning uninitialized object '"
<< reg_type << "'";
} else if (!reg_type.IsReferenceTypes()) {
// We really do expect a reference here.
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-object returns a non-reference type "
<< reg_type;
} else if (!return_type.IsAssignableFrom(reg_type, this)) {
if (reg_type.IsUnresolvedTypes() || return_type.IsUnresolvedTypes()) {
Fail(api_level_ > 29u ? VERIFY_ERROR_BAD_CLASS_SOFT : VERIFY_ERROR_NO_CLASS)
<< " can't resolve returned type '" << return_type << "' or '" << reg_type << "'";
} else {
bool soft_error = false;
// Check whether arrays are involved. They will show a valid class status, even
// if their components are erroneous.
if (reg_type.IsArrayTypes() && return_type.IsArrayTypes()) {
return_type.CanAssignArray(reg_type, reg_types_, class_loader_, this, &soft_error);
if (soft_error) {
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "array with erroneous component type: "
<< reg_type << " vs " << return_type;
}
}
if (!soft_error) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "returning '" << reg_type
<< "', but expected from declaration '" << return_type << "'";
}
}
}
}
}
break;
/* could be boolean, int, float, or a null reference */
case Instruction::CONST_4: {
int32_t val = static_cast<int32_t>(inst->VRegB_11n() << 28) >> 28;
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_11n(), DetermineCat1Constant(val, need_precise_constants_));
break;
}
case Instruction::CONST_16: {
int16_t val = static_cast<int16_t>(inst->VRegB_21s());
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21s(), DetermineCat1Constant(val, need_precise_constants_));
break;
}
case Instruction::CONST: {
int32_t val = inst->VRegB_31i();
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_31i(), DetermineCat1Constant(val, need_precise_constants_));
break;
}
case Instruction::CONST_HIGH16: {
int32_t val = static_cast<int32_t>(inst->VRegB_21h() << 16);
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21h(), DetermineCat1Constant(val, need_precise_constants_));
break;
}
/* could be long or double; resolved upon use */
case Instruction::CONST_WIDE_16: {
int64_t val = static_cast<int16_t>(inst->VRegB_21s());
const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
work_line_->SetRegisterTypeWide(this, inst->VRegA_21s(), lo, hi);
break;
}
case Instruction::CONST_WIDE_32: {
int64_t val = static_cast<int32_t>(inst->VRegB_31i());
const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
work_line_->SetRegisterTypeWide(this, inst->VRegA_31i(), lo, hi);
break;
}
case Instruction::CONST_WIDE: {
int64_t val = inst->VRegB_51l();
const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
work_line_->SetRegisterTypeWide(this, inst->VRegA_51l(), lo, hi);
break;
}
case Instruction::CONST_WIDE_HIGH16: {
int64_t val = static_cast<uint64_t>(inst->VRegB_21h()) << 48;
const RegType& lo = reg_types_.FromCat2ConstLo(static_cast<int32_t>(val), true);
const RegType& hi = reg_types_.FromCat2ConstHi(static_cast<int32_t>(val >> 32), true);
work_line_->SetRegisterTypeWide(this, inst->VRegA_21h(), lo, hi);
break;
}
case Instruction::CONST_STRING:
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21c(), reg_types_.JavaLangString());
break;
case Instruction::CONST_STRING_JUMBO:
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_31c(), reg_types_.JavaLangString());
break;
case Instruction::CONST_CLASS: {
// Get type from instruction if unresolved then we need an access check
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
// Register holds class, ie its type is class, on error it will hold Conflict.
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21c(), res_type.IsConflict() ? res_type
: reg_types_.JavaLangClass());
break;
}
case Instruction::CONST_METHOD_HANDLE:
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodHandle());
break;
case Instruction::CONST_METHOD_TYPE:
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_21c(), reg_types_.JavaLangInvokeMethodType());
break;
case Instruction::MONITOR_ENTER:
work_line_->PushMonitor(this, inst->VRegA_11x(), work_insn_idx_);
// Check whether the previous instruction is a move-object with vAA as a source, creating
// untracked lock aliasing.
if (0 != work_insn_idx_ && !GetInstructionFlags(work_insn_idx_).IsBranchTarget()) {
uint32_t prev_idx = work_insn_idx_ - 1;
while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
prev_idx--;
}
const Instruction& prev_inst = code_item_accessor_.InstructionAt(prev_idx);
switch (prev_inst.Opcode()) {
case Instruction::MOVE_OBJECT:
case Instruction::MOVE_OBJECT_16:
case Instruction::MOVE_OBJECT_FROM16:
if (prev_inst.VRegB() == inst->VRegA_11x()) {
// Redo the copy. This won't change the register types, but update the lock status
// for the aliased register.
work_line_->CopyRegister1(this,
prev_inst.VRegA(),
prev_inst.VRegB(),
kTypeCategoryRef);
}
break;
// Catch a case of register aliasing when two registers are linked to the same
// java.lang.Class object via two consequent const-class instructions immediately
// preceding monitor-enter called on one of those registers.
case Instruction::CONST_CLASS: {
// Get the second previous instruction.
if (prev_idx == 0 || GetInstructionFlags(prev_idx).IsBranchTarget()) {
break;
}
prev_idx--;
while (0 != prev_idx && !GetInstructionFlags(prev_idx).IsOpcode()) {
prev_idx--;
}
const Instruction& prev2_inst = code_item_accessor_.InstructionAt(prev_idx);
// Match the pattern "const-class; const-class; monitor-enter;"
if (prev2_inst.Opcode() != Instruction::CONST_CLASS) {
break;
}
// Ensure both const-classes are called for the same type_idx.
if (prev_inst.VRegB_21c() != prev2_inst.VRegB_21c()) {
break;
}
// Update the lock status for the aliased register.
if (prev_inst.VRegA() == inst->VRegA_11x()) {
work_line_->CopyRegister1(this,
prev2_inst.VRegA(),
inst->VRegA_11x(),
kTypeCategoryRef);
} else if (prev2_inst.VRegA() == inst->VRegA_11x()) {
work_line_->CopyRegister1(this,
prev_inst.VRegA(),
inst->VRegA_11x(),
kTypeCategoryRef);
}
break;
}
default: // Other instruction types ignored.
break;
}
}
break;
case Instruction::MONITOR_EXIT:
/*
* monitor-exit instructions are odd. They can throw exceptions,
* but when they do they act as if they succeeded and the PC is
* pointing to the following instruction. (This behavior goes back
* to the need to handle asynchronous exceptions, a now-deprecated
* feature that Dalvik doesn't support.)
*
* In practice we don't need to worry about this. The only
* exceptions that can be thrown from monitor-exit are for a
* null reference and -exit without a matching -enter. If the
* structured locking checks are working, the former would have
* failed on the -enter instruction, and the latter is impossible.
*
* This is fortunate, because issue 3221411 prevents us from
* chasing the "can throw" path when monitor verification is
* enabled. If we can fully verify the locking we can ignore
* some catch blocks (which will show up as "dead" code when
* we skip them here); if we can't, then the code path could be
* "live" so we still need to check it.
*/
opcode_flags &= ~Instruction::kThrow;
work_line_->PopMonitor(this, inst->VRegA_11x());
break;
case Instruction::CHECK_CAST:
case Instruction::INSTANCE_OF: {
/*
* If this instruction succeeds, we will "downcast" register vA to the type in vB. (This
* could be a "upcast" -- not expected, so we don't try to address it.)
*
* If it fails, an exception is thrown, which we deal with later by ignoring the update to
* dec_insn.vA when branching to a handler.
*/
const bool is_checkcast = (inst->Opcode() == Instruction::CHECK_CAST);
const dex::TypeIndex type_idx((is_checkcast) ? inst->VRegB_21c() : inst->VRegC_22c());
const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
if (res_type.IsConflict()) {
// If this is a primitive type, fail HARD.
ObjPtr<mirror::Class> klass = GetClassLinker()->LookupResolvedType(
type_idx, dex_cache_.Get(), class_loader_.Get());
if (klass != nullptr && klass->IsPrimitive()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "using primitive type "
<< dex_file_->StringByTypeIdx(type_idx) << " in instanceof in "
<< GetDeclaringClass();
break;
}
DCHECK_NE(failures_.size(), 0U);
if (!is_checkcast) {
work_line_->SetRegisterType<LockOp::kClear>(this,
inst->VRegA_22c(),
reg_types_.Boolean());
}
break; // bad class
}
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
uint32_t orig_type_reg = (is_checkcast) ? inst->VRegA_21c() : inst->VRegB_22c();
const RegType& orig_type = work_line_->GetRegisterType(this, orig_type_reg);
if (!res_type.IsNonZeroReferenceTypes()) {
if (is_checkcast) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on unexpected class " << res_type;
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on unexpected class " << res_type;
}
} else if (!orig_type.IsReferenceTypes()) {
if (is_checkcast) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on non-reference in v" << orig_type_reg;
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on non-reference in v" << orig_type_reg;
}
} else if (orig_type.IsUninitializedTypes()) {
if (is_checkcast) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "check-cast on uninitialized reference in v"
<< orig_type_reg;
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance-of on uninitialized reference in v"
<< orig_type_reg;
}
} else {
if (is_checkcast) {
work_line_->SetRegisterType<LockOp::kKeep>(this, inst->VRegA_21c(), res_type);
} else {
work_line_->SetRegisterType<LockOp::kClear>(this,
inst->VRegA_22c(),
reg_types_.Boolean());
}
}
break;
}
case Instruction::ARRAY_LENGTH: {
const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegB_12x());
if (res_type.IsReferenceTypes()) {
if (!res_type.IsArrayTypes() && !res_type.IsZeroOrNull()) {
// ie not an array or null
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
} else {
work_line_->SetRegisterType<LockOp::kClear>(this,
inst->VRegA_12x(),
reg_types_.Integer());
}
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-length on non-array " << res_type;
}
break;
}
case Instruction::NEW_INSTANCE: {
const RegType& res_type = ResolveClass<CheckAccess::kYes>(dex::TypeIndex(inst->VRegB_21c()));
if (res_type.IsConflict()) {
DCHECK_NE(failures_.size(), 0U);
break; // bad class
}
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
// can't create an instance of an interface or abstract class */
if (!res_type.IsInstantiableTypes()) {
Fail(VERIFY_ERROR_INSTANTIATION)
<< "new-instance on primitive, interface or abstract class" << res_type;
// Soft failure so carry on to set register type.
}
const RegType& uninit_type = reg_types_.Uninitialized(res_type, work_insn_idx_);
// Any registers holding previous allocations from this address that have not yet been
// initialized must be marked invalid.
work_line_->MarkUninitRefsAsInvalid(this, uninit_type);
// add the new uninitialized reference to the register state
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_21c(), uninit_type);
break;
}
case Instruction::NEW_ARRAY:
VerifyNewArray(inst, false, false);
break;
case Instruction::FILLED_NEW_ARRAY:
VerifyNewArray(inst, true, false);
just_set_result = true; // Filled new array sets result register
break;
case Instruction::FILLED_NEW_ARRAY_RANGE:
VerifyNewArray(inst, true, true);
just_set_result = true; // Filled new array range sets result register
break;
case Instruction::CMPL_FLOAT:
case Instruction::CMPG_FLOAT:
if (!work_line_->VerifyRegisterType(this, inst->VRegB_23x(), reg_types_.Float())) {
break;
}
if (!work_line_->VerifyRegisterType(this, inst->VRegC_23x(), reg_types_.Float())) {
break;
}
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
break;
case Instruction::CMPL_DOUBLE:
case Instruction::CMPG_DOUBLE:
if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.DoubleLo(),
reg_types_.DoubleHi())) {
break;
}
if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.DoubleLo(),
reg_types_.DoubleHi())) {
break;
}
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
break;
case Instruction::CMP_LONG:
if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegB_23x(), reg_types_.LongLo(),
reg_types_.LongHi())) {
break;
}
if (!work_line_->VerifyRegisterTypeWide(this, inst->VRegC_23x(), reg_types_.LongLo(),
reg_types_.LongHi())) {
break;
}
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Integer());
break;
case Instruction::THROW: {
const RegType& res_type = work_line_->GetRegisterType(this, inst->VRegA_11x());
if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(res_type, this)) {
if (res_type.IsUninitializedTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown exception not initialized";
} else if (!res_type.IsReferenceTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "thrown value of non-reference type " << res_type;
} else {
Fail(res_type.IsUnresolvedTypes() ? VERIFY_ERROR_NO_CLASS : VERIFY_ERROR_BAD_CLASS_SOFT)
<< "thrown class " << res_type << " not instanceof Throwable";
}
}
break;
}
case Instruction::GOTO:
case Instruction::GOTO_16:
case Instruction::GOTO_32:
/* no effect on or use of registers */
break;
case Instruction::PACKED_SWITCH:
case Instruction::SPARSE_SWITCH:
/* verify that vAA is an integer, or can be converted to one */
work_line_->VerifyRegisterType(this, inst->VRegA_31t(), reg_types_.Integer());
break;
case Instruction::FILL_ARRAY_DATA: {
/* Similar to the verification done for APUT */
const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegA_31t());
/* array_type can be null if the reg type is Zero */
if (!array_type.IsZeroOrNull()) {
if (!array_type.IsArrayTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with array type "
<< array_type;
} else if (array_type.IsUnresolvedTypes()) {
// If it's an unresolved array type, it must be non-primitive.
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data for array of type "
<< array_type;
} else {
const RegType& component_type = reg_types_.GetComponentType(array_type,
class_loader_.Get());
DCHECK(!component_type.IsConflict());
if (component_type.IsNonZeroReferenceTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid fill-array-data with component type "
<< component_type;
} else {
// Now verify if the element width in the table matches the element width declared in
// the array
const uint16_t* array_data =
insns + (insns[1] | (static_cast<int32_t>(insns[2]) << 16));
if (array_data[0] != Instruction::kArrayDataSignature) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid magic for array-data";
} else {
size_t elem_width = Primitive::ComponentSize(component_type.GetPrimitiveType());
// Since we don't compress the data in Dex, expect to see equal width of data stored
// in the table and expected from the array class.
if (array_data[1] != elem_width) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array-data size mismatch (" << array_data[1]
<< " vs " << elem_width << ")";
}
}
}
}
}
break;
}
case Instruction::IF_EQ:
case Instruction::IF_NE: {
const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
bool mismatch = false;
if (reg_type1.IsZeroOrNull()) { // zero then integral or reference expected
mismatch = !reg_type2.IsReferenceTypes() && !reg_type2.IsIntegralTypes();
} else if (reg_type1.IsReferenceTypes()) { // both references?
mismatch = !reg_type2.IsReferenceTypes();
} else { // both integral?
mismatch = !reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes();
}
if (mismatch) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to if-eq/if-ne (" << reg_type1 << ","
<< reg_type2 << ") must both be references or integral";
}
break;
}
case Instruction::IF_LT:
case Instruction::IF_GE:
case Instruction::IF_GT:
case Instruction::IF_LE: {
const RegType& reg_type1 = work_line_->GetRegisterType(this, inst->VRegA_22t());
const RegType& reg_type2 = work_line_->GetRegisterType(this, inst->VRegB_22t());
if (!reg_type1.IsIntegralTypes() || !reg_type2.IsIntegralTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "args to 'if' (" << reg_type1 << ","
<< reg_type2 << ") must be integral";
}
break;
}
case Instruction::IF_EQZ:
case Instruction::IF_NEZ: {
const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
if (!reg_type.IsReferenceTypes() && !reg_type.IsIntegralTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
<< " unexpected as arg to if-eqz/if-nez";
}
// Find previous instruction - its existence is a precondition to peephole optimization.
if (UNLIKELY(0 == work_insn_idx_)) {
break;
}
uint32_t instance_of_idx = work_insn_idx_ - 1;
while (0 != instance_of_idx && !GetInstructionFlags(instance_of_idx).IsOpcode()) {
instance_of_idx--;
}
// Dex index 0 must be an opcode.
DCHECK(GetInstructionFlags(instance_of_idx).IsOpcode());
const Instruction& instance_of_inst = code_item_accessor_.InstructionAt(instance_of_idx);
/* Check for peep-hole pattern of:
* ...;
* instance-of vX, vY, T;
* ifXXX vX, label ;
* ...;
* label:
* ...;
* and sharpen the type of vY to be type T.
* Note, this pattern can't be if:
* - if there are other branches to this branch,
* - when vX == vY.
*/
if (!CurrentInsnFlags()->IsBranchTarget() &&
(Instruction::INSTANCE_OF == instance_of_inst.Opcode()) &&
(inst->VRegA_21t() == instance_of_inst.VRegA_22c()) &&
(instance_of_inst.VRegA_22c() != instance_of_inst.VRegB_22c())) {
// Check the type of the instance-of is different than that of registers type, as if they
// are the same there is no work to be done here. Check that the conversion is not to or
// from an unresolved type as type information is imprecise. If the instance-of is to an
// interface then ignore the type information as interfaces can only be treated as Objects
// and we don't want to disallow field and other operations on the object. If the value
// being instance-of checked against is known null (zero) then allow the optimization as
// we didn't have type information. If the merge of the instance-of type with the original
// type is assignable to the original then allow optimization. This check is performed to
// ensure that subsequent merges don't lose type information - such as becoming an
// interface from a class that would lose information relevant to field checks.
//
// Note: do not do an access check. This may mark this with a runtime throw that actually
// happens at the instanceof, not the branch (and branches aren't flagged to throw).
const RegType& orig_type = work_line_->GetRegisterType(this, instance_of_inst.VRegB_22c());
const RegType& cast_type = ResolveClass<CheckAccess::kNo>(
dex::TypeIndex(instance_of_inst.VRegC_22c()));
if (!orig_type.Equals(cast_type) &&
!cast_type.IsUnresolvedTypes() && !orig_type.IsUnresolvedTypes() &&
cast_type.HasClass() && // Could be conflict type, make sure it has a class.
!cast_type.GetClass()->IsInterface() &&
(orig_type.IsZeroOrNull() ||
orig_type.IsStrictlyAssignableFrom(
cast_type.Merge(orig_type, ®_types_, this), this))) {
RegisterLine* update_line = RegisterLine::Create(code_item_accessor_.RegistersSize(),
allocator_,
GetRegTypeCache());
if (inst->Opcode() == Instruction::IF_EQZ) {
fallthrough_line.reset(update_line);
} else {
branch_line.reset(update_line);
}
update_line->CopyFromLine(work_line_.get());
update_line->SetRegisterType<LockOp::kKeep>(this,
instance_of_inst.VRegB_22c(),
cast_type);
if (!GetInstructionFlags(instance_of_idx).IsBranchTarget() && 0 != instance_of_idx) {
// See if instance-of was preceded by a move-object operation, common due to the small
// register encoding space of instance-of, and propagate type information to the source
// of the move-object.
// Note: this is only valid if the move source was not clobbered.
uint32_t move_idx = instance_of_idx - 1;
while (0 != move_idx && !GetInstructionFlags(move_idx).IsOpcode()) {
move_idx--;
}
DCHECK(GetInstructionFlags(move_idx).IsOpcode());
auto maybe_update_fn = [&instance_of_inst, update_line, this, &cast_type](
uint16_t move_src,
uint16_t move_trg)
REQUIRES_SHARED(Locks::mutator_lock_) {
if (move_trg == instance_of_inst.VRegB_22c() &&
move_src != instance_of_inst.VRegA_22c()) {
update_line->SetRegisterType<LockOp::kKeep>(this, move_src, cast_type);
}
};
const Instruction& move_inst = code_item_accessor_.InstructionAt(move_idx);
switch (move_inst.Opcode()) {
case Instruction::MOVE_OBJECT:
maybe_update_fn(move_inst.VRegB_12x(), move_inst.VRegA_12x());
break;
case Instruction::MOVE_OBJECT_FROM16:
maybe_update_fn(move_inst.VRegB_22x(), move_inst.VRegA_22x());
break;
case Instruction::MOVE_OBJECT_16:
maybe_update_fn(move_inst.VRegB_32x(), move_inst.VRegA_32x());
break;
default:
break;
}
}
}
}
break;
}
case Instruction::IF_LTZ:
case Instruction::IF_GEZ:
case Instruction::IF_GTZ:
case Instruction::IF_LEZ: {
const RegType& reg_type = work_line_->GetRegisterType(this, inst->VRegA_21t());
if (!reg_type.IsIntegralTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "type " << reg_type
<< " unexpected as arg to if-ltz/if-gez/if-gtz/if-lez";
}
break;
}
case Instruction::AGET_BOOLEAN:
VerifyAGet(inst, reg_types_.Boolean(), true);
break;
case Instruction::AGET_BYTE:
VerifyAGet(inst, reg_types_.Byte(), true);
break;
case Instruction::AGET_CHAR:
VerifyAGet(inst, reg_types_.Char(), true);
break;
case Instruction::AGET_SHORT:
VerifyAGet(inst, reg_types_.Short(), true);
break;
case Instruction::AGET:
VerifyAGet(inst, reg_types_.Integer(), true);
break;
case Instruction::AGET_WIDE:
VerifyAGet(inst, reg_types_.LongLo(), true);
break;
case Instruction::AGET_OBJECT:
VerifyAGet(inst, reg_types_.JavaLangObject(false), false);
break;
case Instruction::APUT_BOOLEAN:
VerifyAPut(inst, reg_types_.Boolean(), true);
break;
case Instruction::APUT_BYTE:
VerifyAPut(inst, reg_types_.Byte(), true);
break;
case Instruction::APUT_CHAR:
VerifyAPut(inst, reg_types_.Char(), true);
break;
case Instruction::APUT_SHORT:
VerifyAPut(inst, reg_types_.Short(), true);
break;
case Instruction::APUT:
VerifyAPut(inst, reg_types_.Integer(), true);
break;
case Instruction::APUT_WIDE:
VerifyAPut(inst, reg_types_.LongLo(), true);
break;
case Instruction::APUT_OBJECT:
VerifyAPut(inst, reg_types_.JavaLangObject(false), false);
break;
case Instruction::IGET_BOOLEAN:
case Instruction::IGET_BOOLEAN_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, false);
break;
case Instruction::IGET_BYTE:
case Instruction::IGET_BYTE_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, false);
break;
case Instruction::IGET_CHAR:
case Instruction::IGET_CHAR_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, false);
break;
case Instruction::IGET_SHORT:
case Instruction::IGET_SHORT_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, false);
break;
case Instruction::IGET:
case Instruction::IGET_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, false);
break;
case Instruction::IGET_WIDE:
case Instruction::IGET_WIDE_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, false);
break;
case Instruction::IGET_OBJECT:
case Instruction::IGET_OBJECT_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
false);
break;
case Instruction::IPUT_BOOLEAN:
case Instruction::IPUT_BOOLEAN_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, false);
break;
case Instruction::IPUT_BYTE:
case Instruction::IPUT_BYTE_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, false);
break;
case Instruction::IPUT_CHAR:
case Instruction::IPUT_CHAR_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, false);
break;
case Instruction::IPUT_SHORT:
case Instruction::IPUT_SHORT_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, false);
break;
case Instruction::IPUT:
case Instruction::IPUT_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, false);
break;
case Instruction::IPUT_WIDE:
case Instruction::IPUT_WIDE_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, false);
break;
case Instruction::IPUT_OBJECT:
case Instruction::IPUT_OBJECT_QUICK:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
false);
break;
case Instruction::SGET_BOOLEAN:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Boolean(), true, true);
break;
case Instruction::SGET_BYTE:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Byte(), true, true);
break;
case Instruction::SGET_CHAR:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Char(), true, true);
break;
case Instruction::SGET_SHORT:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Short(), true, true);
break;
case Instruction::SGET:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.Integer(), true, true);
break;
case Instruction::SGET_WIDE:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.LongLo(), true, true);
break;
case Instruction::SGET_OBJECT:
VerifyISFieldAccess<FieldAccessType::kAccGet>(inst, reg_types_.JavaLangObject(false), false,
true);
break;
case Instruction::SPUT_BOOLEAN:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Boolean(), true, true);
break;
case Instruction::SPUT_BYTE:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Byte(), true, true);
break;
case Instruction::SPUT_CHAR:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Char(), true, true);
break;
case Instruction::SPUT_SHORT:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Short(), true, true);
break;
case Instruction::SPUT:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.Integer(), true, true);
break;
case Instruction::SPUT_WIDE:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.LongLo(), true, true);
break;
case Instruction::SPUT_OBJECT:
VerifyISFieldAccess<FieldAccessType::kAccPut>(inst, reg_types_.JavaLangObject(false), false,
true);
break;
case Instruction::INVOKE_VIRTUAL:
case Instruction::INVOKE_VIRTUAL_RANGE:
case Instruction::INVOKE_SUPER:
case Instruction::INVOKE_SUPER_RANGE:
case Instruction::INVOKE_VIRTUAL_QUICK:
case Instruction::INVOKE_VIRTUAL_RANGE_QUICK: {
bool is_range = (inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE ||
inst->Opcode() == Instruction::INVOKE_SUPER_RANGE ||
inst->Opcode() == Instruction::INVOKE_VIRTUAL_RANGE_QUICK);
bool is_super = (inst->Opcode() == Instruction::INVOKE_SUPER ||
inst->Opcode() == Instruction::INVOKE_SUPER_RANGE);
MethodType type = is_super ? METHOD_SUPER : METHOD_VIRTUAL;
ArtMethod* called_method = VerifyInvocationArgs(inst, type, is_range);
const RegType* return_type = nullptr;
if (called_method != nullptr) {
ObjPtr<mirror::Class> return_type_class = can_load_classes_
? called_method->ResolveReturnType()
: called_method->LookupResolvedReturnType();
if (return_type_class != nullptr) {
return_type = &FromClass(called_method->GetReturnTypeDescriptor(),
return_type_class,
return_type_class->CannotBeAssignedFromOtherTypes());
} else {
DCHECK(!can_load_classes_ || self_->IsExceptionPending());
self_->ClearException();
}
}
if (return_type == nullptr) {
uint32_t method_idx = GetMethodIdxOfInvoke(inst);
const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
dex::TypeIndex return_type_idx =
dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
const char* descriptor = dex_file_->StringByTypeIdx(return_type_idx);
return_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
}
if (!return_type->IsLowHalf()) {
work_line_->SetResultRegisterType(this, *return_type);
} else {
work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_));
}
just_set_result = true;
break;
}
case Instruction::INVOKE_DIRECT:
case Instruction::INVOKE_DIRECT_RANGE: {
bool is_range = (inst->Opcode() == Instruction::INVOKE_DIRECT_RANGE);
ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_DIRECT, is_range);
const char* return_type_descriptor;
bool is_constructor;
const RegType* return_type = nullptr;
if (called_method == nullptr) {
uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
is_constructor = strcmp("<init>", dex_file_->StringDataByIdx(method_id.name_idx_)) == 0;
dex::TypeIndex return_type_idx =
dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
return_type_descriptor = dex_file_->StringByTypeIdx(return_type_idx);
} else {
is_constructor = called_method->IsConstructor();
return_type_descriptor = called_method->GetReturnTypeDescriptor();
ObjPtr<mirror::Class> return_type_class = can_load_classes_
? called_method->ResolveReturnType()
: called_method->LookupResolvedReturnType();
if (return_type_class != nullptr) {
return_type = &FromClass(return_type_descriptor,
return_type_class,
return_type_class->CannotBeAssignedFromOtherTypes());
} else {
DCHECK(!can_load_classes_ || self_->IsExceptionPending());
self_->ClearException();
}
}
if (is_constructor) {
/*
* Some additional checks when calling a constructor. We know from the invocation arg check
* that the "this" argument is an instance of called_method->klass. Now we further restrict
* that to require that called_method->klass is the same as this->klass or this->super,
* allowing the latter only if the "this" argument is the same as the "this" argument to
* this method (which implies that we're in a constructor ourselves).
*/
const RegType& this_type = work_line_->GetInvocationThis(this, inst);
if (this_type.IsConflict()) // failure.
break;
/* no null refs allowed (?) */
if (this_type.IsZeroOrNull()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unable to initialize null ref";
break;
}
/* must be in same class or in superclass */
// const RegType& this_super_klass = this_type.GetSuperClass(®_types_);
// TODO: re-enable constructor type verification
// if (this_super_klass.IsConflict()) {
// Unknown super class, fail so we re-check at runtime.
// Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "super class unknown for '" << this_type << "'";
// break;
// }
/* arg must be an uninitialized reference */
if (!this_type.IsUninitializedTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Expected initialization on uninitialized reference "
<< this_type;
break;
}
/*
* Replace the uninitialized reference with an initialized one. We need to do this for all
* registers that have the same object instance in them, not just the "this" register.
*/
work_line_->MarkRefsAsInitialized(this, this_type);
}
if (return_type == nullptr) {
return_type = ®_types_.FromDescriptor(class_loader_.Get(),
return_type_descriptor,
false);
}
if (!return_type->IsLowHalf()) {
work_line_->SetResultRegisterType(this, *return_type);
} else {
work_line_->SetResultRegisterTypeWide(*return_type, return_type->HighHalf(®_types_));
}
just_set_result = true;
break;
}
case Instruction::INVOKE_STATIC:
case Instruction::INVOKE_STATIC_RANGE: {
bool is_range = (inst->Opcode() == Instruction::INVOKE_STATIC_RANGE);
ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_STATIC, is_range);
const char* descriptor;
if (called_method == nullptr) {
uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
dex::TypeIndex return_type_idx =
dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
descriptor = dex_file_->StringByTypeIdx(return_type_idx);
} else {
descriptor = called_method->GetReturnTypeDescriptor();
}
const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
descriptor,
false);
if (!return_type.IsLowHalf()) {
work_line_->SetResultRegisterType(this, return_type);
} else {
work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_));
}
just_set_result = true;
}
break;
case Instruction::INVOKE_INTERFACE:
case Instruction::INVOKE_INTERFACE_RANGE: {
bool is_range = (inst->Opcode() == Instruction::INVOKE_INTERFACE_RANGE);
ArtMethod* abs_method = VerifyInvocationArgs(inst, METHOD_INTERFACE, is_range);
if (abs_method != nullptr) {
ObjPtr<mirror::Class> called_interface = abs_method->GetDeclaringClass();
if (!called_interface->IsInterface() && !called_interface->IsObjectClass()) {
Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected interface class in invoke-interface '"
<< abs_method->PrettyMethod() << "'";
break;
}
}
/* Get the type of the "this" arg, which should either be a sub-interface of called
* interface or Object (see comments in RegType::JoinClass).
*/
const RegType& this_type = work_line_->GetInvocationThis(this, inst);
if (this_type.IsZeroOrNull()) {
/* null pointer always passes (and always fails at runtime) */
} else {
if (this_type.IsUninitializedTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "interface call on uninitialized object "
<< this_type;
break;
}
// In the past we have tried to assert that "called_interface" is assignable
// from "this_type.GetClass()", however, as we do an imprecise Join
// (RegType::JoinClass) we don't have full information on what interfaces are
// implemented by "this_type". For example, two classes may implement the same
// interfaces and have a common parent that doesn't implement the interface. The
// join will set "this_type" to the parent class and a test that this implements
// the interface will incorrectly fail.
}
/*
* We don't have an object instance, so we can't find the concrete method. However, all of
* the type information is in the abstract method, so we're good.
*/
const char* descriptor;
if (abs_method == nullptr) {
uint32_t method_idx = (is_range) ? inst->VRegB_3rc() : inst->VRegB_35c();
const dex::MethodId& method_id = dex_file_->GetMethodId(method_idx);
dex::TypeIndex return_type_idx =
dex_file_->GetProtoId(method_id.proto_idx_).return_type_idx_;
descriptor = dex_file_->StringByTypeIdx(return_type_idx);
} else {
descriptor = abs_method->GetReturnTypeDescriptor();
}
const RegType& return_type = reg_types_.FromDescriptor(class_loader_.Get(),
descriptor,
false);
if (!return_type.IsLowHalf()) {
work_line_->SetResultRegisterType(this, return_type);
} else {
work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_));
}
just_set_result = true;
break;
}
case Instruction::INVOKE_POLYMORPHIC:
case Instruction::INVOKE_POLYMORPHIC_RANGE: {
bool is_range = (inst->Opcode() == Instruction::INVOKE_POLYMORPHIC_RANGE);
ArtMethod* called_method = VerifyInvocationArgs(inst, METHOD_POLYMORPHIC, is_range);
if (called_method == nullptr) {
// Convert potential soft failures in VerifyInvocationArgs() to hard errors.
if (failure_messages_.size() > 0) {
std::string message = failure_messages_.back()->str();
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << message;
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-polymorphic verification failure.";
}
break;
}
if (!CheckSignaturePolymorphicMethod(called_method) ||
!CheckSignaturePolymorphicReceiver(inst)) {
DCHECK(HasFailures());
break;
}
const uint16_t vRegH = (is_range) ? inst->VRegH_4rcc() : inst->VRegH_45cc();
const dex::ProtoIndex proto_idx(vRegH);
const char* return_descriptor =
dex_file_->GetReturnTypeDescriptor(dex_file_->GetProtoId(proto_idx));
const RegType& return_type =
reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
if (!return_type.IsLowHalf()) {
work_line_->SetResultRegisterType(this, return_type);
} else {
work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_));
}
just_set_result = true;
break;
}
case Instruction::INVOKE_CUSTOM:
case Instruction::INVOKE_CUSTOM_RANGE: {
// Verify registers based on method_type in the call site.
bool is_range = (inst->Opcode() == Instruction::INVOKE_CUSTOM_RANGE);
// Step 1. Check the call site that produces the method handle for invocation
const uint32_t call_site_idx = is_range ? inst->VRegB_3rc() : inst->VRegB_35c();
if (!CheckCallSite(call_site_idx)) {
DCHECK(HasFailures());
break;
}
// Step 2. Check the register arguments correspond to the expected arguments for the
// method handle produced by step 1. The dex file verifier has checked ranges for
// the first three arguments and CheckCallSite has checked the method handle type.
const dex::ProtoIndex proto_idx = dex_file_->GetProtoIndexForCallSite(call_site_idx);
const dex::ProtoId& proto_id = dex_file_->GetProtoId(proto_idx);
DexFileParameterIterator param_it(*dex_file_, proto_id);
// Treat method as static as it has yet to be determined.
VerifyInvocationArgsFromIterator(¶m_it, inst, METHOD_STATIC, is_range, nullptr);
const char* return_descriptor = dex_file_->GetReturnTypeDescriptor(proto_id);
// Step 3. Propagate return type information
const RegType& return_type =
reg_types_.FromDescriptor(class_loader_.Get(), return_descriptor, false);
if (!return_type.IsLowHalf()) {
work_line_->SetResultRegisterType(this, return_type);
} else {
work_line_->SetResultRegisterTypeWide(return_type, return_type.HighHalf(®_types_));
}
just_set_result = true;
break;
}
case Instruction::NEG_INT:
case Instruction::NOT_INT:
work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer());
break;
case Instruction::NEG_LONG:
case Instruction::NOT_LONG:
work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::NEG_FLOAT:
work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Float());
break;
case Instruction::NEG_DOUBLE:
work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::INT_TO_LONG:
work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.Integer());
break;
case Instruction::INT_TO_FLOAT:
work_line_->CheckUnaryOp(this, inst, reg_types_.Float(), reg_types_.Integer());
break;
case Instruction::INT_TO_DOUBLE:
work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.Integer());
break;
case Instruction::LONG_TO_INT:
work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::LONG_TO_FLOAT:
work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::LONG_TO_DOUBLE:
work_line_->CheckUnaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::FLOAT_TO_INT:
work_line_->CheckUnaryOp(this, inst, reg_types_.Integer(), reg_types_.Float());
break;
case Instruction::FLOAT_TO_LONG:
work_line_->CheckUnaryOpToWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.Float());
break;
case Instruction::FLOAT_TO_DOUBLE:
work_line_->CheckUnaryOpToWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.Float());
break;
case Instruction::DOUBLE_TO_INT:
work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Integer(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::DOUBLE_TO_LONG:
work_line_->CheckUnaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::DOUBLE_TO_FLOAT:
work_line_->CheckUnaryOpFromWide(this, inst, reg_types_.Float(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::INT_TO_BYTE:
work_line_->CheckUnaryOp(this, inst, reg_types_.Byte(), reg_types_.Integer());
break;
case Instruction::INT_TO_CHAR:
work_line_->CheckUnaryOp(this, inst, reg_types_.Char(), reg_types_.Integer());
break;
case Instruction::INT_TO_SHORT:
work_line_->CheckUnaryOp(this, inst, reg_types_.Short(), reg_types_.Integer());
break;
case Instruction::ADD_INT:
case Instruction::SUB_INT:
case Instruction::MUL_INT:
case Instruction::REM_INT:
case Instruction::DIV_INT:
case Instruction::SHL_INT:
case Instruction::SHR_INT:
case Instruction::USHR_INT:
work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
reg_types_.Integer(), false);
break;
case Instruction::AND_INT:
case Instruction::OR_INT:
case Instruction::XOR_INT:
work_line_->CheckBinaryOp(this, inst, reg_types_.Integer(), reg_types_.Integer(),
reg_types_.Integer(), true);
break;
case Instruction::ADD_LONG:
case Instruction::SUB_LONG:
case Instruction::MUL_LONG:
case Instruction::DIV_LONG:
case Instruction::REM_LONG:
case Instruction::AND_LONG:
case Instruction::OR_LONG:
case Instruction::XOR_LONG:
work_line_->CheckBinaryOpWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::SHL_LONG:
case Instruction::SHR_LONG:
case Instruction::USHR_LONG:
/* shift distance is Int, making these different from other binary operations */
work_line_->CheckBinaryOpWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.Integer());
break;
case Instruction::ADD_FLOAT:
case Instruction::SUB_FLOAT:
case Instruction::MUL_FLOAT:
case Instruction::DIV_FLOAT:
case Instruction::REM_FLOAT:
work_line_->CheckBinaryOp(this, inst, reg_types_.Float(), reg_types_.Float(),
reg_types_.Float(), false);
break;
case Instruction::ADD_DOUBLE:
case Instruction::SUB_DOUBLE:
case Instruction::MUL_DOUBLE:
case Instruction::DIV_DOUBLE:
case Instruction::REM_DOUBLE:
work_line_->CheckBinaryOpWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::ADD_INT_2ADDR:
case Instruction::SUB_INT_2ADDR:
case Instruction::MUL_INT_2ADDR:
case Instruction::REM_INT_2ADDR:
case Instruction::SHL_INT_2ADDR:
case Instruction::SHR_INT_2ADDR:
case Instruction::USHR_INT_2ADDR:
work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
reg_types_.Integer(), false);
break;
case Instruction::AND_INT_2ADDR:
case Instruction::OR_INT_2ADDR:
case Instruction::XOR_INT_2ADDR:
work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
reg_types_.Integer(), true);
break;
case Instruction::DIV_INT_2ADDR:
work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Integer(), reg_types_.Integer(),
reg_types_.Integer(), false);
break;
case Instruction::ADD_LONG_2ADDR:
case Instruction::SUB_LONG_2ADDR:
case Instruction::MUL_LONG_2ADDR:
case Instruction::DIV_LONG_2ADDR:
case Instruction::REM_LONG_2ADDR:
case Instruction::AND_LONG_2ADDR:
case Instruction::OR_LONG_2ADDR:
case Instruction::XOR_LONG_2ADDR:
work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.LongLo(), reg_types_.LongHi());
break;
case Instruction::SHL_LONG_2ADDR:
case Instruction::SHR_LONG_2ADDR:
case Instruction::USHR_LONG_2ADDR:
work_line_->CheckBinaryOp2addrWideShift(this, inst, reg_types_.LongLo(), reg_types_.LongHi(),
reg_types_.Integer());
break;
case Instruction::ADD_FLOAT_2ADDR:
case Instruction::SUB_FLOAT_2ADDR:
case Instruction::MUL_FLOAT_2ADDR:
case Instruction::DIV_FLOAT_2ADDR:
case Instruction::REM_FLOAT_2ADDR:
work_line_->CheckBinaryOp2addr(this, inst, reg_types_.Float(), reg_types_.Float(),
reg_types_.Float(), false);
break;
case Instruction::ADD_DOUBLE_2ADDR:
case Instruction::SUB_DOUBLE_2ADDR:
case Instruction::MUL_DOUBLE_2ADDR:
case Instruction::DIV_DOUBLE_2ADDR:
case Instruction::REM_DOUBLE_2ADDR:
work_line_->CheckBinaryOp2addrWide(this, inst, reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi(),
reg_types_.DoubleLo(), reg_types_.DoubleHi());
break;
case Instruction::ADD_INT_LIT16:
case Instruction::RSUB_INT_LIT16:
case Instruction::MUL_INT_LIT16:
case Instruction::DIV_INT_LIT16:
case Instruction::REM_INT_LIT16:
work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
true);
break;
case Instruction::AND_INT_LIT16:
case Instruction::OR_INT_LIT16:
case Instruction::XOR_INT_LIT16:
work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
true);
break;
case Instruction::ADD_INT_LIT8:
case Instruction::RSUB_INT_LIT8:
case Instruction::MUL_INT_LIT8:
case Instruction::DIV_INT_LIT8:
case Instruction::REM_INT_LIT8:
case Instruction::SHL_INT_LIT8:
case Instruction::SHR_INT_LIT8:
case Instruction::USHR_INT_LIT8:
work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), false,
false);
break;
case Instruction::AND_INT_LIT8:
case Instruction::OR_INT_LIT8:
case Instruction::XOR_INT_LIT8:
work_line_->CheckLiteralOp(this, inst, reg_types_.Integer(), reg_types_.Integer(), true,
false);
break;
// Special instructions.
case Instruction::RETURN_VOID_NO_BARRIER:
if (IsConstructor() && !IsStatic()) {
const RegType& declaring_class = GetDeclaringClass();
if (declaring_class.IsUnresolvedReference()) {
// We must iterate over the fields, even if we cannot use mirror classes to do so. Do it
// manually over the underlying dex file.
uint32_t first_index = GetFirstFinalInstanceFieldIndex(*dex_file_,
dex_file_->GetMethodId(dex_method_idx_).class_idx_);
if (first_index != dex::kDexNoIndex) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for field "
<< first_index;
}
break;
}
ObjPtr<mirror::Class> klass = declaring_class.GetClass();
for (uint32_t i = 0, num_fields = klass->NumInstanceFields(); i < num_fields; ++i) {
if (klass->GetInstanceField(i)->IsFinal()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void-no-barrier not expected for "
<< klass->GetInstanceField(i)->PrettyField();
break;
}
}
}
// Handle this like a RETURN_VOID now. Code is duplicated to separate standard from
// quickened opcodes (otherwise this could be a fall-through).
if (!IsConstructor()) {
if (!GetMethodReturnType().IsConflict()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "return-void not expected";
}
}
break;
/* These should never appear during verification. */
case Instruction::UNUSED_3E ... Instruction::UNUSED_43:
case Instruction::UNUSED_F3 ... Instruction::UNUSED_F9:
case Instruction::UNUSED_79:
case Instruction::UNUSED_7A:
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Unexpected opcode " << inst->DumpString(dex_file_);
break;
/*
* DO NOT add a "default" clause here. Without it the compiler will
* complain if an instruction is missing (which is desirable).
*/
} // end - switch (dec_insn.opcode)
if (flags_.have_pending_hard_failure_) {
if (IsAotMode()) {
/* When AOT compiling, check that the last failure is a hard failure */
if (failures_[failures_.size() - 1] != VERIFY_ERROR_BAD_CLASS_HARD) {
LOG(ERROR) << "Pending failures:";
for (auto& error : failures_) {
LOG(ERROR) << error;
}
for (auto& error_msg : failure_messages_) {
LOG(ERROR) << error_msg->str();
}
LOG(FATAL) << "Pending hard failure, but last failure not hard.";
}
}
/* immediate failure, reject class */
info_messages_ << "Rejecting opcode " << inst->DumpString(dex_file_);
return false;
} else if (flags_.have_pending_runtime_throw_failure_) {
LogVerifyInfo() << "Elevating opcode flags from " << opcode_flags << " to Throw";
/* checking interpreter will throw, mark following code as unreachable */
opcode_flags = Instruction::kThrow;
// Note: the flag must be reset as it is only global to decouple Fail and is semantically per
// instruction. However, RETURN checking may throw LOCKING errors, so we clear at the
// very end.
}
/*
* If we didn't just set the result register, clear it out. This ensures that you can only use
* "move-result" immediately after the result is set. (We could check this statically, but it's
* not expensive and it makes our debugging output cleaner.)
*/
if (!just_set_result) {
work_line_->SetResultTypeToUnknown(GetRegTypeCache());
}
/*
* Handle "branch". Tag the branch target.
*
* NOTE: instructions like Instruction::EQZ provide information about the
* state of the register when the branch is taken or not taken. For example,
* somebody could get a reference field, check it for zero, and if the
* branch is taken immediately store that register in a boolean field
* since the value is known to be zero. We do not currently account for
* that, and will reject the code.
*
* TODO: avoid re-fetching the branch target
*/
if ((opcode_flags & Instruction::kBranch) != 0) {
bool isConditional, selfOkay;
if (!GetBranchOffset(work_insn_idx_, &branch_target, &isConditional, &selfOkay)) {
/* should never happen after static verification */
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "bad branch";
return false;
}
DCHECK_EQ(isConditional, (opcode_flags & Instruction::kContinue) != 0);
if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(),
work_insn_idx_ + branch_target)) {
return false;
}
/* update branch target, set "changed" if appropriate */
if (nullptr != branch_line) {
if (!UpdateRegisters(work_insn_idx_ + branch_target, branch_line.get(), false)) {
return false;
}
} else {
if (!UpdateRegisters(work_insn_idx_ + branch_target, work_line_.get(), false)) {
return false;
}
}
}
/*
* Handle "switch". Tag all possible branch targets.
*
* We've already verified that the table is structurally sound, so we
* just need to walk through and tag the targets.
*/
if ((opcode_flags & Instruction::kSwitch) != 0) {
int offset_to_switch = insns[1] | (static_cast<int32_t>(insns[2]) << 16);
const uint16_t* switch_insns = insns + offset_to_switch;
int switch_count = switch_insns[1];
int offset_to_targets, targ;
if ((*insns & 0xff) == Instruction::PACKED_SWITCH) {
/* 0 = sig, 1 = count, 2/3 = first key */
offset_to_targets = 4;
} else {
/* 0 = sig, 1 = count, 2..count * 2 = keys */
DCHECK((*insns & 0xff) == Instruction::SPARSE_SWITCH);
offset_to_targets = 2 + 2 * switch_count;
}
/* verify each switch target */
for (targ = 0; targ < switch_count; targ++) {
int offset;
uint32_t abs_offset;
/* offsets are 32-bit, and only partly endian-swapped */
offset = switch_insns[offset_to_targets + targ * 2] |
(static_cast<int32_t>(switch_insns[offset_to_targets + targ * 2 + 1]) << 16);
abs_offset = work_insn_idx_ + offset;
DCHECK_LT(abs_offset, code_item_accessor_.InsnsSizeInCodeUnits());
if (!CheckNotMoveExceptionOrMoveResult(code_item_accessor_.Insns(), abs_offset)) {
return false;
}
if (!UpdateRegisters(abs_offset, work_line_.get(), false)) {
return false;
}
}
}
/*
* Handle instructions that can throw and that are sitting in a "try" block. (If they're not in a
* "try" block when they throw, control transfers out of the method.)
*/
if ((opcode_flags & Instruction::kThrow) != 0 && GetInstructionFlags(work_insn_idx_).IsInTry()) {
bool has_catch_all_handler = false;
const dex::TryItem* try_item = code_item_accessor_.FindTryItem(work_insn_idx_);
CHECK(try_item != nullptr);
CatchHandlerIterator iterator(code_item_accessor_, *try_item);
// Need the linker to try and resolve the handled class to check if it's Throwable.
ClassLinker* linker = GetClassLinker();
for (; iterator.HasNext(); iterator.Next()) {
dex::TypeIndex handler_type_idx = iterator.GetHandlerTypeIndex();
if (!handler_type_idx.IsValid()) {
has_catch_all_handler = true;
} else {
// It is also a catch-all if it is java.lang.Throwable.
ObjPtr<mirror::Class> klass =
linker->ResolveType(handler_type_idx, dex_cache_, class_loader_);
if (klass != nullptr) {
if (klass == GetClassRoot<mirror::Throwable>()) {
has_catch_all_handler = true;
}
} else {
// Clear exception.
DCHECK(self_->IsExceptionPending());
self_->ClearException();
}
}
/*
* Merge registers into the "catch" block. We want to use the "savedRegs" rather than
* "work_regs", because at runtime the exception will be thrown before the instruction
* modifies any registers.
*/
if (kVerifierDebug) {
LogVerifyInfo() << "Updating exception handler 0x"
<< std::hex << iterator.GetHandlerAddress();
}
if (!UpdateRegisters(iterator.GetHandlerAddress(), saved_line_.get(), false)) {
return false;
}
}
/*
* If the monitor stack depth is nonzero, there must be a "catch all" handler for this
* instruction. This does apply to monitor-exit because of async exception handling.
*/
if (work_line_->MonitorStackDepth() > 0 && !has_catch_all_handler) {
/*
* The state in work_line reflects the post-execution state. If the current instruction is a
* monitor-enter and the monitor stack was empty, we don't need a catch-all (if it throws,
* it will do so before grabbing the lock).
*/
if (inst->Opcode() != Instruction::MONITOR_ENTER || work_line_->MonitorStackDepth() != 1) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "expected to be within a catch-all for an instruction where a monitor is held";
return false;
}
}
}
/* Handle "continue". Tag the next consecutive instruction.
* Note: Keep the code handling "continue" case below the "branch" and "switch" cases,
* because it changes work_line_ when performing peephole optimization
* and this change should not be used in those cases.
*/
if ((opcode_flags & Instruction::kContinue) != 0 && !exc_handler_unreachable) {
DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
uint32_t next_insn_idx = work_insn_idx_ + inst->SizeInCodeUnits();
if (next_insn_idx >= code_item_accessor_.InsnsSizeInCodeUnits()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Execution can walk off end of code area";
return false;
}
// The only way to get to a move-exception instruction is to get thrown there. Make sure the
// next instruction isn't one.
if (!CheckNotMoveException(code_item_accessor_.Insns(), next_insn_idx)) {
return false;
}
if (nullptr != fallthrough_line) {
// Make workline consistent with fallthrough computed from peephole optimization.
work_line_->CopyFromLine(fallthrough_line.get());
}
if (GetInstructionFlags(next_insn_idx).IsReturn()) {
// For returns we only care about the operand to the return, all other registers are dead.
const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn_idx);
AdjustReturnLine(this, ret_inst, work_line_.get());
}
RegisterLine* next_line = reg_table_.GetLine(next_insn_idx);
if (next_line != nullptr) {
// Merge registers into what we have for the next instruction, and set the "changed" flag if
// needed. If the merge changes the state of the registers then the work line will be
// updated.
if (!UpdateRegisters(next_insn_idx, work_line_.get(), true)) {
return false;
}
} else {
/*
* We're not recording register data for the next instruction, so we don't know what the
* prior state was. We have to assume that something has changed and re-evaluate it.
*/
GetModifiableInstructionFlags(next_insn_idx).SetChanged();
}
}
/* If we're returning from the method, make sure monitor stack is empty. */
if ((opcode_flags & Instruction::kReturn) != 0) {
work_line_->VerifyMonitorStackEmpty(this);
}
/*
* Update start_guess. Advance to the next instruction of that's
* possible, otherwise use the branch target if one was found. If
* neither of those exists we're in a return or throw; leave start_guess
* alone and let the caller sort it out.
*/
if ((opcode_flags & Instruction::kContinue) != 0) {
DCHECK_EQ(&code_item_accessor_.InstructionAt(work_insn_idx_), inst);
*start_guess = work_insn_idx_ + inst->SizeInCodeUnits();
} else if ((opcode_flags & Instruction::kBranch) != 0) {
/* we're still okay if branch_target is zero */
*start_guess = work_insn_idx_ + branch_target;
}
DCHECK_LT(*start_guess, code_item_accessor_.InsnsSizeInCodeUnits());
DCHECK(GetInstructionFlags(*start_guess).IsOpcode());
if (flags_.have_pending_runtime_throw_failure_) {
flags_.have_any_pending_runtime_throw_failure_ = true;
// Reset the pending_runtime_throw flag now.
flags_.have_pending_runtime_throw_failure_ = false;
}
return true;
} // NOLINT(readability/fn_size)
template <bool kVerifierDebug>
template <CheckAccess C>
const RegType& MethodVerifier<kVerifierDebug>::ResolveClass(dex::TypeIndex class_idx) {
ClassLinker* linker = GetClassLinker();
ObjPtr<mirror::Class> klass = can_load_classes_
? linker->ResolveType(class_idx, dex_cache_, class_loader_)
: linker->LookupResolvedType(class_idx, dex_cache_.Get(), class_loader_.Get());
if (can_load_classes_ && klass == nullptr) {
DCHECK(self_->IsExceptionPending());
self_->ClearException();
}
const RegType* result = nullptr;
if (klass != nullptr) {
bool precise = klass->CannotBeAssignedFromOtherTypes();
if (precise && !IsInstantiableOrPrimitive(klass)) {
const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
UninstantiableError(descriptor);
precise = false;
}
result = reg_types_.FindClass(klass, precise);
if (result == nullptr) {
const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
result = reg_types_.InsertClass(descriptor, klass, precise);
}
} else {
const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
result = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
}
DCHECK(result != nullptr);
if (result->IsConflict()) {
const char* descriptor = dex_file_->StringByTypeIdx(class_idx);
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "accessing broken descriptor '" << descriptor
<< "' in " << GetDeclaringClass();
return *result;
}
// Record result of class resolution attempt.
VerifierDeps::MaybeRecordClassResolution(*dex_file_, class_idx, klass);
// If requested, check if access is allowed. Unresolved types are included in this check, as the
// interpreter only tests whether access is allowed when a class is not pre-verified and runs in
// the access-checks interpreter. If result is primitive, skip the access check.
//
// Note: we do this for unresolved classes to trigger re-verification at runtime.
if (C != CheckAccess::kNo &&
result->IsNonZeroReferenceTypes() &&
((C == CheckAccess::kYes && IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP))
|| !result->IsUnresolvedTypes())) {
const RegType& referrer = GetDeclaringClass();
if ((IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP) || !referrer.IsUnresolvedTypes()) &&
!referrer.CanAccess(*result)) {
Fail(VERIFY_ERROR_ACCESS_CLASS) << "(possibly) illegal class access: '"
<< referrer << "' -> '" << *result << "'";
}
}
return *result;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::HandleMoveException(const Instruction* inst) {
// We do not allow MOVE_EXCEPTION as the first instruction in a method. This is a simple case
// where one entrypoint to the catch block is not actually an exception path.
if (work_insn_idx_ == 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "move-exception at pc 0x0";
return true;
}
/*
* This statement can only appear as the first instruction in an exception handler. We verify
* that as part of extracting the exception type from the catch block list.
*/
auto caught_exc_type_fn = [&]() REQUIRES_SHARED(Locks::mutator_lock_) ->
std::pair<bool, const RegType*> {
const RegType* common_super = nullptr;
if (code_item_accessor_.TriesSize() != 0) {
const uint8_t* handlers_ptr = code_item_accessor_.GetCatchHandlerData();
uint32_t handlers_size = DecodeUnsignedLeb128(&handlers_ptr);
const RegType* unresolved = nullptr;
for (uint32_t i = 0; i < handlers_size; i++) {
CatchHandlerIterator iterator(handlers_ptr);
for (; iterator.HasNext(); iterator.Next()) {
if (iterator.GetHandlerAddress() == (uint32_t) work_insn_idx_) {
if (!iterator.GetHandlerTypeIndex().IsValid()) {
common_super = ®_types_.JavaLangThrowable(false);
} else {
// Do access checks only on resolved exception classes.
const RegType& exception =
ResolveClass<CheckAccess::kOnResolvedClass>(iterator.GetHandlerTypeIndex());
if (!reg_types_.JavaLangThrowable(false).IsAssignableFrom(exception, this)) {
DCHECK(!exception.IsUninitializedTypes()); // Comes from dex, shouldn't be uninit.
if (exception.IsUnresolvedTypes()) {
if (unresolved == nullptr) {
unresolved = &exception;
} else {
unresolved = &unresolved->SafeMerge(exception, ®_types_, this);
}
} else {
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unexpected non-exception class "
<< exception;
return std::make_pair(true, ®_types_.Conflict());
}
} else if (common_super == nullptr) {
common_super = &exception;
} else if (common_super->Equals(exception)) {
// odd case, but nothing to do
} else {
common_super = &common_super->Merge(exception, ®_types_, this);
if (FailOrAbort(reg_types_.JavaLangThrowable(false).IsAssignableFrom(
*common_super, this),
"java.lang.Throwable is not assignable-from common_super at ",
work_insn_idx_)) {
break;
}
}
}
}
}
handlers_ptr = iterator.EndDataPointer();
}
if (unresolved != nullptr) {
if (!IsAotMode() && common_super == nullptr) {
// This is an unreachable handler.
// We need to post a failure. The compiler currently does not handle unreachable
// code correctly.
Fail(VERIFY_ERROR_SKIP_COMPILER, /*pending_exc=*/ false)
<< "Unresolved catch handler, fail for compiler";
return std::make_pair(false, unresolved);
}
// Soft-fail, but do not handle this with a synthetic throw.
Fail(VERIFY_ERROR_NO_CLASS, /*pending_exc=*/ false) << "Unresolved catch handler";
if (common_super != nullptr) {
unresolved = &unresolved->Merge(*common_super, ®_types_, this);
}
return std::make_pair(true, unresolved);
}
}
if (common_super == nullptr) {
/* no catch blocks, or no catches with classes we can find */
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "unable to find exception handler";
return std::make_pair(true, ®_types_.Conflict());
}
return std::make_pair(true, common_super);
};
auto result = caught_exc_type_fn();
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_11x(), *result.second);
return result.first;
}
template <bool kVerifierDebug>
ArtMethod* MethodVerifier<kVerifierDebug>::ResolveMethodAndCheckAccess(
uint32_t dex_method_idx, MethodType method_type) {
const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx);
const RegType& klass_type = ResolveClass<CheckAccess::kYes>(method_id.class_idx_);
if (klass_type.IsConflict()) {
std::string append(" in attempt to access method ");
append += dex_file_->GetMethodName(method_id);
AppendToLastFailMessage(append);
return nullptr;
}
if (klass_type.IsUnresolvedTypes()) {
return nullptr; // Can't resolve Class so no more to do here
}
ObjPtr<mirror::Class> klass = klass_type.GetClass();
const RegType& referrer = GetDeclaringClass();
ClassLinker* class_linker = GetClassLinker();
PointerSize pointer_size = class_linker->GetImagePointerSize();
ArtMethod* res_method = dex_cache_->GetResolvedMethod(dex_method_idx, pointer_size);
if (res_method == nullptr) {
res_method = class_linker->FindResolvedMethod(
klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
}
// Record result of method resolution attempt. The klass resolution has recorded whether
// the class is an interface or not and therefore the type of the lookup performed above.
// TODO: Maybe we should not record dependency if the invoke type does not match the lookup type.
VerifierDeps::MaybeRecordMethodResolution(*dex_file_, dex_method_idx, res_method);
bool must_fail = false;
// This is traditional and helps with screwy bytecode. It will tell you that, yes, a method
// exists, but that it's called incorrectly. This significantly helps debugging, as locally it's
// hard to see the differences.
// If we don't have res_method here we must fail. Just use this bool to make sure of that with a
// DCHECK.
if (res_method == nullptr) {
must_fail = true;
// Try to find the method also with the other type for better error reporting below
// but do not store such bogus lookup result in the DexCache or VerifierDeps.
res_method = class_linker->FindIncompatibleMethod(
klass, dex_cache_.Get(), class_loader_.Get(), dex_method_idx);
}
if (res_method == nullptr) {
Fail(VERIFY_ERROR_NO_METHOD) << "couldn't find method "
<< klass->PrettyDescriptor() << "."
<< dex_file_->GetMethodName(method_id) << " "
<< dex_file_->GetMethodSignature(method_id);
return nullptr;
}
// Make sure calls to constructors are "direct". There are additional restrictions but we don't
// enforce them here.
if (res_method->IsConstructor() && method_type != METHOD_DIRECT) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting non-direct call to constructor "
<< res_method->PrettyMethod();
return nullptr;
}
// Disallow any calls to class initializers.
if (res_method->IsClassInitializer()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "rejecting call to class initializer "
<< res_method->PrettyMethod();
return nullptr;
}
// Check that interface methods are static or match interface classes.
// We only allow statics if we don't have default methods enabled.
//
// Note: this check must be after the initializer check, as those are required to fail a class,
// while this check implies an IncompatibleClassChangeError.
if (klass->IsInterface()) {
// methods called on interfaces should be invoke-interface, invoke-super, invoke-direct (if
// default methods are supported for the dex file), or invoke-static.
if (method_type != METHOD_INTERFACE &&
method_type != METHOD_STATIC &&
(!dex_file_->SupportsDefaultMethods() ||
method_type != METHOD_DIRECT) &&
method_type != METHOD_SUPER) {
Fail(VERIFY_ERROR_CLASS_CHANGE)
<< "non-interface method " << dex_file_->PrettyMethod(dex_method_idx)
<< " is in an interface class " << klass->PrettyClass();
return nullptr;
}
} else {
if (method_type == METHOD_INTERFACE) {
Fail(VERIFY_ERROR_CLASS_CHANGE)
<< "interface method " << dex_file_->PrettyMethod(dex_method_idx)
<< " is in a non-interface class " << klass->PrettyClass();
return nullptr;
}
}
// Check specifically for non-public object methods being provided for interface dispatch. This
// can occur if we failed to find a method with FindInterfaceMethod but later find one with
// FindClassMethod for error message use.
if (method_type == METHOD_INTERFACE &&
res_method->GetDeclaringClass()->IsObjectClass() &&
!res_method->IsPublic()) {
Fail(VERIFY_ERROR_NO_METHOD) << "invoke-interface " << klass->PrettyDescriptor() << "."
<< dex_file_->GetMethodName(method_id) << " "
<< dex_file_->GetMethodSignature(method_id) << " resolved to "
<< "non-public object method " << res_method->PrettyMethod() << " "
<< "but non-public Object methods are excluded from interface "
<< "method resolution.";
return nullptr;
}
// Check if access is allowed.
if (!referrer.CanAccessMember(res_method->GetDeclaringClass(), res_method->GetAccessFlags())) {
Fail(VERIFY_ERROR_ACCESS_METHOD) << "illegal method access (call "
<< res_method->PrettyMethod()
<< " from " << referrer << ")";
return res_method;
}
// Check that invoke-virtual and invoke-super are not used on private methods of the same class.
if (res_method->IsPrivate() && (method_type == METHOD_VIRTUAL || method_type == METHOD_SUPER)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invoke-super/virtual can't be used on private method "
<< res_method->PrettyMethod();
return nullptr;
}
// See if the method type implied by the invoke instruction matches the access flags for the
// target method. The flags for METHOD_POLYMORPHIC are based on there being precisely two
// signature polymorphic methods supported by the run-time which are native methods with variable
// arguments.
if ((method_type == METHOD_DIRECT && (!res_method->IsDirect() || res_method->IsStatic())) ||
(method_type == METHOD_STATIC && !res_method->IsStatic()) ||
((method_type == METHOD_SUPER ||
method_type == METHOD_VIRTUAL ||
method_type == METHOD_INTERFACE) && res_method->IsDirect()) ||
((method_type == METHOD_POLYMORPHIC) &&
(!res_method->IsNative() || !res_method->IsVarargs()))) {
Fail(VERIFY_ERROR_CLASS_CHANGE) << "invoke type (" << method_type << ") does not match method "
"type of " << res_method->PrettyMethod();
return nullptr;
}
// Make sure we weren't expecting to fail.
DCHECK(!must_fail) << "invoke type (" << method_type << ")"
<< klass->PrettyDescriptor() << "."
<< dex_file_->GetMethodName(method_id) << " "
<< dex_file_->GetMethodSignature(method_id) << " unexpectedly resolved to "
<< res_method->PrettyMethod() << " without error. Initially this method was "
<< "not found so we were expecting to fail for some reason.";
return res_method;
}
template <bool kVerifierDebug>
template <class T>
ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgsFromIterator(
T* it, const Instruction* inst, MethodType method_type, bool is_range, ArtMethod* res_method) {
DCHECK_EQ(!is_range, inst->HasVarArgs());
// We use vAA as our expected arg count, rather than res_method->insSize, because we need to
// match the call to the signature. Also, we might be calling through an abstract method
// definition (which doesn't have register count values).
const size_t expected_args = inst->VRegA();
/* caught by static verifier */
DCHECK(is_range || expected_args <= 5);
if (expected_args > code_item_accessor_.OutsSize()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "invalid argument count (" << expected_args
<< ") exceeds outsSize ("
<< code_item_accessor_.OutsSize() << ")";
return nullptr;
}
/*
* Check the "this" argument, which must be an instance of the class that declared the method.
* For an interface class, we don't do the full interface merge (see JoinClass), so we can't do a
* rigorous check here (which is okay since we have to do it at runtime).
*/
if (method_type != METHOD_STATIC) {
const RegType& actual_arg_type = work_line_->GetInvocationThis(this, inst);
if (actual_arg_type.IsConflict()) { // GetInvocationThis failed.
CHECK(flags_.have_pending_hard_failure_);
return nullptr;
}
bool is_init = false;
if (actual_arg_type.IsUninitializedTypes()) {
if (res_method) {
if (!res_method->IsConstructor()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
return nullptr;
}
} else {
// Check whether the name of the called method is "<init>"
const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
if (strcmp(dex_file_->GetMethodName(dex_file_->GetMethodId(method_idx)), "<init>") != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "'this' arg must be initialized";
return nullptr;
}
}
is_init = true;
}
const RegType& adjusted_type = is_init
? GetRegTypeCache()->FromUninitialized(actual_arg_type)
: actual_arg_type;
if (method_type != METHOD_INTERFACE && !adjusted_type.IsZeroOrNull()) {
const RegType* res_method_class;
// Miranda methods have the declaring interface as their declaring class, not the abstract
// class. It would be wrong to use this for the type check (interface type checks are
// postponed to runtime).
if (res_method != nullptr && !res_method->IsMiranda()) {
ObjPtr<mirror::Class> klass = res_method->GetDeclaringClass();
std::string temp;
res_method_class = &FromClass(klass->GetDescriptor(&temp), klass,
klass->CannotBeAssignedFromOtherTypes());
} else {
const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
const dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
res_method_class = ®_types_.FromDescriptor(
class_loader_.Get(),
dex_file_->StringByTypeIdx(class_idx),
false);
}
if (!res_method_class->IsAssignableFrom(adjusted_type, this)) {
Fail(adjusted_type.IsUnresolvedTypes()
? VERIFY_ERROR_NO_CLASS
: VERIFY_ERROR_BAD_CLASS_SOFT)
<< "'this' argument '" << actual_arg_type << "' not instance of '"
<< *res_method_class << "'";
// Continue on soft failures. We need to find possible hard failures to avoid problems in
// the compiler.
if (flags_.have_pending_hard_failure_) {
return nullptr;
}
}
}
}
uint32_t arg[5];
if (!is_range) {
inst->GetVarArgs(arg);
}
uint32_t sig_registers = (method_type == METHOD_STATIC) ? 0 : 1;
for ( ; it->HasNext(); it->Next()) {
if (sig_registers >= expected_args) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << inst->VRegA() <<
" argument registers, method signature has " << sig_registers + 1 << " or more";
return nullptr;
}
const char* param_descriptor = it->GetDescriptor();
if (param_descriptor == nullptr) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation because of missing signature "
"component";
return nullptr;
}
const RegType& reg_type = reg_types_.FromDescriptor(class_loader_.Get(),
param_descriptor,
false);
uint32_t get_reg = is_range ? inst->VRegC() + static_cast<uint32_t>(sig_registers) :
arg[sig_registers];
if (reg_type.IsIntegralTypes()) {
const RegType& src_type = work_line_->GetRegisterType(this, get_reg);
if (!src_type.IsIntegralTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "register v" << get_reg << " has type " << src_type
<< " but expected " << reg_type;
return nullptr;
}
} else {
if (!work_line_->VerifyRegisterType(this, get_reg, reg_type)) {
// Continue on soft failures. We need to find possible hard failures to avoid problems in
// the compiler.
if (flags_.have_pending_hard_failure_) {
return nullptr;
}
} else if (reg_type.IsLongOrDoubleTypes()) {
// Check that registers are consecutive (for non-range invokes). Invokes are the only
// instructions not specifying register pairs by the first component, but require them
// nonetheless. Only check when there's an actual register in the parameters. If there's
// none, this will fail below.
if (!is_range && sig_registers + 1 < expected_args) {
uint32_t second_reg = arg[sig_registers + 1];
if (second_reg != get_reg + 1) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, long or double parameter "
"at index " << sig_registers << " is not a pair: " << get_reg << " + "
<< second_reg << ".";
return nullptr;
}
}
}
}
sig_registers += reg_type.IsLongOrDoubleTypes() ? 2 : 1;
}
if (expected_args != sig_registers) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Rejecting invocation, expected " << expected_args <<
" argument registers, method signature has " << sig_registers;
return nullptr;
}
return res_method;
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::VerifyInvocationArgsUnresolvedMethod(const Instruction* inst,
MethodType method_type,
bool is_range) {
// As the method may not have been resolved, make this static check against what we expect.
// The main reason for this code block is to fail hard when we find an illegal use, e.g.,
// wrong number of arguments or wrong primitive types, even if the method could not be resolved.
const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
DexFileParameterIterator it(*dex_file_,
dex_file_->GetProtoId(dex_file_->GetMethodId(method_idx).proto_idx_));
VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, nullptr);
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckCallSite(uint32_t call_site_idx) {
if (call_site_idx >= dex_file_->NumCallSiteIds()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Bad call site id #" << call_site_idx
<< " >= " << dex_file_->NumCallSiteIds();
return false;
}
CallSiteArrayValueIterator it(*dex_file_, dex_file_->GetCallSiteId(call_site_idx));
// Check essential arguments are provided. The dex file verifier has verified indices of the
// main values (method handle, name, method_type).
static const size_t kRequiredArguments = 3;
if (it.Size() < kRequiredArguments) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
<< " has too few arguments: "
<< it.Size() << " < " << kRequiredArguments;
return false;
}
std::pair<const EncodedArrayValueIterator::ValueType, size_t> type_and_max[kRequiredArguments] =
{ { EncodedArrayValueIterator::ValueType::kMethodHandle, dex_file_->NumMethodHandles() },
{ EncodedArrayValueIterator::ValueType::kString, dex_file_->NumStringIds() },
{ EncodedArrayValueIterator::ValueType::kMethodType, dex_file_->NumProtoIds() }
};
uint32_t index[kRequiredArguments];
// Check arguments have expected types and are within permitted ranges.
for (size_t i = 0; i < kRequiredArguments; ++i) {
if (it.GetValueType() != type_and_max[i].first) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
<< " argument " << i << " has wrong type "
<< it.GetValueType() << "!=" << type_and_max[i].first;
return false;
}
index[i] = static_cast<uint32_t>(it.GetJavaValue().i);
if (index[i] >= type_and_max[i].second) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site id #" << call_site_idx
<< " argument " << i << " bad index "
<< index[i] << " >= " << type_and_max[i].second;
return false;
}
it.Next();
}
// Check method handle kind is valid.
const dex::MethodHandleItem& mh = dex_file_->GetMethodHandle(index[0]);
if (mh.method_handle_type_ != static_cast<uint16_t>(DexFile::MethodHandleType::kInvokeStatic)) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Call site #" << call_site_idx
<< " argument 0 method handle type is not InvokeStatic: "
<< mh.method_handle_type_;
return false;
}
return true;
}
class MethodParamListDescriptorIterator {
public:
explicit MethodParamListDescriptorIterator(ArtMethod* res_method) :
res_method_(res_method), pos_(0), params_(res_method->GetParameterTypeList()),
params_size_(params_ == nullptr ? 0 : params_->Size()) {
}
bool HasNext() {
return pos_ < params_size_;
}
void Next() {
++pos_;
}
const char* GetDescriptor() REQUIRES_SHARED(Locks::mutator_lock_) {
return res_method_->GetTypeDescriptorFromTypeIdx(params_->GetTypeItem(pos_).type_idx_);
}
private:
ArtMethod* res_method_;
size_t pos_;
const dex::TypeList* params_;
const size_t params_size_;
};
template <bool kVerifierDebug>
ArtMethod* MethodVerifier<kVerifierDebug>::VerifyInvocationArgs(
const Instruction* inst, MethodType method_type, bool is_range) {
// Resolve the method. This could be an abstract or concrete method depending on what sort of call
// we're making.
const uint32_t method_idx = GetMethodIdxOfInvoke(inst);
ArtMethod* res_method = ResolveMethodAndCheckAccess(method_idx, method_type);
if (res_method == nullptr) { // error or class is unresolved
// Check what we can statically.
if (!flags_.have_pending_hard_failure_) {
VerifyInvocationArgsUnresolvedMethod(inst, method_type, is_range);
}
return nullptr;
}
// If we're using invoke-super(method), make sure that the executing method's class' superclass
// has a vtable entry for the target method. Or the target is on a interface.
if (method_type == METHOD_SUPER) {
dex::TypeIndex class_idx = dex_file_->GetMethodId(method_idx).class_idx_;
const RegType& reference_type = reg_types_.FromDescriptor(
class_loader_.Get(),
dex_file_->StringByTypeIdx(class_idx),
false);
if (reference_type.IsUnresolvedTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Unable to find referenced class from invoke-super";
return nullptr;
}
if (reference_type.GetClass()->IsInterface()) {
if (!GetDeclaringClass().HasClass()) {
Fail(VERIFY_ERROR_NO_CLASS) << "Unable to resolve the full class of 'this' used in an"
<< "interface invoke-super";
return nullptr;
} else if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this)) {
Fail(VERIFY_ERROR_CLASS_CHANGE)
<< "invoke-super in " << mirror::Class::PrettyClass(GetDeclaringClass().GetClass())
<< " in method "
<< dex_file_->PrettyMethod(dex_method_idx_) << " to method "
<< dex_file_->PrettyMethod(method_idx) << " references "
<< "non-super-interface type " << mirror::Class::PrettyClass(reference_type.GetClass());
return nullptr;
}
} else {
const RegType& super = GetDeclaringClass().GetSuperClass(®_types_);
if (super.IsUnresolvedTypes()) {
Fail(VERIFY_ERROR_NO_METHOD) << "unknown super class in invoke-super from "
<< dex_file_->PrettyMethod(dex_method_idx_)
<< " to super " << res_method->PrettyMethod();
return nullptr;
}
if (!reference_type.IsStrictlyAssignableFrom(GetDeclaringClass(), this) ||
(res_method->GetMethodIndex() >= super.GetClass()->GetVTableLength())) {
Fail(VERIFY_ERROR_NO_METHOD) << "invalid invoke-super from "
<< dex_file_->PrettyMethod(dex_method_idx_)
<< " to super " << super
<< "." << res_method->GetName()
<< res_method->GetSignature();
return nullptr;
}
}
}
if (UNLIKELY(method_type == METHOD_POLYMORPHIC)) {
// Process the signature of the calling site that is invoking the method handle.
dex::ProtoIndex proto_idx(inst->VRegH());
DexFileParameterIterator it(*dex_file_, dex_file_->GetProtoId(proto_idx));
return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
} else {
// Process the target method's signature.
MethodParamListDescriptorIterator it(res_method);
return VerifyInvocationArgsFromIterator(&it, inst, method_type, is_range, res_method);
}
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicMethod(ArtMethod* method) {
ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
const char* method_name = method->GetName();
const char* expected_return_descriptor;
ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
if (klass == GetClassRoot<mirror::MethodHandle>(class_roots)) {
expected_return_descriptor = mirror::MethodHandle::GetReturnTypeDescriptor(method_name);
} else if (klass == GetClassRoot<mirror::VarHandle>(class_roots)) {
expected_return_descriptor = mirror::VarHandle::GetReturnTypeDescriptor(method_name);
} else {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "Signature polymorphic method in unsuppported class: " << klass->PrettyDescriptor();
return false;
}
if (expected_return_descriptor == nullptr) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "Signature polymorphic method name invalid: " << method_name;
return false;
}
const dex::TypeList* types = method->GetParameterTypeList();
if (types->Size() != 1) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "Signature polymorphic method has too many arguments " << types->Size() << " != 1";
return false;
}
const dex::TypeIndex argument_type_index = types->GetTypeItem(0).type_idx_;
const char* argument_descriptor = method->GetTypeDescriptorFromTypeIdx(argument_type_index);
if (strcmp(argument_descriptor, "[Ljava/lang/Object;") != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "Signature polymorphic method has unexpected argument type: " << argument_descriptor;
return false;
}
const char* return_descriptor = method->GetReturnTypeDescriptor();
if (strcmp(return_descriptor, expected_return_descriptor) != 0) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "Signature polymorphic method has unexpected return type: " << return_descriptor
<< " != " << expected_return_descriptor;
return false;
}
return true;
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::CheckSignaturePolymorphicReceiver(const Instruction* inst) {
const RegType& this_type = work_line_->GetInvocationThis(this, inst);
if (this_type.IsZeroOrNull()) {
/* null pointer always passes (and always fails at run time) */
return true;
} else if (!this_type.IsNonZeroReferenceTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "invoke-polymorphic receiver is not a reference: "
<< this_type;
return false;
} else if (this_type.IsUninitializedReference()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "invoke-polymorphic receiver is uninitialized: "
<< this_type;
return false;
} else if (!this_type.HasClass()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "invoke-polymorphic receiver has no class: "
<< this_type;
return false;
} else {
ObjPtr<mirror::ObjectArray<mirror::Class>> class_roots = GetClassLinker()->GetClassRoots();
if (!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::MethodHandle>(class_roots)) &&
!this_type.GetClass()->IsSubClass(GetClassRoot<mirror::VarHandle>(class_roots))) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD)
<< "invoke-polymorphic receiver is not a subclass of MethodHandle or VarHandle: "
<< this_type;
return false;
}
}
return true;
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::VerifyNewArray(const Instruction* inst,
bool is_filled,
bool is_range) {
dex::TypeIndex type_idx;
if (!is_filled) {
DCHECK_EQ(inst->Opcode(), Instruction::NEW_ARRAY);
type_idx = dex::TypeIndex(inst->VRegC_22c());
} else if (!is_range) {
DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY);
type_idx = dex::TypeIndex(inst->VRegB_35c());
} else {
DCHECK_EQ(inst->Opcode(), Instruction::FILLED_NEW_ARRAY_RANGE);
type_idx = dex::TypeIndex(inst->VRegB_3rc());
}
const RegType& res_type = ResolveClass<CheckAccess::kYes>(type_idx);
if (res_type.IsConflict()) { // bad class
DCHECK_NE(failures_.size(), 0U);
} else {
// TODO: check Compiler::CanAccessTypeWithoutChecks returns false when res_type is unresolved
if (!res_type.IsArrayTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "new-array on non-array class " << res_type;
} else if (!is_filled) {
/* make sure "size" register is valid type */
work_line_->VerifyRegisterType(this, inst->VRegB_22c(), reg_types_.Integer());
/* set register type to array class */
const RegType& precise_type = reg_types_.FromUninitialized(res_type);
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_22c(), precise_type);
} else {
DCHECK(!res_type.IsUnresolvedMergedReference());
// Verify each register. If "arg_count" is bad, VerifyRegisterType() will run off the end of
// the list and fail. It's legal, if silly, for arg_count to be zero.
const RegType& expected_type = reg_types_.GetComponentType(res_type, class_loader_.Get());
uint32_t arg_count = (is_range) ? inst->VRegA_3rc() : inst->VRegA_35c();
uint32_t arg[5];
if (!is_range) {
inst->GetVarArgs(arg);
}
for (size_t ui = 0; ui < arg_count; ui++) {
uint32_t get_reg = is_range ? inst->VRegC_3rc() + ui : arg[ui];
if (!work_line_->VerifyRegisterType(this, get_reg, expected_type)) {
work_line_->SetResultRegisterType(this, reg_types_.Conflict());
return;
}
}
// filled-array result goes into "result" register
const RegType& precise_type = reg_types_.FromUninitialized(res_type);
work_line_->SetResultRegisterType(this, precise_type);
}
}
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::VerifyAGet(const Instruction* inst,
const RegType& insn_type,
bool is_primitive) {
const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
if (!index_type.IsArrayIndexTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
} else {
const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
if (array_type.IsZeroOrNull()) {
// Null array class; this code path will fail at runtime. Infer a merge-able type from the
// instruction type.
if (!is_primitive) {
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), reg_types_.Null());
} else if (insn_type.IsInteger()) {
// Pick a non-zero constant (to distinguish with null) that can fit in any primitive.
// We cannot use 'insn_type' as it could be a float array or an int array.
work_line_->SetRegisterType<LockOp::kClear>(
this, inst->VRegA_23x(), DetermineCat1Constant(1, need_precise_constants_));
} else if (insn_type.IsCategory1Types()) {
// Category 1
// The 'insn_type' is exactly the type we need.
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), insn_type);
} else {
// Category 2
work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(),
reg_types_.FromCat2ConstLo(0, false),
reg_types_.FromCat2ConstHi(0, false));
}
} else if (!array_type.IsArrayTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aget";
} else if (array_type.IsUnresolvedMergedReference()) {
// Unresolved array types must be reference array types.
if (is_primitive) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
<< " source for category 1 aget";
} else {
Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aget for " << array_type
<< " because of missing class";
// Approximate with java.lang.Object[].
work_line_->SetRegisterType<LockOp::kClear>(this,
inst->VRegA_23x(),
reg_types_.JavaLangObject(false));
}
} else {
/* verify the class */
const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
if (!component_type.IsReferenceTypes() && !is_primitive) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
<< " source for aget-object";
} else if (component_type.IsNonZeroReferenceTypes() && is_primitive) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "reference array type " << array_type
<< " source for category 1 aget";
} else if (is_primitive && !insn_type.Equals(component_type) &&
!((insn_type.IsInteger() && component_type.IsFloat()) ||
(insn_type.IsLong() && component_type.IsDouble()))) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "array type " << array_type
<< " incompatible with aget of type " << insn_type;
} else {
// Use knowledge of the field type which is stronger than the type inferred from the
// instruction, which can't differentiate object types and ints from floats, longs from
// doubles.
if (!component_type.IsLowHalf()) {
work_line_->SetRegisterType<LockOp::kClear>(this, inst->VRegA_23x(), component_type);
} else {
work_line_->SetRegisterTypeWide(this, inst->VRegA_23x(), component_type,
component_type.HighHalf(®_types_));
}
}
}
}
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::VerifyPrimitivePut(const RegType& target_type,
const RegType& insn_type,
const uint32_t vregA) {
// Primitive assignability rules are weaker than regular assignability rules.
bool instruction_compatible;
bool value_compatible;
const RegType& value_type = work_line_->GetRegisterType(this, vregA);
if (target_type.IsIntegralTypes()) {
instruction_compatible = target_type.Equals(insn_type);
value_compatible = value_type.IsIntegralTypes();
} else if (target_type.IsFloat()) {
instruction_compatible = insn_type.IsInteger(); // no put-float, so expect put-int
value_compatible = value_type.IsFloatTypes();
} else if (target_type.IsLong()) {
instruction_compatible = insn_type.IsLong();
// Additional register check: this is not checked statically (as part of VerifyInstructions),
// as target_type depends on the resolved type of the field.
if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
value_compatible = value_type.IsLongTypes() && value_type.CheckWidePair(value_type_hi);
} else {
value_compatible = false;
}
} else if (target_type.IsDouble()) {
instruction_compatible = insn_type.IsLong(); // no put-double, so expect put-long
// Additional register check: this is not checked statically (as part of VerifyInstructions),
// as target_type depends on the resolved type of the field.
if (instruction_compatible && work_line_->NumRegs() > vregA + 1) {
const RegType& value_type_hi = work_line_->GetRegisterType(this, vregA + 1);
value_compatible = value_type.IsDoubleTypes() && value_type.CheckWidePair(value_type_hi);
} else {
value_compatible = false;
}
} else {
instruction_compatible = false; // reference with primitive store
value_compatible = false; // unused
}
if (!instruction_compatible) {
// This is a global failure rather than a class change failure as the instructions and
// the descriptors for the type should have been consistent within the same file at
// compile time.
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
<< "' but expected type '" << target_type << "'";
return;
}
if (!value_compatible) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "unexpected value in v" << vregA
<< " of type " << value_type << " but expected " << target_type << " for put";
return;
}
}
template <bool kVerifierDebug>
void MethodVerifier<kVerifierDebug>::VerifyAPut(const Instruction* inst,
const RegType& insn_type,
bool is_primitive) {
const RegType& index_type = work_line_->GetRegisterType(this, inst->VRegC_23x());
if (!index_type.IsArrayIndexTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "Invalid reg type for array index (" << index_type << ")";
} else {
const RegType& array_type = work_line_->GetRegisterType(this, inst->VRegB_23x());
if (array_type.IsZeroOrNull()) {
// Null array type; this code path will fail at runtime.
// Still check that the given value matches the instruction's type.
// Note: this is, as usual, complicated by the fact the the instruction isn't fully typed
// and fits multiple register types.
const RegType* modified_reg_type = &insn_type;
if ((modified_reg_type == ®_types_.Integer()) ||
(modified_reg_type == ®_types_.LongLo())) {
// May be integer or float | long or double. Overwrite insn_type accordingly.
const RegType& value_type = work_line_->GetRegisterType(this, inst->VRegA_23x());
if (modified_reg_type == ®_types_.Integer()) {
if (&value_type == ®_types_.Float()) {
modified_reg_type = &value_type;
}
} else {
if (&value_type == ®_types_.DoubleLo()) {
modified_reg_type = &value_type;
}
}
}
work_line_->VerifyRegisterType(this, inst->VRegA_23x(), *modified_reg_type);
} else if (!array_type.IsArrayTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "not array type " << array_type << " with aput";
} else if (array_type.IsUnresolvedMergedReference()) {
// Unresolved array types must be reference array types.
if (is_primitive) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "put insn has type '" << insn_type
<< "' but unresolved type '" << array_type << "'";
} else {
Fail(VERIFY_ERROR_NO_CLASS) << "cannot verify aput for " << array_type
<< " because of missing class";
}
} else {
const RegType& component_type = reg_types_.GetComponentType(array_type, class_loader_.Get());
const uint32_t vregA = inst->VRegA_23x();
if (is_primitive) {
VerifyPrimitivePut(component_type, insn_type, vregA);
} else {
if (!component_type.IsReferenceTypes()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "primitive array type " << array_type
<< " source for aput-object";
} else {
// The instruction agrees with the type of array, confirm the value to be stored does too
// Note: we use the instruction type (rather than the component type) for aput-object as
// incompatible classes will be caught at runtime as an array store exception
work_line_->VerifyRegisterType(this, vregA, insn_type);
}
}
}
}
}
template <bool kVerifierDebug>
ArtField* MethodVerifier<kVerifierDebug>::GetStaticField(int field_idx) {
const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
// Check access to class
const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
if (klass_type.IsConflict()) { // bad class
AppendToLastFailMessage(StringPrintf(" in attempt to access static field %d (%s) in %s",
field_idx, dex_file_->GetFieldName(field_id),
dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
return nullptr;
}
if (klass_type.IsUnresolvedTypes()) {
// Accessibility checks depend on resolved fields.
DCHECK(klass_type.Equals(GetDeclaringClass()) ||
!failures_.empty() ||
IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
return nullptr; // Can't resolve Class so no more to do here, will do checking at runtime.
}
ClassLinker* class_linker = GetClassLinker();
ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
// Record result of the field resolution attempt.
VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
if (field == nullptr) {
VLOG(verifier) << "Unable to resolve static field " << field_idx << " ("
<< dex_file_->GetFieldName(field_id) << ") in "
<< dex_file_->GetFieldDeclaringClassDescriptor(field_id);
DCHECK(self_->IsExceptionPending());
self_->ClearException();
return nullptr;
} else if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
field->GetAccessFlags())) {
Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access static field " << field->PrettyField()
<< " from " << GetDeclaringClass();
return nullptr;
} else if (!field->IsStatic()) {
Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField() << " to be static";
return nullptr;
}
return field;
}
template <bool kVerifierDebug>
ArtField* MethodVerifier<kVerifierDebug>::GetInstanceField(const RegType& obj_type, int field_idx) {
if (!obj_type.IsZeroOrNull() && !obj_type.IsReferenceTypes()) {
// Trying to read a field from something that isn't a reference.
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "instance field access on object that has "
<< "non-reference type " << obj_type;
return nullptr;
}
const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
// Check access to class.
const RegType& klass_type = ResolveClass<CheckAccess::kYes>(field_id.class_idx_);
if (klass_type.IsConflict()) {
AppendToLastFailMessage(StringPrintf(" in attempt to access instance field %d (%s) in %s",
field_idx, dex_file_->GetFieldName(field_id),
dex_file_->GetFieldDeclaringClassDescriptor(field_id)));
return nullptr;
}
if (klass_type.IsUnresolvedTypes()) {
// Accessibility checks depend on resolved fields.
DCHECK(klass_type.Equals(GetDeclaringClass()) ||
!failures_.empty() ||
IsSdkVersionSetAndLessThan(api_level_, SdkVersion::kP));
return nullptr; // Can't resolve Class so no more to do here
}
ClassLinker* class_linker = GetClassLinker();
ArtField* field = class_linker->ResolveFieldJLS(field_idx, dex_cache_, class_loader_);
// Record result of the field resolution attempt.
VerifierDeps::MaybeRecordFieldResolution(*dex_file_, field_idx, field);
if (field == nullptr) {
VLOG(verifier) << "Unable to resolve instance field " << field_idx << " ("
<< dex_file_->GetFieldName(field_id) << ") in "
<< dex_file_->GetFieldDeclaringClassDescriptor(field_id);
DCHECK(self_->IsExceptionPending());
self_->ClearException();
return nullptr;
} else if (obj_type.IsZeroOrNull()) {
// Cannot infer and check type, however, access will cause null pointer exception.
// Fall through into a few last soft failure checks below.
} else {
std::string temp;
ObjPtr<mirror::Class> klass = field->GetDeclaringClass();
const RegType& field_klass =
FromClass(klass->GetDescriptor(&temp), klass, klass->CannotBeAssignedFromOtherTypes());
if (obj_type.IsUninitializedTypes()) {
// Field accesses through uninitialized references are only allowable for constructors where
// the field is declared in this class.
// Note: this IsConstructor check is technically redundant, as UninitializedThis should only
// appear in constructors.
if (!obj_type.IsUninitializedThisReference() ||
!IsConstructor() ||
!field_klass.Equals(GetDeclaringClass())) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access instance field " << field->PrettyField()
<< " of a not fully initialized object within the context"
<< " of " << dex_file_->PrettyMethod(dex_method_idx_);
return nullptr;
}
} else if (!field_klass.IsAssignableFrom(obj_type, this)) {
// Trying to access C1.field1 using reference of type C2, which is neither C1 or a sub-class
// of C1. For resolution to occur the declared class of the field must be compatible with
// obj_type, we've discovered this wasn't so, so report the field didn't exist.
VerifyError type;
bool is_aot = IsAotMode();
if (is_aot && (field_klass.IsUnresolvedTypes() || obj_type.IsUnresolvedTypes())) {
// Compiler & unresolved types involved, retry at runtime.
type = VerifyError::VERIFY_ERROR_NO_CLASS;
} else {
// Classes known (resolved; and thus assignability check is precise), or we are at runtime
// and still missing classes. This is a hard failure.
type = VerifyError::VERIFY_ERROR_BAD_CLASS_HARD;
}
Fail(type) << "cannot access instance field " << field->PrettyField()
<< " from object of type " << obj_type;
return nullptr;
}
}
// Few last soft failure checks.
if (!GetDeclaringClass().CanAccessMember(field->GetDeclaringClass(),
field->GetAccessFlags())) {
Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot access instance field " << field->PrettyField()
<< " from " << GetDeclaringClass();
return nullptr;
} else if (field->IsStatic()) {
Fail(VERIFY_ERROR_CLASS_CHANGE) << "expected field " << field->PrettyField()
<< " to not be static";
return nullptr;
}
return field;
}
template <bool kVerifierDebug>
template <FieldAccessType kAccType>
void MethodVerifier<kVerifierDebug>::VerifyISFieldAccess(const Instruction* inst,
const RegType& insn_type,
bool is_primitive,
bool is_static) {
uint32_t field_idx = GetFieldIdxOfFieldAccess(inst, is_static);
ArtField* field;
if (is_static) {
field = GetStaticField(field_idx);
} else {
const RegType& object_type = work_line_->GetRegisterType(this, inst->VRegB_22c());
// One is not allowed to access fields on uninitialized references, except to write to
// fields in the constructor (before calling another constructor).
// GetInstanceField does an assignability check which will fail for uninitialized types.
// We thus modify the type if the uninitialized reference is a "this" reference (this also
// checks at the same time that we're verifying a constructor).
bool should_adjust = (kAccType == FieldAccessType::kAccPut) &&
object_type.IsUninitializedThisReference();
const RegType& adjusted_type = should_adjust
? GetRegTypeCache()->FromUninitialized(object_type)
: object_type;
field = GetInstanceField(adjusted_type, field_idx);
if (UNLIKELY(flags_.have_pending_hard_failure_)) {
return;
}
if (should_adjust) {
if (field == nullptr) {
Fail(VERIFY_ERROR_BAD_CLASS_SOFT) << "Might be accessing a superclass instance field prior "
<< "to the superclass being initialized in "
<< dex_file_->PrettyMethod(dex_method_idx_);
} else if (field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "cannot access superclass instance field "
<< field->PrettyField() << " of a not fully initialized "
<< "object within the context of "
<< dex_file_->PrettyMethod(dex_method_idx_);
return;
}
}
}
const RegType* field_type = nullptr;
if (field != nullptr) {
if (kAccType == FieldAccessType::kAccPut) {
if (field->IsFinal() && field->GetDeclaringClass() != GetDeclaringClass().GetClass()) {
Fail(VERIFY_ERROR_ACCESS_FIELD) << "cannot modify final field " << field->PrettyField()
<< " from other class " << GetDeclaringClass();
// Keep hunting for possible hard fails.
}
}
ObjPtr<mirror::Class> field_type_class =
can_load_classes_ ? field->ResolveType() : field->LookupResolvedType();
if (field_type_class != nullptr) {
field_type = &FromClass(field->GetTypeDescriptor(),
field_type_class,
field_type_class->CannotBeAssignedFromOtherTypes());
} else {
DCHECK(!can_load_classes_ || self_->IsExceptionPending());
self_->ClearException();
}
} else if (IsSdkVersionSetAndAtLeast(api_level_, SdkVersion::kP)) {
// If we don't have the field (it seems we failed resolution) and this is a PUT, we need to
// redo verification at runtime as the field may be final, unless the field id shows it's in
// the same class.
//
// For simplicity, it is OK to not distinguish compile-time vs runtime, and post this an
// ACCESS_FIELD failure at runtime. This has the same effect as NO_FIELD - punting the class
// to the access-checks interpreter.
//
// Note: see b/34966607. This and above may be changed in the future.
if (kAccType == FieldAccessType::kAccPut) {
const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
const char* field_class_descriptor = dex_file_->GetFieldDeclaringClassDescriptor(field_id);
const RegType* field_class_type = ®_types_.FromDescriptor(class_loader_.Get(),
field_class_descriptor,
false);
if (!field_class_type->Equals(GetDeclaringClass())) {
Fail(VERIFY_ERROR_ACCESS_FIELD) << "could not check field put for final field modify of "
<< field_class_descriptor
<< "."
<< dex_file_->GetFieldName(field_id)
<< " from other class "
<< GetDeclaringClass();
}
}
}
if (field_type == nullptr) {
const dex::FieldId& field_id = dex_file_->GetFieldId(field_idx);
const char* descriptor = dex_file_->GetFieldTypeDescriptor(field_id);
field_type = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
}
DCHECK(field_type != nullptr);
const uint32_t vregA = (is_static) ? inst->VRegA_21c() : inst->VRegA_22c();
static_assert(kAccType == FieldAccessType::kAccPut || kAccType == FieldAccessType::kAccGet,
"Unexpected third access type");
if (kAccType == FieldAccessType::kAccPut) {
// sput or iput.
if (is_primitive) {
VerifyPrimitivePut(*field_type, insn_type, vregA);
} else {
if (!insn_type.IsAssignableFrom(*field_type, this)) {
// If the field type is not a reference, this is a global failure rather than
// a class change failure as the instructions and the descriptors for the type
// should have been consistent within the same file at compile time.
VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
: VERIFY_ERROR_BAD_CLASS_HARD;
Fail(error) << "expected field " << ArtField::PrettyField(field)
<< " to be compatible with type '" << insn_type
<< "' but found type '" << *field_type
<< "' in put-object";
return;
}
work_line_->VerifyRegisterType(this, vregA, *field_type);
}
} else if (kAccType == FieldAccessType::kAccGet) {
// sget or iget.
if (is_primitive) {
if (field_type->Equals(insn_type) ||
(field_type->IsFloat() && insn_type.IsInteger()) ||
(field_type->IsDouble() && insn_type.IsLong())) {
// expected that read is of the correct primitive type or that int reads are reading
// floats or long reads are reading doubles
} else {
// This is a global failure rather than a class change failure as the instructions and
// the descriptors for the type should have been consistent within the same file at
// compile time
Fail(VERIFY_ERROR_BAD_CLASS_HARD) << "expected field " << ArtField::PrettyField(field)
<< " to be of type '" << insn_type
<< "' but found type '" << *field_type << "' in get";
return;
}
} else {
if (!insn_type.IsAssignableFrom(*field_type, this)) {
// If the field type is not a reference, this is a global failure rather than
// a class change failure as the instructions and the descriptors for the type
// should have been consistent within the same file at compile time.
VerifyError error = field_type->IsReferenceTypes() ? VERIFY_ERROR_BAD_CLASS_SOFT
: VERIFY_ERROR_BAD_CLASS_HARD;
Fail(error) << "expected field " << ArtField::PrettyField(field)
<< " to be compatible with type '" << insn_type
<< "' but found type '" << *field_type
<< "' in get-object";
if (error != VERIFY_ERROR_BAD_CLASS_HARD) {
work_line_->SetRegisterType<LockOp::kClear>(this, vregA, reg_types_.Conflict());
}
return;
}
}
if (!field_type->IsLowHalf()) {
work_line_->SetRegisterType<LockOp::kClear>(this, vregA, *field_type);
} else {
work_line_->SetRegisterTypeWide(this, vregA, *field_type, field_type->HighHalf(®_types_));
}
} else {
LOG(FATAL) << "Unexpected case.";
}
}
template <bool kVerifierDebug>
bool MethodVerifier<kVerifierDebug>::UpdateRegisters(uint32_t next_insn,
RegisterLine* merge_line,
bool update_merge_line) {
bool changed = true;
RegisterLine* target_line = reg_table_.GetLine(next_insn);
if (!GetInstructionFlags(next_insn).IsVisitedOrChanged()) {
/*
* We haven't processed this instruction before, and we haven't touched the registers here, so
* there's nothing to "merge". Copy the registers over and mark it as changed. (This is the
* only way a register can transition out of "unknown", so this is not just an optimization.)
*/
target_line->CopyFromLine(merge_line);
if (GetInstructionFlags(next_insn).IsReturn()) {
// Verify that the monitor stack is empty on return.
merge_line->VerifyMonitorStackEmpty(this);
// For returns we only care about the operand to the return, all other registers are dead.
// Initialize them as conflicts so they don't add to GC and deoptimization information.
const Instruction* ret_inst = &code_item_accessor_.InstructionAt(next_insn);
AdjustReturnLine(this, ret_inst, target_line);
// Directly bail if a hard failure was found.
if (flags_.have_pending_hard_failure_) {
return false;
}
}
} else {
RegisterLineArenaUniquePtr copy;
if (kVerifierDebug) {
copy.reset(RegisterLine::Create(target_line->NumRegs(), allocator_, GetRegTypeCache()));
copy->CopyFromLine(target_line);
}
changed = target_line->MergeRegisters(this, merge_line);
if (flags_.have_pending_hard_failure_) {
return false;
}
if (kVerifierDebug && changed) {
LogVerifyInfo() << "Merging at [" << reinterpret_cast<void*>(work_insn_idx_) << "]"
<< " to [" << reinterpret_cast<void*>(next_insn) << "]: " << "\n"
<< copy->Dump(this) << " MERGE\n"
<< merge_line->Dump(this) << " ==\n"
<< target_line->Dump(this);
}
if (update_merge_line && changed) {
merge_line->CopyFromLine(target_line);
}
}
if (changed) {
GetModifiableInstructionFlags(next_insn).SetChanged();
}
return true;
}
template <bool kVerifierDebug>
const RegType& MethodVerifier<kVerifierDebug>::GetMethodReturnType() {
if (return_type_ == nullptr) {
if (method_being_verified_ != nullptr) {
ObjPtr<mirror::Class> return_type_class = can_load_classes_
? method_being_verified_->ResolveReturnType()
: method_being_verified_->LookupResolvedReturnType();
if (return_type_class != nullptr) {
return_type_ = &FromClass(method_being_verified_->GetReturnTypeDescriptor(),
return_type_class,
return_type_class->CannotBeAssignedFromOtherTypes());
} else {
DCHECK(!can_load_classes_ || self_->IsExceptionPending());
self_->ClearException();
}
}
if (return_type_ == nullptr) {
const dex::MethodId& method_id = dex_file_->GetMethodId(dex_method_idx_);
const dex::ProtoId& proto_id = dex_file_->GetMethodPrototype(method_id);
dex::TypeIndex return_type_idx = proto_id.return_type_idx_;
const char* descriptor = dex_file_->GetTypeDescriptor(dex_file_->GetTypeId(return_type_idx));
return_type_ = ®_types_.FromDescriptor(class_loader_.Get(), descriptor, false);
}
}
return *return_type_;
}
template <bool kVerifierDebug>
const RegType& MethodVerifier<kVerifierDebug>::DetermineCat1Constant(int32_t value, bool precise) {
if (precise) {
// Precise constant type.
return reg_types_.FromCat1Const(value, true);
} else {
// Imprecise constant type.
if (value < -32768) {
return reg_types_.IntConstant();
} else if (value < -128) {
return reg_types_.ShortConstant();
} else if (value < 0) {
return reg_types_.ByteConstant();
} else if (value == 0) {
return reg_types_.Zero();
} else if (value == 1) {
return reg_types_.One();
} else if (value < 128) {
return reg_types_.PosByteConstant();
} else if (value < 32768) {
return reg_types_.PosShortConstant();
} else if (value < 65536) {
return reg_types_.CharConstant();
} else {
return reg_types_.IntConstant();
}
}
}
} // namespace
} // namespace impl
MethodVerifier::MethodVerifier(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
const DexFile* dex_file,
const dex::CodeItem* code_item,
uint32_t dex_method_idx,
bool can_load_classes,
bool allow_thread_suspension,
bool allow_soft_failures,
bool aot_mode)
: self_(self),
arena_stack_(arena_pool),
allocator_(&arena_stack_),
reg_types_(class_linker, can_load_classes, allocator_, allow_thread_suspension),
reg_table_(allocator_),
work_insn_idx_(dex::kDexNoIndex),
dex_method_idx_(dex_method_idx),
dex_file_(dex_file),
code_item_accessor_(*dex_file, code_item),
// TODO: make it designated initialization when we compile as C++20.
flags_({false, false, false, false, aot_mode}),
encountered_failure_types_(0),
can_load_classes_(can_load_classes),
allow_soft_failures_(allow_soft_failures),
has_check_casts_(false),
class_linker_(class_linker),
link_(nullptr) {
self->PushVerifier(this);
}
MethodVerifier::~MethodVerifier() {
Thread::Current()->PopVerifier(this);
STLDeleteElements(&failure_messages_);
}
MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
uint32_t method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item,
ArtMethod* method,
uint32_t method_access_flags,
CompilerCallbacks* callbacks,
VerifierCallback* verifier_callback,
bool allow_soft_failures,
HardFailLogMode log_level,
bool need_precise_constants,
uint32_t api_level,
bool aot_mode,
std::string* hard_failure_msg) {
if (VLOG_IS_ON(verifier_debug)) {
return VerifyMethod<true>(self,
class_linker,
arena_pool,
method_idx,
dex_file,
dex_cache,
class_loader,
class_def,
code_item,
method,
method_access_flags,
callbacks,
verifier_callback,
allow_soft_failures,
log_level,
need_precise_constants,
api_level,
aot_mode,
hard_failure_msg);
} else {
return VerifyMethod<false>(self,
class_linker,
arena_pool,
method_idx,
dex_file,
dex_cache,
class_loader,
class_def,
code_item,
method,
method_access_flags,
callbacks,
verifier_callback,
allow_soft_failures,
log_level,
need_precise_constants,
api_level,
aot_mode,
hard_failure_msg);
}
}
template <bool kVerifierDebug>
MethodVerifier::FailureData MethodVerifier::VerifyMethod(Thread* self,
ClassLinker* class_linker,
ArenaPool* arena_pool,
uint32_t method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item,
ArtMethod* method,
uint32_t method_access_flags,
CompilerCallbacks* callbacks,
VerifierCallback* verifier_callback,
bool allow_soft_failures,
HardFailLogMode log_level,
bool need_precise_constants,
uint32_t api_level,
bool aot_mode,
std::string* hard_failure_msg) {
MethodVerifier::FailureData result;
uint64_t start_ns = kTimeVerifyMethod ? NanoTime() : 0;
impl::MethodVerifier<kVerifierDebug> verifier(self,
class_linker,
arena_pool,
dex_file,
code_item,
method_idx,
/* can_load_classes= */ true,
/* allow_thread_suspension= */ true,
allow_soft_failures,
aot_mode,
dex_cache,
class_loader,
class_def,
method,
method_access_flags,
need_precise_constants,
/* verify to dump */ false,
/* fill_register_lines= */ false,
api_level);
if (verifier.Verify()) {
// Verification completed, however failures may be pending that didn't cause the verification
// to hard fail.
CHECK(!verifier.flags_.have_pending_hard_failure_);
if (code_item != nullptr && callbacks != nullptr) {
// Let the interested party know that the method was verified.
callbacks->MethodVerified(&verifier);
}
bool set_dont_compile = false;
if (verifier.failures_.size() != 0) {
if (VLOG_IS_ON(verifier)) {
verifier.DumpFailures(VLOG_STREAM(verifier) << "Soft verification failures in "
<< dex_file->PrettyMethod(method_idx) << "\n");
}
if (kVerifierDebug) {
LOG(INFO) << verifier.info_messages_.str();
verifier.Dump(LOG_STREAM(INFO));
}
result.kind = FailureKind::kSoftFailure;
if (method != nullptr &&
!CanCompilerHandleVerificationFailure(verifier.encountered_failure_types_)) {
set_dont_compile = true;
}
}
if (method != nullptr) {
if (verifier.HasInstructionThatWillThrow()) {
set_dont_compile = true;
if (aot_mode && (callbacks != nullptr) && !callbacks->IsBootImage()) {
// When compiling apps, make HasInstructionThatWillThrow a soft error to trigger
// re-verification at runtime.
// The dead code after the throw is not verified and might be invalid. This may cause
// the JIT compiler to crash since it assumes that all the code is valid.
//
// There's a strong assumption that the entire boot image is verified and all its dex
// code is valid (even the dead and unverified one). As such this is done only for apps.
// (CompilerDriver DCHECKs in VerifyClassVisitor that methods from boot image are
// fully verified).
result.kind = FailureKind::kSoftFailure;
}
}
bool must_count_locks = false;
if ((verifier.encountered_failure_types_ & VerifyError::VERIFY_ERROR_LOCKING) != 0) {
must_count_locks = true;
}
verifier_callback->SetDontCompile(method, set_dont_compile);
verifier_callback->SetMustCountLocks(method, must_count_locks);
}
} else {
// Bad method data.
CHECK_NE(verifier.failures_.size(), 0U);
if (UNLIKELY(verifier.flags_.have_pending_experimental_failure_)) {
// Failed due to being forced into interpreter. This is ok because
// we just want to skip verification.
result.kind = FailureKind::kSoftFailure;
} else {
CHECK(verifier.flags_.have_pending_hard_failure_);
if (VLOG_IS_ON(verifier)) {
log_level = std::max(HardFailLogMode::kLogVerbose, log_level);
}
if (log_level >= HardFailLogMode::kLogVerbose) {
LogSeverity severity;
switch (log_level) {
case HardFailLogMode::kLogVerbose:
severity = LogSeverity::VERBOSE;
break;
case HardFailLogMode::kLogWarning:
severity = LogSeverity::WARNING;
break;
case HardFailLogMode::kLogInternalFatal:
severity = LogSeverity::FATAL_WITHOUT_ABORT;
break;
default:
LOG(FATAL) << "Unsupported log-level " << static_cast<uint32_t>(log_level);
UNREACHABLE();
}
verifier.DumpFailures(LOG_STREAM(severity) << "Verification error in "
<< dex_file->PrettyMethod(method_idx)
<< "\n");
}
if (hard_failure_msg != nullptr) {
CHECK(!verifier.failure_messages_.empty());
*hard_failure_msg =
verifier.failure_messages_[verifier.failure_messages_.size() - 1]->str();
}
result.kind = FailureKind::kHardFailure;
if (callbacks != nullptr) {
// Let the interested party know that we failed the class.
ClassReference ref(dex_file, dex_file->GetIndexForClassDef(class_def));
callbacks->ClassRejected(ref);
}
}
if (kVerifierDebug || VLOG_IS_ON(verifier)) {
LOG(ERROR) << verifier.info_messages_.str();
verifier.Dump(LOG_STREAM(ERROR));
}
// Under verifier-debug, dump the complete log into the error message.
if (kVerifierDebug && hard_failure_msg != nullptr) {
hard_failure_msg->append("\n");
hard_failure_msg->append(verifier.info_messages_.str());
hard_failure_msg->append("\n");
std::ostringstream oss;
verifier.Dump(oss);
hard_failure_msg->append(oss.str());
}
}
if (kTimeVerifyMethod) {
uint64_t duration_ns = NanoTime() - start_ns;
if (duration_ns > MsToNs(Runtime::Current()->GetVerifierLoggingThresholdMs())) {
double bytecodes_per_second =
verifier.code_item_accessor_.InsnsSizeInCodeUnits() / (duration_ns * 1e-9);
LOG(WARNING) << "Verification of " << dex_file->PrettyMethod(method_idx)
<< " took " << PrettyDuration(duration_ns)
<< (impl::IsLargeMethod(verifier.CodeItem()) ? " (large method)" : "")
<< " (" << StringPrintf("%.2f", bytecodes_per_second) << " bytecodes/s)"
<< " (" << verifier.allocator_.ApproximatePeakBytes()
<< "B approximate peak alloc)";
}
}
result.types = verifier.encountered_failure_types_;
return result;
}
MethodVerifier* MethodVerifier::CalculateVerificationInfo(
Thread* self,
ArtMethod* method,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader) {
std::unique_ptr<impl::MethodVerifier<false>> verifier(
new impl::MethodVerifier<false>(self,
Runtime::Current()->GetClassLinker(),
Runtime::Current()->GetArenaPool(),
method->GetDexFile(),
method->GetCodeItem(),
method->GetDexMethodIndex(),
/* can_load_classes= */ false,
/* allow_thread_suspension= */ false,
/* allow_soft_failures= */ true,
Runtime::Current()->IsAotCompiler(),
dex_cache,
class_loader,
*method->GetDeclaringClass()->GetClassDef(),
method,
method->GetAccessFlags(),
/* need_precise_constants= */ true,
/* verify_to_dump= */ false,
/* fill_register_lines= */ true,
// Just use the verifier at the current skd-version.
// This might affect what soft-verifier errors are reported.
// Callers can then filter out relevant errors if needed.
Runtime::Current()->GetTargetSdkVersion()));
verifier->Verify();
if (VLOG_IS_ON(verifier)) {
verifier->DumpFailures(VLOG_STREAM(verifier));
VLOG(verifier) << verifier->info_messages_.str();
verifier->Dump(VLOG_STREAM(verifier));
}
if (verifier->flags_.have_pending_hard_failure_) {
return nullptr;
} else {
return verifier.release();
}
}
MethodVerifier* MethodVerifier::VerifyMethodAndDump(Thread* self,
VariableIndentationOutputStream* vios,
uint32_t dex_method_idx,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item,
ArtMethod* method,
uint32_t method_access_flags,
uint32_t api_level) {
impl::MethodVerifier<false>* verifier = new impl::MethodVerifier<false>(
self,
Runtime::Current()->GetClassLinker(),
Runtime::Current()->GetArenaPool(),
dex_file,
code_item,
dex_method_idx,
/* can_load_classes= */ true,
/* allow_thread_suspension= */ true,
/* allow_soft_failures= */ true,
Runtime::Current()->IsAotCompiler(),
dex_cache,
class_loader,
class_def,
method,
method_access_flags,
/* need_precise_constants= */ true,
/* verify_to_dump= */ true,
/* fill_register_lines= */ false,
api_level);
verifier->Verify();
verifier->DumpFailures(vios->Stream());
vios->Stream() << verifier->info_messages_.str();
// Only dump and return if no hard failures. Otherwise the verifier may be not fully initialized
// and querying any info is dangerous/can abort.
if (verifier->flags_.have_pending_hard_failure_) {
delete verifier;
return nullptr;
} else {
verifier->Dump(vios);
return verifier;
}
}
void MethodVerifier::FindLocksAtDexPc(
ArtMethod* m,
uint32_t dex_pc,
std::vector<MethodVerifier::DexLockInfo>* monitor_enter_dex_pcs,
uint32_t api_level) {
StackHandleScope<2> hs(Thread::Current());
Handle<mirror::DexCache> dex_cache(hs.NewHandle(m->GetDexCache()));
Handle<mirror::ClassLoader> class_loader(hs.NewHandle(m->GetClassLoader()));
impl::MethodVerifier<false> verifier(hs.Self(),
Runtime::Current()->GetClassLinker(),
Runtime::Current()->GetArenaPool(),
m->GetDexFile(),
m->GetCodeItem(),
m->GetDexMethodIndex(),
/* can_load_classes= */ false,
/* allow_thread_suspension= */ false,
/* allow_soft_failures= */ true,
Runtime::Current()->IsAotCompiler(),
dex_cache,
class_loader,
m->GetClassDef(),
m,
m->GetAccessFlags(),
/* need_precise_constants= */ false,
/* verify_to_dump= */ false,
/* fill_register_lines= */ false,
api_level);
verifier.interesting_dex_pc_ = dex_pc;
verifier.monitor_enter_dex_pcs_ = monitor_enter_dex_pcs;
verifier.FindLocksAtDexPc();
}
MethodVerifier* MethodVerifier::CreateVerifier(Thread* self,
const DexFile* dex_file,
Handle<mirror::DexCache> dex_cache,
Handle<mirror::ClassLoader> class_loader,
const dex::ClassDef& class_def,
const dex::CodeItem* code_item,
uint32_t method_idx,
ArtMethod* method,
uint32_t access_flags,
bool can_load_classes,
bool allow_soft_failures,
bool need_precise_constants,
bool verify_to_dump,
bool allow_thread_suspension,
uint32_t api_level) {
return new impl::MethodVerifier<false>(self,
Runtime::Current()->GetClassLinker(),
Runtime::Current()->GetArenaPool(),
dex_file,
code_item,
method_idx,
can_load_classes,
allow_thread_suspension,
allow_soft_failures,
Runtime::Current()->IsAotCompiler(),
dex_cache,
class_loader,
class_def,
method,
access_flags,
need_precise_constants,
verify_to_dump,
/* fill_register_lines= */ false,
api_level);
}
void MethodVerifier::Init(ClassLinker* class_linker) {
art::verifier::RegTypeCache::Init(class_linker);
}
void MethodVerifier::Shutdown() {
verifier::RegTypeCache::ShutDown();
}
void MethodVerifier::VisitStaticRoots(RootVisitor* visitor) {
RegTypeCache::VisitStaticRoots(visitor);
}
void MethodVerifier::VisitRoots(RootVisitor* visitor, const RootInfo& root_info) {
reg_types_.VisitRoots(visitor, root_info);
}
std::ostream& MethodVerifier::Fail(VerifyError error, bool pending_exc) {
// Mark the error type as encountered.
encountered_failure_types_ |= static_cast<uint32_t>(error);
if (pending_exc) {
switch (error) {
case VERIFY_ERROR_NO_CLASS:
case VERIFY_ERROR_NO_FIELD:
case VERIFY_ERROR_NO_METHOD:
case VERIFY_ERROR_ACCESS_CLASS:
case VERIFY_ERROR_ACCESS_FIELD:
case VERIFY_ERROR_ACCESS_METHOD:
case VERIFY_ERROR_INSTANTIATION:
case VERIFY_ERROR_CLASS_CHANGE:
case VERIFY_ERROR_FORCE_INTERPRETER:
case VERIFY_ERROR_LOCKING:
if (IsAotMode() || !can_load_classes_) {
// If we're optimistically running verification at compile time, turn NO_xxx, ACCESS_xxx,
// class change and instantiation errors into soft verification errors so that we
// re-verify at runtime. We may fail to find or to agree on access because of not yet
// available class loaders, or class loaders that will differ at runtime. In these cases,
// we don't want to affect the soundness of the code being compiled. Instead, the
// generated code runs "slow paths" that dynamically perform the verification and cause
// the behavior to be that akin to an interpreter.
error = VERIFY_ERROR_BAD_CLASS_SOFT;
} else {
// If we fail again at runtime, mark that this instruction would throw and force this
// method to be executed using the interpreter with checks.
flags_.have_pending_runtime_throw_failure_ = true;
}
// How to handle runtime failures for instructions that are not flagged kThrow.
//
// The verifier may fail before we touch any instruction, for the signature of a method. So
// add a check.
if (work_insn_idx_ < dex::kDexNoIndex) {
const Instruction& inst = code_item_accessor_.InstructionAt(work_insn_idx_);
Instruction::Code opcode = inst.Opcode();
if ((Instruction::FlagsOf(opcode) & Instruction::kThrow) == 0 &&
!impl::IsCompatThrow(opcode) &&
GetInstructionFlags(work_insn_idx_).IsInTry()) {
if (Runtime::Current()->IsVerifierMissingKThrowFatal()) {
LOG(FATAL) << "Unexpected throw: " << std::hex << work_insn_idx_ << " " << opcode;
UNREACHABLE();
}
// We need to save the work_line if the instruction wasn't throwing before. Otherwise
// we'll try to merge garbage.
// Note: this assumes that Fail is called before we do any work_line modifications.
saved_line_->CopyFromLine(work_line_.get());
}
}
break;
// Indication that verification should be retried at runtime.
case VERIFY_ERROR_BAD_CLASS_SOFT:
if (!allow_soft_failures_) {
flags_.have_pending_hard_failure_ = true;
}
break;
// Hard verification failures at compile time will still fail at runtime, so the class is
// marked as rejected to prevent it from being compiled.
case VERIFY_ERROR_BAD_CLASS_HARD: {
flags_.have_pending_hard_failure_ = true;
break;
}
case VERIFY_ERROR_SKIP_COMPILER:
// Nothing to do, just remember the failure type.
break;
}
} else if (kIsDebugBuild) {
CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_SOFT);
CHECK_NE(error, VERIFY_ERROR_BAD_CLASS_HARD);
}
failures_.push_back(error);
std::string location(StringPrintf("%s: [0x%X] ", dex_file_->PrettyMethod(dex_method_idx_).c_str(),
work_insn_idx_));
std::ostringstream* failure_message = new std::ostringstream(location, std::ostringstream::ate);
failure_messages_.push_back(failure_message);
return *failure_message;
}
ScopedNewLine MethodVerifier::LogVerifyInfo() {
ScopedNewLine ret{info_messages_};
ret << "VFY: " << dex_file_->PrettyMethod(dex_method_idx_)
<< '[' << reinterpret_cast<void*>(work_insn_idx_) << "] : ";
return ret;
}
static FailureKind FailureKindMax(FailureKind fk1, FailureKind fk2) {
static_assert(FailureKind::kNoFailure < FailureKind::kSoftFailure
&& FailureKind::kSoftFailure < FailureKind::kHardFailure,
"Unexpected FailureKind order");
return std::max(fk1, fk2);
}
void MethodVerifier::FailureData::Merge(const MethodVerifier::FailureData& fd) {
kind = FailureKindMax(kind, fd.kind);
types |= fd.types;
}
} // namespace verifier
} // namespace art
|