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
|
/* Classes for saving, deduplicating, and emitting analyzer diagnostics.
Copyright (C) 2019-2022 Free Software Foundation, Inc.
Contributed by David Malcolm <dmalcolm@redhat.com>.
This file is part of GCC.
GCC is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
GCC is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3. If not see
<http://www.gnu.org/licenses/>. */
#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tree.h"
#include "pretty-print.h"
#include "gcc-rich-location.h"
#include "gimple-pretty-print.h"
#include "function.h"
#include "diagnostic-core.h"
#include "diagnostic-event-id.h"
#include "diagnostic-path.h"
#include "alloc-pool.h"
#include "fibonacci_heap.h"
#include "shortest-paths.h"
#include "sbitmap.h"
#include "bitmap.h"
#include "tristate.h"
#include "selftest.h"
#include "ordered-hash-map.h"
#include "json.h"
#include "analyzer/analyzer.h"
#include "analyzer/analyzer-logging.h"
#include "analyzer/sm.h"
#include "analyzer/pending-diagnostic.h"
#include "analyzer/diagnostic-manager.h"
#include "analyzer/call-string.h"
#include "analyzer/program-point.h"
#include "analyzer/store.h"
#include "analyzer/region-model.h"
#include "analyzer/constraint-manager.h"
#include "cfg.h"
#include "basic-block.h"
#include "gimple.h"
#include "gimple-iterator.h"
#include "cgraph.h"
#include "digraph.h"
#include "analyzer/supergraph.h"
#include "analyzer/program-state.h"
#include "analyzer/exploded-graph.h"
#include "analyzer/trimmed-graph.h"
#include "analyzer/feasible-graph.h"
#include "analyzer/checker-path.h"
#include "analyzer/reachability.h"
#if ENABLE_ANALYZER
namespace ana {
class feasible_worklist;
/* State for finding the shortest feasible exploded_path for a
saved_diagnostic.
This is shared between all diagnostics, so that we avoid repeating work. */
class epath_finder
{
public:
epath_finder (const exploded_graph &eg)
: m_eg (eg),
m_sep (NULL)
{
/* This is shared by all diagnostics, but only needed if
!flag_analyzer_feasibility. */
if (!flag_analyzer_feasibility)
m_sep = new shortest_exploded_paths (eg, eg.get_origin (),
SPS_FROM_GIVEN_ORIGIN);
}
~epath_finder () { delete m_sep; }
logger *get_logger () const { return m_eg.get_logger (); }
exploded_path *get_best_epath (const exploded_node *target_enode,
const char *desc, unsigned diag_idx,
feasibility_problem **out_problem);
private:
DISABLE_COPY_AND_ASSIGN(epath_finder);
exploded_path *explore_feasible_paths (const exploded_node *target_enode,
const char *desc, unsigned diag_idx);
bool process_worklist_item (feasible_worklist *worklist,
const trimmed_graph &tg,
feasible_graph *fg,
const exploded_node *target_enode,
unsigned diag_idx,
exploded_path **out_best_path) const;
void dump_trimmed_graph (const exploded_node *target_enode,
const char *desc, unsigned diag_idx,
const trimmed_graph &tg,
const shortest_paths<eg_traits, exploded_path> &sep);
void dump_feasible_graph (const exploded_node *target_enode,
const char *desc, unsigned diag_idx,
const feasible_graph &fg);
void dump_feasible_path (const exploded_node *target_enode,
unsigned diag_idx,
const feasible_graph &fg,
const feasible_node &fnode) const;
const exploded_graph &m_eg;
shortest_exploded_paths *m_sep;
};
/* class epath_finder. */
/* Get the "best" exploded_path for reaching ENODE from the origin,
returning ownership of it to the caller.
Ideally we want to report the shortest feasible path.
Return NULL if we could not find a feasible path
(when flag_analyzer_feasibility is true).
If flag_analyzer_feasibility is false, then simply return the
shortest path.
Use DESC and DIAG_IDX when logging.
Write any feasibility_problem to *OUT_PROBLEM. */
exploded_path *
epath_finder::get_best_epath (const exploded_node *enode,
const char *desc, unsigned diag_idx,
feasibility_problem **out_problem)
{
logger *logger = get_logger ();
LOG_SCOPE (logger);
unsigned snode_idx = enode->get_supernode ()->m_index;
if (logger)
logger->log ("considering %qs at EN: %i, SN: %i (sd: %i)",
desc, enode->m_index, snode_idx, diag_idx);
/* State-merging means that not every path in the egraph corresponds
to a feasible one w.r.t. states.
We want to find the shortest feasible path from the origin to ENODE
in the egraph. */
if (flag_analyzer_feasibility)
{
/* Attempt to find the shortest feasible path using feasible_graph. */
if (logger)
logger->log ("trying to find shortest feasible path");
if (exploded_path *epath = explore_feasible_paths (enode, desc, diag_idx))
{
if (logger)
logger->log ("accepting %qs at EN: %i, SN: %i (sd: %i)"
" with feasible path (length: %i)",
desc, enode->m_index, snode_idx, diag_idx,
epath->length ());
return epath;
}
else
{
if (logger)
logger->log ("rejecting %qs at EN: %i, SN: %i (sd: %i)"
" due to not finding feasible path",
desc, enode->m_index, snode_idx, diag_idx);
return NULL;
}
}
else
{
/* As a crude approximation to shortest feasible path, simply find
the shortest path, and note whether it is feasible.
There could be longer feasible paths within the egraph, so this
approach would lead to diagnostics being falsely rejected
(PR analyzer/96374). */
if (logger)
logger->log ("trying to find shortest path ignoring feasibility");
gcc_assert (m_sep);
exploded_path *epath
= new exploded_path (m_sep->get_shortest_path (enode));
if (epath->feasible_p (logger, out_problem, m_eg.get_engine (), &m_eg))
{
if (logger)
logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i)"
" with feasible path (length: %i)",
desc, enode->m_index, snode_idx, diag_idx,
epath->length ());
}
else
{
if (logger)
logger->log ("accepting %qs at EN: %i, SN: %i (sn: %i) (length: %i)"
" despite infeasible path (due to %qs)",
desc, enode->m_index, snode_idx, diag_idx,
epath->length (),
"-fno-analyzer-feasibility");
}
return epath;
}
}
/* A class for managing the worklist of feasible_nodes in
epath_finder::explore_feasible_paths, prioritizing them
so that shorter paths appear earlier in the queue. */
class feasible_worklist
{
public:
feasible_worklist (const shortest_paths<eg_traits, exploded_path> &sep)
: m_queue (key_t (*this, NULL)),
m_sep (sep)
{
}
feasible_node *take_next () { return m_queue.extract_min (); }
void add_node (feasible_node *fnode)
{
m_queue.insert (key_t (*this, fnode), fnode);
}
private:
struct key_t
{
key_t (const feasible_worklist &w, feasible_node *fnode)
: m_worklist (w), m_fnode (fnode)
{}
bool operator< (const key_t &other) const
{
return cmp (*this, other) < 0;
}
bool operator== (const key_t &other) const
{
return cmp (*this, other) == 0;
}
bool operator> (const key_t &other) const
{
return !(*this == other || *this < other);
}
private:
static int cmp (const key_t &ka, const key_t &kb)
{
/* Choose the node for which if the remaining path were feasible,
it would be the shortest path (summing the length of the
known-feasible path so far with that of the remaining
possibly-feasible path). */
int ca = ka.m_worklist.get_estimated_cost (ka.m_fnode);
int cb = kb.m_worklist.get_estimated_cost (kb.m_fnode);
return ca - cb;
}
const feasible_worklist &m_worklist;
feasible_node *m_fnode;
};
/* Get the estimated length of a path involving FNODE from
the origin to the target enode.
Sum the length of the known-feasible path so far with
that of the remaining possibly-feasible path. */
int get_estimated_cost (const feasible_node *fnode) const
{
unsigned length_so_far = fnode->get_path_length ();
int shortest_remaining_path
= m_sep.get_shortest_distance (fnode->get_inner_node ());
gcc_assert (shortest_remaining_path >= 0);
/* This should be true since we're only exploring nodes within
the trimmed graph (and we anticipate it being much smaller
than this, and thus not overflowing the sum). */
gcc_assert (shortest_remaining_path < INT_MAX);
return length_so_far + shortest_remaining_path;
}
/* Priority queue, backed by a fibonacci_heap. */
typedef fibonacci_heap<key_t, feasible_node> queue_t;
queue_t m_queue;
const shortest_paths<eg_traits, exploded_path> &m_sep;
};
/* When we're building the exploded graph we want to simplify
overly-complicated symbolic values down to "UNKNOWN" to try to avoid
state explosions and unbounded chains of exploration.
However, when we're building the feasibility graph for a diagnostic
(actually a tree), we don't want UNKNOWN values, as conditions on them
are also unknown: we don't want to have a contradiction such as a path
where (VAL != 0) and then (VAL == 0) along the same path.
Hence this is an RAII class for temporarily disabling complexity-checking
in the region_model_manager, for use within
epath_finder::explore_feasible_paths.
We also disable the creation of unknown_svalue instances during feasibility
checking, instead creating unique svalues, to avoid paradoxes in paths. */
class auto_checking_feasibility
{
public:
auto_checking_feasibility (region_model_manager *mgr) : m_mgr (mgr)
{
m_mgr->begin_checking_feasibility ();
}
~auto_checking_feasibility ()
{
m_mgr->end_checking_feasibility ();
}
private:
region_model_manager *m_mgr;
};
/* Attempt to find the shortest feasible path from the origin to
TARGET_ENODE by iteratively building a feasible_graph, in which
every path to a feasible_node is feasible by construction.
We effectively explore the tree of feasible paths in order of shortest
path until we either find a feasible path to TARGET_ENODE, or hit
a limit and give up.
Preliminaries:
- Find the shortest path from each node to the TARGET_ENODE (without
checking feasibility), so that we can prioritize our worklist.
- Construct a trimmed_graph: the subset of nodes/edges that
are on a path that eventually reaches TARGET_ENODE. We will only need
to consider these when considering the shortest feasible path.
Build a feasible_graph, in which every path to a feasible_node
is feasible by construction.
We use a worklist to flatten the exploration into an iteration.
Starting at the origin, find feasible out-edges within the trimmed graph.
At each stage, choose the node for which if the remaining path were feasible,
it would be the shortest path (summing the length of the known-feasible path
so far with that of the remaining possibly-feasible path).
This way, the first feasible path we find to TARGET_ENODE is the shortest.
We start by trying the shortest possible path, but if that fails,
we explore progressively longer paths, eventually trying iterations through
loops. The exploration is captured in the feasible_graph, which can be
dumped as a .dot file to visualize the exploration. The indices of the
feasible_nodes show the order in which they were created.
This is something of a brute-force approach, but the trimmed_graph
hopefully keeps the complexity manageable.
Terminate with failure when the number of infeasible edges exceeds
a threshold (--param=analyzer-max-infeasible-edges=).
This is guaranteed to eventually lead to terminatation, as
we can't keep creating feasible nodes without eventually
either reaching an infeasible edge, or reaching the
TARGET_ENODE. Specifically, there can't be a cycle of
feasible edges that doesn't reach the target_enode without
an out-edge that either fails feasibility or gets closer
to the TARGET_ENODE: on each iteration we are either:
- effectively getting closer to the TARGET_ENODE (which can't
continue forever without reaching the target), or
- getting monotonically closer to the termination threshold. */
exploded_path *
epath_finder::explore_feasible_paths (const exploded_node *target_enode,
const char *desc, unsigned diag_idx)
{
logger *logger = get_logger ();
LOG_SCOPE (logger);
region_model_manager *mgr = m_eg.get_engine ()->get_model_manager ();
/* Determine the shortest path to TARGET_ENODE from each node in
the exploded graph. */
shortest_paths<eg_traits, exploded_path> sep
(m_eg, target_enode, SPS_TO_GIVEN_TARGET);
/* Construct a trimmed_graph: the subset of nodes/edges that
are on a path that eventually reaches TARGET_ENODE.
We only need to consider these when considering the shortest
feasible path. */
trimmed_graph tg (m_eg, target_enode);
if (flag_dump_analyzer_feasibility)
dump_trimmed_graph (target_enode, desc, diag_idx, tg, sep);
feasible_graph fg;
feasible_worklist worklist (sep);
/* Populate the worklist with the origin node. */
{
feasibility_state init_state (mgr, m_eg.get_supergraph ());
feasible_node *origin = fg.add_node (m_eg.get_origin (), init_state, 0);
worklist.add_node (origin);
}
/* Iteratively explore the tree of feasible paths in order of shortest
path until we either find a feasible path to TARGET_ENODE, or hit
a limit. */
/* Set this if we find a feasible path to TARGET_ENODE. */
exploded_path *best_path = NULL;
{
auto_checking_feasibility sentinel (mgr);
while (process_worklist_item (&worklist, tg, &fg, target_enode, diag_idx,
&best_path))
{
/* Empty; the work is done within process_worklist_item. */
}
}
if (logger)
{
logger->log ("tg for sd: %i:", diag_idx);
logger->inc_indent ();
tg.log_stats (logger);
logger->dec_indent ();
logger->log ("fg for sd: %i:", diag_idx);
logger->inc_indent ();
fg.log_stats (logger);
logger->dec_indent ();
}
/* Dump the feasible_graph. */
if (flag_dump_analyzer_feasibility)
dump_feasible_graph (target_enode, desc, diag_idx, fg);
return best_path;
}
/* Process the next item in WORKLIST, potentially adding new items
based on feasible out-edges, and extending FG accordingly.
Use TG to ignore out-edges that don't lead to TARGET_ENODE.
Return true if the worklist processing should continue.
Return false if the processing of the worklist should stop
(either due to reaching TARGET_ENODE, or hitting a limit).
Write to *OUT_BEST_PATH if stopping due to finding a feasible path
to TARGET_ENODE. */
bool
epath_finder::process_worklist_item (feasible_worklist *worklist,
const trimmed_graph &tg,
feasible_graph *fg,
const exploded_node *target_enode,
unsigned diag_idx,
exploded_path **out_best_path) const
{
logger *logger = get_logger ();
feasible_node *fnode = worklist->take_next ();
if (!fnode)
{
if (logger)
logger->log ("drained worklist for sd: %i"
" without finding feasible path",
diag_idx);
return false;
}
log_scope s (logger, "fg worklist item",
"considering FN: %i (EN: %i) for sd: %i",
fnode->get_index (), fnode->get_inner_node ()->m_index,
diag_idx);
/* Iterate through all out-edges from this item. */
unsigned i;
exploded_edge *succ_eedge;
FOR_EACH_VEC_ELT (fnode->get_inner_node ()->m_succs, i, succ_eedge)
{
log_scope s (logger, "edge", "considering edge: EN:%i -> EN:%i",
succ_eedge->m_src->m_index,
succ_eedge->m_dest->m_index);
/* Reject edges that aren't in the trimmed graph. */
if (!tg.contains_p (succ_eedge))
{
if (logger)
logger->log ("rejecting: not in trimmed graph");
continue;
}
feasibility_state succ_state (fnode->get_state ());
rejected_constraint *rc = NULL;
if (succ_state.maybe_update_for_edge (logger, succ_eedge, &rc))
{
gcc_assert (rc == NULL);
feasible_node *succ_fnode
= fg->add_node (succ_eedge->m_dest,
succ_state,
fnode->get_path_length () + 1);
if (logger)
logger->log ("accepting as FN: %i", succ_fnode->get_index ());
fg->add_edge (new feasible_edge (fnode, succ_fnode, succ_eedge));
/* Have we reached TARGET_ENODE? */
if (succ_fnode->get_inner_node () == target_enode)
{
if (logger)
logger->log ("success: got feasible path to EN: %i (sd: %i)"
" (length: %i)",
target_enode->m_index, diag_idx,
succ_fnode->get_path_length ());
*out_best_path = fg->make_epath (succ_fnode);
if (flag_dump_analyzer_feasibility)
dump_feasible_path (target_enode, diag_idx, *fg, *succ_fnode);
/* Success: stop the worklist iteration. */
return false;
}
else
worklist->add_node (succ_fnode);
}
else
{
if (logger)
logger->log ("infeasible");
gcc_assert (rc);
fg->add_feasibility_problem (fnode,
succ_eedge,
rc);
/* Give up if there have been too many infeasible edges. */
if (fg->get_num_infeasible ()
> (unsigned)param_analyzer_max_infeasible_edges)
{
if (logger)
logger->log ("too many infeasible edges (%i); giving up",
fg->get_num_infeasible ());
return false;
}
}
}
/* Continue the worklist iteration. */
return true;
}
/* Helper class for epath_finder::dump_trimmed_graph
to dump extra per-node information.
Use SEP to add the length of the shortest path from each
node to the target node to each node's dump. */
class dump_eg_with_shortest_path : public eg_traits::dump_args_t
{
public:
dump_eg_with_shortest_path
(const exploded_graph &eg,
const shortest_paths<eg_traits, exploded_path> &sep)
: dump_args_t (eg),
m_sep (sep)
{
}
void dump_extra_info (const exploded_node *enode,
pretty_printer *pp) const FINAL OVERRIDE
{
pp_printf (pp, "sp: %i", m_sep.get_shortest_path (enode).length ());
pp_newline (pp);
}
private:
const shortest_paths<eg_traits, exploded_path> &m_sep;
};
/* Dump TG to "BASE_NAME.DESC.DIAG_IDX.to-enN.tg.dot",
annotating each node with the length of the shortest path
from that node to TARGET_ENODE (using SEP). */
void
epath_finder::
dump_trimmed_graph (const exploded_node *target_enode,
const char *desc, unsigned diag_idx,
const trimmed_graph &tg,
const shortest_paths<eg_traits, exploded_path> &sep)
{
auto_timevar tv (TV_ANALYZER_DUMP);
dump_eg_with_shortest_path inner_args (m_eg, sep);
trimmed_graph::dump_args_t args (inner_args);
pretty_printer pp;
pp_printf (&pp, "%s.%s.%i.to-en%i.tg.dot",
dump_base_name, desc, diag_idx, target_enode->m_index);
char *filename = xstrdup (pp_formatted_text (&pp));
tg.dump_dot (filename, NULL, args);
free (filename);
}
/* Dump FG to "BASE_NAME.DESC.DIAG_IDX.to-enN.fg.dot". */
void
epath_finder::dump_feasible_graph (const exploded_node *target_enode,
const char *desc, unsigned diag_idx,
const feasible_graph &fg)
{
auto_timevar tv (TV_ANALYZER_DUMP);
exploded_graph::dump_args_t inner_args (m_eg);
feasible_graph::dump_args_t args (inner_args);
pretty_printer pp;
pp_printf (&pp, "%s.%s.%i.to-en%i.fg.dot",
dump_base_name, desc, diag_idx, target_enode->m_index);
char *filename = xstrdup (pp_formatted_text (&pp));
fg.dump_dot (filename, NULL, args);
free (filename);
}
/* Dump the path to FNODE to "BASE_NAME.DIAG_IDX.to-enN.fpath.txt". */
void
epath_finder::dump_feasible_path (const exploded_node *target_enode,
unsigned diag_idx,
const feasible_graph &fg,
const feasible_node &fnode) const
{
auto_timevar tv (TV_ANALYZER_DUMP);
pretty_printer pp;
pp_printf (&pp, "%s.%i.to-en%i.fpath.txt",
dump_base_name, diag_idx, target_enode->m_index);
char *filename = xstrdup (pp_formatted_text (&pp));
fg.dump_feasible_path (fnode, filename);
free (filename);
}
/* class saved_diagnostic. */
/* saved_diagnostic's ctor.
Take ownership of D and STMT_FINDER. */
saved_diagnostic::saved_diagnostic (const state_machine *sm,
const exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *stmt_finder,
tree var,
const svalue *sval,
state_machine::state_t state,
pending_diagnostic *d,
unsigned idx)
: m_sm (sm), m_enode (enode), m_snode (snode), m_stmt (stmt),
/* stmt_finder could be on-stack; we want our own copy that can
outlive that. */
m_stmt_finder (stmt_finder ? stmt_finder->clone () : NULL),
m_var (var), m_sval (sval), m_state (state),
m_d (d), m_trailing_eedge (NULL),
m_idx (idx),
m_best_epath (NULL), m_problem (NULL),
m_notes ()
{
gcc_assert (m_stmt || m_stmt_finder);
/* We must have an enode in order to be able to look for paths
through the exploded_graph to this diagnostic. */
gcc_assert (m_enode);
}
/* saved_diagnostic's dtor. */
saved_diagnostic::~saved_diagnostic ()
{
delete m_stmt_finder;
delete m_d;
delete m_best_epath;
delete m_problem;
}
bool
saved_diagnostic::operator== (const saved_diagnostic &other) const
{
if (m_notes.length () != other.m_notes.length ())
return false;
for (unsigned i = 0; i < m_notes.length (); i++)
if (!m_notes[i]->equal_p (*other.m_notes[i]))
return false;
return (m_sm == other.m_sm
/* We don't compare m_enode. */
&& m_snode == other.m_snode
&& m_stmt == other.m_stmt
/* We don't compare m_stmt_finder. */
&& pending_diagnostic::same_tree_p (m_var, other.m_var)
&& m_state == other.m_state
&& m_d->equal_p (*other.m_d)
&& m_trailing_eedge == other.m_trailing_eedge);
}
/* Add PN to this diagnostic, taking ownership of it. */
void
saved_diagnostic::add_note (pending_note *pn)
{
gcc_assert (pn);
m_notes.safe_push (pn);
}
/* Return a new json::object of the form
{"sm": optional str,
"enode": int,
"snode": int,
"sval": optional str,
"state": optional str,
"path_length": optional int,
"pending_diagnostic": str,
"idx": int}. */
json::object *
saved_diagnostic::to_json () const
{
json::object *sd_obj = new json::object ();
if (m_sm)
sd_obj->set ("sm", new json::string (m_sm->get_name ()));
sd_obj->set ("enode", new json::integer_number (m_enode->m_index));
sd_obj->set ("snode", new json::integer_number (m_snode->m_index));
if (m_sval)
sd_obj->set ("sval", m_sval->to_json ());
if (m_state)
sd_obj->set ("state", m_state->to_json ());
if (m_best_epath)
sd_obj->set ("path_length", new json::integer_number (get_epath_length ()));
sd_obj->set ("pending_diagnostic", new json::string (m_d->get_kind ()));
sd_obj->set ("idx", new json::integer_number (m_idx));
/* We're not yet JSONifying the following fields:
const gimple *m_stmt;
stmt_finder *m_stmt_finder;
tree m_var;
exploded_edge *m_trailing_eedge;
enum status m_status;
feasibility_problem *m_problem;
auto_delete_vec <pending_note> m_notes;
*/
return sd_obj;
}
/* Dump this to PP in a form suitable for use as an id in .dot output. */
void
saved_diagnostic::dump_dot_id (pretty_printer *pp) const
{
pp_printf (pp, "sd_%i", m_idx);
}
/* Dump this to PP in a form suitable for use as a node in .dot output. */
void
saved_diagnostic::dump_as_dot_node (pretty_printer *pp) const
{
dump_dot_id (pp);
pp_printf (pp,
" [shape=none,margin=0,style=filled,fillcolor=\"red\",label=\"");
pp_write_text_to_stream (pp);
/* Node label. */
pp_printf (pp, "DIAGNOSTIC: %s (sd: %i)\n",
m_d->get_kind (), m_idx);
if (m_sm)
{
pp_printf (pp, "sm: %s", m_sm->get_name ());
if (m_state)
{
pp_printf (pp, "; state: ");
m_state->dump_to_pp (pp);
}
pp_newline (pp);
}
if (m_stmt)
{
pp_string (pp, "stmt: ");
pp_gimple_stmt_1 (pp, m_stmt, 0, (dump_flags_t)0);
pp_newline (pp);
}
if (m_var)
pp_printf (pp, "var: %qE\n", m_var);
if (m_sval)
{
pp_string (pp, "sval: ");
m_sval->dump_to_pp (pp, true);
pp_newline (pp);
}
if (m_best_epath)
pp_printf (pp, "path length: %i\n", get_epath_length ());
pp_write_text_as_dot_label_to_stream (pp, /*for_record=*/true);
pp_string (pp, "\"];\n\n");
/* Show links to duplicates. */
for (auto iter : m_duplicates)
{
dump_dot_id (pp);
pp_string (pp, " -> ");
iter->dump_dot_id (pp);
pp_string (pp, " [style=\"dotted\" arrowhead=\"none\"];");
pp_newline (pp);
}
}
/* Use PF to find the best exploded_path for this saved_diagnostic,
and store it in m_best_epath.
If m_stmt is still NULL, use m_stmt_finder on the epath to populate
m_stmt.
Return true if a best path was found. */
bool
saved_diagnostic::calc_best_epath (epath_finder *pf)
{
logger *logger = pf->get_logger ();
LOG_SCOPE (logger);
delete m_best_epath;
delete m_problem;
m_problem = NULL;
m_best_epath = pf->get_best_epath (m_enode, m_d->get_kind (), m_idx,
&m_problem);
/* Handle failure to find a feasible path. */
if (m_best_epath == NULL)
return false;
gcc_assert (m_best_epath);
if (m_stmt == NULL)
{
gcc_assert (m_stmt_finder);
m_stmt = m_stmt_finder->find_stmt (*m_best_epath);
}
gcc_assert (m_stmt);
return true;
}
unsigned
saved_diagnostic::get_epath_length () const
{
gcc_assert (m_best_epath);
return m_best_epath->length ();
}
/* Record that OTHER (and its duplicates) are duplicates
of this saved_diagnostic. */
void
saved_diagnostic::add_duplicate (saved_diagnostic *other)
{
gcc_assert (other);
m_duplicates.reserve (m_duplicates.length ()
+ other->m_duplicates.length ()
+ 1);
m_duplicates.splice (other->m_duplicates);
other->m_duplicates.truncate (0);
m_duplicates.safe_push (other);
}
/* Return true if this diagnostic supercedes OTHER, and that OTHER should
therefore not be emitted. */
bool
saved_diagnostic::supercedes_p (const saved_diagnostic &other) const
{
/* They should be at the same stmt. */
if (m_stmt != other.m_stmt)
return false;
return m_d->supercedes_p (*other.m_d);
}
/* Emit any pending notes owned by this diagnostic. */
void
saved_diagnostic::emit_any_notes () const
{
for (auto pn : m_notes)
pn->emit ();
}
/* State for building a checker_path from a particular exploded_path.
In particular, this precomputes reachability information: the set of
source enodes for which a path be found to the diagnostic enode. */
class path_builder
{
public:
path_builder (const exploded_graph &eg,
const exploded_path &epath,
const feasibility_problem *problem,
const saved_diagnostic &sd)
: m_eg (eg),
m_diag_enode (epath.get_final_enode ()),
m_sd (sd),
m_reachability (eg, m_diag_enode),
m_feasibility_problem (problem)
{}
const exploded_node *get_diag_node () const { return m_diag_enode; }
pending_diagnostic *get_pending_diagnostic () const
{
return m_sd.m_d;
}
bool reachable_from_p (const exploded_node *src_enode) const
{
return m_reachability.reachable_from_p (src_enode);
}
const extrinsic_state &get_ext_state () const { return m_eg.get_ext_state (); }
const feasibility_problem *get_feasibility_problem () const
{
return m_feasibility_problem;
}
const state_machine *get_sm () const { return m_sd.m_sm; }
private:
typedef reachability<eg_traits> enode_reachability;
const exploded_graph &m_eg;
/* The enode where the diagnostic occurs. */
const exploded_node *m_diag_enode;
const saved_diagnostic &m_sd;
/* Precompute all enodes from which the diagnostic is reachable. */
enode_reachability m_reachability;
const feasibility_problem *m_feasibility_problem;
};
/* Determine the emission location for PD at STMT in FUN. */
static location_t
get_emission_location (const gimple *stmt, function *fun,
const pending_diagnostic &pd)
{
location_t loc = get_stmt_location (stmt, fun);
/* Allow the pending_diagnostic to fix up the location. */
loc = pd.fixup_location (loc);
return loc;
}
/* class diagnostic_manager. */
/* diagnostic_manager's ctor. */
diagnostic_manager::diagnostic_manager (logger *logger, engine *eng,
int verbosity)
: log_user (logger), m_eng (eng), m_verbosity (verbosity),
m_num_disabled_diagnostics (0)
{
}
/* Queue pending_diagnostic D at ENODE for later emission.
Return true/false signifying if the diagnostic was actually added.
Take ownership of D (or delete it). */
bool
diagnostic_manager::add_diagnostic (const state_machine *sm,
exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *finder,
tree var,
const svalue *sval,
state_machine::state_t state,
pending_diagnostic *d)
{
LOG_FUNC (get_logger ());
/* We must have an enode in order to be able to look for paths
through the exploded_graph to the diagnostic. */
gcc_assert (enode);
/* If this warning is ultimately going to be rejected by a -Wno-analyzer-*
flag, reject it now.
We can only do this for diagnostics where we already know the stmt,
and thus can determine the emission location. */
if (stmt)
{
location_t loc = get_emission_location (stmt, snode->m_fun, *d);
int option = d->get_controlling_option ();
if (!warning_enabled_at (loc, option))
{
if (get_logger ())
get_logger ()->log ("rejecting disabled warning %qs",
d->get_kind ());
delete d;
m_num_disabled_diagnostics++;
return false;
}
}
saved_diagnostic *sd
= new saved_diagnostic (sm, enode, snode, stmt, finder, var, sval,
state, d, m_saved_diagnostics.length ());
m_saved_diagnostics.safe_push (sd);
enode->add_diagnostic (sd);
if (get_logger ())
log ("adding saved diagnostic %i at SN %i to EN %i: %qs",
sd->get_index (),
snode->m_index, enode->m_index, d->get_kind ());
return true;
}
/* Queue pending_diagnostic D at ENODE for later emission.
Return true/false signifying if the diagnostic was actually added.
Take ownership of D (or delete it). */
bool
diagnostic_manager::add_diagnostic (exploded_node *enode,
const supernode *snode, const gimple *stmt,
stmt_finder *finder,
pending_diagnostic *d)
{
gcc_assert (enode);
return add_diagnostic (NULL, enode, snode, stmt, finder, NULL_TREE,
NULL, 0, d);
}
/* Add PN to the most recent saved_diagnostic. */
void
diagnostic_manager::add_note (pending_note *pn)
{
LOG_FUNC (get_logger ());
gcc_assert (pn);
/* Get most recent saved_diagnostic. */
gcc_assert (m_saved_diagnostics.length () > 0);
saved_diagnostic *sd = m_saved_diagnostics[m_saved_diagnostics.length () - 1];
sd->add_note (pn);
}
/* Return a new json::object of the form
{"diagnostics" : [obj for saved_diagnostic]}. */
json::object *
diagnostic_manager::to_json () const
{
json::object *dm_obj = new json::object ();
{
json::array *sd_arr = new json::array ();
int i;
saved_diagnostic *sd;
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
sd_arr->append (sd->to_json ());
dm_obj->set ("diagnostics", sd_arr);
}
return dm_obj;
}
/* A class for identifying sets of duplicated pending_diagnostic.
We want to find the simplest saved_diagnostic amongst those that share a
dedupe_key. */
class dedupe_key
{
public:
dedupe_key (const saved_diagnostic &sd)
: m_sd (sd), m_stmt (sd.m_stmt)
{
gcc_assert (m_stmt);
}
hashval_t hash () const
{
inchash::hash hstate;
hstate.add_ptr (m_stmt);
// TODO: m_sd
return hstate.end ();
}
bool operator== (const dedupe_key &other) const
{
return (m_sd == other.m_sd
&& m_stmt == other.m_stmt);
}
location_t get_location () const
{
return m_stmt->location;
}
/* A qsort comparator for use by dedupe_winners::emit_best
to sort them into location_t order. */
static int
comparator (const void *p1, const void *p2)
{
const dedupe_key *pk1 = *(const dedupe_key * const *)p1;
const dedupe_key *pk2 = *(const dedupe_key * const *)p2;
location_t loc1 = pk1->get_location ();
location_t loc2 = pk2->get_location ();
if (int cmp = linemap_compare_locations (line_table, loc2, loc1))
return cmp;
if (int cmp = ((int)pk1->m_sd.get_epath_length ()
- (int)pk2->m_sd.get_epath_length ()))
return cmp;
if (int cmp = strcmp (pk1->m_sd.m_d->get_kind (),
pk2->m_sd.m_d->get_kind ()))
return cmp;
return 0;
}
const saved_diagnostic &m_sd;
const gimple *m_stmt;
};
/* Traits for use by dedupe_winners. */
class dedupe_hash_map_traits
{
public:
typedef const dedupe_key *key_type;
typedef saved_diagnostic *value_type;
typedef saved_diagnostic *compare_type;
static inline hashval_t hash (const key_type &v)
{
return v->hash ();
}
static inline bool equal_keys (const key_type &k1, const key_type &k2)
{
return *k1 == *k2;
}
template <typename T>
static inline void remove (T &)
{
// TODO
}
template <typename T>
static inline void mark_deleted (T &entry)
{
entry.m_key = reinterpret_cast<key_type> (1);
}
template <typename T>
static inline void mark_empty (T &entry)
{
entry.m_key = NULL;
}
template <typename T>
static inline bool is_deleted (const T &entry)
{
return entry.m_key == reinterpret_cast<key_type> (1);
}
template <typename T>
static inline bool is_empty (const T &entry)
{
return entry.m_key == NULL;
}
static const bool empty_zero_p = true;
};
/* A class for deduplicating diagnostics and finding (and emitting) the
best saved_diagnostic within each partition. */
class dedupe_winners
{
public:
~dedupe_winners ()
{
/* Delete all keys, but not the saved_diagnostics. */
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
delete (*iter).first;
}
/* Determine an exploded_path for SD using PF and, if it's feasible,
determine if SD is the best seen so far for its dedupe_key.
Record the winning SD for each dedupe_key. */
void add (logger *logger,
epath_finder *pf,
saved_diagnostic *sd)
{
/* Determine best epath for SD. */
if (!sd->calc_best_epath (pf))
return;
dedupe_key *key = new dedupe_key (*sd);
if (saved_diagnostic **slot = m_map.get (key))
{
if (logger)
logger->log ("already have this dedupe_key");
saved_diagnostic *cur_best_sd = *slot;
if (sd->get_epath_length () < cur_best_sd->get_epath_length ())
{
/* We've got a shorter path for the key; replace
the current candidate, marking it as a duplicate of SD. */
if (logger)
logger->log ("length %i is better than existing length %i;"
" taking over this dedupe_key",
sd->get_epath_length (),
cur_best_sd->get_epath_length ());
sd->add_duplicate (cur_best_sd);
*slot = sd;
}
else
/* We haven't beaten the current best candidate; add SD
as a duplicate of it. */
{
if (logger)
logger->log ("length %i isn't better than existing length %i;"
" dropping this candidate",
sd->get_epath_length (),
cur_best_sd->get_epath_length ());
cur_best_sd->add_duplicate (sd);
}
delete key;
}
else
{
/* This is the first candidate for this key. */
m_map.put (key, sd);
if (logger)
logger->log ("first candidate for this dedupe_key");
}
}
/* Handle interactions between the dedupe winners, so that some
diagnostics can supercede others (of different kinds).
We want use-after-free to supercede use-of-unitialized-value,
so that if we have these at the same stmt, we don't emit
a use-of-uninitialized, just the use-after-free. */
void handle_interactions (diagnostic_manager *dm)
{
LOG_SCOPE (dm->get_logger ());
auto_vec<const dedupe_key *> superceded;
for (auto outer : m_map)
{
const saved_diagnostic *outer_sd = outer.second;
for (auto inner : m_map)
{
const saved_diagnostic *inner_sd = inner.second;
if (inner_sd->supercedes_p (*outer_sd))
{
superceded.safe_push (outer.first);
if (dm->get_logger ())
dm->log ("sd[%i] \"%s\" superceded by sd[%i] \"%s\"",
outer_sd->get_index (), outer_sd->m_d->get_kind (),
inner_sd->get_index (), inner_sd->m_d->get_kind ());
break;
}
}
}
for (auto iter : superceded)
m_map.remove (iter);
}
/* Emit the simplest diagnostic within each set. */
void emit_best (diagnostic_manager *dm,
const exploded_graph &eg)
{
LOG_SCOPE (dm->get_logger ());
/* Get keys into a vec for sorting. */
auto_vec<const dedupe_key *> keys (m_map.elements ());
for (map_t::iterator iter = m_map.begin ();
iter != m_map.end ();
++iter)
keys.quick_push ((*iter).first);
dm->log ("# keys after de-duplication: %i", keys.length ());
/* Sort into a good emission order. */
keys.qsort (dedupe_key::comparator);
/* Emit the best saved_diagnostics for each key. */
int i;
const dedupe_key *key;
FOR_EACH_VEC_ELT (keys, i, key)
{
saved_diagnostic **slot = m_map.get (key);
gcc_assert (*slot);
const saved_diagnostic *sd = *slot;
dm->emit_saved_diagnostic (eg, *sd);
}
}
private:
/* This maps from each dedupe_key to a current best saved_diagnostic. */
typedef hash_map<const dedupe_key *, saved_diagnostic *,
dedupe_hash_map_traits> map_t;
map_t m_map;
};
/* Emit all saved diagnostics. */
void
diagnostic_manager::emit_saved_diagnostics (const exploded_graph &eg)
{
LOG_SCOPE (get_logger ());
auto_timevar tv (TV_ANALYZER_DIAGNOSTICS);
log ("# saved diagnostics: %i", m_saved_diagnostics.length ());
log ("# disabled diagnostics: %i", m_num_disabled_diagnostics);
if (get_logger ())
{
unsigned i;
saved_diagnostic *sd;
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
log ("[%i] sd: %qs at EN: %i, SN: %i",
i, sd->m_d->get_kind (), sd->m_enode->m_index,
sd->m_snode->m_index);
}
if (m_saved_diagnostics.length () == 0)
return;
/* Compute the shortest_paths once, sharing it between all diagnostics. */
epath_finder pf (eg);
/* Iterate through all saved diagnostics, adding them to a dedupe_winners
instance. This partitions the saved diagnostics by dedupe_key,
generating exploded_paths for them, and retaining the best one in each
partition. */
dedupe_winners best_candidates;
int i;
saved_diagnostic *sd;
FOR_EACH_VEC_ELT (m_saved_diagnostics, i, sd)
best_candidates.add (get_logger (), &pf, sd);
best_candidates.handle_interactions (this);
/* For each dedupe-key, call emit_saved_diagnostic on the "best"
saved_diagnostic. */
best_candidates.emit_best (this, eg);
}
/* Given a saved_diagnostic SD with m_best_epath through EG,
create an checker_path of suitable events and use it to call
SD's underlying pending_diagnostic "emit" vfunc to emit a diagnostic. */
void
diagnostic_manager::emit_saved_diagnostic (const exploded_graph &eg,
const saved_diagnostic &sd)
{
LOG_SCOPE (get_logger ());
log ("sd: %qs at SN: %i", sd.m_d->get_kind (), sd.m_snode->m_index);
log ("num dupes: %i", sd.get_num_dupes ());
pretty_printer *pp = global_dc->printer->clone ();
const exploded_path *epath = sd.get_best_epath ();
gcc_assert (epath);
/* Precompute all enodes from which the diagnostic is reachable. */
path_builder pb (eg, *epath, sd.get_feasibility_problem (), sd);
/* This is the diagnostic_path subclass that will be built for
the diagnostic. */
checker_path emission_path;
/* Populate emission_path with a full description of EPATH. */
build_emission_path (pb, *epath, &emission_path);
/* Now prune it to just cover the most pertinent events. */
prune_path (&emission_path, sd.m_sm, sd.m_sval, sd.m_state);
/* Add a final event to the path, covering the diagnostic itself.
We use the final enode from the epath, which might be different from
the sd.m_enode, as the dedupe code doesn't care about enodes, just
snodes. */
emission_path.add_final_event (sd.m_sm, epath->get_final_enode (), sd.m_stmt,
sd.m_var, sd.m_state);
/* The "final" event might not be final; if the saved_diagnostic has a
trailing eedge stashed, add any events for it. This is for use
in handling longjmp, to show where a longjmp is rewinding to. */
if (sd.m_trailing_eedge)
add_events_for_eedge (pb, *sd.m_trailing_eedge, &emission_path, NULL);
emission_path.prepare_for_emission (sd.m_d);
location_t loc
= get_emission_location (sd.m_stmt, sd.m_snode->m_fun, *sd.m_d);
/* Allow the pending_diagnostic to fix up the locations of events. */
emission_path.fixup_locations (sd.m_d);
gcc_rich_location rich_loc (loc);
rich_loc.set_path (&emission_path);
auto_diagnostic_group d;
auto_cfun sentinel (sd.m_snode->m_fun);
if (sd.m_d->emit (&rich_loc))
{
sd.emit_any_notes ();
unsigned num_dupes = sd.get_num_dupes ();
if (flag_analyzer_show_duplicate_count && num_dupes > 0)
inform_n (loc, num_dupes,
"%i duplicate", "%i duplicates",
num_dupes);
if (flag_dump_analyzer_exploded_paths)
{
auto_timevar tv (TV_ANALYZER_DUMP);
pretty_printer pp;
pp_printf (&pp, "%s.%i.%s.epath.txt",
dump_base_name, sd.get_index (), sd.m_d->get_kind ());
char *filename = xstrdup (pp_formatted_text (&pp));
epath->dump_to_file (filename, eg.get_ext_state ());
inform (loc, "exploded path written to %qs", filename);
free (filename);
}
}
delete pp;
}
/* Emit a "path" of events to EMISSION_PATH describing the exploded path
EPATH within EG. */
void
diagnostic_manager::build_emission_path (const path_builder &pb,
const exploded_path &epath,
checker_path *emission_path) const
{
LOG_SCOPE (get_logger ());
interesting_t interest;
pb.get_pending_diagnostic ()->mark_interesting_stuff (&interest);
/* Add region creation events for any globals of interest, at the
beginning of the path. */
{
for (auto reg : interest.m_region_creation)
switch (reg->get_memory_space ())
{
default:
continue;
case MEMSPACE_CODE:
case MEMSPACE_GLOBALS:
case MEMSPACE_READONLY_DATA:
{
const region *base_reg = reg->get_base_region ();
if (tree decl = base_reg->maybe_get_decl ())
if (DECL_P (decl)
&& DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
{
emission_path->add_region_creation_event
(reg,
DECL_SOURCE_LOCATION (decl),
NULL_TREE,
0);
}
}
}
}
/* Walk EPATH, adding events as appropriate. */
for (unsigned i = 0; i < epath.m_edges.length (); i++)
{
const exploded_edge *eedge = epath.m_edges[i];
add_events_for_eedge (pb, *eedge, emission_path, &interest);
}
}
/* Subclass of state_change_visitor that creates state_change_event
instances. */
class state_change_event_creator : public state_change_visitor
{
public:
state_change_event_creator (const path_builder &pb,
const exploded_edge &eedge,
checker_path *emission_path)
: m_pb (pb),
m_eedge (eedge),
m_emission_path (emission_path)
{}
bool on_global_state_change (const state_machine &sm,
state_machine::state_t src_sm_val,
state_machine::state_t dst_sm_val)
FINAL OVERRIDE
{
if (&sm != m_pb.get_sm ())
return false;
const exploded_node *src_node = m_eedge.m_src;
const program_point &src_point = src_node->get_point ();
const int src_stack_depth = src_point.get_stack_depth ();
const exploded_node *dst_node = m_eedge.m_dest;
const gimple *stmt = src_point.get_stmt ();
const supernode *supernode = src_point.get_supernode ();
const program_state &dst_state = dst_node->get_state ();
int stack_depth = src_stack_depth;
m_emission_path->add_event (new state_change_event (supernode,
stmt,
stack_depth,
sm,
NULL,
src_sm_val,
dst_sm_val,
NULL,
dst_state));
return false;
}
bool on_state_change (const state_machine &sm,
state_machine::state_t src_sm_val,
state_machine::state_t dst_sm_val,
const svalue *sval,
const svalue *dst_origin_sval) FINAL OVERRIDE
{
if (&sm != m_pb.get_sm ())
return false;
const exploded_node *src_node = m_eedge.m_src;
const program_point &src_point = src_node->get_point ();
const int src_stack_depth = src_point.get_stack_depth ();
const exploded_node *dst_node = m_eedge.m_dest;
const gimple *stmt = src_point.get_stmt ();
const supernode *supernode = src_point.get_supernode ();
const program_state &dst_state = dst_node->get_state ();
int stack_depth = src_stack_depth;
if (m_eedge.m_sedge
&& m_eedge.m_sedge->m_kind == SUPEREDGE_CFG_EDGE)
{
supernode = src_point.get_supernode ();
stmt = supernode->get_last_stmt ();
stack_depth = src_stack_depth;
}
/* Bulletproofing for state changes at calls/returns;
TODO: is there a better way? */
if (!stmt)
return false;
m_emission_path->add_event (new state_change_event (supernode,
stmt,
stack_depth,
sm,
sval,
src_sm_val,
dst_sm_val,
dst_origin_sval,
dst_state));
return false;
}
const path_builder &m_pb;
const exploded_edge &m_eedge;
checker_path *m_emission_path;
};
/* Compare SRC_STATE and DST_STATE (which use EXT_STATE), and call
VISITOR's on_state_change for every sm-state change that occurs
to a tree, and on_global_state_change for every global state change
that occurs.
This determines the state changes that ought to be reported to
the user: a combination of the effects of changes to sm_state_map
(which maps svalues to sm-states), and of region_model changes
(which map trees to svalues).
Bail out early and return true if any call to on_global_state_change
or on_state_change returns true, otherwise return false.
This is split out to make it easier to experiment with changes to
exploded_node granularity (so that we can observe what state changes
lead to state_change_events being emitted). */
bool
for_each_state_change (const program_state &src_state,
const program_state &dst_state,
const extrinsic_state &ext_state,
state_change_visitor *visitor)
{
gcc_assert (src_state.m_checker_states.length ()
== ext_state.get_num_checkers ());
gcc_assert (dst_state.m_checker_states.length ()
== ext_state.get_num_checkers ());
for (unsigned i = 0; i < ext_state.get_num_checkers (); i++)
{
const state_machine &sm = ext_state.get_sm (i);
const sm_state_map &src_smap = *src_state.m_checker_states[i];
const sm_state_map &dst_smap = *dst_state.m_checker_states[i];
/* Add events for any global state changes. */
if (src_smap.get_global_state () != dst_smap.get_global_state ())
if (visitor->on_global_state_change (sm,
src_smap.get_global_state (),
dst_smap.get_global_state ()))
return true;
/* Add events for per-svalue state changes. */
for (sm_state_map::iterator_t iter = dst_smap.begin ();
iter != dst_smap.end ();
++iter)
{
const svalue *sval = (*iter).first;
state_machine::state_t dst_sm_val = (*iter).second.m_state;
state_machine::state_t src_sm_val
= src_smap.get_state (sval, ext_state);
if (dst_sm_val != src_sm_val)
{
const svalue *origin_sval = (*iter).second.m_origin;
if (visitor->on_state_change (sm, src_sm_val, dst_sm_val,
sval, origin_sval))
return true;
}
}
}
return false;
}
/* An sm_context for adding state_change_event on assignments to NULL,
where the default state isn't m_start. Storing such state in the
sm_state_map would lead to bloat of the exploded_graph, so we want
to leave it as a default state, and inject state change events here
when we have a diagnostic.
Find transitions of constants, for handling on_zero_assignment. */
struct null_assignment_sm_context : public sm_context
{
null_assignment_sm_context (int sm_idx,
const state_machine &sm,
const program_state *old_state,
const program_state *new_state,
const gimple *stmt,
const program_point *point,
checker_path *emission_path,
const extrinsic_state &ext_state)
: sm_context (sm_idx, sm), m_old_state (old_state), m_new_state (new_state),
m_stmt (stmt), m_point (point), m_emission_path (emission_path),
m_ext_state (ext_state)
{
}
tree get_fndecl_for_call (const gcall */*call*/) FINAL OVERRIDE
{
return NULL_TREE;
}
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED,
tree var) FINAL OVERRIDE
{
const svalue *var_old_sval
= m_old_state->m_region_model->get_rvalue (var, NULL);
const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx];
state_machine::state_t current
= old_smap->get_state (var_old_sval, m_ext_state);
return current;
}
state_machine::state_t get_state (const gimple *stmt ATTRIBUTE_UNUSED,
const svalue *sval) FINAL OVERRIDE
{
const sm_state_map *old_smap = m_old_state->m_checker_states[m_sm_idx];
state_machine::state_t current = old_smap->get_state (sval, m_ext_state);
return current;
}
void set_next_state (const gimple *stmt,
tree var,
state_machine::state_t to,
tree origin ATTRIBUTE_UNUSED) FINAL OVERRIDE
{
state_machine::state_t from = get_state (stmt, var);
if (from != m_sm.get_start_state ())
return;
const svalue *var_new_sval
= m_new_state->m_region_model->get_rvalue (var, NULL);
const supernode *supernode = m_point->get_supernode ();
int stack_depth = m_point->get_stack_depth ();
m_emission_path->add_event (new state_change_event (supernode,
m_stmt,
stack_depth,
m_sm,
var_new_sval,
from, to,
NULL,
*m_new_state));
}
void set_next_state (const gimple *stmt,
const svalue *sval,
state_machine::state_t to,
tree origin ATTRIBUTE_UNUSED) FINAL OVERRIDE
{
state_machine::state_t from = get_state (stmt, sval);
if (from != m_sm.get_start_state ())
return;
const supernode *supernode = m_point->get_supernode ();
int stack_depth = m_point->get_stack_depth ();
m_emission_path->add_event (new state_change_event (supernode,
m_stmt,
stack_depth,
m_sm,
sval,
from, to,
NULL,
*m_new_state));
}
void warn (const supernode *, const gimple *,
tree, pending_diagnostic *d) FINAL OVERRIDE
{
delete d;
}
tree get_diagnostic_tree (tree expr) FINAL OVERRIDE
{
return expr;
}
tree get_diagnostic_tree (const svalue *sval) FINAL OVERRIDE
{
return m_new_state->m_region_model->get_representative_tree (sval);
}
state_machine::state_t get_global_state () const FINAL OVERRIDE
{
return 0;
}
void set_global_state (state_machine::state_t) FINAL OVERRIDE
{
/* No-op. */
}
void on_custom_transition (custom_transition *) FINAL OVERRIDE
{
}
tree is_zero_assignment (const gimple *stmt) FINAL OVERRIDE
{
const gassign *assign_stmt = dyn_cast <const gassign *> (stmt);
if (!assign_stmt)
return NULL_TREE;
if (const svalue *sval
= m_new_state->m_region_model->get_gassign_result (assign_stmt, NULL))
if (tree cst = sval->maybe_get_constant ())
if (::zerop(cst))
return gimple_assign_lhs (assign_stmt);
return NULL_TREE;
}
const program_state *get_old_program_state () const FINAL OVERRIDE
{
return m_old_state;
}
const program_state *m_old_state;
const program_state *m_new_state;
const gimple *m_stmt;
const program_point *m_point;
checker_path *m_emission_path;
const extrinsic_state &m_ext_state;
};
/* Subroutine of diagnostic_manager::build_emission_path.
Add any events for EEDGE to EMISSION_PATH. */
void
diagnostic_manager::add_events_for_eedge (const path_builder &pb,
const exploded_edge &eedge,
checker_path *emission_path,
interesting_t *interest) const
{
const exploded_node *src_node = eedge.m_src;
const program_point &src_point = src_node->get_point ();
const int src_stack_depth = src_point.get_stack_depth ();
const exploded_node *dst_node = eedge.m_dest;
const program_point &dst_point = dst_node->get_point ();
const int dst_stack_depth = dst_point.get_stack_depth ();
if (get_logger ())
{
get_logger ()->start_log_line ();
pretty_printer *pp = get_logger ()->get_printer ();
pp_printf (pp, "EN %i -> EN %i: ",
eedge.m_src->m_index,
eedge.m_dest->m_index);
src_point.print (pp, format (false));
pp_string (pp, "-> ");
dst_point.print (pp, format (false));
get_logger ()->end_log_line ();
}
const program_state &src_state = src_node->get_state ();
const program_state &dst_state = dst_node->get_state ();
/* Add state change events for the states that have changed.
We add these before events for superedges, so that if we have a
state_change_event due to following an edge, we'll get this sequence
of events:
| if (!ptr)
| ~
| |
| (1) assuming 'ptr' is non-NULL (state_change_event)
| (2) following 'false' branch... (start_cfg_edge_event)
...
| do_something (ptr);
| ~~~~~~~~~~~~~^~~~~
| |
| (3) ...to here (end_cfg_edge_event). */
state_change_event_creator visitor (pb, eedge, emission_path);
for_each_state_change (src_state, dst_state, pb.get_ext_state (),
&visitor);
/* Allow non-standard edges to add events, e.g. when rewinding from
longjmp to a setjmp. */
if (eedge.m_custom_info)
eedge.m_custom_info->add_events_to_path (emission_path, eedge);
/* Add events for superedges, function entries, and for statements. */
switch (dst_point.get_kind ())
{
default:
break;
case PK_BEFORE_SUPERNODE:
if (src_point.get_kind () == PK_AFTER_SUPERNODE)
{
if (eedge.m_sedge)
add_events_for_superedge (pb, eedge, emission_path);
}
/* Add function entry events. */
if (dst_point.get_supernode ()->entry_p ())
{
emission_path->add_event
(new function_entry_event
(dst_point.get_supernode ()->get_start_location (),
dst_point.get_fndecl (),
dst_stack_depth));
/* Create region_creation_events for on-stack regions within
this frame. */
if (interest)
{
unsigned i;
const region *reg;
FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg)
if (const frame_region *frame = reg->maybe_get_frame_region ())
if (frame->get_fndecl () == dst_point.get_fndecl ())
{
const region *base_reg = reg->get_base_region ();
if (tree decl = base_reg->maybe_get_decl ())
if (DECL_P (decl)
&& DECL_SOURCE_LOCATION (decl) != UNKNOWN_LOCATION)
{
emission_path->add_region_creation_event
(reg,
DECL_SOURCE_LOCATION (decl),
dst_point.get_fndecl (),
dst_stack_depth);
}
}
}
}
break;
case PK_BEFORE_STMT:
{
const gimple *stmt = dst_point.get_stmt ();
const gcall *call = dyn_cast <const gcall *> (stmt);
if (call && is_setjmp_call_p (call))
emission_path->add_event
(new setjmp_event (stmt->location,
dst_node,
dst_point.get_fndecl (),
dst_stack_depth,
call));
else
emission_path->add_event
(new statement_event (stmt,
dst_point.get_fndecl (),
dst_stack_depth, dst_state));
/* Create state change events for assignment to NULL.
Iterate through the stmts in dst_enode, adding state change
events for them. */
if (dst_state.m_region_model)
{
program_state iter_state (dst_state);
program_point iter_point (dst_point);
while (1)
{
const gimple *stmt = iter_point.get_stmt ();
if (const gassign *assign = dyn_cast<const gassign *> (stmt))
{
const extrinsic_state &ext_state = pb.get_ext_state ();
program_state old_state (iter_state);
iter_state.m_region_model->on_assignment (assign, NULL);
for (unsigned i = 0; i < ext_state.get_num_checkers (); i++)
{
const state_machine &sm = ext_state.get_sm (i);
null_assignment_sm_context sm_ctxt (i, sm,
&old_state,
&iter_state,
stmt,
&iter_point,
emission_path,
pb.get_ext_state ());
sm.on_stmt (&sm_ctxt, dst_point.get_supernode (), stmt);
// TODO: what about phi nodes?
}
}
iter_point.next_stmt ();
if (iter_point.get_kind () == PK_AFTER_SUPERNODE
|| (dst_node->m_succs.length () > 1
&& (iter_point
== dst_node->m_succs[0]->m_dest->get_point ())))
break;
}
}
}
break;
}
/* Look for changes in dynamic extents, which will identify
the creation of heap-based regions and alloca regions. */
if (interest)
{
const region_model *src_model = src_state.m_region_model;
const region_model *dst_model = dst_state.m_region_model;
if (src_model->get_dynamic_extents ()
!= dst_model->get_dynamic_extents ())
{
unsigned i;
const region *reg;
FOR_EACH_VEC_ELT (interest->m_region_creation, i, reg)
{
const region *base_reg = reg->get_base_region ();
const svalue *old_extents
= src_model->get_dynamic_extents (base_reg);
const svalue *new_extents
= dst_model->get_dynamic_extents (base_reg);
if (old_extents == NULL && new_extents != NULL)
switch (base_reg->get_kind ())
{
default:
break;
case RK_HEAP_ALLOCATED:
case RK_ALLOCA:
emission_path->add_region_creation_event
(reg,
src_point.get_location (),
src_point.get_fndecl (),
src_stack_depth);
break;
}
}
}
}
if (pb.get_feasibility_problem ()
&& &pb.get_feasibility_problem ()->m_eedge == &eedge)
{
pretty_printer pp;
pp_format_decoder (&pp) = default_tree_printer;
pp_string (&pp,
"this path would have been rejected as infeasible"
" at this edge: ");
pb.get_feasibility_problem ()->dump_to_pp (&pp);
emission_path->add_event (new precanned_custom_event
(dst_point.get_location (),
dst_point.get_fndecl (),
dst_stack_depth,
pp_formatted_text (&pp)));
}
}
/* Return true if EEDGE is a significant edge in the path to the diagnostic
for PB.
Consider all of the sibling out-eedges from the same source enode
as EEDGE.
If there's no path from the destinations of those eedges to the
diagnostic enode, then we have to take this eedge and thus it's
significant.
Conversely if there is a path from the destination of any other sibling
eedge to the diagnostic enode, then this edge is insignificant.
Example 1: redundant if-else:
(A) if (...) A
(B) ... / \
else B C
(C) ... \ /
(D) [DIAGNOSTIC] D
D is reachable by either B or C, so neither of these edges
are significant.
Example 2: pertinent if-else:
(A) if (...) A
(B) ... / \
else B C
(C) [NECESSARY CONDITION] | |
(D) [POSSIBLE DIAGNOSTIC] D1 D2
D becomes D1 and D2 in the exploded graph, where the diagnostic occurs
at D2. D2 is only reachable via C, so the A -> C edge is significant.
Example 3: redundant loop:
(A) while (...) +-->A
(B) ... | / \
(C) ... +-B C
(D) [DIAGNOSTIC] |
D
D is reachable from both B and C, so the A->C edge is not significant. */
bool
diagnostic_manager::significant_edge_p (const path_builder &pb,
const exploded_edge &eedge) const
{
int i;
exploded_edge *sibling;
FOR_EACH_VEC_ELT (eedge.m_src->m_succs, i, sibling)
{
if (sibling == &eedge)
continue;
if (pb.reachable_from_p (sibling->m_dest))
{
if (get_logger ())
get_logger ()->log (" edge EN: %i -> EN: %i is insignificant as"
" EN: %i is also reachable via"
" EN: %i -> EN: %i",
eedge.m_src->m_index, eedge.m_dest->m_index,
pb.get_diag_node ()->m_index,
sibling->m_src->m_index,
sibling->m_dest->m_index);
return false;
}
}
return true;
}
/* Subroutine of diagnostic_manager::add_events_for_eedge
where EEDGE has an underlying superedge i.e. a CFG edge,
or an interprocedural call/return.
Add any events for the superedge to EMISSION_PATH. */
void
diagnostic_manager::add_events_for_superedge (const path_builder &pb,
const exploded_edge &eedge,
checker_path *emission_path)
const
{
gcc_assert (eedge.m_sedge);
/* Give diagnostics an opportunity to override this function. */
pending_diagnostic *pd = pb.get_pending_diagnostic ();
if (pd->maybe_add_custom_events_for_superedge (eedge, emission_path))
return;
/* Don't add events for insignificant edges at verbosity levels below 3. */
if (m_verbosity < 3)
if (!significant_edge_p (pb, eedge))
return;
const exploded_node *src_node = eedge.m_src;
const program_point &src_point = src_node->get_point ();
const exploded_node *dst_node = eedge.m_dest;
const program_point &dst_point = dst_node->get_point ();
const int src_stack_depth = src_point.get_stack_depth ();
const int dst_stack_depth = dst_point.get_stack_depth ();
const gimple *last_stmt = src_point.get_supernode ()->get_last_stmt ();
switch (eedge.m_sedge->m_kind)
{
case SUPEREDGE_CFG_EDGE:
{
emission_path->add_event
(new start_cfg_edge_event (eedge,
(last_stmt
? last_stmt->location
: UNKNOWN_LOCATION),
src_point.get_fndecl (),
src_stack_depth));
emission_path->add_event
(new end_cfg_edge_event (eedge,
dst_point.get_supernode ()->get_start_location (),
dst_point.get_fndecl (),
dst_stack_depth));
}
break;
case SUPEREDGE_CALL:
{
emission_path->add_event
(new call_event (eedge,
(last_stmt
? last_stmt->location
: UNKNOWN_LOCATION),
src_point.get_fndecl (),
src_stack_depth));
}
break;
case SUPEREDGE_INTRAPROCEDURAL_CALL:
{
/* TODO: add a subclass for this, or generate events for the
summary. */
emission_path->add_event
(new debug_event ((last_stmt
? last_stmt->location
: UNKNOWN_LOCATION),
src_point.get_fndecl (),
src_stack_depth,
"call summary"));
}
break;
case SUPEREDGE_RETURN:
{
const return_superedge *return_edge
= as_a <const return_superedge *> (eedge.m_sedge);
const gcall *call_stmt = return_edge->get_call_stmt ();
emission_path->add_event
(new return_event (eedge,
(call_stmt
? call_stmt->location
: UNKNOWN_LOCATION),
dst_point.get_fndecl (),
dst_stack_depth));
}
break;
}
}
/* Prune PATH, based on the verbosity level, to the most pertinent
events for a diagnostic that involves VAR ending in state STATE
(for state machine SM).
PATH is updated in place, and the redundant checker_events are deleted.
As well as deleting events, call record_critical_state on events in
which state critical to the pending_diagnostic is being handled; see
the comment for diagnostic_manager::prune_for_sm_diagnostic. */
void
diagnostic_manager::prune_path (checker_path *path,
const state_machine *sm,
const svalue *sval,
state_machine::state_t state) const
{
LOG_FUNC (get_logger ());
path->maybe_log (get_logger (), "path");
prune_for_sm_diagnostic (path, sm, sval, state);
prune_interproc_events (path);
consolidate_conditions (path);
finish_pruning (path);
path->maybe_log (get_logger (), "pruned");
}
/* A cheap test to determine if EXPR can be the expression of interest in
an sm-diagnostic, so that we can reject cases where we have a non-lvalue.
We don't have always have a model when calling this, so we can't use
tentative_region_model_context, so there can be false positives. */
static bool
can_be_expr_of_interest_p (tree expr)
{
if (!expr)
return false;
/* Reject constants. */
if (CONSTANT_CLASS_P (expr))
return false;
/* Otherwise assume that it can be an lvalue. */
return true;
}
/* First pass of diagnostic_manager::prune_path: apply verbosity level,
pruning unrelated state change events.
Iterate backwards through PATH, skipping state change events that aren't
VAR but update the pertinent VAR when state-copying occurs.
As well as deleting events, call record_critical_state on events in
which state critical to the pending_diagnostic is being handled, so
that the event's get_desc vfunc can potentially supply a more precise
description of the event to the user.
e.g. improving
"calling 'foo' from 'bar'"
to
"passing possibly-NULL pointer 'ptr' to 'foo' from 'bar' as param 1"
when the diagnostic relates to later dereferencing 'ptr'. */
void
diagnostic_manager::prune_for_sm_diagnostic (checker_path *path,
const state_machine *sm,
const svalue *sval,
state_machine::state_t state) const
{
int idx = path->num_events () - 1;
while (idx >= 0 && idx < (signed)path->num_events ())
{
checker_event *base_event = path->get_checker_event (idx);
if (get_logger ())
{
if (sm)
{
if (sval)
{
label_text sval_desc = sval->get_desc ();
log ("considering event %i (%s), with sval: %qs, state: %qs",
idx, event_kind_to_string (base_event->m_kind),
sval_desc.m_buffer, state->get_name ());
sval_desc.maybe_free ();
}
else
log ("considering event %i (%s), with global state: %qs",
idx, event_kind_to_string (base_event->m_kind),
state->get_name ());
}
else
log ("considering event %i", idx);
}
switch (base_event->m_kind)
{
default:
gcc_unreachable ();
case EK_DEBUG:
if (m_verbosity < 4)
{
log ("filtering event %i: debug event", idx);
path->delete_event (idx);
}
break;
case EK_CUSTOM:
/* Don't filter custom events. */
break;
case EK_STMT:
{
if (m_verbosity < 4)
{
log ("filtering event %i: statement event", idx);
path->delete_event (idx);
}
}
break;
case EK_REGION_CREATION:
/* Don't filter these. */
break;
case EK_FUNCTION_ENTRY:
if (m_verbosity < 1)
{
log ("filtering event %i: function entry", idx);
path->delete_event (idx);
}
break;
case EK_STATE_CHANGE:
{
state_change_event *state_change = (state_change_event *)base_event;
gcc_assert (state_change->m_dst_state.m_region_model);
if (state_change->m_sval == sval)
{
if (state_change->m_origin)
{
if (get_logger ())
{
label_text sval_desc = sval->get_desc ();
label_text origin_sval_desc
= state_change->m_origin->get_desc ();
log ("event %i:"
" switching var of interest from %qs to %qs",
idx, sval_desc.m_buffer,
origin_sval_desc.m_buffer);
sval_desc.maybe_free ();
origin_sval_desc.maybe_free ();
}
sval = state_change->m_origin;
}
log ("event %i: switching state of interest from %qs to %qs",
idx, state_change->m_to->get_name (),
state_change->m_from->get_name ());
state = state_change->m_from;
}
else if (m_verbosity < 4)
{
if (get_logger ())
{
if (state_change->m_sval)
{
label_text change_sval_desc
= state_change->m_sval->get_desc ();
if (sval)
{
label_text sval_desc = sval->get_desc ();
log ("filtering event %i:"
" state change to %qs unrelated to %qs",
idx, change_sval_desc.m_buffer,
sval_desc.m_buffer);
}
else
log ("filtering event %i: state change to %qs",
idx, change_sval_desc.m_buffer);
change_sval_desc.maybe_free ();
}
else
log ("filtering event %i: global state change", idx);
}
path->delete_event (idx);
}
}
break;
case EK_START_CFG_EDGE:
{
cfg_edge_event *event = (cfg_edge_event *)base_event;
/* TODO: is this edge significant to var?
See if var can be in other states in the dest, but not
in other states in the src?
Must have multiple sibling edges. */
if (event->should_filter_p (m_verbosity))
{
log ("filtering events %i and %i: CFG edge", idx, idx + 1);
path->delete_event (idx);
/* Also delete the corresponding EK_END_CFG_EDGE. */
gcc_assert (path->get_checker_event (idx)->m_kind
== EK_END_CFG_EDGE);
path->delete_event (idx);
}
}
break;
case EK_END_CFG_EDGE:
/* These come in pairs with EK_START_CFG_EDGE events and are
filtered when their start event is filtered. */
break;
case EK_CALL_EDGE:
{
call_event *event = (call_event *)base_event;
const region_model *callee_model
= event->m_eedge.m_dest->get_state ().m_region_model;
const region_model *caller_model
= event->m_eedge.m_src->get_state ().m_region_model;
tree callee_var = callee_model->get_representative_tree (sval);
callsite_expr expr;
tree caller_var;
if(event->m_sedge)
{
const callgraph_superedge& cg_superedge
= event->get_callgraph_superedge ();
if (cg_superedge.m_cedge)
caller_var
= cg_superedge.map_expr_from_callee_to_caller (callee_var,
&expr);
else
caller_var = caller_model->get_representative_tree (sval);
}
else
caller_var = caller_model->get_representative_tree (sval);
if (caller_var)
{
if (get_logger ())
{
label_text sval_desc = sval->get_desc ();
log ("event %i:"
" recording critical state for %qs at call"
" from %qE in callee to %qE in caller",
idx, sval_desc.m_buffer, callee_var, caller_var);
sval_desc.maybe_free ();
}
if (expr.param_p ())
event->record_critical_state (caller_var, state);
}
}
break;
case EK_RETURN_EDGE:
{
if (sval)
{
return_event *event = (return_event *)base_event;
const region_model *caller_model
= event->m_eedge.m_dest->get_state ().m_region_model;
tree caller_var = caller_model->get_representative_tree (sval);
const region_model *callee_model
= event->m_eedge.m_src->get_state ().m_region_model;
callsite_expr expr;
tree callee_var;
if (event->m_sedge)
{
const callgraph_superedge& cg_superedge
= event->get_callgraph_superedge ();
if (cg_superedge.m_cedge)
callee_var
= cg_superedge.map_expr_from_caller_to_callee (caller_var,
&expr);
else
callee_var = callee_model->get_representative_tree (sval);
}
else
callee_var = callee_model->get_representative_tree (sval);
if (callee_var)
{
if (get_logger ())
{
label_text sval_desc = sval->get_desc ();
log ("event %i:"
" recording critical state for %qs at return"
" from %qE in caller to %qE in callee",
idx, sval_desc.m_buffer, callee_var, callee_var);
sval_desc.maybe_free ();
}
if (expr.return_value_p ())
event->record_critical_state (callee_var, state);
}
}
}
break;
case EK_SETJMP:
/* TODO: only show setjmp_events that matter i.e. those for which
there is a later rewind event using them. */
case EK_REWIND_FROM_LONGJMP:
case EK_REWIND_TO_SETJMP:
break;
case EK_WARNING:
/* Always show the final "warning" event in the path. */
break;
}
idx--;
}
}
/* Subroutine of diagnostic_manager::prune_for_sm_diagnostic.
If *EXPR is not suitable to be the expression of interest in
an sm-diagnostic, set *EXPR to NULL and log. */
void
diagnostic_manager::update_for_unsuitable_sm_exprs (tree *expr) const
{
gcc_assert (expr);
if (*expr && !can_be_expr_of_interest_p (*expr))
{
log ("new var %qE is unsuitable; setting var to NULL", *expr);
*expr = NULL_TREE;
}
}
/* Second pass of diagnostic_manager::prune_path: remove redundant
interprocedural information.
For example, given:
(1)- calling "f2" from "f1"
(2)--- entry to "f2"
(3)--- calling "f3" from "f2"
(4)----- entry to "f3"
(5)--- returning to "f2" to "f3"
(6)- returning to "f1" to "f2"
with no other intervening events, then none of these events are
likely to be interesting to the user.
Prune [..., call, function-entry, return, ...] triples repeatedly
until nothing has changed. For the example above, this would
remove events (3, 4, 5), and then remove events (1, 2, 6). */
void
diagnostic_manager::prune_interproc_events (checker_path *path) const
{
bool changed = false;
do
{
changed = false;
int idx = (signed)path->num_events () - 1;
while (idx >= 0)
{
/* Prune [..., call, function-entry, return, ...] triples. */
if (idx + 2 < (signed)path->num_events ()
&& path->get_checker_event (idx)->is_call_p ()
&& path->get_checker_event (idx + 1)->is_function_entry_p ()
&& path->get_checker_event (idx + 2)->is_return_p ())
{
if (get_logger ())
{
label_text desc
(path->get_checker_event (idx)->get_desc (false));
log ("filtering events %i-%i:"
" irrelevant call/entry/return: %s",
idx, idx + 2, desc.m_buffer);
desc.maybe_free ();
}
path->delete_event (idx + 2);
path->delete_event (idx + 1);
path->delete_event (idx);
changed = true;
idx--;
continue;
}
/* Prune [..., call, return, ...] pairs
(for -fanalyzer-verbosity=0). */
if (idx + 1 < (signed)path->num_events ()
&& path->get_checker_event (idx)->is_call_p ()
&& path->get_checker_event (idx + 1)->is_return_p ())
{
if (get_logger ())
{
label_text desc
(path->get_checker_event (idx)->get_desc (false));
log ("filtering events %i-%i:"
" irrelevant call/return: %s",
idx, idx + 1, desc.m_buffer);
desc.maybe_free ();
}
path->delete_event (idx + 1);
path->delete_event (idx);
changed = true;
idx--;
continue;
}
idx--;
}
}
while (changed);
}
/* Return true iff event IDX within PATH is on the same line as REF_EXP_LOC. */
static bool
same_line_as_p (const expanded_location &ref_exp_loc,
checker_path *path, unsigned idx)
{
const checker_event *ev = path->get_checker_event (idx);
expanded_location idx_exp_loc = expand_location (ev->get_location ());
gcc_assert (ref_exp_loc.file);
if (idx_exp_loc.file == NULL)
return false;
if (strcmp (ref_exp_loc.file, idx_exp_loc.file))
return false;
return ref_exp_loc.line == idx_exp_loc.line;
}
/* This path-readability optimization reduces the verbosity of compound
conditional statements (without needing to reconstruct the AST, which
has already been lost).
For example, it converts:
| 61 | if (cp[0] != '\0' && cp[0] != '#')
| | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| | | | |
| | | | (6) ...to here
| | | (7) following ‘true’ branch...
| | (5) following ‘true’ branch...
| 62 | {
| 63 | alias = cp++;
| | ~~~~
| | |
| | (8) ...to here
into:
| 61 | if (cp[0] != '\0' && cp[0] != '#')
| | ~
| | |
| | (5) following ‘true’ branch...
| 62 | {
| 63 | alias = cp++;
| | ~~~~
| | |
| | (6) ...to here
by combining events 5-8 into new events 5-6.
Find runs of consecutive (start_cfg_edge_event, end_cfg_edge_event) pairs
in which all events apart from the final end_cfg_edge_event are on the same
line, and for which either all the CFG edges are TRUE edges, or all are
FALSE edges.
Consolidate each such run into a
(start_consolidated_cfg_edges_event, end_consolidated_cfg_edges_event)
pair. */
void
diagnostic_manager::consolidate_conditions (checker_path *path) const
{
/* Don't simplify edges if we're debugging them. */
if (flag_analyzer_verbose_edges)
return;
for (int start_idx = 0;
start_idx < (signed)path->num_events () - 1;
start_idx++)
{
if (path->cfg_edge_pair_at_p (start_idx))
{
const checker_event *old_start_ev
= path->get_checker_event (start_idx);
expanded_location start_exp_loc
= expand_location (old_start_ev->get_location ());
if (start_exp_loc.file == NULL)
continue;
if (!same_line_as_p (start_exp_loc, path, start_idx + 1))
continue;
/* Are we looking for a run of all TRUE edges, or all FALSE edges? */
gcc_assert (old_start_ev->m_kind == EK_START_CFG_EDGE);
const start_cfg_edge_event *old_start_cfg_ev
= (const start_cfg_edge_event *)old_start_ev;
const cfg_superedge& first_cfg_sedge
= old_start_cfg_ev->get_cfg_superedge ();
bool edge_sense;
if (first_cfg_sedge.true_value_p ())
edge_sense = true;
else if (first_cfg_sedge.false_value_p ())
edge_sense = false;
else
continue;
/* Find a run of CFG start/end event pairs from
[start_idx, next_idx)
where all apart from the final event are on the same line,
and all are either TRUE or FALSE edges, matching the initial. */
int next_idx = start_idx + 2;
while (path->cfg_edge_pair_at_p (next_idx)
&& same_line_as_p (start_exp_loc, path, next_idx))
{
const checker_event *iter_ev
= path->get_checker_event (next_idx);
gcc_assert (iter_ev->m_kind == EK_START_CFG_EDGE);
const start_cfg_edge_event *iter_cfg_ev
= (const start_cfg_edge_event *)iter_ev;
const cfg_superedge& iter_cfg_sedge
= iter_cfg_ev->get_cfg_superedge ();
if (edge_sense)
{
if (!iter_cfg_sedge.true_value_p ())
break;
}
else
{
if (!iter_cfg_sedge.false_value_p ())
break;
}
next_idx += 2;
}
/* If we have more than one pair in the run, consolidate. */
if (next_idx > start_idx + 2)
{
const checker_event *old_end_ev
= path->get_checker_event (next_idx - 1);
log ("consolidating CFG edge events %i-%i into %i-%i",
start_idx, next_idx - 1, start_idx, start_idx +1);
start_consolidated_cfg_edges_event *new_start_ev
= new start_consolidated_cfg_edges_event
(old_start_ev->get_location (),
old_start_ev->get_fndecl (),
old_start_ev->get_stack_depth (),
edge_sense);
checker_event *new_end_ev
= new end_consolidated_cfg_edges_event
(old_end_ev->get_location (),
old_end_ev->get_fndecl (),
old_end_ev->get_stack_depth ());
path->replace_event (start_idx, new_start_ev);
path->replace_event (start_idx + 1, new_end_ev);
path->delete_events (start_idx + 2, next_idx - (start_idx + 2));
}
}
}
}
/* Final pass of diagnostic_manager::prune_path.
If all we're left with is in one function, then filter function entry
events. */
void
diagnostic_manager::finish_pruning (checker_path *path) const
{
if (!path->interprocedural_p ())
{
int idx = path->num_events () - 1;
while (idx >= 0 && idx < (signed)path->num_events ())
{
checker_event *base_event = path->get_checker_event (idx);
if (base_event->m_kind == EK_FUNCTION_ENTRY)
{
log ("filtering event %i:"
" function entry for purely intraprocedural path", idx);
path->delete_event (idx);
}
idx--;
}
}
}
} // namespace ana
#endif /* #if ENABLE_ANALYZER */
|