1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952
|
/*
+----------------------------------------------------------------------+
| Zend Engine |
+----------------------------------------------------------------------+
| Copyright (c) Zend Technologies Ltd. (http://www.zend.com) |
+----------------------------------------------------------------------+
| This source file is subject to version 2.00 of the Zend license, |
| that is bundled with this package in the file LICENSE, and is |
| available through the world-wide-web at the following url: |
| http://www.zend.com/license/2_00.txt. |
| If you did not receive a copy of the Zend license and are unable to |
| obtain it through the world-wide-web, please send a note to |
| license@zend.com so we can mail you a copy immediately. |
+----------------------------------------------------------------------+
| Authors: Andi Gutmans <andi@php.net> |
| Zeev Suraski <zeev@php.net> |
+----------------------------------------------------------------------+
*/
#include "zend.h"
#include "zend_API.h"
#include "zend_compile.h"
#include "zend_execute.h"
#include "zend_inheritance.h"
#include "zend_interfaces.h"
#include "zend_smart_str.h"
#include "zend_operators.h"
#include "zend_exceptions.h"
#include "zend_enum.h"
#include "zend_attributes.h"
#include "zend_constants.h"
#include "zend_observer.h"
ZEND_API zend_class_entry* (*zend_inheritance_cache_get)(zend_class_entry *ce, zend_class_entry *parent, zend_class_entry **traits_and_interfaces) = NULL;
ZEND_API zend_class_entry* (*zend_inheritance_cache_add)(zend_class_entry *ce, zend_class_entry *proto, zend_class_entry *parent, zend_class_entry **traits_and_interfaces, HashTable *dependencies) = NULL;
/* Unresolved means that class declarations that are currently not available are needed to
* determine whether the inheritance is valid or not. At runtime UNRESOLVED should be treated
* as an ERROR. */
typedef zend_inheritance_status inheritance_status;
typedef enum {
PROP_INVARIANT,
PROP_COVARIANT,
PROP_CONTRAVARIANT,
} prop_variance;
static void add_dependency_obligation(zend_class_entry *ce, zend_class_entry *dependency_ce);
static void add_compatibility_obligation(
zend_class_entry *ce, const zend_function *child_fn, zend_class_entry *child_scope,
const zend_function *parent_fn, zend_class_entry *parent_scope);
static void add_property_compatibility_obligation(
zend_class_entry *ce, const zend_property_info *child_prop,
const zend_property_info *parent_prop, prop_variance variance);
static void add_class_constant_compatibility_obligation(
zend_class_entry *ce, const zend_class_constant *child_const,
const zend_class_constant *parent_const, const zend_string *const_name);
static void add_property_hook_obligation(
zend_class_entry *ce, const zend_property_info *hooked_prop, const zend_function *hook_func);
static void ZEND_COLD emit_incompatible_method_error(
const zend_function *child, zend_class_entry *child_scope,
const zend_function *parent, zend_class_entry *parent_scope,
inheritance_status status);
static void zend_type_copy_ctor(zend_type *const type, bool use_arena, bool persistent);
static void zend_type_list_copy_ctor(
zend_type *const parent_type,
bool use_arena,
bool persistent
) {
const zend_type_list *const old_list = ZEND_TYPE_LIST(*parent_type);
size_t size = ZEND_TYPE_LIST_SIZE(old_list->num_types);
zend_type_list *new_list = use_arena
? zend_arena_alloc(&CG(arena), size) : pemalloc(size, persistent);
memcpy(new_list, old_list, size);
ZEND_TYPE_SET_LIST(*parent_type, new_list);
if (use_arena) {
ZEND_TYPE_FULL_MASK(*parent_type) |= _ZEND_TYPE_ARENA_BIT;
}
zend_type *list_type;
ZEND_TYPE_LIST_FOREACH(new_list, list_type) {
zend_type_copy_ctor(list_type, use_arena, persistent);
} ZEND_TYPE_LIST_FOREACH_END();
}
static void zend_type_copy_ctor(zend_type *const type, bool use_arena, bool persistent) {
if (ZEND_TYPE_HAS_LIST(*type)) {
zend_type_list_copy_ctor(type, use_arena, persistent);
} else if (ZEND_TYPE_HAS_NAME(*type)) {
zend_string_addref(ZEND_TYPE_NAME(*type));
}
}
static zend_function *zend_duplicate_internal_function(zend_function *func, const zend_class_entry *ce) /* {{{ */
{
zend_function *new_function;
if (UNEXPECTED(ce->type & ZEND_INTERNAL_CLASS)) {
new_function = (zend_function *)pemalloc(sizeof(zend_internal_function), 1);
memcpy(new_function, func, sizeof(zend_internal_function));
} else {
new_function = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_function, func, sizeof(zend_internal_function));
new_function->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
}
if (EXPECTED(new_function->common.function_name)) {
zend_string_addref(new_function->common.function_name);
}
return new_function;
}
/* }}} */
static zend_always_inline zend_function *zend_duplicate_function(zend_function *func, const zend_class_entry *ce) /* {{{ */
{
if (UNEXPECTED(func->type == ZEND_INTERNAL_FUNCTION)) {
return zend_duplicate_internal_function(func, ce);
} else {
if (func->op_array.refcount) {
(*func->op_array.refcount)++;
}
if (EXPECTED(func->op_array.function_name)) {
zend_string_addref(func->op_array.function_name);
}
return func;
}
}
/* }}} */
static void do_inherit_parent_constructor(zend_class_entry *ce) /* {{{ */
{
zend_class_entry *parent = ce->parent;
ZEND_ASSERT(parent != NULL);
/* You cannot change create_object */
ce->create_object = parent->create_object;
/* Inherit special functions if needed */
if (EXPECTED(!ce->get_iterator)) {
ce->get_iterator = parent->get_iterator;
}
if (EXPECTED(!ce->__get)) {
ce->__get = parent->__get;
}
if (EXPECTED(!ce->__set)) {
ce->__set = parent->__set;
}
if (EXPECTED(!ce->__unset)) {
ce->__unset = parent->__unset;
}
if (EXPECTED(!ce->__isset)) {
ce->__isset = parent->__isset;
}
if (EXPECTED(!ce->__call)) {
ce->__call = parent->__call;
}
if (EXPECTED(!ce->__callstatic)) {
ce->__callstatic = parent->__callstatic;
}
if (EXPECTED(!ce->__tostring)) {
ce->__tostring = parent->__tostring;
}
if (EXPECTED(!ce->clone)) {
ce->clone = parent->clone;
}
if (EXPECTED(!ce->__serialize)) {
ce->__serialize = parent->__serialize;
}
if (EXPECTED(!ce->__unserialize)) {
ce->__unserialize = parent->__unserialize;
}
if (EXPECTED(!ce->serialize)) {
ce->serialize = parent->serialize;
}
if (EXPECTED(!ce->unserialize)) {
ce->unserialize = parent->unserialize;
}
if (!ce->destructor) {
ce->destructor = parent->destructor;
}
if (EXPECTED(!ce->__debugInfo)) {
ce->__debugInfo = parent->__debugInfo;
}
if (ce->constructor) {
if (parent->constructor && UNEXPECTED(parent->constructor->common.fn_flags & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_ERROR, "Cannot override final %s::%s() with %s::%s()",
ZSTR_VAL(parent->name), ZSTR_VAL(parent->constructor->common.function_name),
ZSTR_VAL(ce->name), ZSTR_VAL(ce->constructor->common.function_name));
}
return;
}
ce->constructor = parent->constructor;
}
/* }}} */
char *zend_visibility_string(uint32_t fn_flags) /* {{{ */
{
if (fn_flags & ZEND_ACC_PUBLIC) {
return "public";
} else if (fn_flags & ZEND_ACC_PRIVATE) {
return "private";
} else {
ZEND_ASSERT(fn_flags & ZEND_ACC_PROTECTED);
return "protected";
}
}
/* }}} */
static const char *zend_asymmetric_visibility_string(uint32_t fn_flags) /* {{{ */
{
if (fn_flags & ZEND_ACC_PRIVATE_SET) {
return "private(set)";
} else if (fn_flags & ZEND_ACC_PROTECTED_SET) {
return "protected(set)";
} else {
ZEND_ASSERT(!(fn_flags & ZEND_ACC_PUBLIC_SET));
return "omitted";
}
}
static zend_string *resolve_class_name(zend_class_entry *scope, zend_string *name) {
ZEND_ASSERT(scope);
if (zend_string_equals_literal_ci(name, "parent") && scope->parent) {
if (scope->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
return scope->parent->name;
} else {
return scope->parent_name;
}
} else if (zend_string_equals_literal_ci(name, "self")) {
return scope->name;
} else {
return name;
}
}
static bool class_visible(const zend_class_entry *ce) {
if (ce->type == ZEND_INTERNAL_CLASS) {
return !(CG(compiler_options) & ZEND_COMPILE_IGNORE_INTERNAL_CLASSES);
} else {
ZEND_ASSERT(ce->type == ZEND_USER_CLASS);
return !(CG(compiler_options) & ZEND_COMPILE_IGNORE_OTHER_FILES)
|| ce->info.user.filename == CG(compiled_filename);
}
}
static zend_always_inline void register_unresolved_class(zend_string *name) {
/* We'll autoload this class and process delayed variance obligations later. */
if (!CG(delayed_autoloads)) {
ALLOC_HASHTABLE(CG(delayed_autoloads));
zend_hash_init(CG(delayed_autoloads), 0, NULL, NULL, 0);
}
zend_hash_add_empty_element(CG(delayed_autoloads), name);
}
static zend_class_entry *lookup_class_ex(
zend_class_entry *scope, zend_string *name, bool register_unresolved) {
zend_class_entry *ce;
bool in_preload = CG(compiler_options) & ZEND_COMPILE_PRELOAD;
if (UNEXPECTED(!EG(active) && !in_preload)) {
zend_string *lc_name = zend_string_tolower(name);
ce = zend_hash_find_ptr(CG(class_table), lc_name);
zend_string_release(lc_name);
if (register_unresolved && !ce) {
zend_error_noreturn(
E_COMPILE_ERROR, "%s must be registered before %s",
ZSTR_VAL(name), ZSTR_VAL(scope->name));
}
return ce;
}
ce = zend_lookup_class_ex(
name, NULL, ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
if (!CG(in_compilation) || in_preload) {
if (ce) {
return ce;
}
if (register_unresolved) {
register_unresolved_class(name);
}
} else {
if (ce && class_visible(ce)) {
return ce;
}
/* The current class may not be registered yet, so check for it explicitly. */
if (zend_string_equals_ci(scope->name, name)) {
return scope;
}
}
return NULL;
}
static zend_class_entry *lookup_class(zend_class_entry *scope, zend_string *name) {
return lookup_class_ex(scope, name, /* register_unresolved */ false);
}
/* Instanceof that's safe to use on unlinked classes. */
static bool unlinked_instanceof(zend_class_entry *ce1, const zend_class_entry *ce2) {
if (ce1 == ce2) {
return 1;
}
if (ce1->ce_flags & ZEND_ACC_LINKED) {
return instanceof_function(ce1, ce2);
}
if (ce1->parent) {
zend_class_entry *parent_ce;
if (ce1->ce_flags & ZEND_ACC_RESOLVED_PARENT) {
parent_ce = ce1->parent;
} else {
parent_ce = zend_lookup_class_ex(ce1->parent_name, NULL,
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
}
/* It's not sufficient to only check the parent chain itself, as need to do a full
* recursive instanceof in case the parent interfaces haven't been copied yet. */
if (parent_ce && unlinked_instanceof(parent_ce, ce2)) {
return 1;
}
}
if (ce1->num_interfaces) {
uint32_t i;
if (ce1->ce_flags & ZEND_ACC_RESOLVED_INTERFACES) {
/* Unlike the normal instanceof_function(), we have to perform a recursive
* check here, as the parent interfaces might not have been fully copied yet. */
for (i = 0; i < ce1->num_interfaces; i++) {
if (unlinked_instanceof(ce1->interfaces[i], ce2)) {
return 1;
}
}
} else {
for (i = 0; i < ce1->num_interfaces; i++) {
zend_class_entry *ce = zend_lookup_class_ex(
ce1->interface_names[i].name, ce1->interface_names[i].lc_name,
ZEND_FETCH_CLASS_ALLOW_UNLINKED | ZEND_FETCH_CLASS_NO_AUTOLOAD);
/* Avoid recursing if class implements itself. */
if (ce && ce != ce1 && unlinked_instanceof(ce, ce2)) {
return 1;
}
}
}
}
return 0;
}
static bool zend_type_permits_self(
zend_type type, zend_class_entry *scope, zend_class_entry *self) {
if (ZEND_TYPE_FULL_MASK(type) & MAY_BE_OBJECT) {
return 1;
}
/* Any types that may satisfy self must have already been loaded at this point
* (as a parent or interface), so we never need to register delayed variance obligations
* for this case. */
zend_type *single_type;
ZEND_TYPE_FOREACH(type, single_type) {
if (ZEND_TYPE_HAS_NAME(*single_type)) {
zend_string *name = resolve_class_name(scope, ZEND_TYPE_NAME(*single_type));
zend_class_entry *ce = lookup_class(self, name);
if (ce && unlinked_instanceof(self, ce)) {
return 1;
}
}
} ZEND_TYPE_FOREACH_END();
return 0;
}
static void track_class_dependency(zend_class_entry *ce, zend_string *class_name)
{
HashTable *ht;
ZEND_ASSERT(class_name);
if (!CG(current_linking_class) || ce == CG(current_linking_class)) {
return;
} else if (zend_string_equals_literal_ci(class_name, "self")
|| zend_string_equals_literal_ci(class_name, "parent")) {
return;
}
#ifndef ZEND_WIN32
/* On non-Windows systems, internal classes are always the same,
* so there is no need to explicitly track them. */
if (ce->type == ZEND_INTERNAL_CLASS) {
return;
}
#endif
ht = (HashTable*)CG(current_linking_class)->inheritance_cache;
if (!(ce->ce_flags & ZEND_ACC_IMMUTABLE)) {
// TODO: dependency on not-immutable class ???
if (ht) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
CG(current_linking_class)->inheritance_cache = NULL;
}
CG(current_linking_class)->ce_flags &= ~ZEND_ACC_CACHEABLE;
CG(current_linking_class) = NULL;
return;
}
/* Record dependency */
if (!ht) {
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 0, NULL, NULL, 0);
CG(current_linking_class)->inheritance_cache = (zend_inheritance_cache_entry*)ht;
}
zend_hash_add_ptr(ht, class_name, ce);
}
/* Check whether any type in the fe_type intersection type is a subtype of the proto class. */
static inheritance_status zend_is_intersection_subtype_of_class(
zend_class_entry *fe_scope, zend_type fe_type,
zend_class_entry *proto_scope, zend_string *proto_class_name, zend_class_entry *proto_ce)
{
ZEND_ASSERT(ZEND_TYPE_IS_INTERSECTION(fe_type));
bool have_unresolved = false;
zend_type *single_type;
/* Traverse the list of child types and check that at least one is
* a subtype of the parent type being checked */
ZEND_TYPE_FOREACH(fe_type, single_type) {
zend_class_entry *fe_ce;
zend_string *fe_class_name = NULL;
if (ZEND_TYPE_HAS_NAME(*single_type)) {
fe_class_name =
resolve_class_name(fe_scope, ZEND_TYPE_NAME(*single_type));
if (zend_string_equals_ci(fe_class_name, proto_class_name)) {
return INHERITANCE_SUCCESS;
}
if (!proto_ce) proto_ce = lookup_class(proto_scope, proto_class_name);
fe_ce = lookup_class(fe_scope, fe_class_name);
} else {
/* standard type in an intersection type is impossible,
* because it would be a fatal compile error */
ZEND_UNREACHABLE();
continue;
}
if (!fe_ce || !proto_ce) {
have_unresolved = true;
continue;
}
if (unlinked_instanceof(fe_ce, proto_ce)) {
track_class_dependency(fe_ce, fe_class_name);
track_class_dependency(proto_ce, proto_class_name);
return INHERITANCE_SUCCESS;
}
} ZEND_TYPE_FOREACH_END();
return have_unresolved ? INHERITANCE_UNRESOLVED : INHERITANCE_ERROR;
}
/* Check whether a single class proto type is a subtype of a potentially complex fe_type. */
static inheritance_status zend_is_class_subtype_of_type(
zend_class_entry *fe_scope, zend_string *fe_class_name,
zend_class_entry *proto_scope, zend_type proto_type) {
zend_class_entry *fe_ce = NULL;
bool have_unresolved = 0;
/* If the parent has 'object' as a return type, any class satisfies the co-variant check */
if (ZEND_TYPE_FULL_MASK(proto_type) & MAY_BE_OBJECT) {
/* Currently, any class name would be allowed here. We still perform a class lookup
* for forward-compatibility reasons, as we may have named types in the future that
* are not classes (such as typedefs). */
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
if (!fe_ce) {
have_unresolved = 1;
} else {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
}
}
zend_type *single_type;
/* Traverse the list of parent types and check if the current child (FE)
* class is the subtype of at least one of them (union) or all of them (intersection). */
bool is_intersection = ZEND_TYPE_IS_INTERSECTION(proto_type);
ZEND_TYPE_FOREACH(proto_type, single_type) {
if (ZEND_TYPE_IS_INTERSECTION(*single_type)) {
inheritance_status subtype_status = zend_is_class_subtype_of_type(
fe_scope, fe_class_name, proto_scope, *single_type);
switch (subtype_status) {
case INHERITANCE_ERROR:
if (is_intersection) {
return INHERITANCE_ERROR;
}
continue;
case INHERITANCE_UNRESOLVED:
have_unresolved = 1;
continue;
case INHERITANCE_SUCCESS:
if (!is_intersection) {
return INHERITANCE_SUCCESS;
}
continue;
EMPTY_SWITCH_DEFAULT_CASE();
}
}
zend_class_entry *proto_ce;
zend_string *proto_class_name = NULL;
if (ZEND_TYPE_HAS_NAME(*single_type)) {
proto_class_name =
resolve_class_name(proto_scope, ZEND_TYPE_NAME(*single_type));
if (zend_string_equals_ci(fe_class_name, proto_class_name)) {
if (!is_intersection) {
return INHERITANCE_SUCCESS;
}
continue;
}
if (!fe_ce) fe_ce = lookup_class(fe_scope, fe_class_name);
proto_ce = lookup_class(proto_scope, proto_class_name);
} else {
/* standard type */
ZEND_ASSERT(!is_intersection);
continue;
}
if (!fe_ce || !proto_ce) {
have_unresolved = 1;
continue;
}
if (unlinked_instanceof(fe_ce, proto_ce)) {
track_class_dependency(fe_ce, fe_class_name);
track_class_dependency(proto_ce, proto_class_name);
if (!is_intersection) {
return INHERITANCE_SUCCESS;
}
} else {
if (is_intersection) {
return INHERITANCE_ERROR;
}
}
} ZEND_TYPE_FOREACH_END();
if (have_unresolved) {
return INHERITANCE_UNRESOLVED;
}
return is_intersection ? INHERITANCE_SUCCESS : INHERITANCE_ERROR;
}
static zend_string *get_class_from_type(zend_class_entry *scope, zend_type single_type) {
if (ZEND_TYPE_HAS_NAME(single_type)) {
return resolve_class_name(scope, ZEND_TYPE_NAME(single_type));
}
return NULL;
}
static void register_unresolved_classes(zend_class_entry *scope, zend_type type) {
zend_type *single_type;
ZEND_TYPE_FOREACH(type, single_type) {
if (ZEND_TYPE_HAS_LIST(*single_type)) {
register_unresolved_classes(scope, *single_type);
continue;
}
if (ZEND_TYPE_HAS_NAME(*single_type)) {
zend_string *class_name = resolve_class_name(scope, ZEND_TYPE_NAME(*single_type));
lookup_class_ex(scope, class_name, /* register_unresolved */ true);
}
} ZEND_TYPE_FOREACH_END();
}
static inheritance_status zend_is_intersection_subtype_of_type(
zend_class_entry *fe_scope, zend_type fe_type,
zend_class_entry *proto_scope, zend_type proto_type)
{
bool have_unresolved = false;
zend_type *single_type;
uint32_t proto_type_mask = ZEND_TYPE_PURE_MASK(proto_type);
/* Currently, for object type any class name would be allowed here.
* We still perform a class lookup for forward-compatibility reasons,
* as we may have named types in the future that are not classes
* (such as typedefs). */
if (proto_type_mask & MAY_BE_OBJECT) {
ZEND_TYPE_FOREACH(fe_type, single_type) {
zend_string *fe_class_name = get_class_from_type(fe_scope, *single_type);
if (!fe_class_name) {
continue;
}
zend_class_entry *fe_ce = lookup_class(fe_scope, fe_class_name);
if (fe_ce) {
track_class_dependency(fe_ce, fe_class_name);
return INHERITANCE_SUCCESS;
} else {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
}
/* U_1&...&U_n < V_1&...&V_m if forall V_j. exists U_i. U_i < V_j.
* U_1&...&U_n < V_1|...|V_m if exists V_j. exists U_i. U_i < V_j.
* As such, we need to iterate over proto_type (V_j) first and use a different
* quantifier depending on whether fe_type is a union or an intersection. */
inheritance_status early_exit_status =
ZEND_TYPE_IS_INTERSECTION(proto_type) ? INHERITANCE_ERROR : INHERITANCE_SUCCESS;
ZEND_TYPE_FOREACH(proto_type, single_type) {
inheritance_status status;
if (ZEND_TYPE_IS_INTERSECTION(*single_type)) {
status = zend_is_intersection_subtype_of_type(
fe_scope, fe_type, proto_scope, *single_type);
} else {
zend_string *proto_class_name = get_class_from_type(proto_scope, *single_type);
if (!proto_class_name) {
continue;
}
zend_class_entry *proto_ce = NULL;
status = zend_is_intersection_subtype_of_class(
fe_scope, fe_type, proto_scope, proto_class_name, proto_ce);
}
if (status == early_exit_status) {
return status;
}
if (status == INHERITANCE_UNRESOLVED) {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
if (have_unresolved) {
return INHERITANCE_UNRESOLVED;
}
return early_exit_status == INHERITANCE_ERROR ? INHERITANCE_SUCCESS : INHERITANCE_ERROR;
}
ZEND_API inheritance_status zend_perform_covariant_type_check(
zend_class_entry *fe_scope, zend_type fe_type,
zend_class_entry *proto_scope, zend_type proto_type)
{
ZEND_ASSERT(ZEND_TYPE_IS_SET(fe_type) && ZEND_TYPE_IS_SET(proto_type));
/* Apart from void, everything is trivially covariant to the mixed type.
* Handle this case separately to ensure it never requires class loading. */
if (ZEND_TYPE_PURE_MASK(proto_type) == MAY_BE_ANY &&
!ZEND_TYPE_CONTAINS_CODE(fe_type, IS_VOID)) {
return INHERITANCE_SUCCESS;
}
/* Builtin types may be removed, but not added */
uint32_t fe_type_mask = ZEND_TYPE_PURE_MASK(fe_type);
uint32_t proto_type_mask = ZEND_TYPE_PURE_MASK(proto_type);
uint32_t added_types = fe_type_mask & ~proto_type_mask;
if (added_types) {
if ((added_types & MAY_BE_STATIC)
&& zend_type_permits_self(proto_type, proto_scope, fe_scope)) {
/* Replacing type that accepts self with static is okay */
added_types &= ~MAY_BE_STATIC;
}
if (added_types == MAY_BE_NEVER) {
/* never is the bottom type */
return INHERITANCE_SUCCESS;
}
if (added_types) {
/* Otherwise adding new types is illegal */
return INHERITANCE_ERROR;
}
}
zend_type *single_type;
inheritance_status early_exit_status;
bool have_unresolved = false;
if (ZEND_TYPE_IS_INTERSECTION(fe_type)) {
early_exit_status =
ZEND_TYPE_IS_INTERSECTION(proto_type) ? INHERITANCE_ERROR : INHERITANCE_SUCCESS;
inheritance_status status = zend_is_intersection_subtype_of_type(
fe_scope, fe_type, proto_scope, proto_type);
if (status == early_exit_status) {
return status;
}
if (status == INHERITANCE_UNRESOLVED) {
have_unresolved = true;
}
} else {
/* U_1|...|U_n < V_1|...|V_m if forall U_i. exists V_j. U_i < V_j.
* U_1|...|U_n < V_1&...&V_m if forall U_i. forall V_j. U_i < V_j.
* We need to iterate over fe_type (U_i) first and the logic is independent of
* whether proto_type is a union or intersection (only the inner check differs). */
early_exit_status = INHERITANCE_ERROR;
ZEND_TYPE_FOREACH(fe_type, single_type) {
inheritance_status status;
/* Union has an intersection type as it's member */
if (ZEND_TYPE_IS_INTERSECTION(*single_type)) {
status = zend_is_intersection_subtype_of_type(
fe_scope, *single_type, proto_scope, proto_type);
} else {
zend_string *fe_class_name = get_class_from_type(fe_scope, *single_type);
if (!fe_class_name) {
continue;
}
status = zend_is_class_subtype_of_type(
fe_scope, fe_class_name, proto_scope, proto_type);
}
if (status == early_exit_status) {
return status;
}
if (status == INHERITANCE_UNRESOLVED) {
have_unresolved = true;
}
} ZEND_TYPE_FOREACH_END();
}
if (!have_unresolved) {
return early_exit_status == INHERITANCE_ERROR ? INHERITANCE_SUCCESS : INHERITANCE_ERROR;
}
register_unresolved_classes(fe_scope, fe_type);
register_unresolved_classes(proto_scope, proto_type);
return INHERITANCE_UNRESOLVED;
}
static inheritance_status zend_do_perform_arg_type_hint_check(
zend_class_entry *fe_scope, zend_arg_info *fe_arg_info,
zend_class_entry *proto_scope, zend_arg_info *proto_arg_info) /* {{{ */
{
if (!ZEND_TYPE_IS_SET(fe_arg_info->type) || ZEND_TYPE_PURE_MASK(fe_arg_info->type) == MAY_BE_ANY) {
/* Child with no type or mixed type is always compatible */
return INHERITANCE_SUCCESS;
}
if (!ZEND_TYPE_IS_SET(proto_arg_info->type)) {
/* Child defines a type, but parent doesn't, violates LSP */
return INHERITANCE_ERROR;
}
/* Contravariant type check is performed as a covariant type check with swapped
* argument order. */
return zend_perform_covariant_type_check(
proto_scope, proto_arg_info->type, fe_scope, fe_arg_info->type);
}
/* }}} */
/* For trait methods, fe_scope/proto_scope may differ from fe/proto->common.scope,
* as self will refer to the self of the class the trait is used in, not the trait
* the method was declared in. */
static inheritance_status zend_do_perform_implementation_check(
const zend_function *fe, zend_class_entry *fe_scope,
const zend_function *proto, zend_class_entry *proto_scope) /* {{{ */
{
uint32_t i, num_args, proto_num_args, fe_num_args;
inheritance_status status, local_status;
bool proto_is_variadic, fe_is_variadic;
/* Checks for constructors only if they are declared in an interface,
* or explicitly marked as abstract
*/
ZEND_ASSERT(!((fe->common.fn_flags & ZEND_ACC_CTOR)
&& ((proto->common.scope->ce_flags & ZEND_ACC_INTERFACE) == 0
&& (proto->common.fn_flags & ZEND_ACC_ABSTRACT) == 0)));
/* If the prototype method is private and not abstract, we do not enforce a signature.
* private abstract methods can only occur in traits. */
ZEND_ASSERT(!(proto->common.fn_flags & ZEND_ACC_PRIVATE)
|| (proto->common.fn_flags & ZEND_ACC_ABSTRACT));
/* The number of required arguments cannot increase. */
if (proto->common.required_num_args < fe->common.required_num_args) {
return INHERITANCE_ERROR;
}
/* by-ref constraints on return values are covariant */
if ((proto->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)
&& !(fe->common.fn_flags & ZEND_ACC_RETURN_REFERENCE)) {
return INHERITANCE_ERROR;
}
proto_is_variadic = (proto->common.fn_flags & ZEND_ACC_VARIADIC) != 0;
fe_is_variadic = (fe->common.fn_flags & ZEND_ACC_VARIADIC) != 0;
/* A variadic function cannot become non-variadic */
if (proto_is_variadic && !fe_is_variadic) {
return INHERITANCE_ERROR;
}
/* The variadic argument is not included in the stored argument count. */
proto_num_args = proto->common.num_args + proto_is_variadic;
fe_num_args = fe->common.num_args + fe_is_variadic;
num_args = MAX(proto_num_args, fe_num_args);
status = INHERITANCE_SUCCESS;
for (i = 0; i < num_args; i++) {
zend_arg_info *proto_arg_info =
i < proto_num_args ? &proto->common.arg_info[i] :
proto_is_variadic ? &proto->common.arg_info[proto_num_args - 1] : NULL;
zend_arg_info *fe_arg_info =
i < fe_num_args ? &fe->common.arg_info[i] :
fe_is_variadic ? &fe->common.arg_info[fe_num_args - 1] : NULL;
if (!proto_arg_info) {
/* A new (optional) argument has been added, which is fine. */
continue;
}
if (!fe_arg_info) {
/* An argument has been removed. This is considered illegal, because arity checks
* work based on a model where passing more than the declared number of parameters
* to a function is an error. */
return INHERITANCE_ERROR;
}
local_status = zend_do_perform_arg_type_hint_check(
fe_scope, fe_arg_info, proto_scope, proto_arg_info);
if (UNEXPECTED(local_status != INHERITANCE_SUCCESS)) {
if (UNEXPECTED(local_status == INHERITANCE_ERROR)) {
return INHERITANCE_ERROR;
}
ZEND_ASSERT(local_status == INHERITANCE_UNRESOLVED);
status = INHERITANCE_UNRESOLVED;
}
/* by-ref constraints on arguments are invariant */
if (ZEND_ARG_SEND_MODE(fe_arg_info) != ZEND_ARG_SEND_MODE(proto_arg_info)) {
return INHERITANCE_ERROR;
}
}
/* Check return type compatibility, but only if the prototype already specifies
* a return type. Adding a new return type is always valid. */
if (proto->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
/* Removing a return type is not valid, unless the parent return type is tentative. */
if (!(fe->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE)) {
if (!ZEND_ARG_TYPE_IS_TENTATIVE(&proto->common.arg_info[-1])) {
return INHERITANCE_ERROR;
}
if (status == INHERITANCE_SUCCESS) {
return INHERITANCE_WARNING;
}
return status;
}
local_status = zend_perform_covariant_type_check(
fe_scope, fe->common.arg_info[-1].type, proto_scope, proto->common.arg_info[-1].type);
if (UNEXPECTED(local_status != INHERITANCE_SUCCESS)) {
if (local_status == INHERITANCE_ERROR
&& ZEND_ARG_TYPE_IS_TENTATIVE(&proto->common.arg_info[-1])) {
local_status = INHERITANCE_WARNING;
}
return local_status;
}
}
return status;
}
/* }}} */
static ZEND_COLD void zend_append_type_hint(
smart_str *str, zend_class_entry *scope, const zend_arg_info *arg_info, bool return_hint) /* {{{ */
{
if (ZEND_TYPE_IS_SET(arg_info->type)) {
zend_string *type_str = zend_type_to_string_resolved(arg_info->type, scope);
smart_str_append(str, type_str);
zend_string_release(type_str);
if (!return_hint) {
smart_str_appendc(str, ' ');
}
}
}
/* }}} */
static ZEND_COLD zend_string *zend_get_function_declaration(
const zend_function *fptr, zend_class_entry *scope) /* {{{ */
{
smart_str str = {0};
if (fptr->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE) {
smart_str_appends(&str, "& ");
}
if (fptr->common.scope) {
if (fptr->common.scope->ce_flags & ZEND_ACC_ANON_CLASS) {
/* cut off on NULL byte ... class@anonymous */
smart_str_appends(&str, ZSTR_VAL(fptr->common.scope->name));
} else {
smart_str_appendl(&str, ZSTR_VAL(fptr->common.scope->name), ZSTR_LEN(fptr->common.scope->name));
}
smart_str_appends(&str, "::");
}
smart_str_append(&str, fptr->common.function_name);
smart_str_appendc(&str, '(');
if (fptr->common.arg_info) {
uint32_t i, num_args, required;
zend_arg_info *arg_info = fptr->common.arg_info;
required = fptr->common.required_num_args;
num_args = fptr->common.num_args;
if (fptr->common.fn_flags & ZEND_ACC_VARIADIC) {
num_args++;
}
for (i = 0; i < num_args;) {
zend_append_type_hint(&str, scope, arg_info, 0);
if (ZEND_ARG_SEND_MODE(arg_info)) {
smart_str_appendc(&str, '&');
}
if (ZEND_ARG_IS_VARIADIC(arg_info)) {
smart_str_appends(&str, "...");
}
smart_str_appendc(&str, '$');
if (fptr->type == ZEND_INTERNAL_FUNCTION) {
smart_str_appends(&str, ((zend_internal_arg_info*)arg_info)->name);
} else {
smart_str_appendl(&str, ZSTR_VAL(arg_info->name), ZSTR_LEN(arg_info->name));
}
if (i >= required && !ZEND_ARG_IS_VARIADIC(arg_info)) {
smart_str_appends(&str, " = ");
if (fptr->type == ZEND_INTERNAL_FUNCTION) {
if (((zend_internal_arg_info*)arg_info)->default_value) {
smart_str_appends(&str, ((zend_internal_arg_info*)arg_info)->default_value);
} else {
smart_str_appends(&str, "<default>");
}
} else {
zend_op *precv = NULL;
{
uint32_t idx = i;
zend_op *op = fptr->op_array.opcodes;
zend_op *end = op + fptr->op_array.last;
++idx;
while (op < end) {
if ((op->opcode == ZEND_RECV || op->opcode == ZEND_RECV_INIT)
&& op->op1.num == (zend_ulong)idx)
{
precv = op;
}
++op;
}
}
if (precv && precv->opcode == ZEND_RECV_INIT && precv->op2_type != IS_UNUSED) {
zval *zv = RT_CONSTANT(precv, precv->op2);
if (Z_TYPE_P(zv) == IS_FALSE) {
smart_str_appends(&str, "false");
} else if (Z_TYPE_P(zv) == IS_TRUE) {
smart_str_appends(&str, "true");
} else if (Z_TYPE_P(zv) == IS_NULL) {
smart_str_appends(&str, "null");
} else if (Z_TYPE_P(zv) == IS_STRING) {
smart_str_appendc(&str, '\'');
smart_str_appendl(&str, Z_STRVAL_P(zv), MIN(Z_STRLEN_P(zv), 10));
if (Z_STRLEN_P(zv) > 10) {
smart_str_appends(&str, "...");
}
smart_str_appendc(&str, '\'');
} else if (Z_TYPE_P(zv) == IS_ARRAY) {
if (zend_hash_num_elements(Z_ARRVAL_P(zv)) == 0) {
smart_str_appends(&str, "[]");
} else {
smart_str_appends(&str, "[...]");
}
} else if (Z_TYPE_P(zv) == IS_CONSTANT_AST) {
zend_ast *ast = Z_ASTVAL_P(zv);
if (ast->kind == ZEND_AST_CONSTANT) {
smart_str_append(&str, zend_ast_get_constant_name(ast));
} else if (ast->kind == ZEND_AST_CLASS_CONST) {
smart_str_append(&str, zend_ast_get_str(ast->child[0]));
smart_str_appends(&str, "::");
smart_str_append(&str, zend_ast_get_str(ast->child[1]));
} else {
smart_str_appends(&str, "<expression>");
}
} else {
zend_string *tmp_zv_str;
zend_string *zv_str = zval_get_tmp_string(zv, &tmp_zv_str);
smart_str_append(&str, zv_str);
zend_tmp_string_release(tmp_zv_str);
}
}
}
}
if (++i < num_args) {
smart_str_appends(&str, ", ");
}
arg_info++;
}
}
smart_str_appendc(&str, ')');
if (fptr->common.fn_flags & ZEND_ACC_HAS_RETURN_TYPE) {
smart_str_appends(&str, ": ");
zend_append_type_hint(&str, scope, fptr->common.arg_info - 1, 1);
}
smart_str_0(&str);
return str.s;
}
/* }}} */
static zend_always_inline zend_string *func_filename(const zend_function *fn) {
return fn->common.type == ZEND_USER_FUNCTION ? fn->op_array.filename : NULL;
}
static zend_always_inline uint32_t func_lineno(const zend_function *fn) {
return fn->common.type == ZEND_USER_FUNCTION ? fn->op_array.line_start : 0;
}
static void ZEND_COLD emit_incompatible_method_error(
const zend_function *child, zend_class_entry *child_scope,
const zend_function *parent, zend_class_entry *parent_scope,
inheritance_status status) {
zend_string *parent_prototype = zend_get_function_declaration(parent, parent_scope);
zend_string *child_prototype = zend_get_function_declaration(child, child_scope);
if (status == INHERITANCE_UNRESOLVED) {
// TODO Improve error message if first unresolved class is present in child and parent?
/* Fetch the first unresolved class from registered autoloads */
const zend_string *unresolved_class = NULL;
ZEND_HASH_MAP_FOREACH_STR_KEY(CG(delayed_autoloads), unresolved_class) {
break;
} ZEND_HASH_FOREACH_END();
ZEND_ASSERT(unresolved_class);
zend_error_at(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Could not check compatibility between %s and %s, because class %s is not available",
ZSTR_VAL(child_prototype), ZSTR_VAL(parent_prototype), ZSTR_VAL(unresolved_class));
} else if (status == INHERITANCE_WARNING) {
const zend_attribute *return_type_will_change_attribute = zend_get_attribute_str(
child->common.attributes,
"returntypewillchange",
sizeof("returntypewillchange")-1
);
if (!return_type_will_change_attribute) {
zend_error_at(E_DEPRECATED, func_filename(child), func_lineno(child),
"Return type of %s should either be compatible with %s, "
"or the #[\\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice",
ZSTR_VAL(child_prototype), ZSTR_VAL(parent_prototype));
if (EG(exception)) {
zend_exception_uncaught_error(
"During inheritance of %s", ZSTR_VAL(parent_scope->name));
}
}
} else {
zend_error_at(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Declaration of %s must be compatible with %s",
ZSTR_VAL(child_prototype), ZSTR_VAL(parent_prototype));
}
zend_string_efree(child_prototype);
zend_string_efree(parent_prototype);
}
static void perform_delayable_implementation_check(
zend_class_entry *ce,
const zend_function *fe, zend_class_entry *fe_scope,
const zend_function *proto, zend_class_entry *proto_scope)
{
inheritance_status status =
zend_do_perform_implementation_check(fe, fe_scope, proto, proto_scope);
if (UNEXPECTED(status != INHERITANCE_SUCCESS)) {
if (EXPECTED(status == INHERITANCE_UNRESOLVED)) {
add_compatibility_obligation(ce, fe, fe_scope, proto, proto_scope);
} else {
ZEND_ASSERT(status == INHERITANCE_ERROR || status == INHERITANCE_WARNING);
emit_incompatible_method_error(fe, fe_scope, proto, proto_scope, status);
}
}
}
#define ZEND_INHERITANCE_LAZY_CHILD_CLONE (1<<0)
#define ZEND_INHERITANCE_CHECK_SILENT (1<<1) /* don't throw errors */
#define ZEND_INHERITANCE_CHECK_PROTO (1<<2) /* check method prototype (it might be already checked before) */
#define ZEND_INHERITANCE_CHECK_VISIBILITY (1<<3)
#define ZEND_INHERITANCE_SET_CHILD_CHANGED (1<<4)
#define ZEND_INHERITANCE_SET_CHILD_PROTO (1<<5)
#define ZEND_INHERITANCE_RESET_CHILD_OVERRIDE (1<<6)
static inheritance_status do_inheritance_check_on_method(
zend_function *child, zend_class_entry *child_scope,
zend_function *parent, zend_class_entry *parent_scope,
zend_class_entry *ce, zval *child_zv, uint32_t flags) /* {{{ */
{
uint32_t child_flags;
uint32_t parent_flags = parent->common.fn_flags;
zend_function *proto;
#define SEPARATE_METHOD() do { \
if ((flags & ZEND_INHERITANCE_LAZY_CHILD_CLONE) \
&& child_scope != ce && child->type == ZEND_USER_FUNCTION) { \
/* op_array wasn't duplicated yet */ \
zend_function *new_function = zend_arena_alloc(&CG(arena), sizeof(zend_op_array)); \
memcpy(new_function, child, sizeof(zend_op_array)); \
Z_PTR_P(child_zv) = child = new_function; \
flags &= ~ZEND_INHERITANCE_LAZY_CHILD_CLONE; \
} \
} while(0)
if (UNEXPECTED((parent_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_ABSTRACT|ZEND_ACC_CTOR)) == ZEND_ACC_PRIVATE)) {
if (flags & ZEND_INHERITANCE_SET_CHILD_CHANGED) {
SEPARATE_METHOD();
child->common.fn_flags |= ZEND_ACC_CHANGED;
}
/* The parent method is private and not an abstract so we don't need to check any inheritance rules */
return INHERITANCE_SUCCESS;
}
if ((flags & ZEND_INHERITANCE_CHECK_PROTO) && UNEXPECTED(parent_flags & ZEND_ACC_FINAL)) {
if (flags & ZEND_INHERITANCE_CHECK_SILENT) {
return INHERITANCE_ERROR;
}
zend_error_at_noreturn(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Cannot override final method %s::%s()",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name));
}
child_flags = child->common.fn_flags;
/* You cannot change from static to non static and vice versa.
*/
if ((flags & ZEND_INHERITANCE_CHECK_PROTO)
&& UNEXPECTED((child_flags & ZEND_ACC_STATIC) != (parent_flags & ZEND_ACC_STATIC))) {
if (flags & ZEND_INHERITANCE_CHECK_SILENT) {
return INHERITANCE_ERROR;
}
if (child_flags & ZEND_ACC_STATIC) {
zend_error_at_noreturn(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Cannot make non static method %s::%s() static in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
} else {
zend_error_at_noreturn(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Cannot make static method %s::%s() non static in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
}
}
/* Disallow making an inherited method abstract. */
if ((flags & ZEND_INHERITANCE_CHECK_PROTO)
&& UNEXPECTED((child_flags & ZEND_ACC_ABSTRACT) > (parent_flags & ZEND_ACC_ABSTRACT))) {
if (flags & ZEND_INHERITANCE_CHECK_SILENT) {
return INHERITANCE_ERROR;
}
zend_error_at_noreturn(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Cannot make non abstract method %s::%s() abstract in class %s",
ZEND_FN_SCOPE_NAME(parent), ZSTR_VAL(child->common.function_name), ZEND_FN_SCOPE_NAME(child));
}
if ((flags & ZEND_INHERITANCE_SET_CHILD_CHANGED)
&& (parent_flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED))) {
SEPARATE_METHOD();
child->common.fn_flags |= ZEND_ACC_CHANGED;
}
proto = parent->common.prototype ?
parent->common.prototype : parent;
if (parent_flags & ZEND_ACC_CTOR) {
/* ctors only have a prototype if is abstract (or comes from an interface) */
/* and if that is the case, we want to check inheritance against it */
if (!(proto->common.fn_flags & ZEND_ACC_ABSTRACT)) {
return INHERITANCE_SUCCESS;
}
parent = proto;
}
if ((flags & ZEND_INHERITANCE_SET_CHILD_PROTO)
&& child->common.prototype != proto) {
SEPARATE_METHOD();
child->common.prototype = proto;
}
/* Prevent derived classes from restricting access that was available in parent classes (except deriving from non-abstract ctors) */
if ((flags & ZEND_INHERITANCE_CHECK_VISIBILITY)
&& (child_flags & ZEND_ACC_PPP_MASK) > (parent_flags & ZEND_ACC_PPP_MASK)) {
if (flags & ZEND_INHERITANCE_CHECK_SILENT) {
return INHERITANCE_ERROR;
}
zend_error_at_noreturn(E_COMPILE_ERROR, func_filename(child), func_lineno(child),
"Access level to %s::%s() must be %s (as in class %s)%s",
ZEND_FN_SCOPE_NAME(child), ZSTR_VAL(child->common.function_name), zend_visibility_string(parent_flags), ZEND_FN_SCOPE_NAME(parent), (parent_flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
}
if (flags & ZEND_INHERITANCE_CHECK_PROTO) {
if (flags & ZEND_INHERITANCE_CHECK_SILENT) {
return zend_do_perform_implementation_check(child, child_scope, parent, parent_scope);
}
perform_delayable_implementation_check(ce, child, child_scope, parent, parent_scope);
}
if ((flags & ZEND_INHERITANCE_RESET_CHILD_OVERRIDE)
&& (child->common.fn_flags & ZEND_ACC_OVERRIDE)) {
SEPARATE_METHOD();
child->common.fn_flags &= ~ZEND_ACC_OVERRIDE;
}
#undef SEPARATE_METHOD
return INHERITANCE_SUCCESS;
}
/* }}} */
static void do_inherit_method(zend_string *key, zend_function *parent, zend_class_entry *ce, bool is_interface, uint32_t flags) /* {{{ */
{
zval *child = zend_hash_find_known_hash(&ce->function_table, key);
if (child) {
zend_function *func = (zend_function*)Z_PTR_P(child);
if (is_interface && UNEXPECTED(func == parent)) {
/* The same method in interface may be inherited few times */
return;
}
do_inheritance_check_on_method(
func, func->common.scope, parent, parent->common.scope, ce, child, flags);
} else {
if (is_interface || (parent->common.fn_flags & (ZEND_ACC_ABSTRACT))) {
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}
parent = zend_duplicate_function(parent, ce);
if (!is_interface) {
_zend_hash_append_ptr(&ce->function_table, key, parent);
} else {
zend_hash_add_new_ptr(&ce->function_table, key, parent);
}
}
}
/* }}} */
static inheritance_status full_property_types_compatible(
const zend_property_info *parent_info, const zend_property_info *child_info,
prop_variance variance) {
if (ZEND_TYPE_PURE_MASK(parent_info->type) == ZEND_TYPE_PURE_MASK(child_info->type)
&& ZEND_TYPE_NAME(parent_info->type) == ZEND_TYPE_NAME(child_info->type)) {
return INHERITANCE_SUCCESS;
}
if (ZEND_TYPE_IS_SET(parent_info->type) != ZEND_TYPE_IS_SET(child_info->type)) {
return INHERITANCE_ERROR;
}
/* Perform a covariant type check in both directions to determined invariance. */
inheritance_status status1 = variance == PROP_CONTRAVARIANT ? INHERITANCE_SUCCESS :
zend_perform_covariant_type_check(
child_info->ce, child_info->type, parent_info->ce, parent_info->type);
inheritance_status status2 = variance == PROP_COVARIANT ? INHERITANCE_SUCCESS :
zend_perform_covariant_type_check(
parent_info->ce, parent_info->type, child_info->ce, child_info->type);
if (status1 == INHERITANCE_SUCCESS && status2 == INHERITANCE_SUCCESS) {
return INHERITANCE_SUCCESS;
}
if (status1 == INHERITANCE_ERROR || status2 == INHERITANCE_ERROR) {
return INHERITANCE_ERROR;
}
ZEND_ASSERT(status1 == INHERITANCE_UNRESOLVED || status2 == INHERITANCE_UNRESOLVED);
return INHERITANCE_UNRESOLVED;
}
static ZEND_COLD void emit_incompatible_property_error(
const zend_property_info *child, const zend_property_info *parent, prop_variance variance) {
zend_string *type_str = zend_type_to_string_resolved(parent->type, parent->ce);
zend_error_noreturn(E_COMPILE_ERROR,
"Type of %s::$%s must be %s%s (as in class %s)",
ZSTR_VAL(child->ce->name),
zend_get_unmangled_property_name(child->name),
variance == PROP_INVARIANT ? "" :
variance == PROP_COVARIANT ? "subtype of " : "supertype of ",
ZSTR_VAL(type_str),
ZSTR_VAL(parent->ce->name));
}
static ZEND_COLD void emit_set_hook_type_error(const zend_property_info *child, const zend_property_info *parent)
{
zend_type set_type = parent->hooks[ZEND_PROPERTY_HOOK_SET]->common.arg_info[0].type;
zend_string *type_str = zend_type_to_string_resolved(set_type, parent->ce);
zend_error_noreturn(E_COMPILE_ERROR,
"Set type of %s::$%s must be supertype of %s (as in %s %s)",
ZSTR_VAL(child->ce->name),
zend_get_unmangled_property_name(child->name),
ZSTR_VAL(type_str),
zend_get_object_type_case(parent->ce, false),
ZSTR_VAL(parent->ce->name));
}
static inheritance_status verify_property_type_compatibility(
const zend_property_info *parent_info,
const zend_property_info *child_info,
prop_variance variance,
bool throw_on_error,
bool throw_on_unresolved
) {
inheritance_status result = full_property_types_compatible(parent_info, child_info, variance);
if ((result == INHERITANCE_ERROR && throw_on_error) || (result == INHERITANCE_UNRESOLVED && throw_on_unresolved)) {
emit_incompatible_property_error(child_info, parent_info, variance);
}
if (result != INHERITANCE_SUCCESS) {
return result;
}
if (parent_info->flags & ZEND_ACC_ABSTRACT) {
ZEND_ASSERT(parent_info->hooks);
if (parent_info->hooks[ZEND_PROPERTY_HOOK_SET]
&& (!child_info->hooks || !child_info->hooks[ZEND_PROPERTY_HOOK_SET])) {
zend_type set_type = parent_info->hooks[ZEND_PROPERTY_HOOK_SET]->common.arg_info[0].type;
inheritance_status result = zend_perform_covariant_type_check(
parent_info->ce, set_type, child_info->ce, child_info->type);
if ((result == INHERITANCE_ERROR && throw_on_error) || (result == INHERITANCE_UNRESOLVED && throw_on_unresolved)) {
emit_set_hook_type_error(child_info, parent_info);
}
}
}
return INHERITANCE_SUCCESS;
}
static bool property_has_operation(zend_property_info *prop_info, zend_property_hook_kind kind)
{
return (!(prop_info->flags & ZEND_ACC_VIRTUAL)
&& (kind == ZEND_PROPERTY_HOOK_GET || !(prop_info->flags & ZEND_ACC_READONLY)))
|| (prop_info->hooks && prop_info->hooks[kind]);
}
static void inherit_property_hook(
zend_class_entry *ce,
zend_property_info *parent_info,
zend_property_info *child_info,
zend_property_hook_kind kind
) {
zend_function *parent = parent_info->hooks ? parent_info->hooks[kind] : NULL;
zend_function *child = child_info->hooks ? child_info->hooks[kind] : NULL;
if (child
&& (child->common.fn_flags & ZEND_ACC_OVERRIDE)
&& property_has_operation(parent_info, kind)) {
child->common.fn_flags &= ~ZEND_ACC_OVERRIDE;
}
if (!parent) {
return;
}
if (!child) {
if (parent->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* Backed properties are considered to always implement get, and set when they are not readonly. */
if (property_has_operation(child_info, kind)) {
return;
}
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}
if (!child_info->hooks) {
ce->num_hooked_props++;
child_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
memset(child_info->hooks, 0, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
}
child_info->hooks[kind] = zend_duplicate_function(parent, ce);
return;
}
child->common.prototype = parent->common.prototype ? parent->common.prototype : parent;
uint32_t parent_flags = parent->common.fn_flags;
if (parent_flags & ZEND_ACC_PRIVATE) {
child->common.fn_flags |= ZEND_ACC_CHANGED;
return;
}
if (parent_flags & ZEND_ACC_FINAL) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot override final property hook %s::%s()",
ZSTR_VAL(parent->common.scope->name),
ZSTR_VAL(parent->common.function_name));
}
do_inheritance_check_on_method(
child, child->common.scope, parent, parent->common.scope, ce, /* child */ NULL,
ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_CHECK_VISIBILITY
| ZEND_INHERITANCE_SET_CHILD_CHANGED | ZEND_INHERITANCE_SET_CHILD_PROTO
| ZEND_INHERITANCE_RESET_CHILD_OVERRIDE);
/* Other signature compatibility issues should already be covered either by the
* properties being compatible (types), or certain signatures being forbidden by the
* compiler (variadic and by-ref args, etc). */
}
static prop_variance prop_get_variance(const zend_property_info *prop_info) {
bool unbacked = prop_info->flags & ZEND_ACC_VIRTUAL;
if (unbacked && prop_info->hooks) {
if (!prop_info->hooks[ZEND_PROPERTY_HOOK_SET]) {
return PROP_COVARIANT;
}
if (!prop_info->hooks[ZEND_PROPERTY_HOOK_GET]) {
return PROP_CONTRAVARIANT;
}
}
return PROP_INVARIANT;
}
static void do_inherit_property(zend_property_info *parent_info, zend_string *key, zend_class_entry *ce) /* {{{ */
{
zval *child = zend_hash_find_known_hash(&ce->properties_info, key);
zend_property_info *child_info;
if (UNEXPECTED(child)) {
child_info = Z_PTR_P(child);
if (parent_info->flags & (ZEND_ACC_PRIVATE|ZEND_ACC_CHANGED)) {
child_info->flags |= ZEND_ACC_CHANGED;
}
if (parent_info->flags & ZEND_ACC_FINAL) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot override final property %s::$%s",
ZSTR_VAL(parent_info->ce->name), ZSTR_VAL(key));
}
if (!(parent_info->flags & ZEND_ACC_PRIVATE)) {
if (!(parent_info->ce->ce_flags & ZEND_ACC_INTERFACE)) {
child_info->prototype = parent_info->prototype;
}
if (UNEXPECTED((parent_info->flags & ZEND_ACC_STATIC) != (child_info->flags & ZEND_ACC_STATIC))) {
zend_error_noreturn(E_COMPILE_ERROR, "Cannot redeclare %s%s::$%s as %s%s::$%s",
(parent_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", ZSTR_VAL(parent_info->ce->name), ZSTR_VAL(key),
(child_info->flags & ZEND_ACC_STATIC) ? "static " : "non static ", ZSTR_VAL(ce->name), ZSTR_VAL(key));
}
if (UNEXPECTED((child_info->flags & ZEND_ACC_READONLY) != (parent_info->flags & ZEND_ACC_READONLY))) {
if (!(parent_info->flags & ZEND_ACC_ABSTRACT)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot redeclare %s property %s::$%s as %s %s::$%s",
parent_info->flags & ZEND_ACC_READONLY ? "readonly" : "non-readonly",
ZSTR_VAL(parent_info->ce->name), ZSTR_VAL(key),
child_info->flags & ZEND_ACC_READONLY ? "readonly" : "non-readonly",
ZSTR_VAL(ce->name), ZSTR_VAL(key));
}
}
if (UNEXPECTED((child_info->flags & ZEND_ACC_PPP_SET_MASK))
/* Get-only virtual properties have no set visibility, so any child visibility is fine. */
&& !(parent_info->hooks && (parent_info->flags & ZEND_ACC_VIRTUAL) && !parent_info->hooks[ZEND_PROPERTY_HOOK_SET])) {
uint32_t parent_set_visibility = parent_info->flags & ZEND_ACC_PPP_SET_MASK;
/* Adding set protection is fine if it's the same or weaker than
* the parents full property visibility. */
if (!parent_set_visibility) {
parent_set_visibility = zend_visibility_to_set_visibility(parent_info->flags & ZEND_ACC_PPP_MASK);
}
uint32_t child_set_visibility = child_info->flags & ZEND_ACC_PPP_SET_MASK;
if (child_set_visibility > parent_set_visibility) {
zend_error_noreturn(
E_COMPILE_ERROR,
"Set access level of %s::$%s must be %s (as in class %s)%s",
ZSTR_VAL(ce->name), ZSTR_VAL(key),
zend_asymmetric_visibility_string(parent_info->flags), ZSTR_VAL(parent_info->ce->name),
!(parent_info->flags & ZEND_ACC_PPP_SET_MASK) ? "" : " or weaker");
}
}
if (UNEXPECTED((child_info->flags & ZEND_ACC_PPP_MASK) > (parent_info->flags & ZEND_ACC_PPP_MASK))) {
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::$%s must be %s (as in class %s)%s", ZSTR_VAL(ce->name), ZSTR_VAL(key), zend_visibility_string(parent_info->flags), ZSTR_VAL(parent_info->ce->name), (parent_info->flags&ZEND_ACC_PUBLIC) ? "" : " or weaker");
}
if (!(child_info->flags & ZEND_ACC_STATIC) && !(parent_info->flags & ZEND_ACC_VIRTUAL)) {
/* If we added hooks to the child property, we use the child's slot for
* storage to keep the parent slot set to IS_UNDEF. This automatically
* picks the slow path in the JIT. */
bool use_child_prop = !parent_info->hooks && child_info->hooks;
if (use_child_prop && child_info->offset == ZEND_VIRTUAL_PROPERTY_OFFSET) {
child_info->offset = OBJ_PROP_TO_OFFSET(ce->default_properties_count);
ce->default_properties_count++;
ce->default_properties_table = perealloc(ce->default_properties_table, sizeof(zval) * ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
zval *property_default_ptr = &ce->default_properties_table[OBJ_PROP_TO_NUM(child_info->offset)];
ZVAL_UNDEF(property_default_ptr);
Z_PROP_FLAG_P(property_default_ptr) = IS_PROP_UNINIT;
}
int parent_num = OBJ_PROP_TO_NUM(parent_info->offset);
if (child_info->offset != ZEND_VIRTUAL_PROPERTY_OFFSET) {
/* Don't keep default properties in GC (they may be freed by opcache) */
zval_ptr_dtor_nogc(&(ce->default_properties_table[parent_num]));
if (use_child_prop) {
ZVAL_UNDEF(&ce->default_properties_table[parent_num]);
} else {
int child_num = OBJ_PROP_TO_NUM(child_info->offset);
ce->default_properties_table[parent_num] = ce->default_properties_table[child_num];
ZVAL_UNDEF(&ce->default_properties_table[child_num]);
}
} else {
/* Default value was removed in child, remove it from parent too. */
if (ZEND_TYPE_IS_SET(child_info->type)) {
ZVAL_UNDEF(&ce->default_properties_table[parent_num]);
} else {
ZVAL_NULL(&ce->default_properties_table[parent_num]);
}
}
if (!use_child_prop) {
child_info->offset = parent_info->offset;
}
child_info->flags &= ~ZEND_ACC_VIRTUAL;
}
if (parent_info->hooks || child_info->hooks) {
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
inherit_property_hook(ce, parent_info, child_info, i);
}
}
prop_variance variance = prop_get_variance(parent_info);
if (ZEND_TYPE_IS_SET(parent_info->type)) {
inheritance_status status = verify_property_type_compatibility(
parent_info, child_info, variance, true, false);
if (status == INHERITANCE_UNRESOLVED) {
add_property_compatibility_obligation(ce, child_info, parent_info, variance);
}
} else if (UNEXPECTED(ZEND_TYPE_IS_SET(child_info->type) && !ZEND_TYPE_IS_SET(parent_info->type))) {
zend_error_noreturn(E_COMPILE_ERROR,
"Type of %s::$%s must not be defined (as in class %s)",
ZSTR_VAL(ce->name),
ZSTR_VAL(key),
ZSTR_VAL(parent_info->ce->name));
}
}
} else {
zend_function **hooks = parent_info->hooks;
if (hooks) {
ce->num_hooked_props++;
if (parent_info->flags & ZEND_ACC_ABSTRACT) {
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}
}
_zend_hash_append_ptr(&ce->properties_info, key, parent_info);
}
}
/* }}} */
static inline void do_implement_interface(zend_class_entry *ce, zend_class_entry *iface) /* {{{ */
{
if (!(ce->ce_flags & ZEND_ACC_INTERFACE) && iface->interface_gets_implemented && iface->interface_gets_implemented(iface, ce) == FAILURE) {
zend_error_noreturn(E_CORE_ERROR, "%s %s could not implement interface %s", zend_get_object_type_uc(ce), ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
}
/* This should be prevented by the class lookup logic. */
ZEND_ASSERT(ce != iface);
}
/* }}} */
static void zend_do_inherit_interfaces(zend_class_entry *ce, const zend_class_entry *iface) /* {{{ */
{
/* expects interface to be contained in ce's interface list already */
uint32_t i, ce_num, if_num = iface->num_interfaces;
zend_class_entry *entry;
ce_num = ce->num_interfaces;
if (ce->type == ZEND_INTERNAL_CLASS) {
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
} else {
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (ce_num + if_num));
}
/* Inherit the interfaces, only if they're not already inherited by the class */
while (if_num--) {
entry = iface->interfaces[if_num];
for (i = 0; i < ce_num; i++) {
if (ce->interfaces[i] == entry) {
break;
}
}
if (i == ce_num) {
ce->interfaces[ce->num_interfaces++] = entry;
}
}
ce->ce_flags |= ZEND_ACC_RESOLVED_INTERFACES;
/* and now call the implementing handlers */
while (ce_num < ce->num_interfaces) {
do_implement_interface(ce, ce->interfaces[ce_num++]);
}
}
/* }}} */
static void emit_incompatible_class_constant_error(
const zend_class_constant *child, const zend_class_constant *parent, const zend_string *const_name) {
zend_string *type_str = zend_type_to_string_resolved(parent->type, parent->ce);
zend_error_noreturn(E_COMPILE_ERROR,
"Type of %s::%s must be compatible with %s::%s of type %s",
ZSTR_VAL(child->ce->name),
ZSTR_VAL(const_name),
ZSTR_VAL(parent->ce->name),
ZSTR_VAL(const_name),
ZSTR_VAL(type_str));
}
static inheritance_status class_constant_types_compatible(const zend_class_constant *parent, const zend_class_constant *child)
{
ZEND_ASSERT(ZEND_TYPE_IS_SET(parent->type));
if (!ZEND_TYPE_IS_SET(child->type)) {
return INHERITANCE_ERROR;
}
return zend_perform_covariant_type_check(child->ce, child->type, parent->ce, parent->type);
}
static bool do_inherit_constant_check(
zend_class_entry *ce, zend_class_constant *parent_constant, zend_string *name);
static void do_inherit_class_constant(zend_string *name, zend_class_constant *parent_const, zend_class_entry *ce) /* {{{ */
{
zval *zv = zend_hash_find_known_hash(&ce->constants_table, name);
zend_class_constant *c;
if (zv != NULL) {
c = (zend_class_constant*)Z_PTR_P(zv);
bool inherit = do_inherit_constant_check(ce, parent_const, name);
ZEND_ASSERT(!inherit);
} else if (!(ZEND_CLASS_CONST_FLAGS(parent_const) & ZEND_ACC_PRIVATE)) {
if (Z_TYPE(parent_const->value) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
if (ce->parent->ce_flags & ZEND_ACC_IMMUTABLE) {
c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
memcpy(c, parent_const, sizeof(zend_class_constant));
parent_const = c;
Z_CONSTANT_FLAGS(c->value) |= CONST_OWNED;
}
}
if (ce->type & ZEND_INTERNAL_CLASS) {
c = pemalloc(sizeof(zend_class_constant), 1);
memcpy(c, parent_const, sizeof(zend_class_constant));
parent_const = c;
}
_zend_hash_append_ptr(&ce->constants_table, name, parent_const);
}
}
/* }}} */
void zend_build_properties_info_table(zend_class_entry *ce)
{
zend_property_info **table, *prop;
size_t size;
if (ce->default_properties_count == 0) {
return;
}
ZEND_ASSERT(ce->properties_info_table == NULL);
size = sizeof(zend_property_info *) * ce->default_properties_count;
if (ce->type == ZEND_USER_CLASS) {
ce->properties_info_table = table = zend_arena_alloc(&CG(arena), size);
} else {
ce->properties_info_table = table = pemalloc(size, 1);
}
/* Dead slots may be left behind during inheritance. Make sure these are NULLed out. */
memset(table, 0, size);
if (ce->parent && ce->parent->default_properties_count != 0) {
zend_property_info **parent_table = ce->parent->properties_info_table;
memcpy(
table, parent_table,
sizeof(zend_property_info *) * ce->parent->default_properties_count
);
/* Child did not add any new properties, we are done */
if (ce->default_properties_count == ce->parent->default_properties_count) {
return;
}
}
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) {
if (prop->ce == ce && (prop->flags & ZEND_ACC_STATIC) == 0
&& !(prop->flags & ZEND_ACC_VIRTUAL)) {
uint32_t prop_table_offset = OBJ_PROP_TO_NUM(!(prop->prototype->flags & ZEND_ACC_VIRTUAL) ? prop->prototype->offset : prop->offset);
table[prop_table_offset] = prop;
}
} ZEND_HASH_FOREACH_END();
}
ZEND_API void zend_verify_hooked_property(zend_class_entry *ce, zend_property_info *prop_info, zend_string *prop_name)
{
if (!prop_info->hooks) {
return;
}
bool abstract_error = prop_info->flags & ZEND_ACC_ABSTRACT;
/* We specified a default value (otherwise offset would be -1), but the virtual flag wasn't
* removed during inheritance. */
if ((prop_info->flags & ZEND_ACC_VIRTUAL) && prop_info->offset != ZEND_VIRTUAL_PROPERTY_OFFSET) {
if (Z_TYPE(ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]) == IS_UNDEF) {
prop_info->offset = ZEND_VIRTUAL_PROPERTY_OFFSET;
} else {
zend_error_noreturn(E_COMPILE_ERROR,
"Cannot specify default value for virtual hooked property %s::$%s", ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
}
}
/* If the property turns backed during inheritance and no type and default value are set, we want
* the default value to be null. */
if (!(prop_info->flags & ZEND_ACC_VIRTUAL)
&& !ZEND_TYPE_IS_SET(prop_info->type)
&& Z_TYPE(ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]) == IS_UNDEF) {
ZVAL_NULL(&ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)]);
}
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
zend_function *func = prop_info->hooks[i];
if (func) {
if ((zend_property_hook_kind)i == ZEND_PROPERTY_HOOK_GET
&& (func->op_array.fn_flags & ZEND_ACC_RETURN_REFERENCE)
&& !(prop_info->flags & ZEND_ACC_VIRTUAL)
&& prop_info->hooks[ZEND_PROPERTY_HOOK_SET]) {
zend_error_noreturn(E_COMPILE_ERROR, "Get hook of backed property %s::%s with set hook may not return by reference",
ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
}
if (func->common.fn_flags & ZEND_ACC_ABSTRACT) {
abstract_error = false;
}
}
}
if (abstract_error) {
zend_error_noreturn(E_COMPILE_ERROR,
"Abstract property %s::$%s must specify at least one abstract hook", ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
}
if ((prop_info->flags & ZEND_ACC_VIRTUAL)
&& (prop_info->flags & ZEND_ACC_PPP_SET_MASK)
&& (!prop_info->hooks[ZEND_PROPERTY_HOOK_GET] || !prop_info->hooks[ZEND_PROPERTY_HOOK_SET])) {
const char *prefix = !prop_info->hooks[ZEND_PROPERTY_HOOK_GET]
? "Write-only" : "Read-only";
zend_error_noreturn(E_COMPILE_ERROR,
"%s virtual property %s::$%s must not specify asymmetric visibility",
prefix, ZSTR_VAL(ce->name), ZSTR_VAL(prop_name));
}
}
ZEND_API ZEND_COLD ZEND_NORETURN void zend_hooked_property_variance_error_ex(zend_string *value_param_name, zend_string *class_name, zend_string *prop_name)
{
zend_error_noreturn(E_COMPILE_ERROR, "Type of parameter $%s of hook %s::$%s::set must be compatible with property type",
ZSTR_VAL(value_param_name), ZSTR_VAL(class_name), zend_get_unmangled_property_name(prop_name));
}
ZEND_API ZEND_COLD ZEND_NORETURN void zend_hooked_property_variance_error(const zend_property_info *prop_info)
{
zend_string *value_param_name = prop_info->hooks[ZEND_PROPERTY_HOOK_SET]->op_array.arg_info[0].name;
zend_hooked_property_variance_error_ex(value_param_name, prop_info->ce->name, prop_info->name);
}
ZEND_API inheritance_status zend_verify_property_hook_variance(const zend_property_info *prop_info, const zend_function *func)
{
ZEND_ASSERT(prop_info->hooks && prop_info->hooks[ZEND_PROPERTY_HOOK_SET] == func);
zend_arg_info *value_arg_info = &func->op_array.arg_info[0];
if (!ZEND_TYPE_IS_SET(value_arg_info->type)) {
return INHERITANCE_SUCCESS;
}
if (!ZEND_TYPE_IS_SET(prop_info->type)) {
return INHERITANCE_ERROR;
}
zend_class_entry *ce = prop_info->ce;
return zend_perform_covariant_type_check(ce, prop_info->type, ce, value_arg_info->type);
}
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
/* Hooked properties set get_iterator, which causes issues on for shm
* reattachment. Avoid early-binding on Windows and set get_iterator during
* inheritance. The linked class may not use inheritance cache. */
static void zend_link_hooked_object_iter(zend_class_entry *ce) {
if (!ce->get_iterator && ce->num_hooked_props) {
ce->get_iterator = zend_hooked_object_get_iterator;
ce->ce_flags &= ~ZEND_ACC_CACHEABLE;
if (CG(current_linking_class) == ce) {
# if ZEND_DEBUG
/* This check is executed before inheriting any elements that can
* track dependencies. */
HashTable *ht = (HashTable*)ce->inheritance_cache;
ZEND_ASSERT(!ht);
# endif
CG(current_linking_class) = NULL;
}
}
}
#endif
ZEND_API void zend_do_inheritance_ex(zend_class_entry *ce, zend_class_entry *parent_ce, bool checked) /* {{{ */
{
zend_property_info *property_info;
zend_function *func;
zend_string *key;
if (UNEXPECTED(ce->ce_flags & ZEND_ACC_INTERFACE)) {
/* Interface can only inherit other interfaces */
if (UNEXPECTED(!(parent_ce->ce_flags & ZEND_ACC_INTERFACE))) {
zend_error_noreturn(E_COMPILE_ERROR, "Interface %s cannot extend class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
}
} else if (UNEXPECTED(parent_ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_FINAL))) {
/* Class must not extend a final class */
if (parent_ce->ce_flags & ZEND_ACC_FINAL) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend final class %s", ZSTR_VAL(ce->name), ZSTR_VAL(parent_ce->name));
}
/* Class declaration must not extend traits or interfaces */
if ((parent_ce->ce_flags & ZEND_ACC_INTERFACE) || (parent_ce->ce_flags & ZEND_ACC_TRAIT)) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot extend %s %s",
ZSTR_VAL(ce->name), parent_ce->ce_flags & ZEND_ACC_INTERFACE ? "interface" : "trait", ZSTR_VAL(parent_ce->name)
);
}
}
if (UNEXPECTED((ce->ce_flags & ZEND_ACC_READONLY_CLASS) != (parent_ce->ce_flags & ZEND_ACC_READONLY_CLASS))) {
zend_error_noreturn(E_COMPILE_ERROR, "%s class %s cannot extend %s class %s",
ce->ce_flags & ZEND_ACC_READONLY_CLASS ? "Readonly" : "Non-readonly", ZSTR_VAL(ce->name),
parent_ce->ce_flags & ZEND_ACC_READONLY_CLASS ? "readonly" : "non-readonly", ZSTR_VAL(parent_ce->name)
);
}
if (ce->parent_name) {
zend_string_release_ex(ce->parent_name, 0);
}
ce->parent = parent_ce;
ce->default_object_handlers = parent_ce->default_object_handlers;
ce->ce_flags |= ZEND_ACC_RESOLVED_PARENT;
/* Inherit properties */
if (parent_ce->default_properties_count) {
zval *src, *dst, *end;
if (ce->default_properties_count) {
zval *table = pemalloc(sizeof(zval) * (ce->default_properties_count + parent_ce->default_properties_count), ce->type == ZEND_INTERNAL_CLASS);
src = ce->default_properties_table + ce->default_properties_count;
end = table + parent_ce->default_properties_count;
dst = end + ce->default_properties_count;
ce->default_properties_table = table;
do {
dst--;
src--;
ZVAL_COPY_VALUE_PROP(dst, src);
} while (dst != end);
pefree(src, ce->type == ZEND_INTERNAL_CLASS);
end = ce->default_properties_table;
} else {
end = pemalloc(sizeof(zval) * parent_ce->default_properties_count, ce->type == ZEND_INTERNAL_CLASS);
dst = end + parent_ce->default_properties_count;
ce->default_properties_table = end;
}
src = parent_ce->default_properties_table + parent_ce->default_properties_count;
if (UNEXPECTED(parent_ce->type != ce->type)) {
/* User class extends internal */
do {
dst--;
src--;
/* We don't have to account for refcounting because
* zend_declare_typed_property() disallows refcounted defaults for internal classes. */
ZEND_ASSERT(!Z_REFCOUNTED_P(src));
ZVAL_COPY_VALUE_PROP(dst, src);
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
}
continue;
} while (dst != end);
} else {
do {
dst--;
src--;
ZVAL_COPY_PROP(dst, src);
if (Z_OPT_TYPE_P(dst) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_PROPERTIES;
}
continue;
} while (dst != end);
}
ce->default_properties_count += parent_ce->default_properties_count;
}
if (parent_ce->default_static_members_count) {
zval *src, *dst, *end;
if (ce->default_static_members_count) {
zval *table = pemalloc(sizeof(zval) * (ce->default_static_members_count + parent_ce->default_static_members_count), ce->type == ZEND_INTERNAL_CLASS);
src = ce->default_static_members_table + ce->default_static_members_count;
end = table + parent_ce->default_static_members_count;
dst = end + ce->default_static_members_count;
ce->default_static_members_table = table;
do {
dst--;
src--;
ZVAL_COPY_VALUE(dst, src);
} while (dst != end);
pefree(src, ce->type == ZEND_INTERNAL_CLASS);
end = ce->default_static_members_table;
} else {
end = pemalloc(sizeof(zval) * parent_ce->default_static_members_count, ce->type == ZEND_INTERNAL_CLASS);
dst = end + parent_ce->default_static_members_count;
ce->default_static_members_table = end;
}
src = parent_ce->default_static_members_table + parent_ce->default_static_members_count;
do {
dst--;
src--;
if (Z_TYPE_P(src) == IS_INDIRECT) {
ZVAL_INDIRECT(dst, Z_INDIRECT_P(src));
} else {
ZVAL_INDIRECT(dst, src);
}
if (Z_TYPE_P(Z_INDIRECT_P(dst)) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_STATICS;
}
} while (dst != end);
ce->default_static_members_count += parent_ce->default_static_members_count;
if (!ZEND_MAP_PTR(ce->static_members_table)) {
if (ce->type == ZEND_INTERNAL_CLASS &&
ce->info.internal.module->type == MODULE_PERSISTENT) {
ZEND_MAP_PTR_NEW(ce->static_members_table);
}
}
}
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, property_info) {
if (property_info->ce == ce) {
if (property_info->flags & ZEND_ACC_STATIC) {
property_info->offset += parent_ce->default_static_members_count;
} else if (property_info->offset != ZEND_VIRTUAL_PROPERTY_OFFSET) {
property_info->offset += parent_ce->default_properties_count * sizeof(zval);
}
}
} ZEND_HASH_FOREACH_END();
if (zend_hash_num_elements(&parent_ce->properties_info)) {
zend_hash_extend(&ce->properties_info,
zend_hash_num_elements(&ce->properties_info) +
zend_hash_num_elements(&parent_ce->properties_info), 0);
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->properties_info, key, property_info) {
do_inherit_property(property_info, key, ce);
} ZEND_HASH_FOREACH_END();
}
if (ce->num_hooked_props) {
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&ce->properties_info, key, property_info) {
if (property_info->ce == ce && property_info->hooks) {
zend_verify_hooked_property(ce, property_info, key);
}
} ZEND_HASH_FOREACH_END();
}
if (zend_hash_num_elements(&parent_ce->constants_table)) {
zend_class_constant *c;
zend_hash_extend(&ce->constants_table,
zend_hash_num_elements(&ce->constants_table) +
zend_hash_num_elements(&parent_ce->constants_table), 0);
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->constants_table, key, c) {
do_inherit_class_constant(key, c, ce);
} ZEND_HASH_FOREACH_END();
}
if (zend_hash_num_elements(&parent_ce->function_table)) {
zend_hash_extend(&ce->function_table,
zend_hash_num_elements(&ce->function_table) +
zend_hash_num_elements(&parent_ce->function_table), 0);
uint32_t flags =
ZEND_INHERITANCE_LAZY_CHILD_CLONE |
ZEND_INHERITANCE_SET_CHILD_CHANGED |
ZEND_INHERITANCE_SET_CHILD_PROTO |
ZEND_INHERITANCE_RESET_CHILD_OVERRIDE;
if (!checked) {
flags |= ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_CHECK_VISIBILITY;
}
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->function_table, key, func) {
do_inherit_method(key, func, ce, 0, flags);
} ZEND_HASH_FOREACH_END();
}
do_inherit_parent_constructor(ce);
if (ce->type == ZEND_INTERNAL_CLASS) {
if (parent_ce->num_interfaces) {
zend_do_inherit_interfaces(ce, parent_ce);
}
if (ce->ce_flags & ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
ce->ce_flags |= ZEND_ACC_EXPLICIT_ABSTRACT_CLASS;
}
}
ce->ce_flags |= parent_ce->ce_flags & (ZEND_HAS_STATIC_IN_METHODS | ZEND_ACC_HAS_TYPE_HINTS | ZEND_ACC_HAS_READONLY_PROPS | ZEND_ACC_USE_GUARDS | ZEND_ACC_NOT_SERIALIZABLE | ZEND_ACC_ALLOW_DYNAMIC_PROPERTIES);
}
/* }}} */
static zend_always_inline bool check_trait_property_or_constant_value_compatibility(zend_class_entry *ce, zval *op1, zval *op2) /* {{{ */
{
bool is_compatible;
zval op1_tmp, op2_tmp;
/* if any of the values is a constant, we try to resolve it */
if (UNEXPECTED(Z_TYPE_P(op1) == IS_CONSTANT_AST)) {
ZVAL_COPY_OR_DUP(&op1_tmp, op1);
if (UNEXPECTED(zval_update_constant_ex(&op1_tmp, ce) != SUCCESS)) {
zval_ptr_dtor(&op1_tmp);
return false;
}
op1 = &op1_tmp;
}
if (UNEXPECTED(Z_TYPE_P(op2) == IS_CONSTANT_AST)) {
ZVAL_COPY_OR_DUP(&op2_tmp, op2);
if (UNEXPECTED(zval_update_constant_ex(&op2_tmp, ce) != SUCCESS)) {
zval_ptr_dtor(&op2_tmp);
return false;
}
op2 = &op2_tmp;
}
is_compatible = fast_is_identical_function(op1, op2);
if (op1 == &op1_tmp) {
zval_ptr_dtor_nogc(&op1_tmp);
}
if (op2 == &op2_tmp) {
zval_ptr_dtor_nogc(&op2_tmp);
}
return is_compatible;
}
/* }}} */
/** @return bool Returns true if the class constant should be inherited, i.e. whether it doesn't already exist. */
static bool do_inherit_constant_check(
zend_class_entry *ce, zend_class_constant *parent_constant, zend_string *name
) {
zval *zv = zend_hash_find_known_hash(&ce->constants_table, name);
if (zv == NULL) {
return true;
}
zend_class_constant *child_constant = Z_PTR_P(zv);
if (parent_constant->ce != child_constant->ce && (ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_FINAL)) {
zend_error_noreturn(E_COMPILE_ERROR, "%s::%s cannot override final constant %s::%s",
ZSTR_VAL(child_constant->ce->name), ZSTR_VAL(name),
ZSTR_VAL(parent_constant->ce->name), ZSTR_VAL(name)
);
}
if (child_constant->ce != parent_constant->ce && child_constant->ce != ce) {
zend_error_noreturn(E_COMPILE_ERROR,
"%s %s inherits both %s::%s and %s::%s, which is ambiguous",
zend_get_object_type_uc(ce),
ZSTR_VAL(ce->name),
ZSTR_VAL(child_constant->ce->name), ZSTR_VAL(name),
ZSTR_VAL(parent_constant->ce->name), ZSTR_VAL(name));
}
if (UNEXPECTED((ZEND_CLASS_CONST_FLAGS(child_constant) & ZEND_ACC_PPP_MASK) > (ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PPP_MASK))) {
zend_error_noreturn(E_COMPILE_ERROR, "Access level to %s::%s must be %s (as in %s %s)%s",
ZSTR_VAL(ce->name), ZSTR_VAL(name),
zend_visibility_string(ZEND_CLASS_CONST_FLAGS(parent_constant)),
zend_get_object_type(parent_constant->ce),
ZSTR_VAL(parent_constant->ce->name),
(ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PUBLIC) ? "" : " or weaker"
);
}
if (!(ZEND_CLASS_CONST_FLAGS(parent_constant) & ZEND_ACC_PRIVATE) && ZEND_TYPE_IS_SET(parent_constant->type)) {
inheritance_status status = class_constant_types_compatible(parent_constant, child_constant);
if (status == INHERITANCE_ERROR) {
emit_incompatible_class_constant_error(child_constant, parent_constant, name);
} else if (status == INHERITANCE_UNRESOLVED) {
add_class_constant_compatibility_obligation(ce, child_constant, parent_constant, name);
}
}
return false;
}
/* }}} */
static void do_inherit_iface_constant(zend_string *name, zend_class_constant *c, zend_class_entry *ce, zend_class_entry *iface) /* {{{ */
{
if (do_inherit_constant_check(ce, c, name)) {
zend_class_constant *ct;
if (Z_TYPE(c->value) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
if (iface->ce_flags & ZEND_ACC_IMMUTABLE) {
ct = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
memcpy(ct, c, sizeof(zend_class_constant));
c = ct;
Z_CONSTANT_FLAGS(c->value) |= CONST_OWNED;
}
}
if (ce->type & ZEND_INTERNAL_CLASS) {
ct = pemalloc(sizeof(zend_class_constant), 1);
memcpy(ct, c, sizeof(zend_class_constant));
c = ct;
}
zend_hash_update_ptr(&ce->constants_table, name, c);
}
}
/* }}} */
static void do_interface_implementation(zend_class_entry *ce, zend_class_entry *iface) /* {{{ */
{
zend_function *func;
zend_string *key;
zend_class_constant *c;
uint32_t flags = ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_CHECK_VISIBILITY;
if (!(ce->ce_flags & ZEND_ACC_INTERFACE)) {
/* We are not setting the prototype of overridden interface methods because of abstract
* constructors. See Zend/tests/interface_constructor_prototype_001.phpt. */
flags |=
ZEND_INHERITANCE_LAZY_CHILD_CLONE |
ZEND_INHERITANCE_SET_CHILD_PROTO |
ZEND_INHERITANCE_RESET_CHILD_OVERRIDE;
} else {
flags |=
ZEND_INHERITANCE_LAZY_CHILD_CLONE |
ZEND_INHERITANCE_RESET_CHILD_OVERRIDE;
}
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&iface->constants_table, key, c) {
do_inherit_iface_constant(key, c, ce, iface);
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&iface->function_table, key, func) {
do_inherit_method(key, func, ce, 1, flags);
} ZEND_HASH_FOREACH_END();
zend_hash_extend(&ce->properties_info,
zend_hash_num_elements(&ce->properties_info) +
zend_hash_num_elements(&iface->properties_info), 0);
zend_property_info *prop;
ZEND_HASH_FOREACH_STR_KEY_PTR(&iface->properties_info, key, prop) {
do_inherit_property(prop, key, ce);
} ZEND_HASH_FOREACH_END();
do_implement_interface(ce, iface);
if (iface->num_interfaces) {
zend_do_inherit_interfaces(ce, iface);
}
}
/* }}} */
ZEND_API void zend_do_implement_interface(zend_class_entry *ce, zend_class_entry *iface) /* {{{ */
{
uint32_t i, ignore = 0;
uint32_t current_iface_num = ce->num_interfaces;
uint32_t parent_iface_num = ce->parent ? ce->parent->num_interfaces : 0;
zend_string *key;
zend_class_constant *c;
ZEND_ASSERT(ce->ce_flags & ZEND_ACC_LINKED);
for (i = 0; i < ce->num_interfaces; i++) {
if (ce->interfaces[i] == NULL) {
memmove(ce->interfaces + i, ce->interfaces + i + 1, sizeof(zend_class_entry*) * (--ce->num_interfaces - i));
i--;
} else if (ce->interfaces[i] == iface) {
if (EXPECTED(i < parent_iface_num)) {
ignore = 1;
} else {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s cannot implement previously implemented interface %s", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
}
}
}
if (ignore) {
/* Check for attempt to redeclare interface constants */
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&iface->constants_table, key, c) {
do_inherit_constant_check(ce, c, key);
} ZEND_HASH_FOREACH_END();
} else {
if (ce->num_interfaces >= current_iface_num) {
if (ce->type == ZEND_INTERNAL_CLASS) {
ce->interfaces = (zend_class_entry **) realloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
} else {
ce->interfaces = (zend_class_entry **) erealloc(ce->interfaces, sizeof(zend_class_entry *) * (++current_iface_num));
}
}
ce->interfaces[ce->num_interfaces++] = iface;
do_interface_implementation(ce, iface);
}
}
/* }}} */
static void zend_do_implement_interfaces(zend_class_entry *ce, zend_class_entry **interfaces) /* {{{ */
{
zend_class_entry *iface;
uint32_t num_parent_interfaces = ce->parent ? ce->parent->num_interfaces : 0;
uint32_t num_interfaces = num_parent_interfaces;
zend_string *key;
zend_class_constant *c;
uint32_t i, j;
for (i = 0; i < ce->num_interfaces; i++) {
iface = interfaces[num_parent_interfaces + i];
if (!(iface->ce_flags & ZEND_ACC_LINKED)) {
add_dependency_obligation(ce, iface);
}
if (UNEXPECTED(!(iface->ce_flags & ZEND_ACC_INTERFACE))) {
efree(interfaces);
zend_error_noreturn(E_ERROR, "%s cannot implement %s - it is not an interface", ZSTR_VAL(ce->name), ZSTR_VAL(iface->name));
return;
}
for (j = 0; j < num_interfaces; j++) {
if (interfaces[j] == iface) {
if (j >= num_parent_interfaces) {
efree(interfaces);
zend_error_noreturn(E_COMPILE_ERROR, "%s %s cannot implement previously implemented interface %s",
zend_get_object_type_uc(ce),
ZSTR_VAL(ce->name),
ZSTR_VAL(iface->name));
return;
}
/* skip duplications */
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&iface->constants_table, key, c) {
do_inherit_constant_check(ce, c, key);
} ZEND_HASH_FOREACH_END();
iface = NULL;
break;
}
}
if (iface) {
interfaces[num_interfaces] = iface;
num_interfaces++;
}
}
if (!(ce->ce_flags & ZEND_ACC_CACHED)) {
for (i = 0; i < ce->num_interfaces; i++) {
zend_string_release_ex(ce->interface_names[i].name, 0);
zend_string_release_ex(ce->interface_names[i].lc_name, 0);
}
efree(ce->interface_names);
}
ce->num_interfaces = num_interfaces;
ce->interfaces = interfaces;
ce->ce_flags |= ZEND_ACC_RESOLVED_INTERFACES;
for (i = 0; i < num_parent_interfaces; i++) {
do_implement_interface(ce, ce->interfaces[i]);
}
/* Note that new interfaces can be added during this loop due to interface inheritance.
* Use num_interfaces rather than ce->num_interfaces to not re-process the new ones. */
for (; i < num_interfaces; i++) {
do_interface_implementation(ce, ce->interfaces[i]);
}
}
/* }}} */
void zend_inheritance_check_override(zend_class_entry *ce)
{
zend_function *f;
if (ce->ce_flags & ZEND_ACC_TRAIT) {
return;
}
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, f) {
if (f->common.fn_flags & ZEND_ACC_OVERRIDE) {
ZEND_ASSERT(f->type != ZEND_INTERNAL_FUNCTION);
zend_error_at_noreturn(
E_COMPILE_ERROR, f->op_array.filename, f->op_array.line_start,
"%s::%s() has #[\\Override] attribute, but no matching parent method exists",
ZEND_FN_SCOPE_NAME(f), ZSTR_VAL(f->common.function_name));
}
} ZEND_HASH_FOREACH_END();
if (ce->num_hooked_props) {
zend_property_info *prop;
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop) {
if (!prop->hooks) {
continue;
}
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
f = prop->hooks[i];
if (f && f->common.fn_flags & ZEND_ACC_OVERRIDE) {
ZEND_ASSERT(f->type != ZEND_INTERNAL_FUNCTION);
zend_error_at_noreturn(
E_COMPILE_ERROR, f->op_array.filename, f->op_array.line_start,
"%s::%s() has #[\\Override] attribute, but no matching parent method exists",
ZEND_FN_SCOPE_NAME(f), ZSTR_VAL(f->common.function_name));
}
}
} ZEND_HASH_FOREACH_END();
}
}
static zend_class_entry *fixup_trait_scope(const zend_function *fn, zend_class_entry *ce)
{
/* self in trait methods should be resolved to the using class, not the trait. */
return fn->common.scope->ce_flags & ZEND_ACC_TRAIT ? ce : fn->common.scope;
}
static void zend_add_trait_method(zend_class_entry *ce, zend_string *name, zend_string *key, zend_function *fn) /* {{{ */
{
zend_function *existing_fn = NULL;
zend_function *new_fn;
bool check_inheritance = false;
if ((existing_fn = zend_hash_find_ptr(&ce->function_table, key)) != NULL) {
/* if it is the same function with the same visibility and has not been assigned a class scope yet, regardless
* of where it is coming from there is no conflict and we do not need to add it again */
if (existing_fn->op_array.opcodes == fn->op_array.opcodes &&
(existing_fn->common.fn_flags & ZEND_ACC_PPP_MASK) == (fn->common.fn_flags & ZEND_ACC_PPP_MASK) &&
(existing_fn->common.scope->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT) {
return;
}
/* Abstract method signatures from the trait must be satisfied. */
if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* "abstract private" methods in traits were not available prior to PHP 8.
* As such, "abstract protected" was sometimes used to indicate trait requirements,
* even though the "implementing" method was private. Do not check visibility
* requirements to maintain backwards-compatibility with such usage.
*/
do_inheritance_check_on_method(
existing_fn, fixup_trait_scope(existing_fn, ce), fn, fixup_trait_scope(fn, ce),
ce, NULL, ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_RESET_CHILD_OVERRIDE);
return;
}
if (existing_fn->common.scope == ce) {
/* members from the current class override trait methods */
return;
} else if (UNEXPECTED((existing_fn->common.scope->ce_flags & ZEND_ACC_TRAIT)
&& !(existing_fn->common.fn_flags & ZEND_ACC_ABSTRACT))) {
/* two traits can't define the same non-abstract method */
zend_error_noreturn(E_COMPILE_ERROR, "Trait method %s::%s has not been applied as %s::%s, because of collision with %s::%s",
ZSTR_VAL(fn->common.scope->name), ZSTR_VAL(fn->common.function_name),
ZSTR_VAL(ce->name), ZSTR_VAL(name),
ZSTR_VAL(existing_fn->common.scope->name), ZSTR_VAL(existing_fn->common.function_name));
} else {
check_inheritance = true;
}
}
if (UNEXPECTED(fn->type == ZEND_INTERNAL_FUNCTION)) {
new_fn = zend_arena_alloc(&CG(arena), sizeof(zend_internal_function));
memcpy(new_fn, fn, sizeof(zend_internal_function));
new_fn->common.fn_flags |= ZEND_ACC_ARENA_ALLOCATED;
} else {
new_fn = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_fn, fn, sizeof(zend_op_array));
new_fn->op_array.fn_flags &= ~ZEND_ACC_IMMUTABLE;
}
new_fn->common.fn_flags |= ZEND_ACC_TRAIT_CLONE;
/* Reassign method name, in case it is an alias. */
new_fn->common.function_name = name;
function_add_ref(new_fn);
fn = zend_hash_update_ptr(&ce->function_table, key, new_fn);
zend_add_magic_method(ce, fn, key);
if (check_inheritance) {
/* Inherited members are overridden by members inserted by traits.
* Check whether the trait method fulfills the inheritance requirements. */
uint32_t flags = ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_CHECK_VISIBILITY;
if (!(existing_fn->common.scope->ce_flags & ZEND_ACC_TRAIT)) {
flags |= ZEND_INHERITANCE_SET_CHILD_CHANGED |ZEND_INHERITANCE_SET_CHILD_PROTO |
ZEND_INHERITANCE_RESET_CHILD_OVERRIDE;
}
do_inheritance_check_on_method(
fn, fixup_trait_scope(fn, ce), existing_fn, fixup_trait_scope(existing_fn, ce),
ce, NULL, flags);
}
}
/* }}} */
static void zend_fixup_trait_method(zend_function *fn, zend_class_entry *ce) /* {{{ */
{
if ((fn->common.scope->ce_flags & ZEND_ACC_TRAIT) == ZEND_ACC_TRAIT) {
fn->common.scope = ce;
if (fn->common.fn_flags & ZEND_ACC_ABSTRACT) {
ce->ce_flags |= ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}
if (fn->type == ZEND_USER_FUNCTION && fn->op_array.static_variables) {
ce->ce_flags |= ZEND_HAS_STATIC_IN_METHODS;
}
}
}
/* }}} */
static void zend_traits_check_private_final_inheritance(uint32_t original_fn_flags, zend_function *fn_copy, zend_string *name)
{
/* If the function was originally already private+final, then it will have
* already been warned about. Only emit this error when the used trait method
* explicitly became final, avoiding errors for `as private` where it was
* already final. */
if (!(original_fn_flags & ZEND_ACC_FINAL)
&& (fn_copy->common.fn_flags & (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)) == (ZEND_ACC_PRIVATE | ZEND_ACC_FINAL)
&& !zend_string_equals_literal_ci(name, ZEND_CONSTRUCTOR_FUNC_NAME)) {
zend_error(E_COMPILE_WARNING, "Private methods cannot be final as they are never overridden by other classes");
}
}
static void zend_traits_copy_functions(zend_string *fnname, zend_function *fn, zend_class_entry *ce, HashTable *exclude_table, zend_class_entry **aliases) /* {{{ */
{
zend_trait_alias *alias, **alias_ptr;
zend_string *lcname;
zend_function fn_copy;
int i;
/* apply aliases which are qualified with a class name, there should not be any ambiguity */
if (ce->trait_aliases) {
alias_ptr = ce->trait_aliases;
alias = *alias_ptr;
i = 0;
while (alias) {
/* Scope unset or equal to the function we compare to, and the alias applies to fn */
if (alias->alias != NULL
&& fn->common.scope == aliases[i]
&& zend_string_equals_ci(alias->trait_method.method_name, fnname)
) {
fn_copy = *fn;
if (alias->modifiers & ZEND_ACC_PPP_MASK) {
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags & ~ZEND_ACC_PPP_MASK);
} else {
fn_copy.common.fn_flags = alias->modifiers | fn->common.fn_flags;
}
zend_traits_check_private_final_inheritance(fn->common.fn_flags, &fn_copy, alias->alias);
lcname = zend_string_tolower(alias->alias);
zend_add_trait_method(ce, alias->alias, lcname, &fn_copy);
zend_string_release_ex(lcname, 0);
}
alias_ptr++;
alias = *alias_ptr;
i++;
}
}
if (exclude_table == NULL || zend_hash_find(exclude_table, fnname) == NULL) {
/* is not in hashtable, thus, function is not to be excluded */
memcpy(&fn_copy, fn, fn->type == ZEND_USER_FUNCTION ? sizeof(zend_op_array) : sizeof(zend_internal_function));
/* apply aliases which have not alias name, just setting visibility */
if (ce->trait_aliases) {
alias_ptr = ce->trait_aliases;
alias = *alias_ptr;
i = 0;
while (alias) {
/* Scope unset or equal to the function we compare to, and the alias applies to fn */
if (alias->alias == NULL && alias->modifiers != 0
&& fn->common.scope == aliases[i]
&& zend_string_equals_ci(alias->trait_method.method_name, fnname)
) {
if (alias->modifiers & ZEND_ACC_PPP_MASK) {
fn_copy.common.fn_flags = alias->modifiers | (fn->common.fn_flags & ~ZEND_ACC_PPP_MASK);
} else {
fn_copy.common.fn_flags = alias->modifiers | fn->common.fn_flags;
}
}
alias_ptr++;
alias = *alias_ptr;
i++;
}
}
zend_traits_check_private_final_inheritance(fn->common.fn_flags, &fn_copy, fnname);
zend_add_trait_method(ce, fn->common.function_name, fnname, &fn_copy);
}
}
/* }}} */
static uint32_t zend_check_trait_usage(zend_class_entry *ce, zend_class_entry *trait, zend_class_entry **traits) /* {{{ */
{
uint32_t i;
if (UNEXPECTED((trait->ce_flags & ZEND_ACC_TRAIT) != ZEND_ACC_TRAIT)) {
zend_error_noreturn(E_COMPILE_ERROR, "Class %s is not a trait, Only traits may be used in 'as' and 'insteadof' statements", ZSTR_VAL(trait->name));
return 0;
}
for (i = 0; i < ce->num_traits; i++) {
if (traits[i] == trait) {
return i;
}
}
zend_error_noreturn(E_COMPILE_ERROR, "Required Trait %s wasn't added to %s", ZSTR_VAL(trait->name), ZSTR_VAL(ce->name));
return 0;
}
/* }}} */
static void zend_traits_init_trait_structures(zend_class_entry *ce, zend_class_entry **traits, HashTable ***exclude_tables_ptr, zend_class_entry ***aliases_ptr) /* {{{ */
{
size_t i, j = 0;
zend_trait_precedence **precedences;
zend_trait_precedence *cur_precedence;
zend_trait_method_reference *cur_method_ref;
zend_string *lc_trait_name;
zend_string *lcname;
HashTable **exclude_tables = NULL;
zend_class_entry **aliases = NULL;
zend_class_entry *trait;
/* resolve class references */
if (ce->trait_precedences) {
exclude_tables = ecalloc(ce->num_traits, sizeof(HashTable*));
i = 0;
precedences = ce->trait_precedences;
ce->trait_precedences = NULL;
while ((cur_precedence = precedences[i])) {
/** Resolve classes for all precedence operations. */
cur_method_ref = &cur_precedence->trait_method;
lc_trait_name = zend_string_tolower(cur_method_ref->class_name);
trait = zend_hash_find_ptr(EG(class_table), lc_trait_name);
zend_string_release_ex(lc_trait_name, 0);
if (!trait || !(trait->ce_flags & ZEND_ACC_LINKED)) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(cur_method_ref->class_name));
}
zend_check_trait_usage(ce, trait, traits);
/** Ensure that the preferred method is actually available. */
lcname = zend_string_tolower(cur_method_ref->method_name);
if (!zend_hash_exists(&trait->function_table, lcname)) {
zend_error_noreturn(E_COMPILE_ERROR,
"A precedence rule was defined for %s::%s but this method does not exist",
ZSTR_VAL(trait->name),
ZSTR_VAL(cur_method_ref->method_name));
}
/** With the other traits, we are more permissive.
We do not give errors for those. This allows to be more
defensive in such definitions.
However, we want to make sure that the insteadof declaration
is consistent in itself.
*/
for (j = 0; j < cur_precedence->num_excludes; j++) {
zend_string* class_name = cur_precedence->exclude_class_names[j];
zend_class_entry *exclude_ce;
uint32_t trait_num;
lc_trait_name = zend_string_tolower(class_name);
exclude_ce = zend_hash_find_ptr(EG(class_table), lc_trait_name);
zend_string_release_ex(lc_trait_name, 0);
if (!exclude_ce || !(exclude_ce->ce_flags & ZEND_ACC_LINKED)) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(class_name));
}
trait_num = zend_check_trait_usage(ce, exclude_ce, traits);
if (!exclude_tables[trait_num]) {
ALLOC_HASHTABLE(exclude_tables[trait_num]);
zend_hash_init(exclude_tables[trait_num], 0, NULL, NULL, 0);
}
if (zend_hash_add_empty_element(exclude_tables[trait_num], lcname) == NULL) {
zend_error_noreturn(E_COMPILE_ERROR, "Failed to evaluate a trait precedence (%s). Method of trait %s was defined to be excluded multiple times", ZSTR_VAL(precedences[i]->trait_method.method_name), ZSTR_VAL(exclude_ce->name));
}
/* make sure that the trait method is not from a class mentioned in
exclude_from_classes, for consistency */
if (trait == exclude_ce) {
zend_error_noreturn(E_COMPILE_ERROR,
"Inconsistent insteadof definition. "
"The method %s is to be used from %s, but %s is also on the exclude list",
ZSTR_VAL(cur_method_ref->method_name),
ZSTR_VAL(trait->name),
ZSTR_VAL(trait->name));
}
}
zend_string_release_ex(lcname, 0);
i++;
}
ce->trait_precedences = precedences;
}
if (ce->trait_aliases) {
i = 0;
while (ce->trait_aliases[i]) {
i++;
}
aliases = ecalloc(i, sizeof(zend_class_entry*));
i = 0;
while (ce->trait_aliases[i]) {
zend_trait_alias *cur_alias = ce->trait_aliases[i];
cur_method_ref = &ce->trait_aliases[i]->trait_method;
lcname = zend_string_tolower(cur_method_ref->method_name);
if (cur_method_ref->class_name) {
/* For all aliases with an explicit class name, resolve the class now. */
lc_trait_name = zend_string_tolower(cur_method_ref->class_name);
trait = zend_hash_find_ptr(EG(class_table), lc_trait_name);
zend_string_release_ex(lc_trait_name, 0);
if (!trait || !(trait->ce_flags & ZEND_ACC_LINKED)) {
zend_error_noreturn(E_COMPILE_ERROR, "Could not find trait %s", ZSTR_VAL(cur_method_ref->class_name));
}
zend_check_trait_usage(ce, trait, traits);
aliases[i] = trait;
/* And, ensure that the referenced method is resolvable, too. */
if (!zend_hash_exists(&trait->function_table, lcname)) {
zend_error_noreturn(E_COMPILE_ERROR, "An alias was defined for %s::%s but this method does not exist", ZSTR_VAL(trait->name), ZSTR_VAL(cur_method_ref->method_name));
}
} else {
/* Find out which trait this method refers to. */
trait = NULL;
for (j = 0; j < ce->num_traits; j++) {
if (traits[j]) {
if (zend_hash_exists(&traits[j]->function_table, lcname)) {
if (!trait) {
trait = traits[j];
continue;
}
zend_error_noreturn(E_COMPILE_ERROR,
"An alias was defined for method %s(), which exists in both %s and %s. Use %s::%s or %s::%s to resolve the ambiguity",
ZSTR_VAL(cur_method_ref->method_name),
ZSTR_VAL(trait->name), ZSTR_VAL(traits[j]->name),
ZSTR_VAL(trait->name), ZSTR_VAL(cur_method_ref->method_name),
ZSTR_VAL(traits[j]->name), ZSTR_VAL(cur_method_ref->method_name));
}
}
}
/* Non-absolute method reference refers to method that does not exist. */
if (!trait) {
if (cur_alias->alias) {
zend_error_noreturn(E_COMPILE_ERROR,
"An alias (%s) was defined for method %s(), but this method does not exist",
ZSTR_VAL(cur_alias->alias),
ZSTR_VAL(cur_alias->trait_method.method_name));
} else {
zend_error_noreturn(E_COMPILE_ERROR,
"The modifiers of the trait method %s() are changed, but this method does not exist. Error",
ZSTR_VAL(cur_alias->trait_method.method_name));
}
}
aliases[i] = trait;
}
zend_string_release_ex(lcname, 0);
i++;
}
}
*exclude_tables_ptr = exclude_tables;
*aliases_ptr = aliases;
}
/* }}} */
static void zend_do_traits_method_binding(zend_class_entry *ce, zend_class_entry **traits, HashTable **exclude_tables, zend_class_entry **aliases) /* {{{ */
{
uint32_t i;
zend_string *key;
zend_function *fn;
if (exclude_tables) {
for (i = 0; i < ce->num_traits; i++) {
if (traits[i]) {
/* copies functions, applies defined aliasing, and excludes unused trait methods */
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&traits[i]->function_table, key, fn) {
zend_traits_copy_functions(key, fn, ce, exclude_tables[i], aliases);
} ZEND_HASH_FOREACH_END();
if (exclude_tables[i]) {
zend_hash_destroy(exclude_tables[i]);
FREE_HASHTABLE(exclude_tables[i]);
exclude_tables[i] = NULL;
}
}
}
} else {
for (i = 0; i < ce->num_traits; i++) {
if (traits[i]) {
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&traits[i]->function_table, key, fn) {
zend_traits_copy_functions(key, fn, ce, NULL, aliases);
} ZEND_HASH_FOREACH_END();
}
}
}
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, fn) {
zend_fixup_trait_method(fn, ce);
} ZEND_HASH_FOREACH_END();
}
/* }}} */
static const zend_class_entry* find_first_constant_definition(const zend_class_entry *ce, zend_class_entry **traits, size_t current_trait, zend_string *constant_name, const zend_class_entry *colliding_ce) /* {{{ */
{
/* This function is used to show the place of the existing conflicting
* definition in error messages when conflicts occur. Since trait constants
* are flattened into the constants table of the composing class, and thus
* we lose information about which constant was defined in which trait, a
* process like this is needed to find the location of the first definition
* of the constant from traits.
*/
size_t i;
if (colliding_ce == ce) {
for (i = 0; i < current_trait; i++) {
if (traits[i]
&& zend_hash_exists(&traits[i]->constants_table, constant_name)) {
return traits[i];
}
}
}
/* Traits don't have it, then the composing class (or trait) itself has it. */
return colliding_ce;
}
/* }}} */
static void emit_incompatible_trait_constant_error(
const zend_class_entry *ce, const zend_class_constant *existing_constant, const zend_class_constant *trait_constant, zend_string *name,
zend_class_entry **traits, size_t current_trait
) {
zend_error_noreturn(E_COMPILE_ERROR,
"%s and %s define the same constant (%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed",
ZSTR_VAL(find_first_constant_definition(ce, traits, current_trait, name, existing_constant->ce)->name),
ZSTR_VAL(trait_constant->ce->name),
ZSTR_VAL(name),
ZSTR_VAL(ce->name)
);
}
static bool do_trait_constant_check(
zend_class_entry *ce, zend_class_constant *trait_constant, zend_string *name, zend_class_entry **traits, size_t current_trait
) {
uint32_t flags_mask = ZEND_ACC_PPP_MASK | ZEND_ACC_FINAL;
zval *zv = zend_hash_find_known_hash(&ce->constants_table, name);
if (zv == NULL) {
/* No existing constant of the same name, so this one can be added */
return true;
}
zend_class_constant *existing_constant = Z_PTR_P(zv);
if ((ZEND_CLASS_CONST_FLAGS(trait_constant) & flags_mask) != (ZEND_CLASS_CONST_FLAGS(existing_constant) & flags_mask)) {
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
return false;
}
if (ZEND_TYPE_IS_SET(trait_constant->type) != ZEND_TYPE_IS_SET(existing_constant->type)) {
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
return false;
} else if (ZEND_TYPE_IS_SET(trait_constant->type)) {
inheritance_status status1 = zend_perform_covariant_type_check(ce, existing_constant->type, traits[current_trait], trait_constant->type);
inheritance_status status2 = zend_perform_covariant_type_check(traits[current_trait], trait_constant->type, ce, existing_constant->type);
if (status1 == INHERITANCE_ERROR || status2 == INHERITANCE_ERROR) {
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
return false;
}
}
if (!check_trait_property_or_constant_value_compatibility(ce, &trait_constant->value, &existing_constant->value)) {
/* There is an existing constant of the same name, and it conflicts with the new one, so let's throw a fatal error */
emit_incompatible_trait_constant_error(ce, existing_constant, trait_constant, name, traits, current_trait);
return false;
}
/* There is an existing constant which is compatible with the new one, so no need to add it */
return false;
}
static void zend_do_traits_constant_binding(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
{
size_t i;
for (i = 0; i < ce->num_traits; i++) {
zend_string *constant_name;
zend_class_constant *constant;
if (!traits[i]) {
continue;
}
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&traits[i]->constants_table, constant_name, constant) {
if (do_trait_constant_check(ce, constant, constant_name, traits, i)) {
zend_class_constant *ct = NULL;
ct = zend_arena_alloc(&CG(arena),sizeof(zend_class_constant));
memcpy(ct, constant, sizeof(zend_class_constant));
constant = ct;
if (Z_TYPE(constant->value) == IS_CONSTANT_AST) {
ce->ce_flags &= ~ZEND_ACC_CONSTANTS_UPDATED;
ce->ce_flags |= ZEND_ACC_HAS_AST_CONSTANTS;
}
/* Unlike interface implementations and class inheritances,
* access control of the trait constants is done by the scope
* of the composing class. So let's replace the ce here.
*/
constant->ce = ce;
Z_TRY_ADDREF(constant->value);
constant->doc_comment = constant->doc_comment ? zend_string_copy(constant->doc_comment) : NULL;
if (constant->attributes && (!(GC_FLAGS(constant->attributes) & IS_ARRAY_IMMUTABLE))) {
GC_ADDREF(constant->attributes);
}
zend_hash_update_ptr(&ce->constants_table, constant_name, constant);
}
} ZEND_HASH_FOREACH_END();
}
}
/* }}} */
static const zend_class_entry* find_first_property_definition(const zend_class_entry *ce, zend_class_entry **traits, size_t current_trait, zend_string *prop_name, const zend_class_entry *colliding_ce) /* {{{ */
{
size_t i;
if (colliding_ce == ce) {
for (i = 0; i < current_trait; i++) {
if (traits[i]
&& zend_hash_exists(&traits[i]->properties_info, prop_name)) {
return traits[i];
}
}
}
return colliding_ce;
}
/* }}} */
static void zend_do_traits_property_binding(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
{
size_t i;
zend_property_info *property_info;
const zend_property_info *colliding_prop;
zend_property_info *new_prop;
zend_string* prop_name;
zval* prop_value;
zend_string *doc_comment;
/* In the following steps the properties are inserted into the property table
* for that, a very strict approach is applied:
* - check for compatibility, if not compatible with any property in class -> fatal
* - if compatible, then strict notice
*/
for (i = 0; i < ce->num_traits; i++) {
if (!traits[i]) {
continue;
}
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&traits[i]->properties_info, prop_name, property_info) {
uint32_t flags = property_info->flags;
/* next: check for conflicts with current class */
if ((colliding_prop = zend_hash_find_ptr(&ce->properties_info, prop_name)) != NULL) {
if ((colliding_prop->flags & ZEND_ACC_PRIVATE) && colliding_prop->ce != ce) {
zend_hash_del(&ce->properties_info, prop_name);
flags |= ZEND_ACC_CHANGED;
} else {
bool is_compatible = false;
uint32_t flags_mask = ZEND_ACC_PPP_MASK | ZEND_ACC_STATIC | ZEND_ACC_READONLY;
if (colliding_prop->hooks || property_info->hooks) {
zend_error_noreturn(E_COMPILE_ERROR,
"%s and %s define the same hooked property ($%s) in the composition of %s. Conflict resolution between hooked properties is currently not supported. Class was composed",
ZSTR_VAL(find_first_property_definition(ce, traits, i, prop_name, colliding_prop->ce)->name),
ZSTR_VAL(property_info->ce->name),
ZSTR_VAL(prop_name),
ZSTR_VAL(ce->name));
}
if ((colliding_prop->flags & flags_mask) == (flags & flags_mask) &&
verify_property_type_compatibility(property_info, colliding_prop, PROP_INVARIANT, false, false) == INHERITANCE_SUCCESS
) {
/* the flags are identical, thus, the properties may be compatible */
zval *op1, *op2;
if (flags & ZEND_ACC_STATIC) {
op1 = &ce->default_static_members_table[colliding_prop->offset];
op2 = &traits[i]->default_static_members_table[property_info->offset];
ZVAL_DEINDIRECT(op1);
ZVAL_DEINDIRECT(op2);
} else {
op1 = &ce->default_properties_table[OBJ_PROP_TO_NUM(colliding_prop->offset)];
op2 = &traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
}
is_compatible = check_trait_property_or_constant_value_compatibility(ce, op1, op2);
}
if (!is_compatible) {
zend_error_noreturn(E_COMPILE_ERROR,
"%s and %s define the same property ($%s) in the composition of %s. However, the definition differs and is considered incompatible. Class was composed",
ZSTR_VAL(find_first_property_definition(ce, traits, i, prop_name, colliding_prop->ce)->name),
ZSTR_VAL(property_info->ce->name),
ZSTR_VAL(prop_name),
ZSTR_VAL(ce->name));
}
if (!(flags & ZEND_ACC_STATIC)) {
continue;
}
}
}
if ((ce->ce_flags & ZEND_ACC_READONLY_CLASS) && !(property_info->flags & ZEND_ACC_READONLY)) {
zend_error_noreturn(E_COMPILE_ERROR,
"Readonly class %s cannot use trait with a non-readonly property %s::$%s",
ZSTR_VAL(ce->name),
ZSTR_VAL(property_info->ce->name),
ZSTR_VAL(prop_name)
);
}
/* property not found, so lets add it */
zval tmp_prop_value;
if (!(flags & ZEND_ACC_VIRTUAL)) {
if (flags & ZEND_ACC_STATIC) {
prop_value = &traits[i]->default_static_members_table[property_info->offset];
ZEND_ASSERT(Z_TYPE_P(prop_value) != IS_INDIRECT);
} else {
prop_value = &traits[i]->default_properties_table[OBJ_PROP_TO_NUM(property_info->offset)];
}
Z_TRY_ADDREF_P(prop_value);
} else {
prop_value = &tmp_prop_value;
ZVAL_UNDEF(&tmp_prop_value);
}
doc_comment = property_info->doc_comment ? zend_string_copy(property_info->doc_comment) : NULL;
zend_type type = property_info->type;
/* Assumption: only userland classes can use traits, as such the type must be arena allocated */
zend_type_copy_ctor(&type, /* use arena */ true, /* persistent */ false);
new_prop = zend_declare_typed_property(ce, prop_name, prop_value, flags, doc_comment, type);
if (property_info->attributes) {
new_prop->attributes = property_info->attributes;
if (!(GC_FLAGS(new_prop->attributes) & IS_ARRAY_IMMUTABLE)) {
GC_ADDREF(new_prop->attributes);
}
}
if (property_info->hooks) {
zend_function **hooks = new_prop->hooks =
zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
memcpy(hooks, property_info->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
if (hooks[i]) {
zend_function *old_fn = hooks[i];
/* Hooks are not yet supported for internal properties. */
ZEND_ASSERT(ZEND_USER_CODE(old_fn->type));
/* Copy the function, because we need to adjust the scope. */
zend_function *new_fn = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_fn, old_fn, sizeof(zend_op_array));
new_fn->op_array.fn_flags &= ~ZEND_ACC_IMMUTABLE;
new_fn->common.fn_flags |= ZEND_ACC_TRAIT_CLONE;
new_fn->common.prop_info = new_prop;
function_add_ref(new_fn);
zend_fixup_trait_method(new_fn, ce);
hooks[i] = new_fn;
}
}
ce->ce_flags |= ZEND_ACC_USE_GUARDS;
ce->num_hooked_props++;
}
} ZEND_HASH_FOREACH_END();
}
}
/* }}} */
static void zend_do_bind_traits(zend_class_entry *ce, zend_class_entry **traits) /* {{{ */
{
HashTable **exclude_tables;
zend_class_entry **aliases;
ZEND_ASSERT(ce->num_traits > 0);
/* complete initialization of trait structures in ce */
zend_traits_init_trait_structures(ce, traits, &exclude_tables, &aliases);
/* first care about all methods to be flattened into the class */
zend_do_traits_method_binding(ce, traits, exclude_tables, aliases);
if (aliases) {
efree(aliases);
}
if (exclude_tables) {
efree(exclude_tables);
}
/* then flatten the constants and properties into it, to, mostly to notify developer about problems */
zend_do_traits_constant_binding(ce, traits);
zend_do_traits_property_binding(ce, traits);
}
/* }}} */
#define MAX_ABSTRACT_INFO_CNT 3
#define MAX_ABSTRACT_INFO_FMT "%s%s%s%s"
#define DISPLAY_ABSTRACT_FN(idx) \
ai.afn[idx] ? ZEND_FN_SCOPE_NAME(ai.afn[idx]) : "", \
ai.afn[idx] ? "::" : "", \
ai.afn[idx] ? ZSTR_VAL(ai.afn[idx]->common.function_name) : "", \
ai.afn[idx] && ai.afn[idx + 1] ? ", " : (ai.afn[idx] && ai.cnt > MAX_ABSTRACT_INFO_CNT ? ", ..." : "")
typedef struct _zend_abstract_info {
const zend_function *afn[MAX_ABSTRACT_INFO_CNT + 1];
int cnt;
} zend_abstract_info;
static void zend_verify_abstract_class_function(const zend_function *fn, zend_abstract_info *ai) /* {{{ */
{
if (ai->cnt < MAX_ABSTRACT_INFO_CNT) {
ai->afn[ai->cnt] = fn;
}
ai->cnt++;
}
/* }}} */
void zend_verify_abstract_class(zend_class_entry *ce) /* {{{ */
{
const zend_function *func;
zend_abstract_info ai;
bool is_explicit_abstract = (ce->ce_flags & ZEND_ACC_EXPLICIT_ABSTRACT_CLASS) != 0;
bool can_be_abstract = (ce->ce_flags & ZEND_ACC_ENUM) == 0;
memset(&ai, 0, sizeof(ai));
ZEND_HASH_MAP_FOREACH_PTR(&ce->function_table, func) {
if (func->common.fn_flags & ZEND_ACC_ABSTRACT) {
/* If the class is explicitly abstract, we only check private abstract methods,
* because only they must be declared in the same class. */
if (!is_explicit_abstract || (func->common.fn_flags & ZEND_ACC_PRIVATE)) {
zend_verify_abstract_class_function(func, &ai);
}
}
} ZEND_HASH_FOREACH_END();
if (!is_explicit_abstract) {
const zend_property_info *prop_info;
ZEND_HASH_FOREACH_PTR(&ce->properties_info, prop_info) {
if (prop_info->hooks) {
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
const zend_function *fn = prop_info->hooks[i];
if (fn && (fn->common.fn_flags & ZEND_ACC_ABSTRACT)) {
zend_verify_abstract_class_function(fn, &ai);
}
}
}
} ZEND_HASH_FOREACH_END();
}
if (ai.cnt) {
zend_error_noreturn(E_ERROR, !is_explicit_abstract && can_be_abstract
? "%s %s contains %d abstract method%s and must therefore be declared abstract or implement the remaining methods (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")"
: "%s %s must implement %d abstract private method%s (" MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT MAX_ABSTRACT_INFO_FMT ")",
zend_get_object_type_uc(ce),
ZSTR_VAL(ce->name), ai.cnt,
ai.cnt > 1 ? "s" : "",
DISPLAY_ABSTRACT_FN(0),
DISPLAY_ABSTRACT_FN(1),
DISPLAY_ABSTRACT_FN(2)
);
} else {
/* now everything should be fine and an added ZEND_ACC_IMPLICIT_ABSTRACT_CLASS should be removed */
ce->ce_flags &= ~ZEND_ACC_IMPLICIT_ABSTRACT_CLASS;
}
}
/* }}} */
typedef struct {
enum {
OBLIGATION_DEPENDENCY,
OBLIGATION_COMPATIBILITY,
OBLIGATION_PROPERTY_COMPATIBILITY,
OBLIGATION_CLASS_CONSTANT_COMPATIBILITY,
OBLIGATION_PROPERTY_HOOK,
} type;
union {
zend_class_entry *dependency_ce;
struct {
/* Traits may use temporary on-stack functions during inheritance checks,
* so use copies of functions here as well. */
zend_function parent_fn;
zend_function child_fn;
zend_class_entry *child_scope;
zend_class_entry *parent_scope;
};
struct {
const zend_property_info *parent_prop;
const zend_property_info *child_prop;
prop_variance variance;
};
struct {
const zend_string *const_name;
const zend_class_constant *parent_const;
const zend_class_constant *child_const;
};
struct {
const zend_property_info *hooked_prop;
const zend_function *hook_func;
};
};
} variance_obligation;
static void variance_obligation_dtor(zval *zv) {
efree(Z_PTR_P(zv));
}
static void variance_obligation_ht_dtor(zval *zv) {
zend_hash_destroy(Z_PTR_P(zv));
FREE_HASHTABLE(Z_PTR_P(zv));
}
static HashTable *get_or_init_obligations_for_class(zend_class_entry *ce) {
HashTable *ht;
zend_ulong key;
if (!CG(delayed_variance_obligations)) {
ALLOC_HASHTABLE(CG(delayed_variance_obligations));
zend_hash_init(CG(delayed_variance_obligations), 0, NULL, variance_obligation_ht_dtor, 0);
}
key = (zend_ulong) (uintptr_t) ce;
ht = zend_hash_index_find_ptr(CG(delayed_variance_obligations), key);
if (ht) {
return ht;
}
ALLOC_HASHTABLE(ht);
zend_hash_init(ht, 0, NULL, variance_obligation_dtor, 0);
zend_hash_index_add_new_ptr(CG(delayed_variance_obligations), key, ht);
ce->ce_flags |= ZEND_ACC_UNRESOLVED_VARIANCE;
return ht;
}
static void add_dependency_obligation(zend_class_entry *ce, zend_class_entry *dependency_ce) {
HashTable *obligations = get_or_init_obligations_for_class(ce);
variance_obligation *obligation = emalloc(sizeof(variance_obligation));
obligation->type = OBLIGATION_DEPENDENCY;
obligation->dependency_ce = dependency_ce;
zend_hash_next_index_insert_ptr(obligations, obligation);
}
static void add_compatibility_obligation(
zend_class_entry *ce,
const zend_function *child_fn, zend_class_entry *child_scope,
const zend_function *parent_fn, zend_class_entry *parent_scope) {
HashTable *obligations = get_or_init_obligations_for_class(ce);
variance_obligation *obligation = emalloc(sizeof(variance_obligation));
obligation->type = OBLIGATION_COMPATIBILITY;
/* Copy functions, because they may be stack-allocated in the case of traits. */
if (child_fn->common.type == ZEND_INTERNAL_FUNCTION) {
memcpy(&obligation->child_fn, child_fn, sizeof(zend_internal_function));
} else {
memcpy(&obligation->child_fn, child_fn, sizeof(zend_op_array));
}
if (parent_fn->common.type == ZEND_INTERNAL_FUNCTION) {
memcpy(&obligation->parent_fn, parent_fn, sizeof(zend_internal_function));
} else {
memcpy(&obligation->parent_fn, parent_fn, sizeof(zend_op_array));
}
obligation->child_scope = child_scope;
obligation->parent_scope = parent_scope;
zend_hash_next_index_insert_ptr(obligations, obligation);
}
static void add_property_compatibility_obligation(
zend_class_entry *ce, const zend_property_info *child_prop,
const zend_property_info *parent_prop, prop_variance variance) {
HashTable *obligations = get_or_init_obligations_for_class(ce);
variance_obligation *obligation = emalloc(sizeof(variance_obligation));
obligation->type = OBLIGATION_PROPERTY_COMPATIBILITY;
obligation->child_prop = child_prop;
obligation->parent_prop = parent_prop;
obligation->variance = variance;
zend_hash_next_index_insert_ptr(obligations, obligation);
}
static void add_class_constant_compatibility_obligation(
zend_class_entry *ce, const zend_class_constant *child_const,
const zend_class_constant *parent_const, const zend_string *const_name) {
HashTable *obligations = get_or_init_obligations_for_class(ce);
variance_obligation *obligation = emalloc(sizeof(variance_obligation));
obligation->type = OBLIGATION_CLASS_CONSTANT_COMPATIBILITY;
obligation->const_name = const_name;
obligation->child_const = child_const;
obligation->parent_const = parent_const;
zend_hash_next_index_insert_ptr(obligations, obligation);
}
static void add_property_hook_obligation(
zend_class_entry *ce, const zend_property_info *hooked_prop, const zend_function *hook_func) {
HashTable *obligations = get_or_init_obligations_for_class(ce);
variance_obligation *obligation = emalloc(sizeof(variance_obligation));
obligation->type = OBLIGATION_PROPERTY_HOOK;
obligation->hooked_prop = hooked_prop;
obligation->hook_func = hook_func;
zend_hash_next_index_insert_ptr(obligations, obligation);
}
static void resolve_delayed_variance_obligations(zend_class_entry *ce);
static void check_variance_obligation(variance_obligation *obligation) {
if (obligation->type == OBLIGATION_DEPENDENCY) {
zend_class_entry *dependency_ce = obligation->dependency_ce;
if (dependency_ce->ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE) {
zend_class_entry *orig_linking_class = CG(current_linking_class);
CG(current_linking_class) =
(dependency_ce->ce_flags & ZEND_ACC_CACHEABLE) ? dependency_ce : NULL;
resolve_delayed_variance_obligations(dependency_ce);
CG(current_linking_class) = orig_linking_class;
}
} else if (obligation->type == OBLIGATION_COMPATIBILITY) {
inheritance_status status = zend_do_perform_implementation_check(
&obligation->child_fn, obligation->child_scope,
&obligation->parent_fn, obligation->parent_scope);
if (UNEXPECTED(status != INHERITANCE_SUCCESS)) {
emit_incompatible_method_error(
&obligation->child_fn, obligation->child_scope,
&obligation->parent_fn, obligation->parent_scope, status);
}
/* Either the compatibility check was successful or only threw a warning. */
} else if (obligation->type == OBLIGATION_PROPERTY_COMPATIBILITY) {
verify_property_type_compatibility(obligation->parent_prop, obligation->child_prop, obligation->variance, true, true);
} else if (obligation->type == OBLIGATION_CLASS_CONSTANT_COMPATIBILITY) {
inheritance_status status =
class_constant_types_compatible(obligation->parent_const, obligation->child_const);
if (status != INHERITANCE_SUCCESS) {
emit_incompatible_class_constant_error(obligation->child_const, obligation->parent_const, obligation->const_name);
}
} else if (obligation->type == OBLIGATION_PROPERTY_HOOK) {
inheritance_status status = zend_verify_property_hook_variance(obligation->hooked_prop, obligation->hook_func);
if (status != INHERITANCE_SUCCESS) {
zend_hooked_property_variance_error(obligation->hooked_prop);
}
} else {
ZEND_UNREACHABLE();
}
}
static void load_delayed_classes(zend_class_entry *ce) {
HashTable *delayed_autoloads = CG(delayed_autoloads);
if (!delayed_autoloads) {
return;
}
/* Autoloading can trigger linking of another class, which may register new delayed autoloads.
* For that reason, this code uses a loop that pops and loads the first element of the HT. If
* this triggers linking, then the remaining classes may get loaded when linking the newly
* loaded class. This is important, as otherwise necessary dependencies may not be available
* if the new class is lower in the hierarchy than the current one. */
HashPosition pos = 0;
zend_string *name;
zend_ulong idx;
while (zend_hash_get_current_key_ex(delayed_autoloads, &name, &idx, &pos)
!= HASH_KEY_NON_EXISTENT) {
zend_string_addref(name);
zend_hash_del(delayed_autoloads, name);
zend_lookup_class(name);
zend_string_release(name);
if (EG(exception)) {
zend_exception_uncaught_error(
"During inheritance of %s, while autoloading %s",
ZSTR_VAL(ce->name), ZSTR_VAL(name));
}
}
}
static void resolve_delayed_variance_obligations(zend_class_entry *ce) {
HashTable *all_obligations = CG(delayed_variance_obligations), *obligations;
zend_ulong num_key = (zend_ulong) (uintptr_t) ce;
ZEND_ASSERT(all_obligations != NULL);
obligations = zend_hash_index_find_ptr(all_obligations, num_key);
ZEND_ASSERT(obligations != NULL);
variance_obligation *obligation;
ZEND_HASH_FOREACH_PTR(obligations, obligation) {
check_variance_obligation(obligation);
} ZEND_HASH_FOREACH_END();
zend_inheritance_check_override(ce);
ce->ce_flags &= ~ZEND_ACC_UNRESOLVED_VARIANCE;
ce->ce_flags |= ZEND_ACC_LINKED;
zend_hash_index_del(all_obligations, num_key);
}
static void check_unrecoverable_load_failure(const zend_class_entry *ce) {
/* If this class has been used while unlinked through a variance obligation, it is not legal
* to remove the class from the class table and throw an exception, because there is already
* a dependence on the inheritance hierarchy of this specific class. Instead we fall back to
* a fatal error, as would happen if we did not allow exceptions in the first place. */
if (CG(unlinked_uses)
&& zend_hash_index_del(CG(unlinked_uses), (zend_long)(uintptr_t)ce) == SUCCESS) {
zend_exception_uncaught_error(
"During inheritance of %s with variance dependencies", ZSTR_VAL(ce->name));
}
}
#define zend_update_inherited_handler(handler) do { \
if (ce->handler == (zend_function*)op_array) { \
ce->handler = (zend_function*)new_op_array; \
} \
} while (0)
static zend_op_array *zend_lazy_method_load(
zend_op_array *op_array, zend_class_entry *ce, const zend_class_entry *pce) {
ZEND_ASSERT(op_array->type == ZEND_USER_FUNCTION);
ZEND_ASSERT(op_array->scope == pce);
ZEND_ASSERT(op_array->prototype == NULL);
zend_op_array *new_op_array = zend_arena_alloc(&CG(arena), sizeof(zend_op_array));
memcpy(new_op_array, op_array, sizeof(zend_op_array));
new_op_array->fn_flags &= ~ZEND_ACC_IMMUTABLE;
new_op_array->scope = ce;
ZEND_MAP_PTR_INIT(new_op_array->run_time_cache, NULL);
ZEND_MAP_PTR_INIT(new_op_array->static_variables_ptr, NULL);
return new_op_array;
}
static zend_class_entry *zend_lazy_class_load(zend_class_entry *pce)
{
zend_class_entry *ce;
Bucket *p, *end;
ce = zend_arena_alloc(&CG(arena), sizeof(zend_class_entry));
memcpy(ce, pce, sizeof(zend_class_entry));
ce->ce_flags &= ~ZEND_ACC_IMMUTABLE;
ce->refcount = 1;
ce->inheritance_cache = NULL;
if (CG(compiler_options) & ZEND_COMPILE_PRELOAD) {
ZEND_MAP_PTR_NEW(ce->mutable_data);
} else {
ZEND_MAP_PTR_INIT(ce->mutable_data, NULL);
}
/* properties */
if (ce->default_properties_table) {
zval *dst = emalloc(sizeof(zval) * ce->default_properties_count);
zval *src = ce->default_properties_table;
zval *end = src + ce->default_properties_count;
ce->default_properties_table = dst;
for (; src != end; src++, dst++) {
ZVAL_COPY_VALUE_PROP(dst, src);
}
}
/* methods */
ce->function_table.pDestructor = ZEND_FUNCTION_DTOR;
if (!(HT_FLAGS(&ce->function_table) & HASH_FLAG_UNINITIALIZED)) {
p = emalloc(HT_SIZE(&ce->function_table));
memcpy(p, HT_GET_DATA_ADDR(&ce->function_table), HT_USED_SIZE(&ce->function_table));
HT_SET_DATA_ADDR(&ce->function_table, p);
p = ce->function_table.arData;
end = p + ce->function_table.nNumUsed;
for (; p != end; p++) {
zend_op_array *op_array = Z_PTR(p->val);
zend_op_array *new_op_array = Z_PTR(p->val) = zend_lazy_method_load(op_array, ce, pce);
zend_update_inherited_handler(constructor);
zend_update_inherited_handler(destructor);
zend_update_inherited_handler(clone);
zend_update_inherited_handler(__get);
zend_update_inherited_handler(__set);
zend_update_inherited_handler(__call);
zend_update_inherited_handler(__isset);
zend_update_inherited_handler(__unset);
zend_update_inherited_handler(__tostring);
zend_update_inherited_handler(__callstatic);
zend_update_inherited_handler(__debugInfo);
zend_update_inherited_handler(__serialize);
zend_update_inherited_handler(__unserialize);
}
}
/* static members */
if (ce->default_static_members_table) {
zval *dst = emalloc(sizeof(zval) * ce->default_static_members_count);
zval *src = ce->default_static_members_table;
zval *end = src + ce->default_static_members_count;
ce->default_static_members_table = dst;
for (; src != end; src++, dst++) {
ZVAL_COPY_VALUE(dst, src);
}
}
ZEND_MAP_PTR_INIT(ce->static_members_table, NULL);
/* properties_info */
if (!(HT_FLAGS(&ce->properties_info) & HASH_FLAG_UNINITIALIZED)) {
p = emalloc(HT_SIZE(&ce->properties_info));
memcpy(p, HT_GET_DATA_ADDR(&ce->properties_info), HT_USED_SIZE(&ce->properties_info));
HT_SET_DATA_ADDR(&ce->properties_info, p);
p = ce->properties_info.arData;
end = p + ce->properties_info.nNumUsed;
for (; p != end; p++) {
zend_property_info *prop_info, *new_prop_info;
prop_info = Z_PTR(p->val);
ZEND_ASSERT(prop_info->ce == pce);
ZEND_ASSERT(prop_info->prototype == prop_info);
new_prop_info= zend_arena_alloc(&CG(arena), sizeof(zend_property_info));
Z_PTR(p->val) = new_prop_info;
memcpy(new_prop_info, prop_info, sizeof(zend_property_info));
new_prop_info->ce = ce;
new_prop_info->prototype = new_prop_info;
/* Deep copy the type information */
zend_type_copy_ctor(&new_prop_info->type, /* use_arena */ true, /* persistent */ false);
if (new_prop_info->hooks) {
new_prop_info->hooks = zend_arena_alloc(&CG(arena), ZEND_PROPERTY_HOOK_STRUCT_SIZE);
memcpy(new_prop_info->hooks, prop_info->hooks, ZEND_PROPERTY_HOOK_STRUCT_SIZE);
for (uint32_t i = 0; i < ZEND_PROPERTY_HOOK_COUNT; i++) {
if (new_prop_info->hooks[i]) {
zend_op_array *hook = zend_lazy_method_load((zend_op_array *) new_prop_info->hooks[i], ce, pce);
ZEND_ASSERT(hook->prop_info == prop_info);
hook->prop_info = new_prop_info;
new_prop_info->ce = ce;
new_prop_info->hooks[i] = (zend_function *) hook;
}
}
}
}
}
/* constants table */
if (!(HT_FLAGS(&ce->constants_table) & HASH_FLAG_UNINITIALIZED)) {
p = emalloc(HT_SIZE(&ce->constants_table));
memcpy(p, HT_GET_DATA_ADDR(&ce->constants_table), HT_USED_SIZE(&ce->constants_table));
HT_SET_DATA_ADDR(&ce->constants_table, p);
p = ce->constants_table.arData;
end = p + ce->constants_table.nNumUsed;
for (; p != end; p++) {
zend_class_constant *c, *new_c;
c = Z_PTR(p->val);
ZEND_ASSERT(c->ce == pce);
new_c = zend_arena_alloc(&CG(arena), sizeof(zend_class_constant));
Z_PTR(p->val) = new_c;
memcpy(new_c, c, sizeof(zend_class_constant));
new_c->ce = ce;
}
}
return ce;
}
#ifndef ZEND_OPCACHE_SHM_REATTACHMENT
# define UPDATE_IS_CACHEABLE(ce) do { \
if ((ce)->type == ZEND_USER_CLASS) { \
is_cacheable &= (ce)->ce_flags; \
} \
} while (0)
#else
// TODO: ASLR may cause different addresses in different workers ???
# define UPDATE_IS_CACHEABLE(ce) do { \
is_cacheable &= (ce)->ce_flags; \
} while (0)
#endif
ZEND_API zend_class_entry *zend_do_link_class(zend_class_entry *ce, zend_string *lc_parent_name, zend_string *key) /* {{{ */
{
/* Load parent/interface dependencies first, so we can still gracefully abort linking
* with an exception and remove the class from the class table. This is only possible
* if no variance obligations on the current class have been added during autoloading. */
zend_class_entry *parent = NULL;
zend_class_entry **traits_and_interfaces = NULL;
zend_class_entry *proto = NULL;
zend_class_entry *orig_linking_class;
uint32_t is_cacheable = ce->ce_flags & ZEND_ACC_IMMUTABLE;
uint32_t i, j;
zval *zv;
ALLOCA_FLAG(use_heap)
SET_ALLOCA_FLAG(use_heap);
ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_LINKED));
if (ce->parent_name) {
parent = zend_fetch_class_by_name(
ce->parent_name, lc_parent_name,
ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED | ZEND_FETCH_CLASS_EXCEPTION);
if (!parent) {
check_unrecoverable_load_failure(ce);
return NULL;
}
UPDATE_IS_CACHEABLE(parent);
}
if (ce->num_traits || ce->num_interfaces) {
traits_and_interfaces = do_alloca(sizeof(zend_class_entry*) * (ce->num_traits + ce->num_interfaces), use_heap);
for (i = 0; i < ce->num_traits; i++) {
zend_class_entry *trait = zend_fetch_class_by_name(ce->trait_names[i].name,
ce->trait_names[i].lc_name, ZEND_FETCH_CLASS_TRAIT);
if (UNEXPECTED(trait == NULL)) {
free_alloca(traits_and_interfaces, use_heap);
return NULL;
}
if (UNEXPECTED(!(trait->ce_flags & ZEND_ACC_TRAIT))) {
zend_error_noreturn(E_ERROR, "%s cannot use %s - it is not a trait", ZSTR_VAL(ce->name), ZSTR_VAL(trait->name));
free_alloca(traits_and_interfaces, use_heap);
return NULL;
}
for (j = 0; j < i; j++) {
if (traits_and_interfaces[j] == trait) {
/* skip duplications */
trait = NULL;
break;
}
}
traits_and_interfaces[i] = trait;
if (trait) {
UPDATE_IS_CACHEABLE(trait);
}
}
}
if (ce->num_interfaces) {
for (i = 0; i < ce->num_interfaces; i++) {
zend_class_entry *iface = zend_fetch_class_by_name(
ce->interface_names[i].name, ce->interface_names[i].lc_name,
ZEND_FETCH_CLASS_INTERFACE |
ZEND_FETCH_CLASS_ALLOW_NEARLY_LINKED | ZEND_FETCH_CLASS_EXCEPTION);
if (!iface) {
check_unrecoverable_load_failure(ce);
free_alloca(traits_and_interfaces, use_heap);
return NULL;
}
traits_and_interfaces[ce->num_traits + i] = iface;
if (iface) {
UPDATE_IS_CACHEABLE(iface);
}
}
}
#ifndef ZEND_WIN32
if (ce->ce_flags & ZEND_ACC_ENUM) {
/* We will add internal methods. */
is_cacheable = false;
}
#endif
bool orig_record_errors = EG(record_errors);
if (ce->ce_flags & ZEND_ACC_IMMUTABLE && is_cacheable) {
if (zend_inheritance_cache_get && zend_inheritance_cache_add) {
zend_class_entry *ret = zend_inheritance_cache_get(ce, parent, traits_and_interfaces);
if (ret) {
if (traits_and_interfaces) {
free_alloca(traits_and_interfaces, use_heap);
}
zv = zend_hash_find_known_hash(CG(class_table), key);
Z_CE_P(zv) = ret;
return ret;
}
/* Make sure warnings (such as deprecations) thrown during inheritance
* will be recorded in the inheritance cache. */
zend_begin_record_errors();
} else {
is_cacheable = 0;
}
proto = ce;
}
zend_try {
if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
/* Lazy class loading */
ce = zend_lazy_class_load(ce);
zv = zend_hash_find_known_hash(CG(class_table), key);
Z_CE_P(zv) = ce;
} else if (ce->ce_flags & ZEND_ACC_FILE_CACHED) {
/* Lazy class loading */
ce = zend_lazy_class_load(ce);
ce->ce_flags &= ~ZEND_ACC_FILE_CACHED;
zv = zend_hash_find_known_hash(CG(class_table), key);
Z_CE_P(zv) = ce;
}
if (CG(unlinked_uses)) {
zend_hash_index_del(CG(unlinked_uses), (zend_long)(uintptr_t) ce);
}
orig_linking_class = CG(current_linking_class);
CG(current_linking_class) = is_cacheable ? ce : NULL;
if (ce->ce_flags & ZEND_ACC_ENUM) {
/* Only register builtin enum methods during inheritance to avoid persisting them in
* opcache. */
zend_enum_register_funcs(ce);
}
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
zend_link_hooked_object_iter(ce);
#endif
if (parent) {
if (!(parent->ce_flags & ZEND_ACC_LINKED)) {
add_dependency_obligation(ce, parent);
}
zend_do_inheritance(ce, parent);
}
if (ce->num_traits) {
zend_do_bind_traits(ce, traits_and_interfaces);
}
if (ce->num_interfaces) {
/* Also copy the parent interfaces here, so we don't need to reallocate later. */
uint32_t num_parent_interfaces = parent ? parent->num_interfaces : 0;
zend_class_entry **interfaces = emalloc(
sizeof(zend_class_entry *) * (ce->num_interfaces + num_parent_interfaces));
if (num_parent_interfaces) {
memcpy(interfaces, parent->interfaces,
sizeof(zend_class_entry *) * num_parent_interfaces);
}
memcpy(interfaces + num_parent_interfaces, traits_and_interfaces + ce->num_traits,
sizeof(zend_class_entry *) * ce->num_interfaces);
zend_do_implement_interfaces(ce, interfaces);
} else if (parent && parent->num_interfaces) {
zend_do_inherit_interfaces(ce, parent);
}
if (!(ce->ce_flags & (ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT))
&& (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS))
) {
zend_verify_abstract_class(ce);
}
if (ce->ce_flags & ZEND_ACC_ENUM) {
zend_verify_enum(ce);
}
if (ce->num_hooked_prop_variance_checks) {
const zend_property_info *prop_info;
ZEND_HASH_MAP_FOREACH_PTR(&ce->properties_info, prop_info) {
if (prop_info->ce == ce && prop_info->hooks && prop_info->hooks[ZEND_PROPERTY_HOOK_SET]) {
switch (zend_verify_property_hook_variance(prop_info, prop_info->hooks[ZEND_PROPERTY_HOOK_SET])) {
case INHERITANCE_SUCCESS:
break;
case INHERITANCE_ERROR:
zend_hooked_property_variance_error(prop_info);
break;
case INHERITANCE_UNRESOLVED:
add_property_hook_obligation(ce, prop_info, prop_info->hooks[ZEND_PROPERTY_HOOK_SET]);
break;
case INHERITANCE_WARNING:
ZEND_UNREACHABLE();
}
}
} ZEND_HASH_FOREACH_END();
}
/* Normally Stringable is added during compilation. However, if it is imported from a trait,
* we need to explicitly add the interface here. */
if (ce->__tostring && !(ce->ce_flags & ZEND_ACC_TRAIT)
&& !zend_class_implements_interface(ce, zend_ce_stringable)) {
ZEND_ASSERT(ce->__tostring->common.fn_flags & ZEND_ACC_TRAIT_CLONE);
ce->ce_flags |= ZEND_ACC_RESOLVED_INTERFACES;
ce->num_interfaces++;
ce->interfaces = perealloc(ce->interfaces,
sizeof(zend_class_entry *) * ce->num_interfaces, ce->type == ZEND_INTERNAL_CLASS);
ce->interfaces[ce->num_interfaces - 1] = zend_ce_stringable;
do_interface_implementation(ce, zend_ce_stringable);
}
zend_build_properties_info_table(ce);
} zend_catch {
/* Do not leak recorded errors to the next linked class. */
if (!orig_record_errors) {
EG(record_errors) = false;
zend_free_recorded_errors();
}
zend_bailout();
} zend_end_try();
EG(record_errors) = orig_record_errors;
if (!(ce->ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE)) {
zend_inheritance_check_override(ce);
ce->ce_flags |= ZEND_ACC_LINKED;
} else {
ce->ce_flags |= ZEND_ACC_NEARLY_LINKED;
if (CG(current_linking_class)) {
ce->ce_flags |= ZEND_ACC_CACHEABLE;
}
load_delayed_classes(ce);
if (ce->ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE) {
resolve_delayed_variance_obligations(ce);
}
if (ce->ce_flags & ZEND_ACC_CACHEABLE) {
ce->ce_flags &= ~ZEND_ACC_CACHEABLE;
} else {
CG(current_linking_class) = NULL;
}
}
if (!CG(current_linking_class)) {
is_cacheable = 0;
}
CG(current_linking_class) = orig_linking_class;
if (is_cacheable) {
HashTable *ht = (HashTable*)ce->inheritance_cache;
zend_class_entry *new_ce;
ce->inheritance_cache = NULL;
new_ce = zend_inheritance_cache_add(ce, proto, parent, traits_and_interfaces, ht);
if (new_ce) {
zv = zend_hash_find_known_hash(CG(class_table), key);
ce = new_ce;
Z_CE_P(zv) = ce;
}
if (ht) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
}
}
if (!orig_record_errors) {
zend_free_recorded_errors();
}
if (traits_and_interfaces) {
free_alloca(traits_and_interfaces, use_heap);
}
if (ZSTR_HAS_CE_CACHE(ce->name)) {
ZSTR_SET_CE_CACHE(ce->name, ce);
}
return ce;
}
/* }}} */
/* Check whether early binding is prevented due to unresolved types in inheritance checks. */
static inheritance_status zend_can_early_bind(zend_class_entry *ce, zend_class_entry *parent_ce) /* {{{ */
{
zend_string *key;
zend_function *parent_func;
const zend_property_info *parent_info;
const zend_class_constant *parent_const;
inheritance_status overall_status = INHERITANCE_SUCCESS;
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->function_table, key, parent_func) {
zval *zv = zend_hash_find_known_hash(&ce->function_table, key);
if (zv) {
zend_function *child_func = Z_FUNC_P(zv);
inheritance_status status =
do_inheritance_check_on_method(
child_func, child_func->common.scope,
parent_func, parent_func->common.scope,
ce, NULL,
ZEND_INHERITANCE_CHECK_SILENT | ZEND_INHERITANCE_CHECK_PROTO | ZEND_INHERITANCE_CHECK_VISIBILITY);
if (UNEXPECTED(status == INHERITANCE_WARNING)) {
overall_status = INHERITANCE_WARNING;
} else if (UNEXPECTED(status != INHERITANCE_SUCCESS)) {
return status;
}
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->properties_info, key, parent_info) {
const zval *zv;
if ((parent_info->flags & ZEND_ACC_PRIVATE) || !ZEND_TYPE_IS_SET(parent_info->type)) {
continue;
}
zv = zend_hash_find_known_hash(&ce->properties_info, key);
if (zv) {
const zend_property_info *child_info = Z_PTR_P(zv);
if (ZEND_TYPE_IS_SET(child_info->type)) {
inheritance_status status = verify_property_type_compatibility(parent_info, child_info, prop_get_variance(parent_info), false, false);
if (UNEXPECTED(status != INHERITANCE_SUCCESS)) {
return status;
}
}
}
} ZEND_HASH_FOREACH_END();
ZEND_HASH_MAP_FOREACH_STR_KEY_PTR(&parent_ce->constants_table, key, parent_const) {
const zval *zv;
if ((ZEND_CLASS_CONST_FLAGS(parent_const) & ZEND_ACC_PRIVATE) || !ZEND_TYPE_IS_SET(parent_const->type)) {
continue;
}
zv = zend_hash_find_known_hash(&ce->constants_table, key);
if (zv) {
const zend_class_constant *child_const = Z_PTR_P(zv);
if (ZEND_TYPE_IS_SET(child_const->type)) {
inheritance_status status = class_constant_types_compatible(parent_const, child_const);
ZEND_ASSERT(status != INHERITANCE_WARNING);
if (UNEXPECTED(status != INHERITANCE_SUCCESS)) {
return status;
}
}
}
} ZEND_HASH_FOREACH_END();
return overall_status;
}
/* }}} */
static zend_always_inline bool register_early_bound_ce(zval *delayed_early_binding, zend_string *lcname, zend_class_entry *ce) {
if (delayed_early_binding) {
if (EXPECTED(!(ce->ce_flags & ZEND_ACC_PRELOADED))) {
if (zend_hash_set_bucket_key(EG(class_table), (Bucket *)delayed_early_binding, lcname) != NULL) {
Z_CE_P(delayed_early_binding) = ce;
return true;
}
} else {
/* If preloading is used, don't replace the existing bucket, add a new one. */
if (zend_hash_add_ptr(EG(class_table), lcname, ce) != NULL) {
return true;
}
}
zend_class_entry *old_ce = zend_hash_find_ptr(EG(class_table), lcname);
ZEND_ASSERT(old_ce);
zend_class_redeclaration_error(E_COMPILE_ERROR, old_ce);
return false;
}
if (zend_hash_add_ptr(CG(class_table), lcname, ce) != NULL) {
return true;
}
return false;
}
ZEND_API zend_class_entry *zend_try_early_bind(zend_class_entry *ce, zend_class_entry *parent_ce, zend_string *lcname, zval *delayed_early_binding) /* {{{ */
{
inheritance_status status;
zend_class_entry *proto = NULL;
zend_class_entry *orig_linking_class;
if (ce->ce_flags & ZEND_ACC_LINKED) {
ZEND_ASSERT(ce->parent == NULL);
if (UNEXPECTED(!register_early_bound_ce(delayed_early_binding, lcname, ce))) {
return NULL;
}
zend_observer_class_linked_notify(ce, lcname);
return ce;
}
uint32_t is_cacheable = ce->ce_flags & ZEND_ACC_IMMUTABLE;
UPDATE_IS_CACHEABLE(parent_ce);
if (is_cacheable) {
if (zend_inheritance_cache_get && zend_inheritance_cache_add) {
zend_class_entry *ret = zend_inheritance_cache_get(ce, parent_ce, NULL);
if (ret) {
if (UNEXPECTED(!register_early_bound_ce(delayed_early_binding, lcname, ret))) {
return NULL;
}
zend_observer_class_linked_notify(ret, lcname);
return ret;
}
} else {
is_cacheable = 0;
}
proto = ce;
}
orig_linking_class = CG(current_linking_class);
CG(current_linking_class) = NULL;
status = zend_can_early_bind(ce, parent_ce);
CG(current_linking_class) = orig_linking_class;
if (EXPECTED(status != INHERITANCE_UNRESOLVED)) {
if (ce->ce_flags & ZEND_ACC_IMMUTABLE) {
/* Lazy class loading */
ce = zend_lazy_class_load(ce);
} else if (ce->ce_flags & ZEND_ACC_FILE_CACHED) {
/* Lazy class loading */
ce = zend_lazy_class_load(ce);
ce->ce_flags &= ~ZEND_ACC_FILE_CACHED;
}
if (UNEXPECTED(!register_early_bound_ce(delayed_early_binding, lcname, ce))) {
return NULL;
}
orig_linking_class = CG(current_linking_class);
CG(current_linking_class) = is_cacheable ? ce : NULL;
zend_try{
CG(zend_lineno) = ce->info.user.line_start;
if (is_cacheable) {
zend_begin_record_errors();
}
#ifdef ZEND_OPCACHE_SHM_REATTACHMENT
zend_link_hooked_object_iter(ce);
#endif
zend_do_inheritance_ex(ce, parent_ce, status == INHERITANCE_SUCCESS);
if (parent_ce && parent_ce->num_interfaces) {
zend_do_inherit_interfaces(ce, parent_ce);
}
zend_build_properties_info_table(ce);
if ((ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_INTERFACE|ZEND_ACC_TRAIT|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) == ZEND_ACC_IMPLICIT_ABSTRACT_CLASS) {
zend_verify_abstract_class(ce);
}
zend_inheritance_check_override(ce);
ZEND_ASSERT(!(ce->ce_flags & ZEND_ACC_UNRESOLVED_VARIANCE));
ce->ce_flags |= ZEND_ACC_LINKED;
CG(current_linking_class) = orig_linking_class;
} zend_catch {
EG(record_errors) = false;
zend_free_recorded_errors();
zend_bailout();
} zend_end_try();
EG(record_errors) = false;
if (is_cacheable) {
HashTable *ht = (HashTable*)ce->inheritance_cache;
zend_class_entry *new_ce;
ce->inheritance_cache = NULL;
new_ce = zend_inheritance_cache_add(ce, proto, parent_ce, NULL, ht);
if (new_ce) {
zval *zv = zend_hash_find_known_hash(CG(class_table), lcname);
ce = new_ce;
Z_CE_P(zv) = ce;
}
if (ht) {
zend_hash_destroy(ht);
FREE_HASHTABLE(ht);
}
}
if (ZSTR_HAS_CE_CACHE(ce->name)) {
ZSTR_SET_CE_CACHE(ce->name, ce);
}
zend_observer_class_linked_notify(ce, lcname);
return ce;
}
return NULL;
}
/* }}} */
|