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
|
/*
* 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 "dex_file_verifier.h"
#include <algorithm>
#include <bitset>
#include <limits>
#include <memory>
#include "android-base/logging.h"
#include "android-base/macros.h"
#include "android-base/stringprintf.h"
#include "base/hash_map.h"
#include "base/leb128.h"
#include "base/safe_map.h"
#include "class_accessor-inl.h"
#include "code_item_accessors-inl.h"
#include "descriptors_names.h"
#include "dex_file-inl.h"
#include "dex_file_types.h"
#include "modifiers.h"
#include "utf-inl.h"
namespace art {
namespace dex {
using android::base::StringAppendV;
using android::base::StringPrintf;
namespace {
constexpr uint32_t kTypeIdLimit = std::numeric_limits<uint16_t>::max();
constexpr bool IsValidOrNoTypeId(uint16_t low, uint16_t high) {
return (high == 0) || ((high == 0xffffU) && (low == 0xffffU));
}
constexpr bool IsValidTypeId(uint16_t low ATTRIBUTE_UNUSED, uint16_t high) {
return (high == 0);
}
constexpr uint32_t MapTypeToBitMask(DexFile::MapItemType map_item_type) {
switch (map_item_type) {
case DexFile::kDexTypeHeaderItem: return 1 << 0;
case DexFile::kDexTypeStringIdItem: return 1 << 1;
case DexFile::kDexTypeTypeIdItem: return 1 << 2;
case DexFile::kDexTypeProtoIdItem: return 1 << 3;
case DexFile::kDexTypeFieldIdItem: return 1 << 4;
case DexFile::kDexTypeMethodIdItem: return 1 << 5;
case DexFile::kDexTypeClassDefItem: return 1 << 6;
case DexFile::kDexTypeCallSiteIdItem: return 1 << 7;
case DexFile::kDexTypeMethodHandleItem: return 1 << 8;
case DexFile::kDexTypeMapList: return 1 << 9;
case DexFile::kDexTypeTypeList: return 1 << 10;
case DexFile::kDexTypeAnnotationSetRefList: return 1 << 11;
case DexFile::kDexTypeAnnotationSetItem: return 1 << 12;
case DexFile::kDexTypeClassDataItem: return 1 << 13;
case DexFile::kDexTypeCodeItem: return 1 << 14;
case DexFile::kDexTypeStringDataItem: return 1 << 15;
case DexFile::kDexTypeDebugInfoItem: return 1 << 16;
case DexFile::kDexTypeAnnotationItem: return 1 << 17;
case DexFile::kDexTypeEncodedArrayItem: return 1 << 18;
case DexFile::kDexTypeAnnotationsDirectoryItem: return 1 << 19;
case DexFile::kDexTypeHiddenapiClassData: return 1 << 20;
}
return 0;
}
constexpr bool IsDataSectionType(DexFile::MapItemType map_item_type) {
switch (map_item_type) {
case DexFile::kDexTypeHeaderItem:
case DexFile::kDexTypeStringIdItem:
case DexFile::kDexTypeTypeIdItem:
case DexFile::kDexTypeProtoIdItem:
case DexFile::kDexTypeFieldIdItem:
case DexFile::kDexTypeMethodIdItem:
case DexFile::kDexTypeClassDefItem:
return false;
case DexFile::kDexTypeCallSiteIdItem:
case DexFile::kDexTypeMethodHandleItem:
case DexFile::kDexTypeMapList:
case DexFile::kDexTypeTypeList:
case DexFile::kDexTypeAnnotationSetRefList:
case DexFile::kDexTypeAnnotationSetItem:
case DexFile::kDexTypeClassDataItem:
case DexFile::kDexTypeCodeItem:
case DexFile::kDexTypeStringDataItem:
case DexFile::kDexTypeDebugInfoItem:
case DexFile::kDexTypeAnnotationItem:
case DexFile::kDexTypeEncodedArrayItem:
case DexFile::kDexTypeAnnotationsDirectoryItem:
case DexFile::kDexTypeHiddenapiClassData:
return true;
}
return true;
}
// Fields and methods may have only one of public/protected/private.
ALWAYS_INLINE
constexpr bool CheckAtMostOneOfPublicProtectedPrivate(uint32_t flags) {
// Semantically we want 'return POPCOUNT(flags & kAcc) <= 1;'.
static_assert(IsPowerOfTwo(0), "0 not marked as power of two");
static_assert(IsPowerOfTwo(kAccPublic), "kAccPublic not marked as power of two");
static_assert(IsPowerOfTwo(kAccProtected), "kAccProtected not marked as power of two");
static_assert(IsPowerOfTwo(kAccPrivate), "kAccPrivate not marked as power of two");
return IsPowerOfTwo(flags & (kAccPublic | kAccProtected | kAccPrivate));
}
// Helper functions to retrieve names from the dex file. We do not want to rely on DexFile
// functionality, as we're still verifying the dex file. begin and header correspond to the
// underscored variants in the DexFileVerifier.
std::string GetString(const uint8_t* const begin,
const DexFile::Header* const header,
dex::StringIndex string_idx) {
// All sources of the `string_idx` have already been checked in CheckIntraSection().
DCHECK_LT(string_idx.index_, header->string_ids_size_);
const dex::StringId* string_id =
reinterpret_cast<const dex::StringId*>(begin + header->string_ids_off_) + string_idx.index_;
// The string offset has been checked at the start of `CheckInterSection()`
// to point to a string data item checked by `CheckIntraSection()`.
const uint8_t* ptr = begin + string_id->string_data_off_;
DecodeUnsignedLeb128(&ptr); // Ignore the result.
return reinterpret_cast<const char*>(ptr);
}
std::string GetClass(const uint8_t* const begin,
const DexFile::Header* const header,
dex::TypeIndex class_idx) {
// All sources of `class_idx` have already been checked in CheckIntraSection().
CHECK_LT(class_idx.index_, header->type_ids_size_);
const dex::TypeId* type_id =
reinterpret_cast<const dex::TypeId*>(begin + header->type_ids_off_) + class_idx.index_;
// The `type_id->descriptor_idx_` has already been checked in CheckIntraTypeIdItem().
// However, it may not have been checked to be a valid descriptor, so return the raw
// string without converting with `PrettyDescriptor()`.
return GetString(begin, header, type_id->descriptor_idx_);
}
std::string GetFieldDescription(const uint8_t* const begin,
const DexFile::Header* const header,
uint32_t idx) {
// The `idx` has already been checked in `DexFileVerifier::CheckIntraClassDataItemFields()`.
CHECK_LT(idx, header->field_ids_size_);
const dex::FieldId* field_id =
reinterpret_cast<const dex::FieldId*>(begin + header->field_ids_off_) + idx;
// Indexes in `*field_id` have already been checked in CheckIntraFieldIdItem().
std::string class_name = GetClass(begin, header, field_id->class_idx_);
std::string field_name = GetString(begin, header, field_id->name_idx_);
return class_name + "." + field_name;
}
std::string GetMethodDescription(const uint8_t* const begin,
const DexFile::Header* const header,
uint32_t idx) {
// The `idx` has already been checked in `DexFileVerifier::CheckIntraClassDataItemMethods()`.
CHECK_LT(idx, header->method_ids_size_);
const dex::MethodId* method_id =
reinterpret_cast<const dex::MethodId*>(begin + header->method_ids_off_) + idx;
// Indexes in `*method_id` have already been checked in CheckIntraMethodIdItem().
std::string class_name = GetClass(begin, header, method_id->class_idx_);
std::string method_name = GetString(begin, header, method_id->name_idx_);
return class_name + "." + method_name;
}
} // namespace
// Note: the anonymous namespace would be nice, but we need friend access into accessors.
class DexFileVerifier {
public:
DexFileVerifier(const DexFile* dex_file,
const uint8_t* begin,
size_t size,
const char* location,
bool verify_checksum)
: dex_file_(dex_file),
begin_(begin),
size_(size),
location_(location),
verify_checksum_(verify_checksum),
header_(&dex_file->GetHeader()),
ptr_(nullptr),
previous_item_(nullptr),
init_indices_{std::numeric_limits<size_t>::max(),
std::numeric_limits<size_t>::max(),
std::numeric_limits<size_t>::max(),
std::numeric_limits<size_t>::max()} {
}
bool Verify();
const std::string& FailureReason() const {
return failure_reason_;
}
private:
bool CheckShortyDescriptorMatch(char shorty_char, const char* descriptor, bool is_return_type);
bool CheckListSize(const void* start, size_t count, size_t element_size, const char* label);
// Check a list. The head is assumed to be at *ptr, and elements to be of size element_size. If
// successful, the ptr will be moved forward the amount covered by the list.
bool CheckList(size_t element_size, const char* label, const uint8_t* *ptr);
// Checks whether the offset is zero (when size is zero) or that the offset falls within the area
// claimed by the file.
bool CheckValidOffsetAndSize(uint32_t offset, uint32_t size, size_t alignment, const char* label);
// Checks whether the size is less than the limit.
ALWAYS_INLINE bool CheckSizeLimit(uint32_t size, uint32_t limit, const char* label) {
if (size > limit) {
ErrorStringPrintf("Size(%u) should not exceed limit(%u) for %s.", size, limit, label);
return false;
}
return true;
}
ALWAYS_INLINE bool CheckIndex(uint32_t field, uint32_t limit, const char* label) {
if (UNLIKELY(field >= limit)) {
ErrorStringPrintf("Bad index for %s: %x >= %x", label, field, limit);
return false;
}
return true;
}
bool CheckHeader();
bool CheckMap();
uint32_t ReadUnsignedLittleEndian(uint32_t size) {
uint32_t result = 0;
if (LIKELY(CheckListSize(ptr_, size, sizeof(uint8_t), "encoded_value"))) {
for (uint32_t i = 0; i < size; i++) {
result |= ((uint32_t) *(ptr_++)) << (i * 8);
}
}
return result;
}
bool CheckAndGetHandlerOffsets(const dex::CodeItem* code_item,
uint32_t* handler_offsets, uint32_t handlers_size);
bool CheckClassDataItemField(uint32_t idx,
uint32_t access_flags,
uint32_t class_access_flags,
dex::TypeIndex class_type_index);
bool CheckClassDataItemMethod(uint32_t idx,
uint32_t access_flags,
uint32_t class_access_flags,
dex::TypeIndex class_type_index,
uint32_t code_offset,
bool expect_direct);
ALWAYS_INLINE
bool CheckOrder(const char* type_descr, uint32_t curr_index, uint32_t prev_index) {
if (UNLIKELY(curr_index < prev_index)) {
ErrorStringPrintf("out-of-order %s indexes %" PRIu32 " and %" PRIu32,
type_descr,
prev_index,
curr_index);
return false;
}
return true;
}
bool CheckStaticFieldTypes(const dex::ClassDef& class_def);
bool CheckPadding(size_t offset, uint32_t aligned_offset, DexFile::MapItemType type);
bool CheckEncodedValue();
bool CheckEncodedArray();
bool CheckEncodedAnnotation();
bool CheckIntraTypeIdItem();
bool CheckIntraProtoIdItem();
bool CheckIntraFieldIdItem();
bool CheckIntraMethodIdItem();
bool CheckIntraClassDefItem(uint32_t class_def_index);
bool CheckIntraMethodHandleItem();
bool CheckIntraTypeList();
// Check all fields of the given type, reading `encoded_field` entries from `ptr_`.
template <bool kStatic>
bool CheckIntraClassDataItemFields(size_t count);
// Check direct or virtual methods, reading `encoded_method` entries from `ptr_`.
// Check virtual methods against duplicates with direct methods.
bool CheckIntraClassDataItemMethods(size_t num_methods,
ClassAccessor::Method* direct_methods,
size_t num_direct_methods);
bool CheckIntraClassDataItem();
bool CheckIntraCodeItem();
bool CheckIntraStringDataItem();
bool CheckIntraDebugInfoItem();
bool CheckIntraAnnotationItem();
bool CheckIntraAnnotationsDirectoryItem();
bool CheckIntraHiddenapiClassData();
template <DexFile::MapItemType kType>
bool CheckIntraSectionIterate(size_t offset, uint32_t count);
template <DexFile::MapItemType kType>
bool CheckIntraIdSection(size_t offset, uint32_t count);
template <DexFile::MapItemType kType>
bool CheckIntraDataSection(size_t offset, uint32_t count);
bool CheckIntraSection();
bool CheckOffsetToTypeMap(size_t offset, uint16_t type);
// Returns kDexNoIndex if there are no fields/methods, otherwise a 16-bit type index.
uint32_t FindFirstClassDataDefiner(const ClassAccessor& accessor);
uint32_t FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr);
bool CheckInterStringIdItem();
bool CheckInterTypeIdItem();
bool CheckInterProtoIdItem();
bool CheckInterFieldIdItem();
bool CheckInterMethodIdItem();
bool CheckInterClassDefItem();
bool CheckInterCallSiteIdItem();
bool CheckInterAnnotationSetRefList();
bool CheckInterAnnotationSetItem();
bool CheckInterClassDataItem();
bool CheckInterAnnotationsDirectoryItem();
bool CheckInterSectionIterate(size_t offset, uint32_t count, DexFile::MapItemType type);
bool CheckInterSection();
void ErrorStringPrintf(const char* fmt, ...)
__attribute__((__format__(__printf__, 2, 3))) COLD_ATTR {
va_list ap;
va_start(ap, fmt);
DCHECK(failure_reason_.empty()) << failure_reason_;
failure_reason_ = StringPrintf("Failure to verify dex file '%s': ", location_);
StringAppendV(&failure_reason_, fmt, ap);
va_end(ap);
}
bool FailureReasonIsSet() const { return failure_reason_.size() != 0; }
// Check validity of the given access flags, interpreted for a field in the context of a class
// with the given second access flags.
bool CheckFieldAccessFlags(uint32_t idx,
uint32_t field_access_flags,
uint32_t class_access_flags,
std::string* error_message);
// Check validity of the given method and access flags, in the context of a class with the given
// second access flags.
bool CheckMethodAccessFlags(uint32_t method_index,
uint32_t method_access_flags,
uint32_t class_access_flags,
uint32_t constructor_flags_by_name,
bool has_code,
bool expect_direct,
std::string* error_message);
// Check validity of given method if it's a constructor or class initializer.
bool CheckConstructorProperties(uint32_t method_index, uint32_t constructor_flags);
void FindStringRangesForMethodNames();
template <typename ExtraCheckFn>
bool VerifyTypeDescriptor(dex::TypeIndex idx, const char* error_msg, ExtraCheckFn extra_check);
const DexFile* const dex_file_;
const uint8_t* const begin_;
const size_t size_;
const char* const location_;
const bool verify_checksum_;
const DexFile::Header* const header_;
struct OffsetTypeMapEmptyFn {
// Make a hash map slot empty by making the offset 0. Offset 0 is a valid dex file offset that
// is in the offset of the dex file header. However, we only store data section items in the
// map, and these are after the header.
void MakeEmpty(std::pair<uint32_t, uint16_t>& pair) const {
pair.first = 0u;
}
// Check if a hash map slot is empty.
bool IsEmpty(const std::pair<uint32_t, uint16_t>& pair) const {
return pair.first == 0;
}
};
struct OffsetTypeMapHashCompareFn {
// Hash function for offset.
size_t operator()(const uint32_t key) const {
return key;
}
// std::equal function for offset.
bool operator()(const uint32_t a, const uint32_t b) const {
return a == b;
}
};
// Map from offset to dex file type, HashMap for performance reasons.
HashMap<uint32_t,
uint16_t,
OffsetTypeMapEmptyFn,
OffsetTypeMapHashCompareFn,
OffsetTypeMapHashCompareFn> offset_to_type_map_;
const uint8_t* ptr_;
const void* previous_item_;
std::string failure_reason_;
// Cached string indices for "interesting" entries wrt/ method names. Will be populated by
// FindStringRangesForMethodNames (which is automatically called before verifying the
// classdataitem section).
//
// Strings starting with '<' are in the range
// [angle_bracket_start_index_,angle_bracket_end_index_).
// angle_init_angle_index_ and angle_clinit_angle_index_ denote the indices of "<init>" and
// "<clinit>", respectively. If any value is not found, the corresponding index will be larger
// than any valid string index for this dex file.
struct {
size_t angle_bracket_start_index;
size_t angle_bracket_end_index;
size_t angle_init_angle_index;
size_t angle_clinit_angle_index;
} init_indices_;
// A bitvector for verified type descriptors. Each bit corresponds to a type index. A set
// bit denotes that the descriptor has been verified wrt/ IsValidDescriptor.
std::vector<char> verified_type_descriptors_;
// Set of type ids for which there are ClassDef elements in the dex file. Using a bitset
// avoids all allocations. The bitset should be implemented as 8K of storage, which is
// tight enough for all callers.
std::bitset<kTypeIdLimit + 1> defined_classes_;
// Class definition indexes, valid only if corresponding `defined_classes_[.]` is true.
std::vector<uint16_t> defined_class_indexes_;
};
template <typename ExtraCheckFn>
bool DexFileVerifier::VerifyTypeDescriptor(dex::TypeIndex idx,
const char* error_msg,
ExtraCheckFn extra_check) {
// All sources of the `idx` have already been checked in CheckIntraSection().
DCHECK_LT(idx.index_, header_->type_ids_size_);
auto err_fn = [&](const char* descriptor) {
ErrorStringPrintf("%s: '%s'", error_msg, descriptor);
};
char cached_char = verified_type_descriptors_[idx.index_];
if (cached_char != 0) {
if (!extra_check(cached_char)) {
const char* descriptor = dex_file_->StringByTypeIdx(idx);
err_fn(descriptor);
return false;
}
return true;
}
const char* descriptor = dex_file_->StringByTypeIdx(idx);
if (UNLIKELY(!IsValidDescriptor(descriptor))) {
err_fn(descriptor);
return false;
}
verified_type_descriptors_[idx.index_] = descriptor[0];
if (!extra_check(descriptor[0])) {
err_fn(descriptor);
return false;
}
return true;
}
bool DexFileVerifier::CheckShortyDescriptorMatch(char shorty_char, const char* descriptor,
bool is_return_type) {
switch (shorty_char) {
case 'V':
if (UNLIKELY(!is_return_type)) {
ErrorStringPrintf("Invalid use of void");
return false;
}
FALLTHROUGH_INTENDED;
case 'B':
case 'C':
case 'D':
case 'F':
case 'I':
case 'J':
case 'S':
case 'Z':
if (UNLIKELY((descriptor[0] != shorty_char) || (descriptor[1] != '\0'))) {
ErrorStringPrintf("Shorty vs. primitive type mismatch: '%c', '%s'",
shorty_char, descriptor);
return false;
}
break;
case 'L':
if (UNLIKELY((descriptor[0] != 'L') && (descriptor[0] != '['))) {
ErrorStringPrintf("Shorty vs. type mismatch: '%c', '%s'", shorty_char, descriptor);
return false;
}
break;
default:
ErrorStringPrintf("Bad shorty character: '%c'", shorty_char);
return false;
}
return true;
}
bool DexFileVerifier::CheckListSize(const void* start, size_t count, size_t elem_size,
const char* label) {
// Check that element size is not 0.
DCHECK_NE(elem_size, 0U);
size_t offset = reinterpret_cast<const uint8_t*>(start) - begin_;
if (UNLIKELY(offset > size_)) {
ErrorStringPrintf("Offset beyond end of file for %s: %zx to %zx", label, offset, size_);
return false;
}
// Calculate the number of elements that fit until the end of file,
// rather than calculating the end of the range as that could overflow.
size_t max_elements = (size_ - offset) / elem_size;
if (UNLIKELY(max_elements < count)) {
ErrorStringPrintf(
"List too large for %s: %zx+%zu*%zu > %zx", label, offset, count, elem_size, size_);
return false;
}
return true;
}
bool DexFileVerifier::CheckList(size_t element_size, const char* label, const uint8_t* *ptr) {
// Check that the list is available. The first 4B are the count.
if (!CheckListSize(*ptr, 1, 4U, label)) {
return false;
}
uint32_t count = *reinterpret_cast<const uint32_t*>(*ptr);
if (count > 0) {
if (!CheckListSize(*ptr + 4, count, element_size, label)) {
return false;
}
}
*ptr += 4 + count * element_size;
return true;
}
bool DexFileVerifier::CheckValidOffsetAndSize(uint32_t offset,
uint32_t size,
size_t alignment,
const char* label) {
if (size == 0) {
if (offset != 0) {
ErrorStringPrintf("Offset(%d) should be zero when size is zero for %s.", offset, label);
return false;
}
}
if (size_ <= offset) {
ErrorStringPrintf("Offset(%d) should be within file size(%zu) for %s.", offset, size_, label);
return false;
}
if (alignment != 0 && !IsAlignedParam(offset, alignment)) {
ErrorStringPrintf("Offset(%d) should be aligned by %zu for %s.", offset, alignment, label);
return false;
}
return true;
}
bool DexFileVerifier::CheckHeader() {
// Check file size from the header.
uint32_t expected_size = header_->file_size_;
if (size_ != expected_size) {
ErrorStringPrintf("Bad file size (%zd, expected %u)", size_, expected_size);
return false;
}
uint32_t adler_checksum = dex_file_->CalculateChecksum();
// Compute and verify the checksum in the header.
if (adler_checksum != header_->checksum_) {
if (verify_checksum_) {
ErrorStringPrintf("Bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_);
return false;
} else {
LOG(WARNING) << StringPrintf(
"Ignoring bad checksum (%08x, expected %08x)", adler_checksum, header_->checksum_);
}
}
// Check the contents of the header.
if (header_->endian_tag_ != DexFile::kDexEndianConstant) {
ErrorStringPrintf("Unexpected endian_tag: %x", header_->endian_tag_);
return false;
}
const uint32_t expected_header_size = dex_file_->IsCompactDexFile()
? sizeof(CompactDexFile::Header)
: sizeof(StandardDexFile::Header);
if (header_->header_size_ != expected_header_size) {
ErrorStringPrintf("Bad header size: %ud expected %ud",
header_->header_size_,
expected_header_size);
return false;
}
// Check that all offsets are inside the file.
bool result =
CheckValidOffsetAndSize(header_->link_off_,
header_->link_size_,
/* alignment= */ 0,
"link") &&
CheckValidOffsetAndSize(header_->map_off_,
header_->map_off_,
/* alignment= */ 4,
"map") &&
CheckValidOffsetAndSize(header_->string_ids_off_,
header_->string_ids_size_,
/* alignment= */ 4,
"string-ids") &&
CheckValidOffsetAndSize(header_->type_ids_off_,
header_->type_ids_size_,
/* alignment= */ 4,
"type-ids") &&
CheckSizeLimit(header_->type_ids_size_, DexFile::kDexNoIndex16, "type-ids") &&
CheckValidOffsetAndSize(header_->proto_ids_off_,
header_->proto_ids_size_,
/* alignment= */ 4,
"proto-ids") &&
CheckSizeLimit(header_->proto_ids_size_, DexFile::kDexNoIndex16, "proto-ids") &&
CheckValidOffsetAndSize(header_->field_ids_off_,
header_->field_ids_size_,
/* alignment= */ 4,
"field-ids") &&
CheckValidOffsetAndSize(header_->method_ids_off_,
header_->method_ids_size_,
/* alignment= */ 4,
"method-ids") &&
CheckValidOffsetAndSize(header_->class_defs_off_,
header_->class_defs_size_,
/* alignment= */ 4,
"class-defs") &&
CheckValidOffsetAndSize(header_->data_off_,
header_->data_size_,
// Unaligned, spec doesn't talk about it, even though size
// is supposed to be a multiple of 4.
/* alignment= */ 0,
"data");
return result;
}
bool DexFileVerifier::CheckMap() {
const dex::MapList* map = reinterpret_cast<const dex::MapList*>(begin_ + header_->map_off_);
// Check that map list content is available.
if (!CheckListSize(map, 1, sizeof(dex::MapList), "maplist content")) {
return false;
}
const dex::MapItem* item = map->list_;
uint32_t count = map->size_;
uint32_t last_offset = 0;
uint32_t last_type = 0;
uint32_t data_item_count = 0;
uint32_t data_items_left = header_->data_size_;
uint32_t used_bits = 0;
// Sanity check the size of the map list.
if (!CheckListSize(item, count, sizeof(dex::MapItem), "map size")) {
return false;
}
// Check the items listed in the map.
for (uint32_t i = 0; i < count; i++) {
if (UNLIKELY(last_offset >= item->offset_ && i != 0)) {
ErrorStringPrintf("Out of order map item: %x then %x for type %x last type was %x",
last_offset,
item->offset_,
static_cast<uint32_t>(item->type_),
last_type);
return false;
}
if (UNLIKELY(item->offset_ >= header_->file_size_)) {
ErrorStringPrintf("Map item after end of file: %x, size %x",
item->offset_, header_->file_size_);
return false;
}
DexFile::MapItemType item_type = static_cast<DexFile::MapItemType>(item->type_);
if (IsDataSectionType(item_type)) {
uint32_t icount = item->size_;
if (UNLIKELY(icount > data_items_left)) {
ErrorStringPrintf("Too many items in data section: %ud item_type %zx",
data_item_count + icount,
static_cast<size_t>(item_type));
return false;
}
data_items_left -= icount;
data_item_count += icount;
}
uint32_t bit = MapTypeToBitMask(item_type);
if (UNLIKELY(bit == 0)) {
ErrorStringPrintf("Unknown map section type %x", item->type_);
return false;
}
if (UNLIKELY((used_bits & bit) != 0)) {
ErrorStringPrintf("Duplicate map section of type %x", item->type_);
return false;
}
used_bits |= bit;
last_offset = item->offset_;
last_type = item->type_;
item++;
}
// Check for missing sections in the map.
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeHeaderItem)) == 0)) {
ErrorStringPrintf("Map is missing header entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMapList)) == 0)) {
ErrorStringPrintf("Map is missing map_list entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeStringIdItem)) == 0 &&
((header_->string_ids_off_ != 0) || (header_->string_ids_size_ != 0)))) {
ErrorStringPrintf("Map is missing string_ids entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeTypeIdItem)) == 0 &&
((header_->type_ids_off_ != 0) || (header_->type_ids_size_ != 0)))) {
ErrorStringPrintf("Map is missing type_ids entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeProtoIdItem)) == 0 &&
((header_->proto_ids_off_ != 0) || (header_->proto_ids_size_ != 0)))) {
ErrorStringPrintf("Map is missing proto_ids entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeFieldIdItem)) == 0 &&
((header_->field_ids_off_ != 0) || (header_->field_ids_size_ != 0)))) {
ErrorStringPrintf("Map is missing field_ids entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeMethodIdItem)) == 0 &&
((header_->method_ids_off_ != 0) || (header_->method_ids_size_ != 0)))) {
ErrorStringPrintf("Map is missing method_ids entry");
return false;
}
if (UNLIKELY((used_bits & MapTypeToBitMask(DexFile::kDexTypeClassDefItem)) == 0 &&
((header_->class_defs_off_ != 0) || (header_->class_defs_size_ != 0)))) {
ErrorStringPrintf("Map is missing class_defs entry");
return false;
}
return true;
}
#define DECODE_UNSIGNED_CHECKED_FROM_WITH_ERROR_VALUE(ptr, var, error_value) \
uint32_t var; \
if (!DecodeUnsignedLeb128Checked(&(ptr), begin_ + size_, &(var))) { \
return error_value; \
}
#define DECODE_UNSIGNED_CHECKED_FROM(ptr, var) \
uint32_t var; \
if (!DecodeUnsignedLeb128Checked(&(ptr), begin_ + size_, &(var))) { \
ErrorStringPrintf("Read out of bounds"); \
return false; \
}
#define DECODE_SIGNED_CHECKED_FROM(ptr, var) \
int32_t var; \
if (!DecodeSignedLeb128Checked(&(ptr), begin_ + size_, &(var))) { \
ErrorStringPrintf("Read out of bounds"); \
return false; \
}
bool DexFileVerifier::CheckAndGetHandlerOffsets(const dex::CodeItem* code_item,
uint32_t* handler_offsets,
uint32_t handlers_size) {
CodeItemDataAccessor accessor(*dex_file_, code_item);
const uint8_t* handlers_base = accessor.GetCatchHandlerData();
for (uint32_t i = 0; i < handlers_size; i++) {
bool catch_all;
size_t offset = ptr_ - handlers_base;
DECODE_SIGNED_CHECKED_FROM(ptr_, size);
if (UNLIKELY((size < -65536) || (size > 65536))) {
ErrorStringPrintf("Invalid exception handler size: %d", size);
return false;
}
if (size <= 0) {
catch_all = true;
size = -size;
} else {
catch_all = false;
}
handler_offsets[i] = static_cast<uint32_t>(offset);
while (size-- > 0) {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, type_idx);
if (!CheckIndex(type_idx, header_->type_ids_size_, "handler type_idx")) {
return false;
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, addr);
if (UNLIKELY(addr >= accessor.InsnsSizeInCodeUnits())) {
ErrorStringPrintf("Invalid handler addr: %x", addr);
return false;
}
}
if (catch_all) {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, addr);
if (UNLIKELY(addr >= accessor.InsnsSizeInCodeUnits())) {
ErrorStringPrintf("Invalid handler catch_all_addr: %x", addr);
return false;
}
}
}
return true;
}
bool DexFileVerifier::CheckClassDataItemField(uint32_t idx,
uint32_t access_flags,
uint32_t class_access_flags,
dex::TypeIndex class_type_index) {
// The `idx` has already been checked in `CheckIntraClassDataItemFields()`.
DCHECK_LE(idx, header_->field_ids_size_);
// Check that it's the right class.
dex::TypeIndex my_class_index =
(reinterpret_cast<const dex::FieldId*>(begin_ + header_->field_ids_off_) + idx)->class_idx_;
if (class_type_index != my_class_index) {
ErrorStringPrintf("Field's class index unexpected, %" PRIu16 "vs %" PRIu16,
my_class_index.index_,
class_type_index.index_);
return false;
}
// Check field access flags.
std::string error_msg;
if (!CheckFieldAccessFlags(idx, access_flags, class_access_flags, &error_msg)) {
ErrorStringPrintf("%s", error_msg.c_str());
return false;
}
return true;
}
bool DexFileVerifier::CheckClassDataItemMethod(uint32_t idx,
uint32_t access_flags,
uint32_t class_access_flags,
dex::TypeIndex class_type_index,
uint32_t code_offset,
bool expect_direct) {
// The `idx` has already been checked in `CheckIntraClassDataItemMethods()`.
DCHECK_LT(idx, header_->method_ids_size_);
const dex::MethodId& method_id =
*(reinterpret_cast<const dex::MethodId*>(begin_ + header_->method_ids_off_) + idx);
// Check that it's the right class.
dex::TypeIndex my_class_index = method_id.class_idx_;
if (class_type_index != my_class_index) {
ErrorStringPrintf("Method's class index unexpected, %" PRIu16 " vs %" PRIu16,
my_class_index.index_,
class_type_index.index_);
return false;
}
std::string error_msg;
uint32_t constructor_flags_by_name = 0;
{
uint32_t string_idx = method_id.name_idx_.index_;
if (!CheckIndex(string_idx, header_->string_ids_size_, "method flags verification")) {
return false;
}
if (UNLIKELY(string_idx < init_indices_.angle_bracket_end_index) &&
string_idx >= init_indices_.angle_bracket_start_index) {
if (string_idx == init_indices_.angle_clinit_angle_index) {
constructor_flags_by_name = kAccStatic | kAccConstructor;
} else if (string_idx == init_indices_.angle_init_angle_index) {
constructor_flags_by_name = kAccConstructor;
} else {
ErrorStringPrintf("Bad method name for method index %u", idx);
return false;
}
}
}
bool has_code = (code_offset != 0);
if (!CheckMethodAccessFlags(idx,
access_flags,
class_access_flags,
constructor_flags_by_name,
has_code,
expect_direct,
&error_msg)) {
ErrorStringPrintf("%s", error_msg.c_str());
return false;
}
if (constructor_flags_by_name != 0) {
if (!CheckConstructorProperties(idx, constructor_flags_by_name)) {
DCHECK(FailureReasonIsSet());
return false;
}
}
return true;
}
bool DexFileVerifier::CheckPadding(size_t offset,
uint32_t aligned_offset,
DexFile::MapItemType type) {
if (offset < aligned_offset) {
if (!CheckListSize(begin_ + offset, aligned_offset - offset, sizeof(uint8_t), "section")) {
return false;
}
while (offset < aligned_offset) {
if (UNLIKELY(*ptr_ != '\0')) {
ErrorStringPrintf("Non-zero padding %x before section of type %zu at offset 0x%zx",
*ptr_,
static_cast<size_t>(type),
offset);
return false;
}
ptr_++;
offset++;
}
}
return true;
}
bool DexFileVerifier::CheckEncodedValue() {
if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "encoded_value header")) {
return false;
}
uint8_t header_byte = *(ptr_++);
uint32_t value_type = header_byte & DexFile::kDexAnnotationValueTypeMask;
uint32_t value_arg = header_byte >> DexFile::kDexAnnotationValueArgShift;
switch (value_type) {
case DexFile::kDexAnnotationByte:
if (UNLIKELY(value_arg != 0)) {
ErrorStringPrintf("Bad encoded_value byte size %x", value_arg);
return false;
}
ptr_++;
break;
case DexFile::kDexAnnotationShort:
case DexFile::kDexAnnotationChar:
if (UNLIKELY(value_arg > 1)) {
ErrorStringPrintf("Bad encoded_value char/short size %x", value_arg);
return false;
}
ptr_ += value_arg + 1;
break;
case DexFile::kDexAnnotationInt:
case DexFile::kDexAnnotationFloat:
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value int/float size %x", value_arg);
return false;
}
ptr_ += value_arg + 1;
break;
case DexFile::kDexAnnotationLong:
case DexFile::kDexAnnotationDouble:
ptr_ += value_arg + 1;
break;
case DexFile::kDexAnnotationString: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value string size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, header_->string_ids_size_, "encoded_value string")) {
return false;
}
break;
}
case DexFile::kDexAnnotationType: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value type size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, header_->type_ids_size_, "encoded_value type")) {
return false;
}
break;
}
case DexFile::kDexAnnotationField:
case DexFile::kDexAnnotationEnum: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value field/enum size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, header_->field_ids_size_, "encoded_value field")) {
return false;
}
break;
}
case DexFile::kDexAnnotationMethod: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value method size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, header_->method_ids_size_, "encoded_value method")) {
return false;
}
break;
}
case DexFile::kDexAnnotationArray:
if (UNLIKELY(value_arg != 0)) {
ErrorStringPrintf("Bad encoded_value array value_arg %x", value_arg);
return false;
}
if (!CheckEncodedArray()) {
return false;
}
break;
case DexFile::kDexAnnotationAnnotation:
if (UNLIKELY(value_arg != 0)) {
ErrorStringPrintf("Bad encoded_value annotation value_arg %x", value_arg);
return false;
}
if (!CheckEncodedAnnotation()) {
return false;
}
break;
case DexFile::kDexAnnotationNull:
if (UNLIKELY(value_arg != 0)) {
ErrorStringPrintf("Bad encoded_value null value_arg %x", value_arg);
return false;
}
break;
case DexFile::kDexAnnotationBoolean:
if (UNLIKELY(value_arg > 1)) {
ErrorStringPrintf("Bad encoded_value boolean size %x", value_arg);
return false;
}
break;
case DexFile::kDexAnnotationMethodType: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value method type size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, header_->proto_ids_size_, "method_type value")) {
return false;
}
break;
}
case DexFile::kDexAnnotationMethodHandle: {
if (UNLIKELY(value_arg > 3)) {
ErrorStringPrintf("Bad encoded_value method handle size %x", value_arg);
return false;
}
uint32_t idx = ReadUnsignedLittleEndian(value_arg + 1);
if (!CheckIndex(idx, dex_file_->NumMethodHandles(), "method_handle value")) {
return false;
}
break;
}
default:
ErrorStringPrintf("Bogus encoded_value value_type %x", value_type);
return false;
}
return true;
}
bool DexFileVerifier::CheckEncodedArray() {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, size);
for (; size != 0u; --size) {
if (!CheckEncodedValue()) {
failure_reason_ = StringPrintf("Bad encoded_array value: %s", failure_reason_.c_str());
return false;
}
}
return true;
}
bool DexFileVerifier::CheckEncodedAnnotation() {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, anno_idx);
if (!CheckIndex(anno_idx, header_->type_ids_size_, "encoded_annotation type_idx")) {
return false;
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, size);
uint32_t last_idx = 0;
for (uint32_t i = 0; i < size; i++) {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, idx);
if (!CheckIndex(idx, header_->string_ids_size_, "annotation_element name_idx")) {
return false;
}
if (UNLIKELY(last_idx >= idx && i != 0)) {
ErrorStringPrintf("Out-of-order annotation_element name_idx: %x then %x",
last_idx, idx);
return false;
}
if (!CheckEncodedValue()) {
return false;
}
last_idx = idx;
}
return true;
}
bool DexFileVerifier::CheckStaticFieldTypes(const dex::ClassDef& class_def) {
ClassAccessor accessor(*dex_file_, ptr_);
EncodedStaticFieldValueIterator array_it(*dex_file_, class_def);
for (const ClassAccessor::Field& field : accessor.GetStaticFields()) {
if (!array_it.HasNext()) {
break;
}
uint32_t index = field.GetIndex();
// The `index` has already been checked in `CheckIntraClassDataItemFields()`.
DCHECK_LT(index, header_->field_ids_size_);
const dex::TypeId& type_id = dex_file_->GetTypeId(dex_file_->GetFieldId(index).type_idx_);
const char* field_type_name =
dex_file_->GetStringData(dex_file_->GetStringId(type_id.descriptor_idx_));
Primitive::Type field_type = Primitive::GetType(field_type_name[0]);
EncodedArrayValueIterator::ValueType array_type = array_it.GetValueType();
// Ensure this matches RuntimeEncodedStaticFieldValueIterator.
switch (array_type) {
case EncodedArrayValueIterator::ValueType::kBoolean:
if (field_type != Primitive::kPrimBoolean) {
ErrorStringPrintf("unexpected static field initial value type: 'Z' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kByte:
if (field_type != Primitive::kPrimByte) {
ErrorStringPrintf("unexpected static field initial value type: 'B' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kShort:
if (field_type != Primitive::kPrimShort) {
ErrorStringPrintf("unexpected static field initial value type: 'S' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kChar:
if (field_type != Primitive::kPrimChar) {
ErrorStringPrintf("unexpected static field initial value type: 'C' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kInt:
if (field_type != Primitive::kPrimInt) {
ErrorStringPrintf("unexpected static field initial value type: 'I' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kLong:
if (field_type != Primitive::kPrimLong) {
ErrorStringPrintf("unexpected static field initial value type: 'J' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kFloat:
if (field_type != Primitive::kPrimFloat) {
ErrorStringPrintf("unexpected static field initial value type: 'F' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kDouble:
if (field_type != Primitive::kPrimDouble) {
ErrorStringPrintf("unexpected static field initial value type: 'D' vs '%c'",
field_type_name[0]);
return false;
}
break;
case EncodedArrayValueIterator::ValueType::kNull:
case EncodedArrayValueIterator::ValueType::kString:
case EncodedArrayValueIterator::ValueType::kType:
if (field_type != Primitive::kPrimNot) {
ErrorStringPrintf("unexpected static field initial value type: 'L' vs '%c'",
field_type_name[0]);
return false;
}
break;
default:
ErrorStringPrintf("unexpected static field initial value type: %x", array_type);
return false;
}
array_it.Next();
}
if (array_it.HasNext()) {
ErrorStringPrintf("too many static field initial values");
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraTypeIdItem() {
if (!CheckListSize(ptr_, 1, sizeof(dex::TypeId), "type_ids")) {
return false;
}
const dex::TypeId* type_id = reinterpret_cast<const dex::TypeId*>(ptr_);
if (!CheckIndex(type_id->descriptor_idx_.index_,
header_->string_ids_size_,
"type_id.descriptor")) {
return false;
}
ptr_ += sizeof(dex::TypeId);
return true;
}
bool DexFileVerifier::CheckIntraProtoIdItem() {
if (!CheckListSize(ptr_, 1, sizeof(dex::ProtoId), "proto_ids")) {
return false;
}
const dex::ProtoId* proto_id = reinterpret_cast<const dex::ProtoId*>(ptr_);
if (!CheckIndex(proto_id->shorty_idx_.index_, header_->string_ids_size_, "proto_id.shorty") ||
!CheckIndex(proto_id->return_type_idx_.index_,
header_->type_ids_size_,
"proto_id.return_type")) {
return false;
}
ptr_ += sizeof(dex::ProtoId);
return true;
}
bool DexFileVerifier::CheckIntraFieldIdItem() {
if (!CheckListSize(ptr_, 1, sizeof(dex::FieldId), "field_ids")) {
return false;
}
const dex::FieldId* field_id = reinterpret_cast<const dex::FieldId*>(ptr_);
if (!CheckIndex(field_id->class_idx_.index_, header_->type_ids_size_, "field_id.class") ||
!CheckIndex(field_id->type_idx_.index_, header_->type_ids_size_, "field_id.type") ||
!CheckIndex(field_id->name_idx_.index_, header_->string_ids_size_, "field_id.name")) {
return false;
}
ptr_ += sizeof(dex::FieldId);
return true;
}
bool DexFileVerifier::CheckIntraMethodIdItem() {
if (!CheckListSize(ptr_, 1, sizeof(dex::MethodId), "method_ids")) {
return false;
}
const dex::MethodId* method_id = reinterpret_cast<const dex::MethodId*>(ptr_);
if (!CheckIndex(method_id->class_idx_.index_, header_->type_ids_size_, "method_id.class") ||
!CheckIndex(method_id->proto_idx_.index_, header_->proto_ids_size_, "method_id.proto") ||
!CheckIndex(method_id->name_idx_.index_, header_->string_ids_size_, "method_id.name")) {
return false;
}
ptr_ += sizeof(dex::MethodId);
return true;
}
bool DexFileVerifier::CheckIntraClassDefItem(uint32_t class_def_index) {
if (!CheckListSize(ptr_, 1, sizeof(dex::ClassDef), "class_defs")) {
return false;
}
const dex::ClassDef* class_def = reinterpret_cast<const dex::ClassDef*>(ptr_);
if (!CheckIndex(class_def->class_idx_.index_, header_->type_ids_size_, "class_def.class")) {
return false;
}
// Check superclass, if any.
if (UNLIKELY(class_def->pad2_ != 0u)) {
uint32_t combined =
(static_cast<uint32_t>(class_def->pad2_) << 16) + class_def->superclass_idx_.index_;
if (combined != 0xffffffffu) {
ErrorStringPrintf("Invalid superclass type padding/index: %x", combined);
return false;
}
} else if (!CheckIndex(class_def->superclass_idx_.index_,
header_->type_ids_size_,
"class_def.superclass")) {
return false;
}
DCHECK_LE(class_def->class_idx_.index_, kTypeIdLimit);
DCHECK_LT(kTypeIdLimit, defined_classes_.size());
if (defined_classes_[class_def->class_idx_.index_]) {
ErrorStringPrintf("Redefinition of class with type idx: '%u'", class_def->class_idx_.index_);
return false;
}
defined_classes_[class_def->class_idx_.index_] = true;
DCHECK_LE(class_def->class_idx_.index_, defined_class_indexes_.size());
defined_class_indexes_[class_def->class_idx_.index_] = class_def_index;
ptr_ += sizeof(dex::ClassDef);
return true;
}
bool DexFileVerifier::CheckIntraMethodHandleItem() {
if (!CheckListSize(ptr_, 1, sizeof(dex::MethodHandleItem), "method_handles")) {
return false;
}
const dex::MethodHandleItem* item = reinterpret_cast<const dex::MethodHandleItem*>(ptr_);
DexFile::MethodHandleType method_handle_type =
static_cast<DexFile::MethodHandleType>(item->method_handle_type_);
if (method_handle_type > DexFile::MethodHandleType::kLast) {
ErrorStringPrintf("Bad method handle type %x", item->method_handle_type_);
return false;
}
uint32_t index = item->field_or_method_idx_;
switch (method_handle_type) {
case DexFile::MethodHandleType::kStaticPut:
case DexFile::MethodHandleType::kStaticGet:
case DexFile::MethodHandleType::kInstancePut:
case DexFile::MethodHandleType::kInstanceGet:
if (!CheckIndex(index, header_->field_ids_size_, "method_handle_item field_idx")) {
return false;
}
break;
case DexFile::MethodHandleType::kInvokeStatic:
case DexFile::MethodHandleType::kInvokeInstance:
case DexFile::MethodHandleType::kInvokeConstructor:
case DexFile::MethodHandleType::kInvokeDirect:
case DexFile::MethodHandleType::kInvokeInterface: {
if (!CheckIndex(index, header_->method_ids_size_, "method_handle_item method_idx")) {
return false;
}
break;
}
}
ptr_ += sizeof(dex::MethodHandleItem);
return true;
}
bool DexFileVerifier::CheckIntraTypeList() {
const dex::TypeList* type_list = reinterpret_cast<const dex::TypeList*>(ptr_);
if (!CheckList(sizeof(dex::TypeItem), "type_list", &ptr_)) {
return false;
}
for (uint32_t i = 0, size = type_list->Size(); i != size; ++i) {
if (!CheckIndex(type_list->GetTypeItem(i).type_idx_.index_,
header_->type_ids_size_,
"type_list.type")) {
return false;
}
}
return true;
}
template <bool kStatic>
bool DexFileVerifier::CheckIntraClassDataItemFields(size_t count) {
constexpr const char* kTypeDescr = kStatic ? "static field" : "instance field";
// We cannot use ClassAccessor::Field yet as it could read beyond the end of the data section.
const uint8_t* ptr = ptr_;
const uint8_t* data_end = begin_ + header_->data_off_ + header_->data_size_;
uint32_t prev_index = 0;
for (size_t i = 0; i != count; ++i) {
uint32_t field_idx_diff, access_flags;
if (UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &field_idx_diff)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &access_flags))) {
ErrorStringPrintf("encoded_field read out of bounds");
return false;
}
uint32_t curr_index = prev_index + field_idx_diff;
// Check for overflow.
if (!CheckIndex(curr_index, header_->field_ids_size_, "class_data_item field_idx")) {
return false;
}
if (!CheckOrder(kTypeDescr, curr_index, prev_index)) {
return false;
}
// Check that it falls into the right class-data list.
bool is_static = (access_flags & kAccStatic) != 0;
if (UNLIKELY(is_static != kStatic)) {
ErrorStringPrintf("Static/instance field not in expected list");
return false;
}
prev_index = curr_index;
}
ptr_ = ptr;
return true;
}
bool DexFileVerifier::CheckIntraClassDataItemMethods(size_t num_methods,
ClassAccessor::Method* direct_methods,
size_t num_direct_methods) {
DCHECK(num_direct_methods == 0u || direct_methods != nullptr);
const char* kTypeDescr = (direct_methods == nullptr) ? "direct method" : "virtual method";
// We cannot use ClassAccessor::Method yet as it could read beyond the end of the data section.
const uint8_t* ptr = ptr_;
const uint8_t* data_end = begin_ + header_->data_off_ + header_->data_size_;
// Load the first direct method for the check below.
size_t remaining_direct_methods = num_direct_methods;
if (remaining_direct_methods != 0u) {
DCHECK(direct_methods != nullptr);
direct_methods->Read();
}
uint32_t prev_index = 0;
for (size_t i = 0; i != num_methods; ++i) {
uint32_t method_idx_diff, access_flags, code_off;
if (UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &method_idx_diff)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &access_flags)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &code_off))) {
ErrorStringPrintf("encoded_method read out of bounds");
return false;
}
uint32_t curr_index = prev_index + method_idx_diff;
// Check for overflow.
if (!CheckIndex(curr_index, header_->method_ids_size_, "class_data_item method_idx")) {
return false;
}
if (!CheckOrder(kTypeDescr, curr_index, prev_index)) {
return false;
}
// For virtual methods, we cross reference the method index to make sure
// it doesn't match any direct methods.
if (remaining_direct_methods != 0) {
// The direct methods are already known to be in ascending index order.
// So just keep up with the current index.
while (true) {
const uint32_t direct_idx = direct_methods->GetIndex();
if (direct_idx > curr_index) {
break;
}
if (direct_idx == curr_index) {
ErrorStringPrintf("Found virtual method with same index as direct method: %u",
curr_index);
return false;
}
--remaining_direct_methods;
if (remaining_direct_methods == 0u) {
break;
}
direct_methods->Read();
}
}
prev_index = curr_index;
}
ptr_ = ptr;
return true;
}
bool DexFileVerifier::CheckIntraClassDataItem() {
// We cannot use ClassAccessor yet as it could read beyond the end of the data section.
const uint8_t* ptr = ptr_;
const uint8_t* data_end = begin_ + header_->data_off_ + header_->data_size_;
uint32_t static_fields_size, instance_fields_size, direct_methods_size, virtual_methods_size;
if (UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &static_fields_size)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &instance_fields_size)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &direct_methods_size)) ||
UNLIKELY(!DecodeUnsignedLeb128Checked(&ptr, data_end, &virtual_methods_size))) {
ErrorStringPrintf("class_data_item read out of bounds");
return false;
}
ptr_ = ptr;
// Check fields.
if (!CheckIntraClassDataItemFields</*kStatic=*/ true>(static_fields_size)) {
return false;
}
if (!CheckIntraClassDataItemFields</*kStatic=*/ false>(instance_fields_size)) {
return false;
}
// Check methods.
const uint8_t* direct_methods_ptr = ptr_;
if (!CheckIntraClassDataItemMethods(direct_methods_size,
/*direct_methods=*/ nullptr,
/*num_direct_methods=*/ 0u)) {
return false;
}
// Direct methods have been checked, so we can now use ClassAccessor::Method to read them again.
ClassAccessor::Method direct_methods(*dex_file_, direct_methods_ptr);
if (!CheckIntraClassDataItemMethods(virtual_methods_size, &direct_methods, direct_methods_size)) {
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraCodeItem() {
const dex::CodeItem* code_item = reinterpret_cast<const dex::CodeItem*>(ptr_);
if (!CheckListSize(code_item, 1, sizeof(dex::CodeItem), "code")) {
return false;
}
CodeItemDataAccessor accessor(*dex_file_, code_item);
if (UNLIKELY(accessor.InsSize() > accessor.RegistersSize())) {
ErrorStringPrintf("ins_size (%ud) > registers_size (%ud)",
accessor.InsSize(), accessor.RegistersSize());
return false;
}
if (UNLIKELY(accessor.OutsSize() > 5 && accessor.OutsSize() > accessor.RegistersSize())) {
/*
* outs_size can be up to 5, even if registers_size is smaller, since the
* short forms of method invocation allow repetitions of a register multiple
* times within a single parameter list. However, longer parameter lists
* need to be represented in-order in the register file.
*/
ErrorStringPrintf("outs_size (%ud) > registers_size (%ud)",
accessor.OutsSize(), accessor.RegistersSize());
return false;
}
const uint16_t* insns = accessor.Insns();
uint32_t insns_size = accessor.InsnsSizeInCodeUnits();
if (!CheckListSize(insns, insns_size, sizeof(uint16_t), "insns size")) {
return false;
}
// Grab the end of the insns if there are no try_items.
uint32_t try_items_size = accessor.TriesSize();
if (try_items_size == 0) {
ptr_ = reinterpret_cast<const uint8_t*>(&insns[insns_size]);
return true;
}
// try_items are 4-byte aligned. Verify the spacer is 0.
if (((reinterpret_cast<uintptr_t>(&insns[insns_size]) & 3) != 0) && (insns[insns_size] != 0)) {
ErrorStringPrintf("Non-zero padding: %x", insns[insns_size]);
return false;
}
const dex::TryItem* try_items = accessor.TryItems().begin();
if (!CheckListSize(try_items, try_items_size, sizeof(dex::TryItem), "try_items size")) {
return false;
}
ptr_ = accessor.GetCatchHandlerData();
DECODE_UNSIGNED_CHECKED_FROM(ptr_, handlers_size);
if (UNLIKELY((handlers_size == 0) || (handlers_size >= 65536))) {
ErrorStringPrintf("Invalid handlers_size: %ud", handlers_size);
return false;
}
// Avoid an expensive allocation, if possible.
std::unique_ptr<uint32_t[]> handler_offsets_uptr;
uint32_t* handler_offsets;
constexpr size_t kAllocaMaxSize = 1024;
if (handlers_size < kAllocaMaxSize/sizeof(uint32_t)) {
// Note: Clang does not specify alignment guarantees for alloca. So align by hand.
handler_offsets =
AlignUp(reinterpret_cast<uint32_t*>(alloca((handlers_size + 1) * sizeof(uint32_t))),
alignof(uint32_t[]));
} else {
handler_offsets_uptr.reset(new uint32_t[handlers_size]);
handler_offsets = handler_offsets_uptr.get();
}
if (!CheckAndGetHandlerOffsets(code_item, &handler_offsets[0], handlers_size)) {
return false;
}
uint32_t last_addr = 0;
for (; try_items_size != 0u; --try_items_size) {
if (UNLIKELY(try_items->start_addr_ < last_addr)) {
ErrorStringPrintf("Out-of_order try_item with start_addr: %x", try_items->start_addr_);
return false;
}
if (UNLIKELY(try_items->start_addr_ >= insns_size)) {
ErrorStringPrintf("Invalid try_item start_addr: %x", try_items->start_addr_);
return false;
}
uint32_t i;
for (i = 0; i < handlers_size; i++) {
if (try_items->handler_off_ == handler_offsets[i]) {
break;
}
}
if (UNLIKELY(i == handlers_size)) {
ErrorStringPrintf("Bogus handler offset: %x", try_items->handler_off_);
return false;
}
last_addr = try_items->start_addr_ + try_items->insn_count_;
if (UNLIKELY(last_addr > insns_size)) {
ErrorStringPrintf("Invalid try_item insn_count: %x", try_items->insn_count_);
return false;
}
try_items++;
}
return true;
}
bool DexFileVerifier::CheckIntraStringDataItem() {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, size);
const uint8_t* file_end = begin_ + size_;
for (uint32_t i = 0; i < size; i++) {
CHECK_LT(i, size); // b/15014252 Prevents hitting the impossible case below
if (UNLIKELY(ptr_ >= file_end)) {
ErrorStringPrintf("String data would go beyond end-of-file");
return false;
}
uint8_t byte = *(ptr_++);
// Switch on the high 4 bits.
switch (byte >> 4) {
case 0x00:
// Special case of bit pattern 0xxx.
if (UNLIKELY(byte == 0)) {
CHECK_LT(i, size); // b/15014252 Actually hit this impossible case with clang
ErrorStringPrintf("String data shorter than indicated utf16_size %x", size);
return false;
}
break;
case 0x01:
case 0x02:
case 0x03:
case 0x04:
case 0x05:
case 0x06:
case 0x07:
// No extra checks necessary for bit pattern 0xxx.
break;
case 0x08:
case 0x09:
case 0x0a:
case 0x0b:
case 0x0f:
// Illegal bit patterns 10xx or 1111.
// Note: 1111 is valid for normal UTF-8, but not here.
ErrorStringPrintf("Illegal start byte %x in string data", byte);
return false;
case 0x0c:
case 0x0d: {
// Bit pattern 110x has an additional byte.
uint8_t byte2 = *(ptr_++);
if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
return false;
}
uint16_t value = ((byte & 0x1f) << 6) | (byte2 & 0x3f);
if (UNLIKELY((value != 0) && (value < 0x80))) {
ErrorStringPrintf("Illegal representation for value %x in string data", value);
return false;
}
break;
}
case 0x0e: {
// Bit pattern 1110 has 2 additional bytes.
uint8_t byte2 = *(ptr_++);
if (UNLIKELY((byte2 & 0xc0) != 0x80)) {
ErrorStringPrintf("Illegal continuation byte %x in string data", byte2);
return false;
}
uint8_t byte3 = *(ptr_++);
if (UNLIKELY((byte3 & 0xc0) != 0x80)) {
ErrorStringPrintf("Illegal continuation byte %x in string data", byte3);
return false;
}
uint16_t value = ((byte & 0x0f) << 12) | ((byte2 & 0x3f) << 6) | (byte3 & 0x3f);
if (UNLIKELY(value < 0x800)) {
ErrorStringPrintf("Illegal representation for value %x in string data", value);
return false;
}
break;
}
}
}
if (UNLIKELY(*(ptr_++) != '\0')) {
ErrorStringPrintf("String longer than indicated size %x", size);
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraDebugInfoItem() {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, dummy);
DECODE_UNSIGNED_CHECKED_FROM(ptr_, parameters_size);
if (UNLIKELY(parameters_size > 65536)) {
ErrorStringPrintf("Invalid parameters_size: %x", parameters_size);
return false;
}
for (uint32_t j = 0; j < parameters_size; j++) {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, parameter_name);
if (parameter_name != 0) {
parameter_name--;
if (!CheckIndex(parameter_name, header_->string_ids_size_, "debug_info_item parameter_name")) {
return false;
}
}
}
while (true) {
uint8_t opcode = *(ptr_++);
switch (opcode) {
case DexFile::DBG_END_SEQUENCE: {
return true;
}
case DexFile::DBG_ADVANCE_PC: {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, advance_pc_dummy);
break;
}
case DexFile::DBG_ADVANCE_LINE: {
DECODE_SIGNED_CHECKED_FROM(ptr_, advance_line_dummy);
break;
}
case DexFile::DBG_START_LOCAL: {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, reg_num);
if (UNLIKELY(reg_num >= 65536)) {
ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
return false;
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, name_idx);
if (name_idx != 0) {
name_idx--;
if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL name_idx")) {
return false;
}
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, type_idx);
if (type_idx != 0) {
type_idx--;
if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL type_idx")) {
return false;
}
}
break;
}
case DexFile::DBG_END_LOCAL:
case DexFile::DBG_RESTART_LOCAL: {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, reg_num);
if (UNLIKELY(reg_num >= 65536)) {
ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
return false;
}
break;
}
case DexFile::DBG_START_LOCAL_EXTENDED: {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, reg_num);
if (UNLIKELY(reg_num >= 65536)) {
ErrorStringPrintf("Bad reg_num for opcode %x", opcode);
return false;
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, name_idx);
if (name_idx != 0) {
name_idx--;
if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED name_idx")) {
return false;
}
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, type_idx);
if (type_idx != 0) {
type_idx--;
if (!CheckIndex(type_idx, header_->type_ids_size_, "DBG_START_LOCAL_EXTENDED type_idx")) {
return false;
}
}
DECODE_UNSIGNED_CHECKED_FROM(ptr_, sig_idx);
if (sig_idx != 0) {
sig_idx--;
if (!CheckIndex(sig_idx, header_->string_ids_size_, "DBG_START_LOCAL_EXTENDED sig_idx")) {
return false;
}
}
break;
}
case DexFile::DBG_SET_FILE: {
DECODE_UNSIGNED_CHECKED_FROM(ptr_, name_idx);
if (name_idx != 0) {
name_idx--;
if (!CheckIndex(name_idx, header_->string_ids_size_, "DBG_SET_FILE name_idx")) {
return false;
}
}
break;
}
}
}
}
bool DexFileVerifier::CheckIntraAnnotationItem() {
if (!CheckListSize(ptr_, 1, sizeof(uint8_t), "annotation visibility")) {
return false;
}
// Check visibility
switch (*(ptr_++)) {
case DexFile::kDexVisibilityBuild:
case DexFile::kDexVisibilityRuntime:
case DexFile::kDexVisibilitySystem:
break;
default:
ErrorStringPrintf("Bad annotation visibility: %x", *ptr_);
return false;
}
if (!CheckEncodedAnnotation()) {
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraHiddenapiClassData() {
const dex::HiddenapiClassData* item = reinterpret_cast<const dex::HiddenapiClassData*>(ptr_);
// Check expected header size.
uint32_t num_header_elems = dex_file_->NumClassDefs() + 1;
uint32_t elem_size = sizeof(uint32_t);
uint32_t header_size = num_header_elems * elem_size;
if (!CheckListSize(item, num_header_elems, elem_size, "hiddenapi class data section header")) {
return false;
}
// Check total size.
if (!CheckListSize(item, item->size_, 1u, "hiddenapi class data section")) {
return false;
}
// Check that total size can fit header.
if (item->size_ < header_size) {
ErrorStringPrintf(
"Hiddenapi class data too short to store header (%u < %u)", item->size_, header_size);
return false;
}
const uint8_t* data_end = ptr_ + item->size_;
ptr_ += header_size;
// Check offsets for each class def.
for (uint32_t i = 0; i < dex_file_->NumClassDefs(); ++i) {
const dex::ClassDef& class_def = dex_file_->GetClassDef(i);
const uint8_t* class_data = dex_file_->GetClassData(class_def);
uint32_t offset = item->flags_offset_[i];
if (offset == 0) {
continue;
}
// Check that class defs with no class data do not have any hiddenapi class data.
if (class_data == nullptr) {
ErrorStringPrintf(
"Hiddenapi class data offset not zero for class def %u with no class data", i);
return false;
}
// Check that the offset is within the section.
if (offset > item->size_) {
ErrorStringPrintf(
"Hiddenapi class data offset out of section bounds (%u > %u) for class def %u",
offset, item->size_, i);
return false;
}
// Check that the offset matches current pointer position. We do not allow
// offsets into already parsed data, or gaps between class def data.
uint32_t ptr_offset = ptr_ - reinterpret_cast<const uint8_t*>(item);
if (offset != ptr_offset) {
ErrorStringPrintf(
"Hiddenapi class data unexpected offset (%u != %u) for class def %u",
offset, ptr_offset, i);
return false;
}
// Parse a uleb128 value for each field and method of this class.
bool failure = false;
auto fn_member = [&](const ClassAccessor::BaseItem& member, const char* member_type) {
if (failure) {
return;
}
uint32_t decoded_flags;
if (!DecodeUnsignedLeb128Checked(&ptr_, data_end, &decoded_flags)) {
ErrorStringPrintf("Hiddenapi class data value out of bounds (%p > %p) for %s %i",
ptr_, data_end, member_type, member.GetIndex());
failure = true;
return;
}
if (!hiddenapi::ApiList(decoded_flags).IsValid()) {
ErrorStringPrintf("Hiddenapi class data flags invalid (%u) for %s %i",
decoded_flags, member_type, member.GetIndex());
failure = true;
return;
}
};
auto fn_field = [&](const ClassAccessor::Field& field) { fn_member(field, "field"); };
auto fn_method = [&](const ClassAccessor::Method& method) { fn_member(method, "method"); };
ClassAccessor accessor(*dex_file_, class_data);
accessor.VisitFieldsAndMethods(fn_field, fn_field, fn_method, fn_method);
if (failure) {
return false;
}
}
if (ptr_ != data_end) {
ErrorStringPrintf("Hiddenapi class data wrong reported size (%u != %u)",
static_cast<uint32_t>(ptr_ - reinterpret_cast<const uint8_t*>(item)),
item->size_);
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraAnnotationsDirectoryItem() {
const dex::AnnotationsDirectoryItem* item =
reinterpret_cast<const dex::AnnotationsDirectoryItem*>(ptr_);
if (!CheckListSize(item, 1, sizeof(dex::AnnotationsDirectoryItem), "annotations_directory")) {
return false;
}
// Field annotations follow immediately after the annotations directory.
const dex::FieldAnnotationsItem* field_item =
reinterpret_cast<const dex::FieldAnnotationsItem*>(item + 1);
uint32_t field_count = item->fields_size_;
if (!CheckListSize(field_item,
field_count,
sizeof(dex::FieldAnnotationsItem),
"field_annotations list")) {
return false;
}
uint32_t last_idx = 0;
for (uint32_t i = 0; i < field_count; i++) {
if (!CheckIndex(field_item->field_idx_, header_->field_ids_size_, "field annotation")) {
return false;
}
if (UNLIKELY(last_idx >= field_item->field_idx_ && i != 0)) {
ErrorStringPrintf("Out-of-order field_idx for annotation: %x then %x",
last_idx, field_item->field_idx_);
return false;
}
last_idx = field_item->field_idx_;
field_item++;
}
// Method annotations follow immediately after field annotations.
const dex::MethodAnnotationsItem* method_item =
reinterpret_cast<const dex::MethodAnnotationsItem*>(field_item);
uint32_t method_count = item->methods_size_;
if (!CheckListSize(method_item,
method_count,
sizeof(dex::MethodAnnotationsItem),
"method_annotations list")) {
return false;
}
last_idx = 0;
for (uint32_t i = 0; i < method_count; i++) {
if (!CheckIndex(method_item->method_idx_, header_->method_ids_size_, "method annotation")) {
return false;
}
if (UNLIKELY(last_idx >= method_item->method_idx_ && i != 0)) {
ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
last_idx, method_item->method_idx_);
return false;
}
last_idx = method_item->method_idx_;
method_item++;
}
// Parameter annotations follow immediately after method annotations.
const dex::ParameterAnnotationsItem* parameter_item =
reinterpret_cast<const dex::ParameterAnnotationsItem*>(method_item);
uint32_t parameter_count = item->parameters_size_;
if (!CheckListSize(parameter_item, parameter_count, sizeof(dex::ParameterAnnotationsItem),
"parameter_annotations list")) {
return false;
}
last_idx = 0;
for (uint32_t i = 0; i < parameter_count; i++) {
if (!CheckIndex(parameter_item->method_idx_,
header_->method_ids_size_,
"parameter annotation method")) {
return false;
}
if (UNLIKELY(last_idx >= parameter_item->method_idx_ && i != 0)) {
ErrorStringPrintf("Out-of-order method_idx for annotation: %x then %x",
last_idx, parameter_item->method_idx_);
return false;
}
last_idx = parameter_item->method_idx_;
parameter_item++;
}
// Return a pointer to the end of the annotations.
ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
return true;
}
template <DexFile::MapItemType kType>
bool DexFileVerifier::CheckIntraSectionIterate(size_t offset, uint32_t section_count) {
// Get the right alignment mask for the type of section.
size_t alignment_mask;
switch (kType) {
case DexFile::kDexTypeClassDataItem:
case DexFile::kDexTypeStringDataItem:
case DexFile::kDexTypeDebugInfoItem:
case DexFile::kDexTypeAnnotationItem:
case DexFile::kDexTypeEncodedArrayItem:
alignment_mask = sizeof(uint8_t) - 1;
break;
default:
alignment_mask = sizeof(uint32_t) - 1;
break;
}
// Iterate through the items in the section.
for (uint32_t i = 0; i < section_count; i++) {
size_t aligned_offset = (offset + alignment_mask) & ~alignment_mask;
// Check the padding between items.
if (!CheckPadding(offset, aligned_offset, kType)) {
return false;
}
// Check depending on the section type.
const uint8_t* start_ptr = ptr_;
switch (kType) {
case DexFile::kDexTypeStringIdItem: {
if (!CheckListSize(ptr_, 1, sizeof(dex::StringId), "string_ids")) {
return false;
}
ptr_ += sizeof(dex::StringId);
break;
}
case DexFile::kDexTypeTypeIdItem: {
if (!CheckIntraTypeIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeProtoIdItem: {
if (!CheckIntraProtoIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeFieldIdItem: {
if (!CheckIntraFieldIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeMethodIdItem: {
if (!CheckIntraMethodIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeClassDefItem: {
if (!CheckIntraClassDefItem(/*class_def_index=*/ i)) {
return false;
}
break;
}
case DexFile::kDexTypeCallSiteIdItem: {
if (!CheckListSize(ptr_, 1, sizeof(dex::CallSiteIdItem), "call_site_ids")) {
return false;
}
ptr_ += sizeof(dex::CallSiteIdItem);
break;
}
case DexFile::kDexTypeMethodHandleItem: {
if (!CheckIntraMethodHandleItem()) {
return false;
}
break;
}
case DexFile::kDexTypeTypeList: {
if (!CheckIntraTypeList()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationSetRefList: {
if (!CheckList(sizeof(dex::AnnotationSetRefItem), "annotation_set_ref_list", &ptr_)) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationSetItem: {
if (!CheckList(sizeof(uint32_t), "annotation_set_item", &ptr_)) {
return false;
}
break;
}
case DexFile::kDexTypeClassDataItem: {
if (!CheckIntraClassDataItem()) {
return false;
}
break;
}
case DexFile::kDexTypeCodeItem: {
if (!CheckIntraCodeItem()) {
return false;
}
break;
}
case DexFile::kDexTypeStringDataItem: {
if (!CheckIntraStringDataItem()) {
return false;
}
break;
}
case DexFile::kDexTypeDebugInfoItem: {
if (!CheckIntraDebugInfoItem()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationItem: {
if (!CheckIntraAnnotationItem()) {
return false;
}
break;
}
case DexFile::kDexTypeEncodedArrayItem: {
if (!CheckEncodedArray()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationsDirectoryItem: {
if (!CheckIntraAnnotationsDirectoryItem()) {
return false;
}
break;
}
case DexFile::kDexTypeHiddenapiClassData: {
if (!CheckIntraHiddenapiClassData()) {
return false;
}
break;
}
case DexFile::kDexTypeHeaderItem:
case DexFile::kDexTypeMapList:
break;
}
if (start_ptr == ptr_) {
ErrorStringPrintf("Unknown map item type %x", kType);
return false;
}
if (IsDataSectionType(kType)) {
if (aligned_offset == 0u) {
ErrorStringPrintf("Item %d offset is 0", i);
return false;
}
DCHECK(offset_to_type_map_.find(aligned_offset) == offset_to_type_map_.end());
offset_to_type_map_.insert(std::pair<uint32_t, uint16_t>(aligned_offset, kType));
}
aligned_offset = ptr_ - begin_;
if (UNLIKELY(aligned_offset > size_)) {
ErrorStringPrintf("Item %d at ends out of bounds", i);
return false;
}
offset = aligned_offset;
}
return true;
}
template <DexFile::MapItemType kType>
bool DexFileVerifier::CheckIntraIdSection(size_t offset, uint32_t count) {
uint32_t expected_offset;
uint32_t expected_size;
// Get the expected offset and size from the header.
switch (kType) {
case DexFile::kDexTypeStringIdItem:
expected_offset = header_->string_ids_off_;
expected_size = header_->string_ids_size_;
break;
case DexFile::kDexTypeTypeIdItem:
expected_offset = header_->type_ids_off_;
expected_size = header_->type_ids_size_;
break;
case DexFile::kDexTypeProtoIdItem:
expected_offset = header_->proto_ids_off_;
expected_size = header_->proto_ids_size_;
break;
case DexFile::kDexTypeFieldIdItem:
expected_offset = header_->field_ids_off_;
expected_size = header_->field_ids_size_;
break;
case DexFile::kDexTypeMethodIdItem:
expected_offset = header_->method_ids_off_;
expected_size = header_->method_ids_size_;
break;
case DexFile::kDexTypeClassDefItem:
expected_offset = header_->class_defs_off_;
expected_size = header_->class_defs_size_;
break;
default:
ErrorStringPrintf("Bad type for id section: %x", kType);
return false;
}
// Check that the offset and size are what were expected from the header.
if (UNLIKELY(offset != expected_offset)) {
ErrorStringPrintf("Bad offset for section: got %zx, expected %x", offset, expected_offset);
return false;
}
if (UNLIKELY(count != expected_size)) {
ErrorStringPrintf("Bad size for section: got %x, expected %x", count, expected_size);
return false;
}
return CheckIntraSectionIterate<kType>(offset, count);
}
template <DexFile::MapItemType kType>
bool DexFileVerifier::CheckIntraDataSection(size_t offset, uint32_t count) {
size_t data_start = header_->data_off_;
size_t data_end = data_start + header_->data_size_;
// Sanity check the offset of the section.
if (UNLIKELY((offset < data_start) || (offset > data_end))) {
ErrorStringPrintf("Bad offset for data subsection: %zx", offset);
return false;
}
if (!CheckIntraSectionIterate<kType>(offset, count)) {
return false;
}
// FIXME: Doing this check late means we may have already read memory outside the
// data section and potentially outside the file, thus risking a segmentation fault.
size_t next_offset = ptr_ - begin_;
if (next_offset > data_end) {
ErrorStringPrintf("Out-of-bounds end of data subsection: %zu data_off=%u data_size=%u",
next_offset,
header_->data_off_,
header_->data_size_);
return false;
}
return true;
}
bool DexFileVerifier::CheckIntraSection() {
const dex::MapList* map = reinterpret_cast<const dex::MapList*>(begin_ + header_->map_off_);
const dex::MapItem* item = map->list_;
size_t offset = 0;
uint32_t count = map->size_;
ptr_ = begin_;
// Preallocate offset map to avoid some allocations. We can only guess from the list items,
// not derived things.
offset_to_type_map_.reserve(
std::min(header_->class_defs_size_, 65535u) +
std::min(header_->string_ids_size_, 65535u) +
2 * std::min(header_->method_ids_size_, 65535u));
// Check the items listed in the map.
for (; count != 0u; --count) {
const size_t current_offset = offset;
uint32_t section_offset = item->offset_;
uint32_t section_count = item->size_;
DexFile::MapItemType type = static_cast<DexFile::MapItemType>(item->type_);
// Check for padding and overlap between items.
if (!CheckPadding(offset, section_offset, type)) {
return false;
} else if (UNLIKELY(offset > section_offset)) {
ErrorStringPrintf("Section overlap or out-of-order map: %zx, %x", offset, section_offset);
return false;
}
if (type == DexFile::kDexTypeClassDataItem) {
FindStringRangesForMethodNames();
}
// Check each item based on its type.
switch (type) {
case DexFile::kDexTypeHeaderItem:
if (UNLIKELY(section_count != 1)) {
ErrorStringPrintf("Multiple header items");
return false;
}
if (UNLIKELY(section_offset != 0)) {
ErrorStringPrintf("Header at %x, not at start of file", section_offset);
return false;
}
ptr_ = begin_ + header_->header_size_;
offset = header_->header_size_;
break;
#define CHECK_INTRA_ID_SECTION_CASE(type) \
case type: \
if (!CheckIntraIdSection<type>(section_offset, section_count)) { \
return false; \
} \
offset = ptr_ - begin_; \
break;
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeStringIdItem)
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeTypeIdItem)
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeProtoIdItem)
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeFieldIdItem)
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeMethodIdItem)
CHECK_INTRA_ID_SECTION_CASE(DexFile::kDexTypeClassDefItem)
#undef CHECK_INTRA_ID_SECTION_CASE
case DexFile::kDexTypeMapList:
if (UNLIKELY(section_count != 1)) {
ErrorStringPrintf("Multiple map list items");
return false;
}
if (UNLIKELY(section_offset != header_->map_off_)) {
ErrorStringPrintf("Map not at header-defined offset: %x, expected %x",
section_offset, header_->map_off_);
return false;
}
ptr_ += sizeof(uint32_t) + (map->size_ * sizeof(dex::MapItem));
offset = section_offset + sizeof(uint32_t) + (map->size_ * sizeof(dex::MapItem));
break;
#define CHECK_INTRA_SECTION_ITERATE_CASE(type) \
case type: \
if (!CheckIntraSectionIterate<type>(section_offset, section_count)) { \
return false; \
} \
offset = ptr_ - begin_; \
break;
CHECK_INTRA_SECTION_ITERATE_CASE(DexFile::kDexTypeMethodHandleItem)
CHECK_INTRA_SECTION_ITERATE_CASE(DexFile::kDexTypeCallSiteIdItem)
#undef CHECK_INTRA_SECTION_ITERATE_CASE
#define CHECK_INTRA_DATA_SECTION_CASE(type) \
case type: \
if (!CheckIntraDataSection<type>(section_offset, section_count)) { \
return false; \
} \
offset = ptr_ - begin_; \
break;
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeTypeList)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeAnnotationSetRefList)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeAnnotationSetItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeClassDataItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeCodeItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeStringDataItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeDebugInfoItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeAnnotationItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeEncodedArrayItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeAnnotationsDirectoryItem)
CHECK_INTRA_DATA_SECTION_CASE(DexFile::kDexTypeHiddenapiClassData)
#undef CHECK_INTRA_DATA_SECTION_CASE
}
if (offset == current_offset) {
ErrorStringPrintf("Unknown map item type %x", type);
return false;
}
item++;
}
return true;
}
bool DexFileVerifier::CheckOffsetToTypeMap(size_t offset, uint16_t type) {
DCHECK_NE(offset, 0u);
auto it = offset_to_type_map_.find(offset);
if (UNLIKELY(it == offset_to_type_map_.end())) {
ErrorStringPrintf("No data map entry found @ %zx; expected %x", offset, type);
return false;
}
if (UNLIKELY(it->second != type)) {
ErrorStringPrintf("Unexpected data map entry @ %zx; expected %x, found %x",
offset, type, it->second);
return false;
}
return true;
}
uint32_t DexFileVerifier::FindFirstClassDataDefiner(const ClassAccessor& accessor) {
// The data item and field/method indexes have already been checked in
// `CheckIntraClassDataItem()` or its helper functions.
if (accessor.NumFields() != 0) {
ClassAccessor::Field read_field(*dex_file_, accessor.ptr_pos_);
read_field.Read();
DCHECK_LE(read_field.GetIndex(), dex_file_->NumFieldIds());
return dex_file_->GetFieldId(read_field.GetIndex()).class_idx_.index_;
}
if (accessor.NumMethods() != 0) {
ClassAccessor::Method read_method(*dex_file_, accessor.ptr_pos_);
read_method.Read();
DCHECK_LE(read_method.GetIndex(), dex_file_->NumMethodIds());
return dex_file_->GetMethodId(read_method.GetIndex()).class_idx_.index_;
}
return kDexNoIndex;
}
uint32_t DexFileVerifier::FindFirstAnnotationsDirectoryDefiner(const uint8_t* ptr) {
// The annotations directory and field/method indexes have already been checked in
// `CheckIntraAnnotationsDirectoryItem()`.
const dex::AnnotationsDirectoryItem* item =
reinterpret_cast<const dex::AnnotationsDirectoryItem*>(ptr);
if (item->fields_size_ != 0) {
dex::FieldAnnotationsItem* field_items = (dex::FieldAnnotationsItem*) (item + 1);
DCHECK_LE(field_items[0].field_idx_, dex_file_->NumFieldIds());
return dex_file_->GetFieldId(field_items[0].field_idx_).class_idx_.index_;
}
if (item->methods_size_ != 0) {
dex::MethodAnnotationsItem* method_items = (dex::MethodAnnotationsItem*) (item + 1);
DCHECK_LE(method_items[0].method_idx_, dex_file_->NumMethodIds());
return dex_file_->GetMethodId(method_items[0].method_idx_).class_idx_.index_;
}
if (item->parameters_size_ != 0) {
dex::ParameterAnnotationsItem* parameter_items = (dex::ParameterAnnotationsItem*) (item + 1);
DCHECK_LE(parameter_items[0].method_idx_, dex_file_->NumMethodIds());
return dex_file_->GetMethodId(parameter_items[0].method_idx_).class_idx_.index_;
}
return kDexNoIndex;
}
bool DexFileVerifier::CheckInterStringIdItem() {
const dex::StringId* item = reinterpret_cast<const dex::StringId*>(ptr_);
// Note: The mapping to string data items is eagerly verified at the start of CheckInterSection().
// Check ordering between items.
if (previous_item_ != nullptr) {
const dex::StringId* prev_item = reinterpret_cast<const dex::StringId*>(previous_item_);
const char* prev_str = dex_file_->GetStringData(*prev_item);
const char* str = dex_file_->GetStringData(*item);
if (UNLIKELY(CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(prev_str, str) >= 0)) {
ErrorStringPrintf("Out-of-order string_ids: '%s' then '%s'", prev_str, str);
return false;
}
}
ptr_ += sizeof(dex::StringId);
return true;
}
bool DexFileVerifier::CheckInterTypeIdItem() {
const dex::TypeId* item = reinterpret_cast<const dex::TypeId*>(ptr_);
{
// Translate to index to potentially use cache.
// The check in `CheckIntraIdSection()` guarantees that this index is valid.
size_t index = item - reinterpret_cast<const dex::TypeId*>(begin_ + header_->type_ids_off_);
DCHECK_LE(index, header_->type_ids_size_);
if (UNLIKELY(!VerifyTypeDescriptor(
dex::TypeIndex(static_cast<decltype(dex::TypeIndex::index_)>(index)),
"Invalid type descriptor",
[](char) { return true; }))) {
return false;
}
}
// Check ordering between items.
if (previous_item_ != nullptr) {
const dex::TypeId* prev_item = reinterpret_cast<const dex::TypeId*>(previous_item_);
if (UNLIKELY(prev_item->descriptor_idx_ >= item->descriptor_idx_)) {
ErrorStringPrintf("Out-of-order type_ids: %x then %x",
prev_item->descriptor_idx_.index_,
item->descriptor_idx_.index_);
return false;
}
}
ptr_ += sizeof(dex::TypeId);
return true;
}
bool DexFileVerifier::CheckInterProtoIdItem() {
const dex::ProtoId* item = reinterpret_cast<const dex::ProtoId*>(ptr_);
const char* shorty = dex_file_->StringDataByIdx(item->shorty_idx_);
if (item->parameters_off_ != 0 &&
!CheckOffsetToTypeMap(item->parameters_off_, DexFile::kDexTypeTypeList)) {
return false;
}
// Check that return type is representable as a uint16_t;
if (UNLIKELY(!IsValidOrNoTypeId(item->return_type_idx_.index_, item->pad_))) {
ErrorStringPrintf("proto with return type idx outside uint16_t range '%x:%x'",
item->pad_, item->return_type_idx_.index_);
return false;
}
// Check the return type and advance the shorty.
const char* return_type = dex_file_->StringByTypeIdx(item->return_type_idx_);
if (!CheckShortyDescriptorMatch(*shorty, return_type, true)) {
return false;
}
shorty++;
DexFileParameterIterator it(*dex_file_, *item);
while (it.HasNext() && *shorty != '\0') {
if (!CheckIndex(it.GetTypeIdx().index_,
dex_file_->NumTypeIds(),
"inter_proto_id_item shorty type_idx")) {
return false;
}
const char* descriptor = it.GetDescriptor();
if (!CheckShortyDescriptorMatch(*shorty, descriptor, false)) {
return false;
}
it.Next();
shorty++;
}
if (UNLIKELY(it.HasNext() || *shorty != '\0')) {
ErrorStringPrintf("Mismatched length for parameters and shorty");
return false;
}
// Check ordering between items. This relies on type_ids being in order.
if (previous_item_ != nullptr) {
const dex::ProtoId* prev = reinterpret_cast<const dex::ProtoId*>(previous_item_);
if (UNLIKELY(prev->return_type_idx_ > item->return_type_idx_)) {
ErrorStringPrintf("Out-of-order proto_id return types");
return false;
} else if (prev->return_type_idx_ == item->return_type_idx_) {
DexFileParameterIterator curr_it(*dex_file_, *item);
DexFileParameterIterator prev_it(*dex_file_, *prev);
while (curr_it.HasNext() && prev_it.HasNext()) {
dex::TypeIndex prev_idx = prev_it.GetTypeIdx();
dex::TypeIndex curr_idx = curr_it.GetTypeIdx();
DCHECK_NE(prev_idx, dex::TypeIndex(DexFile::kDexNoIndex16));
DCHECK_NE(curr_idx, dex::TypeIndex(DexFile::kDexNoIndex16));
if (prev_idx < curr_idx) {
break;
} else if (UNLIKELY(prev_idx > curr_idx)) {
ErrorStringPrintf("Out-of-order proto_id arguments");
return false;
}
prev_it.Next();
curr_it.Next();
}
if (!curr_it.HasNext()) {
// Either a duplicate ProtoId or a ProtoId with a shorter argument list follows
// a ProtoId with a longer one. Both cases are forbidden by the specification.
ErrorStringPrintf("Out-of-order proto_id arguments");
return false;
}
}
}
ptr_ += sizeof(dex::ProtoId);
return true;
}
bool DexFileVerifier::CheckInterFieldIdItem() {
const dex::FieldId* item = reinterpret_cast<const dex::FieldId*>(ptr_);
// Check that the class descriptor is valid.
if (UNLIKELY(!VerifyTypeDescriptor(item->class_idx_,
"Invalid descriptor for class_idx",
[](char d) { return d == 'L'; }))) {
return false;
}
// Check that the type descriptor is a valid field name.
if (UNLIKELY(!VerifyTypeDescriptor(item->type_idx_,
"Invalid descriptor for type_idx",
[](char d) { return d != 'V'; }))) {
return false;
}
// Check that the name is valid.
const char* field_name = dex_file_->StringDataByIdx(item->name_idx_);
if (UNLIKELY(!IsValidMemberName(field_name))) {
ErrorStringPrintf("Invalid field name: '%s'", field_name);
return false;
}
// Check ordering between items. This relies on the other sections being in order.
if (previous_item_ != nullptr) {
const dex::FieldId* prev_item = reinterpret_cast<const dex::FieldId*>(previous_item_);
if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
ErrorStringPrintf("Out-of-order field_ids");
return false;
} else if (prev_item->class_idx_ == item->class_idx_) {
if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
ErrorStringPrintf("Out-of-order field_ids");
return false;
} else if (prev_item->name_idx_ == item->name_idx_) {
if (UNLIKELY(prev_item->type_idx_ >= item->type_idx_)) {
ErrorStringPrintf("Out-of-order field_ids");
return false;
}
}
}
}
ptr_ += sizeof(dex::FieldId);
return true;
}
bool DexFileVerifier::CheckInterMethodIdItem() {
const dex::MethodId* item = reinterpret_cast<const dex::MethodId*>(ptr_);
// Check that the class descriptor is a valid reference name.
if (UNLIKELY(!VerifyTypeDescriptor(item->class_idx_,
"Invalid descriptor for class_idx",
[](char d) { return d == 'L' || d == '['; }))) {
return false;
}
// Check that the name is valid.
const char* method_name = dex_file_->StringDataByIdx(item->name_idx_);
if (UNLIKELY(!IsValidMemberName(method_name))) {
ErrorStringPrintf("Invalid method name: '%s'", method_name);
return false;
}
// Check that the proto id is valid.
if (UNLIKELY(!CheckIndex(item->proto_idx_.index_, dex_file_->NumProtoIds(),
"inter_method_id_item proto_idx"))) {
return false;
}
// Check ordering between items. This relies on the other sections being in order.
if (previous_item_ != nullptr) {
const dex::MethodId* prev_item = reinterpret_cast<const dex::MethodId*>(previous_item_);
if (UNLIKELY(prev_item->class_idx_ > item->class_idx_)) {
ErrorStringPrintf("Out-of-order method_ids");
return false;
} else if (prev_item->class_idx_ == item->class_idx_) {
if (UNLIKELY(prev_item->name_idx_ > item->name_idx_)) {
ErrorStringPrintf("Out-of-order method_ids");
return false;
} else if (prev_item->name_idx_ == item->name_idx_) {
if (UNLIKELY(prev_item->proto_idx_ >= item->proto_idx_)) {
ErrorStringPrintf("Out-of-order method_ids");
return false;
}
}
}
}
ptr_ += sizeof(dex::MethodId);
return true;
}
bool DexFileVerifier::CheckInterClassDefItem() {
const dex::ClassDef* item = reinterpret_cast<const dex::ClassDef*>(ptr_);
// Check that class_idx_ is representable as a uint16_t;
if (UNLIKELY(!IsValidTypeId(item->class_idx_.index_, item->pad1_))) {
ErrorStringPrintf("class with type idx outside uint16_t range '%x:%x'", item->pad1_,
item->class_idx_.index_);
return false;
}
// Check that superclass_idx_ is representable as a uint16_t;
if (UNLIKELY(!IsValidOrNoTypeId(item->superclass_idx_.index_, item->pad2_))) {
ErrorStringPrintf("class with superclass type idx outside uint16_t range '%x:%x'", item->pad2_,
item->superclass_idx_.index_);
return false;
}
// Check for duplicate class def.
if (UNLIKELY(!VerifyTypeDescriptor(item->class_idx_,
"Invalid class descriptor",
[](char d) { return d == 'L'; }))) {
return false;
}
// Only allow non-runtime modifiers.
if ((item->access_flags_ & ~kAccJavaFlagsMask) != 0) {
ErrorStringPrintf("Invalid class flags: '%d'", item->access_flags_);
return false;
}
if (item->interfaces_off_ != 0 &&
!CheckOffsetToTypeMap(item->interfaces_off_, DexFile::kDexTypeTypeList)) {
return false;
}
if (item->annotations_off_ != 0 &&
!CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationsDirectoryItem)) {
return false;
}
if (item->class_data_off_ != 0 &&
!CheckOffsetToTypeMap(item->class_data_off_, DexFile::kDexTypeClassDataItem)) {
return false;
}
if (item->static_values_off_ != 0 &&
!CheckOffsetToTypeMap(item->static_values_off_, DexFile::kDexTypeEncodedArrayItem)) {
return false;
}
if (item->superclass_idx_.IsValid()) {
if (header_->GetVersion() >= DexFile::kClassDefinitionOrderEnforcedVersion) {
// Check that a class does not inherit from itself directly (by having
// the same type idx as its super class).
if (UNLIKELY(item->superclass_idx_ == item->class_idx_)) {
ErrorStringPrintf("Class with same type idx as its superclass: '%d'",
item->class_idx_.index_);
return false;
}
// Check that a class is defined after its super class (if the
// latter is defined in the same Dex file).
const dex::ClassDef* superclass_def = dex_file_->FindClassDef(item->superclass_idx_);
if (superclass_def != nullptr) {
// The superclass is defined in this Dex file.
if (superclass_def > item) {
// ClassDef item for super class appearing after the class' ClassDef item.
ErrorStringPrintf("Invalid class definition ordering:"
" class with type idx: '%d' defined before"
" superclass with type idx: '%d'",
item->class_idx_.index_,
item->superclass_idx_.index_);
return false;
}
}
}
if (UNLIKELY(!VerifyTypeDescriptor(item->superclass_idx_,
"Invalid superclass",
[](char d) { return d == 'L'; }))) {
return false;
}
}
// Check interfaces.
const dex::TypeList* interfaces = dex_file_->GetInterfacesList(*item);
if (interfaces != nullptr) {
uint32_t size = interfaces->Size();
for (uint32_t i = 0; i < size; i++) {
if (header_->GetVersion() >= DexFile::kClassDefinitionOrderEnforcedVersion) {
// Check that a class does not implement itself directly (by having the
// same type idx as one of its immediate implemented interfaces).
if (UNLIKELY(interfaces->GetTypeItem(i).type_idx_ == item->class_idx_)) {
ErrorStringPrintf("Class with same type idx as implemented interface: '%d'",
item->class_idx_.index_);
return false;
}
// Check that a class is defined after the interfaces it implements
// (if they are defined in the same Dex file).
const dex::ClassDef* interface_def =
dex_file_->FindClassDef(interfaces->GetTypeItem(i).type_idx_);
if (interface_def != nullptr) {
// The interface is defined in this Dex file.
if (interface_def > item) {
// ClassDef item for interface appearing after the class' ClassDef item.
ErrorStringPrintf("Invalid class definition ordering:"
" class with type idx: '%d' defined before"
" implemented interface with type idx: '%d'",
item->class_idx_.index_,
interfaces->GetTypeItem(i).type_idx_.index_);
return false;
}
}
}
// Ensure that the interface refers to a class (not an array nor a primitive type).
if (UNLIKELY(!VerifyTypeDescriptor(interfaces->GetTypeItem(i).type_idx_,
"Invalid interface",
[](char d) { return d == 'L'; }))) {
return false;
}
}
/*
* Ensure that there are no duplicates. This is an O(N^2) test, but in
* practice the number of interfaces implemented by any given class is low.
*/
for (uint32_t i = 1; i < size; i++) {
dex::TypeIndex idx1 = interfaces->GetTypeItem(i).type_idx_;
for (uint32_t j =0; j < i; j++) {
dex::TypeIndex idx2 = interfaces->GetTypeItem(j).type_idx_;
if (UNLIKELY(idx1 == idx2)) {
ErrorStringPrintf("Duplicate interface: '%s'", dex_file_->StringByTypeIdx(idx1));
return false;
}
}
}
}
// Check that references in class_data_item are to the right class.
if (item->class_data_off_ != 0) {
ClassAccessor accessor(*dex_file_, begin_ + item->class_data_off_);
uint32_t data_definer = FindFirstClassDataDefiner(accessor);
DCHECK(IsUint<16>(data_definer) || data_definer == kDexNoIndex) << data_definer;
if (UNLIKELY((data_definer != item->class_idx_.index_) && (data_definer != kDexNoIndex))) {
ErrorStringPrintf("Invalid class_data_item");
return false;
}
}
// Check that references in annotations_directory_item are to right class.
if (item->annotations_off_ != 0) {
// annotations_off_ is supposed to be aligned by 4.
if (!IsAlignedParam(item->annotations_off_, 4)) {
ErrorStringPrintf("Invalid annotations_off_, not aligned by 4");
return false;
}
const uint8_t* data = begin_ + item->annotations_off_;
uint32_t defining_class = FindFirstAnnotationsDirectoryDefiner(data);
DCHECK(IsUint<16>(defining_class) || defining_class == kDexNoIndex) << defining_class;
if (UNLIKELY((defining_class != item->class_idx_.index_) && (defining_class != kDexNoIndex))) {
ErrorStringPrintf("Invalid annotations_directory_item");
return false;
}
}
ptr_ += sizeof(dex::ClassDef);
return true;
}
bool DexFileVerifier::CheckInterCallSiteIdItem() {
const dex::CallSiteIdItem* item = reinterpret_cast<const dex::CallSiteIdItem*>(ptr_);
// Check call site referenced by item is in encoded array section.
if (!CheckOffsetToTypeMap(item->data_off_, DexFile::kDexTypeEncodedArrayItem)) {
ErrorStringPrintf("Invalid offset in CallSideIdItem");
return false;
}
CallSiteArrayValueIterator it(*dex_file_, *item);
// Check Method Handle
if (!it.HasNext() || it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodHandle) {
ErrorStringPrintf("CallSiteArray missing method handle");
return false;
}
uint32_t handle_index = static_cast<uint32_t>(it.GetJavaValue().i);
if (handle_index >= dex_file_->NumMethodHandles()) {
ErrorStringPrintf("CallSite has bad method handle id: %x", handle_index);
return false;
}
// Check target method name.
it.Next();
if (!it.HasNext() ||
it.GetValueType() != EncodedArrayValueIterator::ValueType::kString) {
ErrorStringPrintf("CallSiteArray missing target method name");
return false;
}
uint32_t name_index = static_cast<uint32_t>(it.GetJavaValue().i);
if (name_index >= dex_file_->NumStringIds()) {
ErrorStringPrintf("CallSite has bad method name id: %x", name_index);
return false;
}
// Check method type.
it.Next();
if (!it.HasNext() ||
it.GetValueType() != EncodedArrayValueIterator::ValueType::kMethodType) {
ErrorStringPrintf("CallSiteArray missing method type");
return false;
}
uint32_t proto_index = static_cast<uint32_t>(it.GetJavaValue().i);
if (proto_index >= dex_file_->NumProtoIds()) {
ErrorStringPrintf("CallSite has bad method type: %x", proto_index);
return false;
}
ptr_ += sizeof(dex::CallSiteIdItem);
return true;
}
bool DexFileVerifier::CheckInterAnnotationSetRefList() {
const dex::AnnotationSetRefList* list = reinterpret_cast<const dex::AnnotationSetRefList*>(ptr_);
const dex::AnnotationSetRefItem* item = list->list_;
uint32_t count = list->size_;
for (; count != 0u; --count) {
if (item->annotations_off_ != 0 &&
!CheckOffsetToTypeMap(item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
return false;
}
item++;
}
ptr_ = reinterpret_cast<const uint8_t*>(item);
return true;
}
bool DexFileVerifier::CheckInterAnnotationSetItem() {
const dex::AnnotationSetItem* set = reinterpret_cast<const dex::AnnotationSetItem*>(ptr_);
const uint32_t* offsets = set->entries_;
uint32_t count = set->size_;
uint32_t last_idx = 0;
for (uint32_t i = 0; i < count; i++) {
if (*offsets != 0 && !CheckOffsetToTypeMap(*offsets, DexFile::kDexTypeAnnotationItem)) {
return false;
}
// Get the annotation from the offset and the type index for the annotation.
const dex::AnnotationItem* annotation =
reinterpret_cast<const dex::AnnotationItem*>(begin_ + *offsets);
const uint8_t* data = annotation->annotation_;
DECODE_UNSIGNED_CHECKED_FROM(data, idx);
if (UNLIKELY(last_idx >= idx && i != 0)) {
ErrorStringPrintf("Out-of-order entry types: %x then %x", last_idx, idx);
return false;
}
last_idx = idx;
offsets++;
}
ptr_ = reinterpret_cast<const uint8_t*>(offsets);
return true;
}
bool DexFileVerifier::CheckInterClassDataItem() {
ClassAccessor accessor(*dex_file_, ptr_);
uint32_t defining_class = FindFirstClassDataDefiner(accessor);
DCHECK(IsUint<16>(defining_class) || defining_class == kDexNoIndex) << defining_class;
if (defining_class == kDexNoIndex) {
return true; // Empty definitions are OK (but useless) and could be shared by multiple classes.
}
if (!defined_classes_[defining_class]) {
// Should really have a class definition for this class data item.
ErrorStringPrintf("Could not find declaring class for non-empty class data item.");
return false;
}
const dex::TypeIndex class_type_index(defining_class);
const dex::ClassDef& class_def = dex_file_->GetClassDef(defined_class_indexes_[defining_class]);
for (const ClassAccessor::Field& read_field : accessor.GetFields()) {
// The index has already been checked in `CheckIntraClassDataItemFields()`.
DCHECK_LE(read_field.GetIndex(), header_->field_ids_size_);
const dex::FieldId& field = dex_file_->GetFieldId(read_field.GetIndex());
if (UNLIKELY(field.class_idx_ != class_type_index)) {
ErrorStringPrintf("Mismatched defining class for class_data_item field");
return false;
}
if (!CheckClassDataItemField(read_field.GetIndex(),
read_field.GetAccessFlags(),
class_def.access_flags_,
class_type_index)) {
return false;
}
}
size_t num_direct_methods = accessor.NumDirectMethods();
size_t num_processed_methods = 0u;
auto methods = accessor.GetMethods();
auto it = methods.begin();
for (; it != methods.end(); ++it, ++num_processed_methods) {
uint32_t code_off = it->GetCodeItemOffset();
if (code_off != 0 && !CheckOffsetToTypeMap(code_off, DexFile::kDexTypeCodeItem)) {
return false;
}
// The index has already been checked in `CheckIntraClassDataItemMethods()`.
DCHECK_LE(it->GetIndex(), header_->method_ids_size_);
const dex::MethodId& method = dex_file_->GetMethodId(it->GetIndex());
if (UNLIKELY(method.class_idx_ != class_type_index)) {
ErrorStringPrintf("Mismatched defining class for class_data_item method");
return false;
}
bool expect_direct = num_processed_methods < num_direct_methods;
if (!CheckClassDataItemMethod(it->GetIndex(),
it->GetAccessFlags(),
class_def.access_flags_,
class_type_index,
it->GetCodeItemOffset(),
expect_direct)) {
return false;
}
}
// Check static field types against initial static values in encoded array.
if (!CheckStaticFieldTypes(class_def)) {
return false;
}
ptr_ = it.GetDataPointer();
return true;
}
bool DexFileVerifier::CheckInterAnnotationsDirectoryItem() {
const dex::AnnotationsDirectoryItem* item =
reinterpret_cast<const dex::AnnotationsDirectoryItem*>(ptr_);
uint32_t defining_class = FindFirstAnnotationsDirectoryDefiner(ptr_);
DCHECK(IsUint<16>(defining_class) || defining_class == kDexNoIndex) << defining_class;
if (item->class_annotations_off_ != 0 &&
!CheckOffsetToTypeMap(item->class_annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
return false;
}
// Field annotations follow immediately after the annotations directory.
const dex::FieldAnnotationsItem* field_item =
reinterpret_cast<const dex::FieldAnnotationsItem*>(item + 1);
uint32_t field_count = item->fields_size_;
for (uint32_t i = 0; i < field_count; i++) {
// The index has already been checked in `CheckIntraAnnotationsDirectoryItem()`.
DCHECK_LE(field_item->field_idx_, header_->field_ids_size_);
const dex::FieldId& field = dex_file_->GetFieldId(field_item->field_idx_);
if (UNLIKELY(field.class_idx_.index_ != defining_class)) {
ErrorStringPrintf("Mismatched defining class for field_annotation");
return false;
}
if (!CheckOffsetToTypeMap(field_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
return false;
}
field_item++;
}
// Method annotations follow immediately after field annotations.
const dex::MethodAnnotationsItem* method_item =
reinterpret_cast<const dex::MethodAnnotationsItem*>(field_item);
uint32_t method_count = item->methods_size_;
for (uint32_t i = 0; i < method_count; i++) {
// The index has already been checked in `CheckIntraAnnotationsDirectoryItem()`.
DCHECK_LE(method_item->method_idx_, header_->method_ids_size_);
const dex::MethodId& method = dex_file_->GetMethodId(method_item->method_idx_);
if (UNLIKELY(method.class_idx_.index_ != defining_class)) {
ErrorStringPrintf("Mismatched defining class for method_annotation");
return false;
}
if (!CheckOffsetToTypeMap(method_item->annotations_off_, DexFile::kDexTypeAnnotationSetItem)) {
return false;
}
method_item++;
}
// Parameter annotations follow immediately after method annotations.
const dex::ParameterAnnotationsItem* parameter_item =
reinterpret_cast<const dex::ParameterAnnotationsItem*>(method_item);
uint32_t parameter_count = item->parameters_size_;
for (uint32_t i = 0; i < parameter_count; i++) {
// The index has already been checked in `CheckIntraAnnotationsDirectoryItem()`.
DCHECK_LE(parameter_item->method_idx_, header_->method_ids_size_);
const dex::MethodId& parameter_method = dex_file_->GetMethodId(parameter_item->method_idx_);
if (UNLIKELY(parameter_method.class_idx_.index_ != defining_class)) {
ErrorStringPrintf("Mismatched defining class for parameter_annotation");
return false;
}
if (!CheckOffsetToTypeMap(parameter_item->annotations_off_,
DexFile::kDexTypeAnnotationSetRefList)) {
return false;
}
parameter_item++;
}
ptr_ = reinterpret_cast<const uint8_t*>(parameter_item);
return true;
}
bool DexFileVerifier::CheckInterSectionIterate(size_t offset,
uint32_t count,
DexFile::MapItemType type) {
// Get the right alignment mask for the type of section.
size_t alignment_mask;
switch (type) {
case DexFile::kDexTypeClassDataItem:
alignment_mask = sizeof(uint8_t) - 1;
break;
default:
alignment_mask = sizeof(uint32_t) - 1;
break;
}
// Iterate through the items in the section.
previous_item_ = nullptr;
for (uint32_t i = 0; i < count; i++) {
uint32_t new_offset = (offset + alignment_mask) & ~alignment_mask;
ptr_ = begin_ + new_offset;
const uint8_t* prev_ptr = ptr_;
if (MapTypeToBitMask(type) == 0) {
ErrorStringPrintf("Unknown map item type %x", type);
return false;
}
// Check depending on the section type.
switch (type) {
case DexFile::kDexTypeHeaderItem:
case DexFile::kDexTypeMethodHandleItem:
case DexFile::kDexTypeMapList:
case DexFile::kDexTypeTypeList:
case DexFile::kDexTypeCodeItem:
case DexFile::kDexTypeStringDataItem:
case DexFile::kDexTypeDebugInfoItem:
case DexFile::kDexTypeAnnotationItem:
case DexFile::kDexTypeEncodedArrayItem:
case DexFile::kDexTypeHiddenapiClassData:
break;
case DexFile::kDexTypeStringIdItem: {
if (!CheckInterStringIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeTypeIdItem: {
if (!CheckInterTypeIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeProtoIdItem: {
if (!CheckInterProtoIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeFieldIdItem: {
if (!CheckInterFieldIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeMethodIdItem: {
if (!CheckInterMethodIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeClassDefItem: {
// There shouldn't be more class definitions than type ids allow.
// This is checked in `CheckIntraClassDefItem()` by checking the type
// index against `kTypeIdLimit` and rejecting dulicate definitions.
DCHECK_LE(i, kTypeIdLimit);
if (!CheckInterClassDefItem()) {
return false;
}
break;
}
case DexFile::kDexTypeCallSiteIdItem: {
if (!CheckInterCallSiteIdItem()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationSetRefList: {
if (!CheckInterAnnotationSetRefList()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationSetItem: {
if (!CheckInterAnnotationSetItem()) {
return false;
}
break;
}
case DexFile::kDexTypeClassDataItem: {
// There shouldn't be more class data than type ids allow.
// This check should be redundant, since there are checks that the
// class_idx_ is within range and that there is only one definition
// for a given type id.
if (i > kTypeIdLimit) {
ErrorStringPrintf("Too many class data items");
return false;
}
if (!CheckInterClassDataItem()) {
return false;
}
break;
}
case DexFile::kDexTypeAnnotationsDirectoryItem: {
if (!CheckInterAnnotationsDirectoryItem()) {
return false;
}
break;
}
}
previous_item_ = prev_ptr;
offset = ptr_ - begin_;
}
return true;
}
bool DexFileVerifier::CheckInterSection() {
// Eagerly verify that `StringId` offsets map to string data items to make sure
// we can retrieve the string data for verifying other items (types, shorties, etc.).
// After this we can safely use `DexFile` helpers such as `GetFieldId()` or `GetMethodId()`
// but not `PrettyMethod()` or `PrettyField()` as descriptors have not been verified yet.
const dex::StringId* string_ids =
reinterpret_cast<const dex::StringId*>(begin_ + header_->string_ids_off_);
for (size_t i = 0, num_strings = header_->string_ids_size_; i != num_strings; ++i) {
if (!CheckOffsetToTypeMap(string_ids[i].string_data_off_, DexFile::kDexTypeStringDataItem)) {
return false;
}
}
const dex::MapList* map = reinterpret_cast<const dex::MapList*>(begin_ + header_->map_off_);
const dex::MapItem* item = map->list_;
uint32_t count = map->size_;
// Cross check the items listed in the map.
for (; count != 0u; --count) {
uint32_t section_offset = item->offset_;
uint32_t section_count = item->size_;
DexFile::MapItemType type = static_cast<DexFile::MapItemType>(item->type_);
bool found = false;
switch (type) {
case DexFile::kDexTypeHeaderItem:
case DexFile::kDexTypeMapList:
case DexFile::kDexTypeTypeList:
case DexFile::kDexTypeCodeItem:
case DexFile::kDexTypeStringDataItem:
case DexFile::kDexTypeDebugInfoItem:
case DexFile::kDexTypeAnnotationItem:
case DexFile::kDexTypeEncodedArrayItem:
found = true;
break;
case DexFile::kDexTypeStringIdItem:
case DexFile::kDexTypeTypeIdItem:
case DexFile::kDexTypeProtoIdItem:
case DexFile::kDexTypeFieldIdItem:
case DexFile::kDexTypeMethodIdItem:
case DexFile::kDexTypeClassDefItem:
case DexFile::kDexTypeCallSiteIdItem:
case DexFile::kDexTypeMethodHandleItem:
case DexFile::kDexTypeAnnotationSetRefList:
case DexFile::kDexTypeAnnotationSetItem:
case DexFile::kDexTypeClassDataItem:
case DexFile::kDexTypeAnnotationsDirectoryItem:
case DexFile::kDexTypeHiddenapiClassData: {
if (!CheckInterSectionIterate(section_offset, section_count, type)) {
return false;
}
found = true;
break;
}
}
if (!found) {
ErrorStringPrintf("Unknown map item type %x", item->type_);
return false;
}
item++;
}
return true;
}
bool DexFileVerifier::Verify() {
// Check the header.
if (!CheckHeader()) {
return false;
}
// Check the map section.
if (!CheckMap()) {
return false;
}
DCHECK_LE(header_->type_ids_size_, kTypeIdLimit + 1u); // Checked in CheckHeader().
verified_type_descriptors_.resize(header_->type_ids_size_, 0);
defined_class_indexes_.resize(header_->type_ids_size_);
// Check structure within remaining sections.
if (!CheckIntraSection()) {
return false;
}
// Check references from one section to another.
if (!CheckInterSection()) {
return false;
}
return true;
}
bool DexFileVerifier::CheckFieldAccessFlags(uint32_t idx,
uint32_t field_access_flags,
uint32_t class_access_flags,
std::string* error_msg) {
// Generally sort out >16-bit flags.
if ((field_access_flags & ~kAccJavaFlagsMask) != 0) {
*error_msg = StringPrintf("Bad field access_flags for %s: %x(%s)",
GetFieldDescription(begin_, header_, idx).c_str(),
field_access_flags,
PrettyJavaAccessFlags(field_access_flags).c_str());
return false;
}
// Flags allowed on fields, in general. Other lower-16-bit flags are to be ignored.
constexpr uint32_t kFieldAccessFlags = kAccPublic |
kAccPrivate |
kAccProtected |
kAccStatic |
kAccFinal |
kAccVolatile |
kAccTransient |
kAccSynthetic |
kAccEnum;
// Fields may have only one of public/protected/final.
if (!CheckAtMostOneOfPublicProtectedPrivate(field_access_flags)) {
*error_msg = StringPrintf("Field may have only one of public/protected/private, %s: %x(%s)",
GetFieldDescription(begin_, header_, idx).c_str(),
field_access_flags,
PrettyJavaAccessFlags(field_access_flags).c_str());
return false;
}
// Interfaces have a pretty restricted list.
if ((class_access_flags & kAccInterface) != 0) {
// Interface fields must be public final static.
constexpr uint32_t kPublicFinalStatic = kAccPublic | kAccFinal | kAccStatic;
if ((field_access_flags & kPublicFinalStatic) != kPublicFinalStatic) {
*error_msg = StringPrintf("Interface field is not public final static, %s: %x(%s)",
GetFieldDescription(begin_, header_, idx).c_str(),
field_access_flags,
PrettyJavaAccessFlags(field_access_flags).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
// Interface fields may be synthetic, but may not have other flags.
constexpr uint32_t kDisallowed = ~(kPublicFinalStatic | kAccSynthetic);
if ((field_access_flags & kFieldAccessFlags & kDisallowed) != 0) {
*error_msg = StringPrintf("Interface field has disallowed flag, %s: %x(%s)",
GetFieldDescription(begin_, header_, idx).c_str(),
field_access_flags,
PrettyJavaAccessFlags(field_access_flags).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
return true;
}
// Volatile fields may not be final.
constexpr uint32_t kVolatileFinal = kAccVolatile | kAccFinal;
if ((field_access_flags & kVolatileFinal) == kVolatileFinal) {
*error_msg = StringPrintf("Fields may not be volatile and final: %s",
GetFieldDescription(begin_, header_, idx).c_str());
return false;
}
return true;
}
void DexFileVerifier::FindStringRangesForMethodNames() {
// Use DexFile::StringId* as RandomAccessIterator.
const dex::StringId* first = reinterpret_cast<const dex::StringId*>(
begin_ + header_->string_ids_off_);
const dex::StringId* last = first + header_->string_ids_size_;
auto get_string = [begin = begin_](const dex::StringId& id) {
const uint8_t* str_data_ptr = begin + id.string_data_off_;
DecodeUnsignedLeb128(&str_data_ptr);
return reinterpret_cast<const char*>(str_data_ptr);
};
auto compare = [&get_string](const dex::StringId& lhs, const char* rhs) {
return CompareModifiedUtf8ToModifiedUtf8AsUtf16CodePointValues(get_string(lhs), rhs) < 0;
};
// '=' follows '<'
static_assert('<' + 1 == '=', "Unexpected character relation");
const auto angle_end = std::lower_bound(first, last, "=", compare);
init_indices_.angle_bracket_end_index = angle_end - first;
const auto angle_start = std::lower_bound(first, angle_end, "<", compare);
init_indices_.angle_bracket_start_index = angle_start - first;
if (angle_start == angle_end) {
// No strings starting with '<'.
init_indices_.angle_init_angle_index = std::numeric_limits<size_t>::max();
init_indices_.angle_clinit_angle_index = std::numeric_limits<size_t>::max();
return;
}
{
constexpr const char* kClinit = "<clinit>";
const auto it = std::lower_bound(angle_start, angle_end, kClinit, compare);
if (it != angle_end && strcmp(get_string(*it), kClinit) == 0) {
init_indices_.angle_clinit_angle_index = it - first;
} else {
init_indices_.angle_clinit_angle_index = std::numeric_limits<size_t>::max();
}
}
{
constexpr const char* kInit = "<init>";
const auto it = std::lower_bound(angle_start, angle_end, kInit, compare);
if (it != angle_end && strcmp(get_string(*it), kInit) == 0) {
init_indices_.angle_init_angle_index = it - first;
} else {
init_indices_.angle_init_angle_index = std::numeric_limits<size_t>::max();
}
}
}
bool DexFileVerifier::CheckMethodAccessFlags(uint32_t method_index,
uint32_t method_access_flags,
uint32_t class_access_flags,
uint32_t constructor_flags_by_name,
bool has_code,
bool expect_direct,
std::string* error_msg) {
// Generally sort out >16-bit flags, except dex knows Constructor and DeclaredSynchronized.
constexpr uint32_t kAllMethodFlags =
kAccJavaFlagsMask | kAccConstructor | kAccDeclaredSynchronized;
if ((method_access_flags & ~kAllMethodFlags) != 0) {
*error_msg = StringPrintf("Bad method access_flags for %s: %x",
GetMethodDescription(begin_, header_, method_index).c_str(),
method_access_flags);
return false;
}
// Flags allowed on fields, in general. Other lower-16-bit flags are to be ignored.
constexpr uint32_t kMethodAccessFlags = kAccPublic |
kAccPrivate |
kAccProtected |
kAccStatic |
kAccFinal |
kAccSynthetic |
kAccSynchronized |
kAccBridge |
kAccVarargs |
kAccNative |
kAccAbstract |
kAccStrict;
// Methods may have only one of public/protected/final.
if (!CheckAtMostOneOfPublicProtectedPrivate(method_access_flags)) {
*error_msg = StringPrintf("Method may have only one of public/protected/private, %s: %x",
GetMethodDescription(begin_, header_, method_index).c_str(),
method_access_flags);
return false;
}
constexpr uint32_t kConstructorFlags = kAccStatic | kAccConstructor;
const bool is_constructor_by_name = (constructor_flags_by_name & kConstructorFlags) != 0;
const bool is_clinit_by_name = constructor_flags_by_name == kConstructorFlags;
// Only methods named "<clinit>" or "<init>" may be marked constructor. Note: we cannot enforce
// the reverse for backwards compatibility reasons.
if (((method_access_flags & kAccConstructor) != 0) && !is_constructor_by_name) {
*error_msg =
StringPrintf("Method %" PRIu32 "(%s) is marked constructor, but doesn't match name",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
return false;
}
if (is_constructor_by_name) {
// Check that the static constructor (= static initializer) is named "<clinit>" and that the
// instance constructor is called "<init>".
bool is_static = (method_access_flags & kAccStatic) != 0;
if (is_static ^ is_clinit_by_name) {
*error_msg = StringPrintf("Constructor %" PRIu32 "(%s) is not flagged correctly wrt/ static.",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
}
// Check that static and private methods, as well as constructors, are in the direct methods list,
// and other methods in the virtual methods list.
bool is_direct = ((method_access_flags & (kAccStatic | kAccPrivate)) != 0) ||
is_constructor_by_name;
if (is_direct != expect_direct) {
*error_msg = StringPrintf("Direct/virtual method %" PRIu32 "(%s) not in expected list %d",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str(),
expect_direct);
return false;
}
// From here on out it is easier to mask out the bits we're supposed to ignore.
method_access_flags &= kMethodAccessFlags;
// Interfaces are special.
if ((class_access_flags & kAccInterface) != 0) {
// Non-static interface methods must be public or private.
uint32_t desired_flags = (kAccPublic | kAccStatic);
if (dex_file_->SupportsDefaultMethods()) {
desired_flags |= kAccPrivate;
}
if ((method_access_flags & desired_flags) == 0) {
*error_msg = StringPrintf("Interface virtual method %" PRIu32 "(%s) is not public",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
}
// If there aren't any instructions, make sure that's expected.
if (!has_code) {
// Only native or abstract methods may not have code.
if ((method_access_flags & (kAccNative | kAccAbstract)) == 0) {
*error_msg = StringPrintf("Method %" PRIu32 "(%s) has no code, but is not marked native or "
"abstract",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
return false;
}
// Constructors must always have code.
if (is_constructor_by_name) {
*error_msg = StringPrintf("Constructor %u(%s) must not be abstract or native",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
if ((method_access_flags & kAccAbstract) != 0) {
// Abstract methods are not allowed to have the following flags.
constexpr uint32_t kForbidden =
kAccPrivate | kAccStatic | kAccFinal | kAccNative | kAccStrict | kAccSynchronized;
if ((method_access_flags & kForbidden) != 0) {
*error_msg = StringPrintf("Abstract method %" PRIu32 "(%s) has disallowed access flags %x",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str(),
method_access_flags);
return false;
}
// Abstract methods should be in an abstract class or interface.
if ((class_access_flags & (kAccInterface | kAccAbstract)) == 0) {
LOG(WARNING) << "Method " << GetMethodDescription(begin_, header_, method_index)
<< " is abstract, but the declaring class is neither abstract nor an "
<< "interface in dex file "
<< dex_file_->GetLocation();
}
}
// Interfaces are special.
if ((class_access_flags & kAccInterface) != 0) {
// Interface methods without code must be abstract.
if ((method_access_flags & (kAccPublic | kAccAbstract)) != (kAccPublic | kAccAbstract)) {
*error_msg = StringPrintf("Interface method %" PRIu32 "(%s) is not public and abstract",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
if (dex_file_->SupportsDefaultMethods()) {
return false;
} else {
// Allow in older versions, but warn.
LOG(WARNING) << "This dex file is invalid and will be rejected in the future. Error is: "
<< *error_msg;
}
}
// At this point, we know the method is public and abstract. This means that all the checks
// for invalid combinations above applies. In addition, interface methods must not be
// protected. This is caught by the check for only-one-of-public-protected-private.
}
return true;
}
// When there's code, the method must not be native or abstract.
if ((method_access_flags & (kAccNative | kAccAbstract)) != 0) {
*error_msg = StringPrintf("Method %" PRIu32 "(%s) has code, but is marked native or abstract",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
return false;
}
// Instance constructors must not be synchronized and a few other flags.
if (constructor_flags_by_name == kAccConstructor) {
static constexpr uint32_t kInitAllowed =
kAccPrivate | kAccProtected | kAccPublic | kAccStrict | kAccVarargs | kAccSynthetic;
if ((method_access_flags & ~kInitAllowed) != 0) {
*error_msg = StringPrintf("Constructor %" PRIu32 "(%s) flagged inappropriately %x",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str(),
method_access_flags);
return false;
}
}
return true;
}
bool DexFileVerifier::CheckConstructorProperties(
uint32_t method_index,
uint32_t constructor_flags) {
DCHECK(constructor_flags == kAccConstructor ||
constructor_flags == (kAccConstructor | kAccStatic));
// Check signature matches expectations.
// The `method_index` has already been checked in `CheckIntraClassDataItemMethods()`.
CHECK_LT(method_index, header_->method_ids_size_);
const dex::MethodId& method_id = dex_file_->GetMethodId(method_index);
// The `method_id.proto_idx_` has already been checked in `CheckIntraMethodIdItem()`
DCHECK_LE(method_id.proto_idx_.index_, header_->proto_ids_size_);
Signature signature = dex_file_->GetMethodSignature(method_id);
if (constructor_flags == (kAccStatic | kAccConstructor)) {
if (!signature.IsVoid() || signature.GetNumberOfParameters() != 0) {
ErrorStringPrintf("<clinit> must have descriptor ()V");
return false;
}
} else if (!signature.IsVoid()) {
ErrorStringPrintf("Constructor %u(%s) must be void",
method_index,
GetMethodDescription(begin_, header_, method_index).c_str());
return false;
}
return true;
}
bool Verify(const DexFile* dex_file,
const uint8_t* begin,
size_t size,
const char* location,
bool verify_checksum,
std::string* error_msg) {
std::unique_ptr<DexFileVerifier> verifier(
new DexFileVerifier(dex_file, begin, size, location, verify_checksum));
if (!verifier->Verify()) {
*error_msg = verifier->FailureReason();
return false;
}
return true;
}
} // namespace dex
} // namespace art
|