10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 */
17
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
25
26 struct sqlite3 *smatch_db;
27 struct sqlite3 *mem_db;
28 struct sqlite3 *cache_db;
29
30 static int return_id;
31
32 #define SQLITE_CACHE_PAGES 1000
33
34 struct def_callback {
35 int hook_type;
36 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
37 };
38 ALLOCATOR(def_callback, "definition db hook callbacks");
39 DECLARE_PTR_LIST(callback_list, struct def_callback);
40 static struct callback_list *select_caller_info_callbacks;
41
42 struct member_info_callback {
43 int owner;
44 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
45 };
46 ALLOCATOR(member_info_callback, "caller_info callbacks");
47 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
48 static struct member_info_cb_list *member_callbacks;
49
50 struct returned_state_callback {
51 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
88 buf[j++] = '\\';
89 if (j == sizeof(buf))
90 break;
91 buf[j] = 'n';
92 }
93
94 if (!found)
95 return alloc_sname(str);
96
97 if (j == sizeof(buf))
98 buf[j - 1] = '\0';
99 return alloc_sname(buf);
100 }
101
102 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
103 {
104 int i;
105
106 for (i = 0; i < argc; i++) {
107 if (i != 0)
108 printf(", ");
109 sm_printf("%s", argv[i]);
110 }
111 sm_printf("\n");
112 return 0;
113 }
114
115 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
116 {
117 char *err = NULL;
118 int rc;
119
120 if (!db)
121 return;
122
123 if (option_debug) {
124 sm_msg("%s", sql);
125 if (strncasecmp(sql, "select", strlen("select")) == 0)
126 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
127 }
128
129 rc = sqlite3_exec(db, sql, callback, data, &err);
130 if (rc != SQLITE_OK && !parse_error) {
131 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
132 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
133 parse_error = 1;
134 }
135 }
136
137 static int replace_count;
138 static char **replace_table;
139 static const char *replace_return_ranges(const char *return_ranges)
140 {
141 int i;
142
143 if (!get_function()) {
784 if (!p)
785 return 0;
786 p++;
787 while (true) {
788 next = strchr(p, '>');
789 if (!next)
790 return 0;
791 next++;
792
793 size = next - p;
794 if (size >= sizeof(buf))
795 return 0;
796 memcpy(buf, p, size);
797 buf[size] = '\0';
798 if (strstr(next, buf))
799 return 1;
800 p = next;
801 }
802 }
803
804 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
805 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
806 {
807 struct sm_state *sm;
808 const char *sm_name;
809 char *name;
810 struct symbol *sym;
811 int len;
812 char printed_name[256];
813 int is_address = 0;
814 bool add_star;
815 struct symbol *type;
816
817 expr = strip_expr(expr);
818 if (!expr)
819 return;
820 type = get_type(expr);
821 if (type && type_bits(type) < type_bits(&ulong_ctype))
822 return;
823
1400 return alloc_sname(buf);
1401 }
1402 if (math_str) {
1403 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1404 return alloc_sname(buf);
1405 }
1406 compare_str = get_return_compare_str(expr);
1407 if (compare_str) {
1408 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1409 return alloc_sname(buf);
1410 }
1411
1412 return return_ranges;
1413 }
1414
1415 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1416 {
1417 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1418 }
1419
1420 static void call_return_state_hooks_conditional(struct expression *expr)
1421 {
1422 struct returned_state_callback *cb;
1423 struct range_list *rl;
1424 const char *return_ranges;
1425 int final_pass_orig = final_pass;
1426
1427 __push_fake_cur_stree();
1428
1429 final_pass = 0;
1430 __split_whole_condition(expr->conditional);
1431 final_pass = final_pass_orig;
1432
1433 return_ranges = get_return_ranges_str(expr->cond_true ?: expr->conditional, &rl);
1434
1435 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1436
1437 return_id++;
1438 FOR_EACH_PTR(returned_state_callbacks, cb) {
1439 cb->callback(return_id, (char *)return_ranges, expr->cond_true);
1440 } END_FOR_EACH_PTR(cb);
1441
1442 __push_true_states();
1443 __use_false_states();
1444
1445 return_ranges = get_return_ranges_str(expr->cond_false, &rl);
1446 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(rl));
1447
1448 return_id++;
1449 FOR_EACH_PTR(returned_state_callbacks, cb) {
1450 cb->callback(return_id, (char *)return_ranges, expr->cond_false);
1451 } END_FOR_EACH_PTR(cb);
1452
1453 __merge_true_states();
1454 __free_fake_cur_stree();
1455 }
1456
1457 static void call_return_state_hooks_compare(struct expression *expr)
1458 {
1459 struct returned_state_callback *cb;
1460 char *return_ranges;
1461 int final_pass_orig = final_pass;
1462 sval_t sval = { .type = &int_ctype };
1463 sval_t ret;
1464
1465 if (!get_implied_value(expr, &ret))
1466 ret.value = -1;
1467
1468 __push_fake_cur_stree();
1469
1470 final_pass = 0;
1471 __split_whole_condition(expr);
1472 final_pass = final_pass_orig;
1473
1474 if (ret.value != 0) {
1519 char *return_ranges;
1520 struct sm_state *tmp;
1521 int ret = 0;
1522 int nr_possible, nr_states;
1523 char *compare_str;
1524 char buf[128];
1525 struct state_list *already_handled = NULL;
1526 sval_t sval;
1527
1528 if (!sm || !sm->merged)
1529 return 0;
1530
1531 if (too_many_possible(sm))
1532 return 0;
1533
1534 /* bail if it gets too complicated */
1535 nr_possible = 0;
1536 FOR_EACH_PTR(sm->possible, tmp) {
1537 if (tmp->merged)
1538 continue;
1539 nr_possible++;
1540 } END_FOR_EACH_PTR(tmp);
1541 nr_states = get_db_state_count();
1542 if (nr_states * nr_possible >= 2000)
1543 return 0;
1544
1545 FOR_EACH_PTR(sm->possible, tmp) {
1546 if (tmp->merged)
1547 continue;
1548 if (ptr_in_list(tmp, already_handled))
1549 continue;
1550 add_ptr_list(&already_handled, tmp);
1551
1552 ret = 1;
1553 __push_fake_cur_stree();
1554
1555 overwrite_states_using_pool(sm, tmp);
1556
1557 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1558 return_ranges = show_rl(rl);
1559 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1560 if (!rl_to_sval(rl, &sval)) {
1576 free_slist(&already_handled);
1577
1578 return ret;
1579 }
1580
1581 static int call_return_state_hooks_split_possible(struct expression *expr)
1582 {
1583 struct sm_state *sm;
1584
1585 if (!expr || expr_equal_to_param(expr, -1))
1586 return 0;
1587
1588 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1589 return split_possible_helper(sm, expr);
1590 }
1591
1592 static bool has_possible_negative(struct sm_state *sm)
1593 {
1594 struct sm_state *tmp;
1595
1596 FOR_EACH_PTR(sm->possible, tmp) {
1597 if (!estate_rl(tmp->state))
1598 continue;
1599 if (sval_is_negative(estate_min(tmp->state)) &&
1600 sval_is_negative(estate_max(tmp->state)))
1601 return true;
1602 } END_FOR_EACH_PTR(tmp);
1603
1604 return false;
1605 }
1606
1607 static bool has_possible_zero_null(struct sm_state *sm)
1608 {
1609 struct sm_state *tmp;
1610 sval_t sval;
1611
1612 FOR_EACH_PTR(sm->possible, tmp) {
1613 if (!estate_get_single_value(tmp->state, &sval))
1614 continue;
1615 if (sval.value == 0)
1616 return true;
1617 } END_FOR_EACH_PTR(tmp);
1618
1619 return false;
1620 }
1621
1622 static int split_positive_from_negative(struct expression *expr)
1623 {
1624 struct sm_state *sm;
1625 struct returned_state_callback *cb;
1626 struct range_list *rl;
1627 const char *return_ranges;
1628 struct range_list *ret_rl;
1629 int undo;
1630 bool has_zero;
1631
1632 /* We're going to print the states 3 times */
1633 if (get_db_state_count() > 10000 / 3)
1634 return 0;
1635
1636 if (!get_implied_rl(expr, &rl) || !rl)
1637 return 0;
1638 if (is_whole_rl(rl) || is_whole_rl_non_zero(rl))
1639 return 0;
1640 /* Forget about INT_MAX and larger */
1641 if (rl_max(rl).value <= 0)
1642 return 0;
1643 if (!sval_is_negative(rl_min(rl)))
1644 return 0;
1645
1646 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1647 if (!sm)
1648 return 0;
1649 if (!has_possible_negative(sm))
1650 return 0;
1651 has_zero = has_possible_zero_null(sm);
1652
1653 if (!assume(compare_expression(expr, has_zero ? '>' : SPECIAL_GTE, zero_expr())))
1654 return 0;
1655
1656 return_id++;
1657 return_ranges = get_return_ranges_str(expr, &ret_rl);
1658 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1659 FOR_EACH_PTR(returned_state_callbacks, cb) {
1660 cb->callback(return_id, (char *)return_ranges, expr);
1661 } END_FOR_EACH_PTR(cb);
1662
1663 end_assume();
1664
1665 if (has_zero) {
1666 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1667
1668 return_id++;
1669 return_ranges = get_return_ranges_str(expr, &ret_rl);
1670 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1671 FOR_EACH_PTR(returned_state_callbacks, cb) {
1672 cb->callback(return_id, (char *)return_ranges, expr);
1673 } END_FOR_EACH_PTR(cb);
1674
1675 if (undo)
1676 end_assume();
1677 }
1678
1679 undo = assume(compare_expression(expr, '<', zero_expr()));
1680
1681 return_id++;
1682 return_ranges = get_return_ranges_str(expr, &ret_rl);
1683 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1684 FOR_EACH_PTR(returned_state_callbacks, cb) {
1685 cb->callback(return_id, (char *)return_ranges, expr);
1702 struct sm_state *sm;
1703 struct smatch_state *state;
1704 int nr_states;
1705 int final_pass_orig = final_pass;
1706
1707 if (!expr || expr_equal_to_param(expr, -1))
1708 return 0;
1709 if (expr->type == EXPR_CALL)
1710 return 0;
1711
1712 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1713 if (!sm)
1714 return 0;
1715 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1716 return 0;
1717 state = sm->state;
1718 if (!estate_rl(state))
1719 return 0;
1720 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1721 return 0;
1722 if (!has_possible_zero_null(sm))
1723 return 0;
1724
1725 nr_states = get_db_state_count();
1726 if (option_info && nr_states >= 1500)
1727 return 0;
1728
1729 rl = estate_rl(state);
1730
1731 __push_fake_cur_stree();
1732
1733 final_pass = 0;
1734 __split_whole_condition(expr);
1735 final_pass = final_pass_orig;
1736
1737 nonnull_rl = rl_filter(rl, rl_zero());
1738 return_ranges = show_rl(nonnull_rl);
1739 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1740
1741 return_id++;
1742 FOR_EACH_PTR(returned_state_callbacks, cb) {
1744 } END_FOR_EACH_PTR(cb);
1745
1746 __push_true_states();
1747 __use_false_states();
1748
1749 return_ranges = alloc_sname("0");
1750 null_sval = sval_type_val(rl_type(rl), 0);
1751 add_range(&null_rl, null_sval, null_sval);
1752 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1753 return_id++;
1754 FOR_EACH_PTR(returned_state_callbacks, cb) {
1755 cb->callback(return_id, return_ranges, expr);
1756 } END_FOR_EACH_PTR(cb);
1757
1758 __merge_true_states();
1759 __free_fake_cur_stree();
1760
1761 return 1;
1762 }
1763
1764 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1765 {
1766 struct sm_state *sm;
1767 struct range_list *rl;
1768 struct range_list *nonzero_rl;
1769 sval_t zero_sval;
1770 struct range_list *zero_rl = NULL;
1771 int nr_states;
1772 struct returned_state_callback *cb;
1773 char *return_ranges;
1774 int final_pass_orig = final_pass;
1775
1776 if (option_project != PROJ_KERNEL)
1777 return 0;
1778
1779 nr_states = get_db_state_count();
1780 if (nr_states > 1500)
1781 return 0;
1782
1783 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1784 if (!sm)
1785 return 0;
1786 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1787 return 0;
1788
1789 rl = estate_rl(sm->state);
1790 if (!rl)
1791 return 0;
1792
1793 if (rl_min(rl).value < -4095 || rl_min(rl).value >= 0)
1794 return 0;
1795 if (rl_max(rl).value != 0)
1796 return 0;
1797 if (!has_possible_zero_null(sm))
1798 return 0;
1799
1800 __push_fake_cur_stree();
1801
1802 final_pass = 0;
1803 __split_whole_condition(expr);
1804 final_pass = final_pass_orig;
1805
1806 nonzero_rl = rl_filter(rl, rl_zero());
1807 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1808 return_ranges = show_rl(nonzero_rl);
1809 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1810
1811 return_id++;
1812 FOR_EACH_PTR(returned_state_callbacks, cb) {
1813 cb->callback(return_id, return_ranges, expr);
1814 } END_FOR_EACH_PTR(cb);
1815
1816 __push_true_states();
1817 __use_false_states();
1818
1819 return_ranges = alloc_sname("0");
1825 cb->callback(return_id, return_ranges, expr);
1826 } END_FOR_EACH_PTR(cb);
1827
1828 __merge_true_states();
1829 __free_fake_cur_stree();
1830
1831 return 1;
1832 }
1833
1834 static int is_boolean(struct expression *expr)
1835 {
1836 struct range_list *rl;
1837
1838 if (!get_implied_rl(expr, &rl))
1839 return 0;
1840 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1841 return 1;
1842 return 0;
1843 }
1844
1845 static int is_conditional(struct expression *expr)
1846 {
1847 if (!expr)
1848 return 0;
1849 if (expr->type == EXPR_CONDITIONAL || expr->type == EXPR_SELECT)
1850 return 1;
1851 return 0;
1852 }
1853
1854 static int splitable_function_call(struct expression *expr)
1855 {
1856 struct sm_state *sm;
1857 char buf[64];
1858
1859 if (!expr || expr->type != EXPR_CALL)
1860 return 0;
1861 snprintf(buf, sizeof(buf), "return %p", expr);
1862 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1863 return split_possible_helper(sm, expr);
1864 }
1865
1866 static struct sm_state *find_bool_param(void)
1867 {
1868 struct stree *start_states;
1869 struct symbol *arg;
1870 struct sm_state *sm, *tmp;
1871 sval_t sval;
1872
1873 start_states = get_start_states();
1891 * Check if it's splitable. If not, then splitting it up is likely not
1892 * useful for the callers.
1893 */
1894 FOR_EACH_PTR(sm->possible, tmp) {
1895 if (is_merged(tmp))
1896 continue;
1897 if (!estate_get_single_value(tmp->state, &sval))
1898 return NULL;
1899 } END_FOR_EACH_PTR(tmp);
1900
1901 return sm;
1902 }
1903
1904 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1905 {
1906 struct returned_state_callback *cb;
1907 struct range_list *ret_rl;
1908 const char *return_ranges;
1909 struct sm_state *tmp;
1910 int ret = 0;
1911 int nr_possible, nr_states;
1912 struct state_list *already_handled = NULL;
1913
1914 if (!sm || !sm->merged)
1915 return 0;
1916
1917 if (too_many_possible(sm))
1918 return 0;
1919
1920 /* bail if it gets too complicated */
1921 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
1922 nr_states = get_db_state_count();
1923 if (nr_states * nr_possible >= 2000)
1924 return 0;
1925
1926 FOR_EACH_PTR(sm->possible, tmp) {
1927 if (tmp->merged)
1928 continue;
1929 if (ptr_in_list(tmp, already_handled))
1930 continue;
1931 add_ptr_list(&already_handled, tmp);
1932
1933 ret = 1;
1934 __push_fake_cur_stree();
1935
1936 overwrite_states_using_pool(sm, tmp);
1937
1938 return_ranges = get_return_ranges_str(expr, &ret_rl);
1939 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1940 return_id++;
1941 FOR_EACH_PTR(returned_state_callbacks, cb) {
1942 cb->callback(return_id, (char *)return_ranges, expr);
1943 } END_FOR_EACH_PTR(cb);
1944
1945 __free_fake_cur_stree();
1946 } END_FOR_EACH_PTR(tmp);
1947
1948 free_slist(&already_handled);
1949
1950 return ret;
1951 }
1952
1953 static int split_by_bool_param(struct expression *expr)
1954 {
1955 struct sm_state *start_sm, *sm;
1956 sval_t sval;
1957
1958 start_sm = find_bool_param();
1959 if (!start_sm)
1960 return 0;
1961 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
1962 if (!sm || estate_get_single_value(sm->state, &sval))
1963 return 0;
1964 return split_on_bool_sm(sm, expr);
1965 }
1966
1967 static int split_by_null_nonnull_param(struct expression *expr)
1968 {
1969 struct symbol *arg;
1970 struct sm_state *sm;
1971 sval_t zero = {
1972 .type = &ulong_ctype,
1973 };
1974
1975 /* function must only take one pointer */
1976 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
1977 return 0;
1978 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
1979 if (!arg->ident)
1980 return 0;
1981 if (get_real_base_type(arg)->type != SYM_PTR)
1982 return 0;
1983
1984 if (param_was_set_var_sym(arg->ident->name, arg))
1985 return 0;
1986 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
1987 if (!sm)
1988 return 0;
1989
1990 if (!rl_has_sval(estate_rl(sm->state), zero))
1991 return 0;
1992
1993 return split_on_bool_sm(sm, expr);
1994 }
1995
1996 struct expression *strip_expr_statement(struct expression *expr)
1997 {
1998 struct expression *orig = expr;
1999 struct statement *stmt, *last_stmt;
2000
2001 if (!expr)
2002 return NULL;
2003 if (expr->type == EXPR_PREOP && expr->op == '(')
2004 expr = expr->unop;
2005 if (expr->type != EXPR_STATEMENT)
2006 return orig;
2007 stmt = expr->statement;
2008 if (!stmt || stmt->type != STMT_COMPOUND)
2009 return orig;
2010
2011 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2012 if (!last_stmt || last_stmt->type == STMT_LABEL)
2021 struct returned_state_callback *cb;
2022 struct range_list *ret_rl;
2023 const char *return_ranges;
2024 int nr_states;
2025 sval_t sval;
2026
2027 if (__path_is_null())
2028 return;
2029
2030 expr = strip_expr(expr);
2031 expr = strip_expr_statement(expr);
2032
2033 if (is_impossible_path())
2034 goto vanilla;
2035
2036 if (expr && (expr->type == EXPR_COMPARE ||
2037 !get_implied_value(expr, &sval)) &&
2038 (is_condition(expr) || is_boolean(expr))) {
2039 call_return_state_hooks_compare(expr);
2040 return;
2041 } else if (is_conditional(expr)) {
2042 call_return_state_hooks_conditional(expr);
2043 return;
2044 } else if (call_return_state_hooks_split_possible(expr)) {
2045 return;
2046 } else if (split_positive_from_negative(expr)) {
2047 return;
2048 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2049 return;
2050 } else if (call_return_state_hooks_split_success_fail(expr)) {
2051 return;
2052 } else if (splitable_function_call(expr)) {
2053 return;
2054 } else if (split_by_bool_param(expr)) {
2055 } else if (split_by_null_nonnull_param(expr)) {
2056 return;
2057 }
2058
2059 vanilla:
2060 return_ranges = get_return_ranges_str(expr, &ret_rl);
2061 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2062
2475 free_string(name);
2476 return alloc_string(member_name);
2477 }
2478
2479 while (expr->type == EXPR_ASSIGNMENT)
2480 expr = strip_expr(expr->right);
2481 if (expr->type != EXPR_CALL)
2482 return NULL;
2483
2484 arg = get_argument_from_call_expr(expr->args, param);
2485 if (!arg)
2486 return NULL;
2487
2488 return get_variable_from_key(arg, key, sym);
2489 }
2490
2491 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2492 {
2493 char buf[256];
2494 char *tmp;
2495 bool add_star = false;
2496
2497 if (!arg)
2498 return NULL;
2499
2500 arg = strip_expr(arg);
2501
2502 if (strcmp(key, "$") == 0)
2503 return expr_to_var_sym(arg, sym);
2504
2505 if (strcmp(key, "*$") == 0) {
2506 if (arg->type == EXPR_PREOP && arg->op == '&') {
2507 arg = strip_expr(arg->unop);
2508 return expr_to_var_sym(arg, sym);
2509 } else {
2510 tmp = expr_to_var_sym(arg, sym);
2511 if (!tmp)
2512 return NULL;
2513 snprintf(buf, sizeof(buf), "*%s", tmp);
2514 free_string(tmp);
2515 return alloc_string(buf);
2516 }
2517 }
2518
2519 if (key[0] == '*') {
2520 add_star = true;
2521 key++;
2522 }
2523
2524 if (arg->type == EXPR_PREOP && arg->op == '&') {
2525 arg = strip_expr(arg->unop);
2526 tmp = expr_to_var_sym(arg, sym);
2527 if (!tmp)
2528 return NULL;
2529 snprintf(buf, sizeof(buf), "%s%s.%s",
2530 add_star ? "*" : "", tmp, key + 3);
2531 return alloc_string(buf);
2532 }
2533
2534 tmp = expr_to_var_sym(arg, sym);
2535 if (!tmp)
2536 return NULL;
2537 snprintf(buf, sizeof(buf), "%s%s%s", add_star ? "*" : "", tmp, key + 1);
2538 free_string(tmp);
2539 return alloc_string(buf);
2540 }
2541
2542 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2543 {
2544 *vsl = NULL;
2545
2546 if (strcmp("$", key) == 0)
2547 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2548 return get_variable_from_key(arg, key, sym);
2549 }
2550
2551 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2552 {
2553 int name_len;
2554 static char buf[256];
2555 bool add_star = false;
2556
2557 name_len = strlen(param_name);
2558
2559 if (state_name[0] == '*') {
2560 add_star = true;
2561 state_name++;
2562 }
2563
2564 if (strcmp(state_name, param_name) == 0) {
2565 snprintf(buf, sizeof(buf), "%s$", add_star ? "*" : "");
2566 return alloc_sname(buf);
2567 }
2568
2569 if (state_name[name_len] == '-' && /* check for '-' from "->" */
2570 strncmp(state_name, param_name, name_len) == 0) {
2571 snprintf(buf, sizeof(buf), "%s$%s",
2572 add_star ? "*" : "", state_name + name_len);
2573 return alloc_sname(buf);
2574 }
2575 return NULL;
2576 }
2577
2578 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2579 {
2580 if (!sym || !sym->ident)
2581 return NULL;
2582
2583 return state_name_to_param_name(name, sym->ident->name);
2584 }
2585
2586 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2587 {
2588 struct symbol *type;
2589 const char *sym_name;
2590 int name_len;
2591 static char buf[256];
2592
|
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 */
17
18 #include <string.h>
19 #include <errno.h>
20 #include <unistd.h>
21 #include <ctype.h>
22 #include "smatch.h"
23 #include "smatch_slist.h"
24 #include "smatch_extra.h"
25
26 struct sqlite3 *smatch_db;
27 struct sqlite3 *mem_db;
28 struct sqlite3 *cache_db;
29
30 int debug_db;
31
32 static int return_id;
33
34 static void call_return_state_hooks(struct expression *expr);
35
36 #define SQLITE_CACHE_PAGES 1000
37
38 struct def_callback {
39 int hook_type;
40 void (*callback)(const char *name, struct symbol *sym, char *key, char *value);
41 };
42 ALLOCATOR(def_callback, "definition db hook callbacks");
43 DECLARE_PTR_LIST(callback_list, struct def_callback);
44 static struct callback_list *select_caller_info_callbacks;
45
46 struct member_info_callback {
47 int owner;
48 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm);
49 };
50 ALLOCATOR(member_info_callback, "caller_info callbacks");
51 DECLARE_PTR_LIST(member_info_cb_list, struct member_info_callback);
52 static struct member_info_cb_list *member_callbacks;
53
54 struct returned_state_callback {
55 void (*callback)(int return_id, char *return_ranges, struct expression *return_expr);
92 buf[j++] = '\\';
93 if (j == sizeof(buf))
94 break;
95 buf[j] = 'n';
96 }
97
98 if (!found)
99 return alloc_sname(str);
100
101 if (j == sizeof(buf))
102 buf[j - 1] = '\0';
103 return alloc_sname(buf);
104 }
105
106 static int print_sql_output(void *unused, int argc, char **argv, char **azColName)
107 {
108 int i;
109
110 for (i = 0; i < argc; i++) {
111 if (i != 0)
112 sm_printf(", ");
113 sm_printf("%s", argv[i]);
114 }
115 sm_printf("\n");
116 return 0;
117 }
118
119 void sql_exec(struct sqlite3 *db, int (*callback)(void*, int, char**, char**), void *data, const char *sql)
120 {
121 char *err = NULL;
122 int rc;
123
124 if (!db)
125 return;
126
127 if (option_debug || debug_db) {
128 sm_msg("%s", sql);
129 if (strncasecmp(sql, "select", strlen("select")) == 0)
130 sqlite3_exec(db, sql, print_sql_output, NULL, NULL);
131 }
132
133 rc = sqlite3_exec(db, sql, callback, data, &err);
134 if (rc != SQLITE_OK && !parse_error) {
135 sm_ierror("%s:%d SQL error #2: %s\n", get_filename(), get_lineno(), err);
136 sm_ierror("%s:%d SQL: '%s'\n", get_filename(), get_lineno(), sql);
137 parse_error = 1;
138 }
139 }
140
141 static int replace_count;
142 static char **replace_table;
143 static const char *replace_return_ranges(const char *return_ranges)
144 {
145 int i;
146
147 if (!get_function()) {
788 if (!p)
789 return 0;
790 p++;
791 while (true) {
792 next = strchr(p, '>');
793 if (!next)
794 return 0;
795 next++;
796
797 size = next - p;
798 if (size >= sizeof(buf))
799 return 0;
800 memcpy(buf, p, size);
801 buf[size] = '\0';
802 if (strstr(next, buf))
803 return 1;
804 p = next;
805 }
806 }
807
808 char *sm_to_arg_name(struct expression *expr, struct sm_state *sm)
809 {
810 struct symbol *sym;
811 const char *sm_name;
812 char *name;
813 bool is_address = false;
814 bool add_star = false;
815 char buf[256];
816 char *ret = NULL;
817 int len;
818
819 expr = strip_expr(expr);
820 if (!expr)
821 return NULL;
822
823 if (expr->type == EXPR_PREOP && expr->op == '&') {
824 expr = strip_expr(expr->unop);
825 is_address = true;
826 }
827
828 name = expr_to_var_sym(expr, &sym);
829 if (!name || !sym)
830 goto free;
831 if (sym != sm->sym)
832 goto free;
833
834 sm_name = sm->name;
835 add_star = false;
836 if (sm_name[0] == '*') {
837 add_star = true;
838 sm_name++;
839 }
840
841 len = strlen(name);
842 if (strncmp(name, sm_name, len) != 0)
843 goto free;
844 if (sm_name[len] == '\0') {
845 snprintf(buf, sizeof(buf), "%s%s$",
846 add_star ? "*" : "", is_address ? "*" : "");
847 } else {
848 if (sm_name[len] != '.' && sm_name[len] != '-')
849 goto free;
850 if (sm_name[len] == '-')
851 len++;
852 // FIXME does is_address really imply that sm_name[len] == '-'
853 snprintf(buf, sizeof(buf), "%s$->%s", add_star ? "*" : "",
854 sm_name + len);
855 }
856
857 ret = alloc_sname(buf);
858 free:
859 free_string(name);
860 return ret;
861 }
862
863 static void print_struct_members(struct expression *call, struct expression *expr, int param, int offset, struct stree *stree,
864 void (*callback)(struct expression *call, int param, char *printed_name, struct sm_state *sm))
865 {
866 struct sm_state *sm;
867 const char *sm_name;
868 char *name;
869 struct symbol *sym;
870 int len;
871 char printed_name[256];
872 int is_address = 0;
873 bool add_star;
874 struct symbol *type;
875
876 expr = strip_expr(expr);
877 if (!expr)
878 return;
879 type = get_type(expr);
880 if (type && type_bits(type) < type_bits(&ulong_ctype))
881 return;
882
1459 return alloc_sname(buf);
1460 }
1461 if (math_str) {
1462 snprintf(buf, sizeof(buf), "%s[%s]", return_ranges, math_str);
1463 return alloc_sname(buf);
1464 }
1465 compare_str = get_return_compare_str(expr);
1466 if (compare_str) {
1467 snprintf(buf, sizeof(buf), "%s%s", return_ranges, compare_str);
1468 return alloc_sname(buf);
1469 }
1470
1471 return return_ranges;
1472 }
1473
1474 static void match_return_info(int return_id, char *return_ranges, struct expression *expr)
1475 {
1476 sql_insert_return_states(return_id, return_ranges, INTERNAL, -1, "", function_signature());
1477 }
1478
1479 static bool call_return_state_hooks_conditional(struct expression *expr)
1480 {
1481 int final_pass_orig = final_pass;
1482 static int recurse;
1483
1484 if (recurse >= 2)
1485 return false;
1486 if (!expr ||
1487 (expr->type != EXPR_CONDITIONAL && expr->type != EXPR_SELECT))
1488 return false;
1489
1490 recurse++;
1491
1492 __push_fake_cur_stree();
1493
1494 final_pass = 0;
1495 __split_whole_condition(expr->conditional);
1496 final_pass = final_pass_orig;
1497
1498 call_return_state_hooks(expr->cond_true ?: expr->conditional);
1499
1500 __push_true_states();
1501 __use_false_states();
1502
1503 call_return_state_hooks(expr->cond_false);
1504
1505 __merge_true_states();
1506 __free_fake_cur_stree();
1507
1508 recurse--;
1509 return true;
1510 }
1511
1512 static void call_return_state_hooks_compare(struct expression *expr)
1513 {
1514 struct returned_state_callback *cb;
1515 char *return_ranges;
1516 int final_pass_orig = final_pass;
1517 sval_t sval = { .type = &int_ctype };
1518 sval_t ret;
1519
1520 if (!get_implied_value(expr, &ret))
1521 ret.value = -1;
1522
1523 __push_fake_cur_stree();
1524
1525 final_pass = 0;
1526 __split_whole_condition(expr);
1527 final_pass = final_pass_orig;
1528
1529 if (ret.value != 0) {
1574 char *return_ranges;
1575 struct sm_state *tmp;
1576 int ret = 0;
1577 int nr_possible, nr_states;
1578 char *compare_str;
1579 char buf[128];
1580 struct state_list *already_handled = NULL;
1581 sval_t sval;
1582
1583 if (!sm || !sm->merged)
1584 return 0;
1585
1586 if (too_many_possible(sm))
1587 return 0;
1588
1589 /* bail if it gets too complicated */
1590 nr_possible = 0;
1591 FOR_EACH_PTR(sm->possible, tmp) {
1592 if (tmp->merged)
1593 continue;
1594 if (ptr_in_list(tmp, already_handled))
1595 continue;
1596 add_ptr_list(&already_handled, tmp);
1597 nr_possible++;
1598 } END_FOR_EACH_PTR(tmp);
1599 free_slist(&already_handled);
1600 nr_states = get_db_state_count();
1601 if (nr_states * nr_possible >= 2000)
1602 return 0;
1603
1604 FOR_EACH_PTR(sm->possible, tmp) {
1605 if (tmp->merged)
1606 continue;
1607 if (ptr_in_list(tmp, already_handled))
1608 continue;
1609 add_ptr_list(&already_handled, tmp);
1610
1611 ret = 1;
1612 __push_fake_cur_stree();
1613
1614 overwrite_states_using_pool(sm, tmp);
1615
1616 rl = cast_rl(cur_func_return_type(), estate_rl(tmp->state));
1617 return_ranges = show_rl(rl);
1618 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(clone_rl(rl)));
1619 if (!rl_to_sval(rl, &sval)) {
1635 free_slist(&already_handled);
1636
1637 return ret;
1638 }
1639
1640 static int call_return_state_hooks_split_possible(struct expression *expr)
1641 {
1642 struct sm_state *sm;
1643
1644 if (!expr || expr_equal_to_param(expr, -1))
1645 return 0;
1646
1647 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1648 return split_possible_helper(sm, expr);
1649 }
1650
1651 static bool has_possible_negative(struct sm_state *sm)
1652 {
1653 struct sm_state *tmp;
1654
1655 if (!type_signed(estate_type(sm->state)))
1656 return false;
1657
1658 FOR_EACH_PTR(sm->possible, tmp) {
1659 if (!estate_rl(tmp->state))
1660 continue;
1661 if (sval_is_negative(estate_min(tmp->state)) &&
1662 sval_is_negative(estate_max(tmp->state)))
1663 return true;
1664 } END_FOR_EACH_PTR(tmp);
1665
1666 return false;
1667 }
1668
1669 static bool has_separate_zero_null(struct sm_state *sm)
1670 {
1671 struct sm_state *tmp;
1672 sval_t sval;
1673
1674 FOR_EACH_PTR(sm->possible, tmp) {
1675 if (!estate_get_single_value(tmp->state, &sval))
1676 continue;
1677 if (sval.value == 0)
1678 return true;
1679 } END_FOR_EACH_PTR(tmp);
1680
1681 return false;
1682 }
1683
1684 static int split_positive_from_negative(struct expression *expr)
1685 {
1686 struct sm_state *sm;
1687 struct returned_state_callback *cb;
1688 struct range_list *rl;
1689 const char *return_ranges;
1690 struct range_list *ret_rl;
1691 bool separate_zero;
1692 int undo;
1693
1694 /* We're going to print the states 3 times */
1695 if (get_db_state_count() > 10000 / 3)
1696 return 0;
1697
1698 if (!get_implied_rl(expr, &rl) || !rl)
1699 return 0;
1700 /* Forget about INT_MAX and larger */
1701 if (rl_max(rl).value <= 0)
1702 return 0;
1703 if (!sval_is_negative(rl_min(rl)))
1704 return 0;
1705
1706 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1707 if (!sm)
1708 return 0;
1709 if (!has_possible_negative(sm))
1710 return 0;
1711 separate_zero = has_separate_zero_null(sm);
1712
1713 if (!assume(compare_expression(expr, separate_zero ? '>' : SPECIAL_GTE, zero_expr())))
1714 return 0;
1715
1716 return_id++;
1717 return_ranges = get_return_ranges_str(expr, &ret_rl);
1718 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1719 FOR_EACH_PTR(returned_state_callbacks, cb) {
1720 cb->callback(return_id, (char *)return_ranges, expr);
1721 } END_FOR_EACH_PTR(cb);
1722
1723 end_assume();
1724
1725 if (separate_zero) {
1726 undo = assume(compare_expression(expr, SPECIAL_EQUAL, zero_expr()));
1727
1728 return_id++;
1729 return_ranges = get_return_ranges_str(expr, &ret_rl);
1730 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1731 FOR_EACH_PTR(returned_state_callbacks, cb) {
1732 cb->callback(return_id, (char *)return_ranges, expr);
1733 } END_FOR_EACH_PTR(cb);
1734
1735 if (undo)
1736 end_assume();
1737 }
1738
1739 undo = assume(compare_expression(expr, '<', zero_expr()));
1740
1741 return_id++;
1742 return_ranges = get_return_ranges_str(expr, &ret_rl);
1743 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
1744 FOR_EACH_PTR(returned_state_callbacks, cb) {
1745 cb->callback(return_id, (char *)return_ranges, expr);
1762 struct sm_state *sm;
1763 struct smatch_state *state;
1764 int nr_states;
1765 int final_pass_orig = final_pass;
1766
1767 if (!expr || expr_equal_to_param(expr, -1))
1768 return 0;
1769 if (expr->type == EXPR_CALL)
1770 return 0;
1771
1772 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1773 if (!sm)
1774 return 0;
1775 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1776 return 0;
1777 state = sm->state;
1778 if (!estate_rl(state))
1779 return 0;
1780 if (estate_min(state).value == 0 && estate_max(state).value == 0)
1781 return 0;
1782 if (!has_separate_zero_null(sm))
1783 return 0;
1784
1785 nr_states = get_db_state_count();
1786 if (option_info && nr_states >= 1500)
1787 return 0;
1788
1789 rl = estate_rl(state);
1790
1791 __push_fake_cur_stree();
1792
1793 final_pass = 0;
1794 __split_whole_condition(expr);
1795 final_pass = final_pass_orig;
1796
1797 nonnull_rl = rl_filter(rl, rl_zero());
1798 return_ranges = show_rl(nonnull_rl);
1799 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonnull_rl));
1800
1801 return_id++;
1802 FOR_EACH_PTR(returned_state_callbacks, cb) {
1804 } END_FOR_EACH_PTR(cb);
1805
1806 __push_true_states();
1807 __use_false_states();
1808
1809 return_ranges = alloc_sname("0");
1810 null_sval = sval_type_val(rl_type(rl), 0);
1811 add_range(&null_rl, null_sval, null_sval);
1812 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(null_rl));
1813 return_id++;
1814 FOR_EACH_PTR(returned_state_callbacks, cb) {
1815 cb->callback(return_id, return_ranges, expr);
1816 } END_FOR_EACH_PTR(cb);
1817
1818 __merge_true_states();
1819 __free_fake_cur_stree();
1820
1821 return 1;
1822 }
1823
1824 static bool is_kernel_success_fail(struct sm_state *sm)
1825 {
1826 struct sm_state *tmp;
1827 struct range_list *rl;
1828 bool has_zero = false;
1829 bool has_neg = false;
1830
1831 if (!type_signed(estate_type(sm->state)))
1832 return false;
1833
1834 FOR_EACH_PTR(sm->possible, tmp) {
1835 rl = estate_rl(tmp->state);
1836 if (!rl)
1837 return false;
1838 if (rl_min(rl).value == 0 && rl_max(rl).value == 0) {
1839 has_zero = true;
1840 continue;
1841 }
1842 has_neg = true;
1843 if (rl_min(rl).value >= -4095 && rl_max(rl).value < 0)
1844 continue;
1845 if (strcmp(tmp->state->name, "s32min-(-1)") == 0)
1846 continue;
1847 if (strcmp(tmp->state->name, "s32min-(-1),1-s32max") == 0)
1848 continue;
1849 return false;
1850 } END_FOR_EACH_PTR(tmp);
1851
1852 return has_zero && has_neg;
1853 }
1854
1855 static int call_return_state_hooks_split_success_fail(struct expression *expr)
1856 {
1857 struct sm_state *sm;
1858 struct range_list *rl;
1859 struct range_list *nonzero_rl;
1860 sval_t zero_sval;
1861 struct range_list *zero_rl = NULL;
1862 int nr_states;
1863 struct returned_state_callback *cb;
1864 char *return_ranges;
1865 int final_pass_orig = final_pass;
1866
1867 if (option_project != PROJ_KERNEL)
1868 return 0;
1869
1870 nr_states = get_db_state_count();
1871 if (nr_states > 2000)
1872 return 0;
1873
1874 sm = get_sm_state_expr(SMATCH_EXTRA, expr);
1875 if (!sm)
1876 return 0;
1877 if (ptr_list_size((struct ptr_list *)sm->possible) == 1)
1878 return 0;
1879 if (!is_kernel_success_fail(sm))
1880 return 0;
1881
1882 rl = estate_rl(sm->state);
1883 if (!rl)
1884 return 0;
1885
1886 __push_fake_cur_stree();
1887
1888 final_pass = 0;
1889 __split_whole_condition(expr);
1890 final_pass = final_pass_orig;
1891
1892 nonzero_rl = rl_filter(rl, rl_zero());
1893 nonzero_rl = cast_rl(cur_func_return_type(), nonzero_rl);
1894 return_ranges = show_rl(nonzero_rl);
1895 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(nonzero_rl));
1896
1897 return_id++;
1898 FOR_EACH_PTR(returned_state_callbacks, cb) {
1899 cb->callback(return_id, return_ranges, expr);
1900 } END_FOR_EACH_PTR(cb);
1901
1902 __push_true_states();
1903 __use_false_states();
1904
1905 return_ranges = alloc_sname("0");
1911 cb->callback(return_id, return_ranges, expr);
1912 } END_FOR_EACH_PTR(cb);
1913
1914 __merge_true_states();
1915 __free_fake_cur_stree();
1916
1917 return 1;
1918 }
1919
1920 static int is_boolean(struct expression *expr)
1921 {
1922 struct range_list *rl;
1923
1924 if (!get_implied_rl(expr, &rl))
1925 return 0;
1926 if (rl_min(rl).value == 0 && rl_max(rl).value == 1)
1927 return 1;
1928 return 0;
1929 }
1930
1931 static int splitable_function_call(struct expression *expr)
1932 {
1933 struct sm_state *sm;
1934 char buf[64];
1935
1936 if (!expr || expr->type != EXPR_CALL)
1937 return 0;
1938 snprintf(buf, sizeof(buf), "return %p", expr);
1939 sm = get_sm_state(SMATCH_EXTRA, buf, NULL);
1940 return split_possible_helper(sm, expr);
1941 }
1942
1943 static struct sm_state *find_bool_param(void)
1944 {
1945 struct stree *start_states;
1946 struct symbol *arg;
1947 struct sm_state *sm, *tmp;
1948 sval_t sval;
1949
1950 start_states = get_start_states();
1968 * Check if it's splitable. If not, then splitting it up is likely not
1969 * useful for the callers.
1970 */
1971 FOR_EACH_PTR(sm->possible, tmp) {
1972 if (is_merged(tmp))
1973 continue;
1974 if (!estate_get_single_value(tmp->state, &sval))
1975 return NULL;
1976 } END_FOR_EACH_PTR(tmp);
1977
1978 return sm;
1979 }
1980
1981 static int split_on_bool_sm(struct sm_state *sm, struct expression *expr)
1982 {
1983 struct returned_state_callback *cb;
1984 struct range_list *ret_rl;
1985 const char *return_ranges;
1986 struct sm_state *tmp;
1987 int ret = 0;
1988 struct state_list *already_handled = NULL;
1989
1990 if (!sm || !sm->merged)
1991 return 0;
1992
1993 if (too_many_possible(sm))
1994 return 0;
1995
1996 FOR_EACH_PTR(sm->possible, tmp) {
1997 if (tmp->merged)
1998 continue;
1999 if (ptr_in_list(tmp, already_handled))
2000 continue;
2001 add_ptr_list(&already_handled, tmp);
2002
2003 ret = 1;
2004 __push_fake_cur_stree();
2005
2006 overwrite_states_using_pool(sm, tmp);
2007
2008 return_ranges = get_return_ranges_str(expr, &ret_rl);
2009 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2010 return_id++;
2011 FOR_EACH_PTR(returned_state_callbacks, cb) {
2012 cb->callback(return_id, (char *)return_ranges, expr);
2013 } END_FOR_EACH_PTR(cb);
2014
2015 __free_fake_cur_stree();
2016 } END_FOR_EACH_PTR(tmp);
2017
2018 free_slist(&already_handled);
2019
2020 return ret;
2021 }
2022
2023 static int split_by_bool_param(struct expression *expr)
2024 {
2025 struct sm_state *start_sm, *sm;
2026 sval_t sval;
2027
2028 start_sm = find_bool_param();
2029 if (!start_sm)
2030 return 0;
2031 sm = get_sm_state(SMATCH_EXTRA, start_sm->name, start_sm->sym);
2032 if (!sm || estate_get_single_value(sm->state, &sval))
2033 return 0;
2034
2035 if (get_db_state_count() * 2 >= 2000)
2036 return 0;
2037
2038 return split_on_bool_sm(sm, expr);
2039 }
2040
2041 static int split_by_null_nonnull_param(struct expression *expr)
2042 {
2043 struct symbol *arg;
2044 struct sm_state *sm;
2045 int nr_possible;
2046
2047 /* function must only take one pointer */
2048 if (ptr_list_size((struct ptr_list *)cur_func_sym->ctype.base_type->arguments) != 1)
2049 return 0;
2050 arg = first_ptr_list((struct ptr_list *)cur_func_sym->ctype.base_type->arguments);
2051 if (!arg->ident)
2052 return 0;
2053 if (get_real_base_type(arg)->type != SYM_PTR)
2054 return 0;
2055
2056 if (param_was_set_var_sym(arg->ident->name, arg))
2057 return 0;
2058 sm = get_sm_state(SMATCH_EXTRA, arg->ident->name, arg);
2059 if (!sm)
2060 return 0;
2061
2062 if (!has_separate_zero_null(sm))
2063 return 0;
2064
2065 nr_possible = ptr_list_size((struct ptr_list *)sm->possible);
2066 if (get_db_state_count() * nr_possible >= 2000)
2067 return 0;
2068
2069 return split_on_bool_sm(sm, expr);
2070 }
2071
2072 struct expression *strip_expr_statement(struct expression *expr)
2073 {
2074 struct expression *orig = expr;
2075 struct statement *stmt, *last_stmt;
2076
2077 if (!expr)
2078 return NULL;
2079 if (expr->type == EXPR_PREOP && expr->op == '(')
2080 expr = expr->unop;
2081 if (expr->type != EXPR_STATEMENT)
2082 return orig;
2083 stmt = expr->statement;
2084 if (!stmt || stmt->type != STMT_COMPOUND)
2085 return orig;
2086
2087 last_stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
2088 if (!last_stmt || last_stmt->type == STMT_LABEL)
2097 struct returned_state_callback *cb;
2098 struct range_list *ret_rl;
2099 const char *return_ranges;
2100 int nr_states;
2101 sval_t sval;
2102
2103 if (__path_is_null())
2104 return;
2105
2106 expr = strip_expr(expr);
2107 expr = strip_expr_statement(expr);
2108
2109 if (is_impossible_path())
2110 goto vanilla;
2111
2112 if (expr && (expr->type == EXPR_COMPARE ||
2113 !get_implied_value(expr, &sval)) &&
2114 (is_condition(expr) || is_boolean(expr))) {
2115 call_return_state_hooks_compare(expr);
2116 return;
2117 } else if (call_return_state_hooks_conditional(expr)) {
2118 return;
2119 } else if (call_return_state_hooks_split_possible(expr)) {
2120 return;
2121 } else if (split_positive_from_negative(expr)) {
2122 return;
2123 } else if (call_return_state_hooks_split_null_non_null_zero(expr)) {
2124 return;
2125 } else if (call_return_state_hooks_split_success_fail(expr)) {
2126 return;
2127 } else if (splitable_function_call(expr)) {
2128 return;
2129 } else if (split_by_bool_param(expr)) {
2130 } else if (split_by_null_nonnull_param(expr)) {
2131 return;
2132 }
2133
2134 vanilla:
2135 return_ranges = get_return_ranges_str(expr, &ret_rl);
2136 set_state(RETURN_ID, "return_ranges", NULL, alloc_estate_rl(ret_rl));
2137
2550 free_string(name);
2551 return alloc_string(member_name);
2552 }
2553
2554 while (expr->type == EXPR_ASSIGNMENT)
2555 expr = strip_expr(expr->right);
2556 if (expr->type != EXPR_CALL)
2557 return NULL;
2558
2559 arg = get_argument_from_call_expr(expr->args, param);
2560 if (!arg)
2561 return NULL;
2562
2563 return get_variable_from_key(arg, key, sym);
2564 }
2565
2566 char *get_variable_from_key(struct expression *arg, const char *key, struct symbol **sym)
2567 {
2568 char buf[256];
2569 char *tmp;
2570 int star_cnt = 0;
2571
2572 if (!arg)
2573 return NULL;
2574
2575 arg = strip_expr(arg);
2576
2577 if (strcmp(key, "$") == 0)
2578 return expr_to_var_sym(arg, sym);
2579
2580 if (strcmp(key, "*$") == 0) {
2581 if (arg->type == EXPR_PREOP && arg->op == '&') {
2582 arg = strip_expr(arg->unop);
2583 return expr_to_var_sym(arg, sym);
2584 } else {
2585 tmp = expr_to_var_sym(arg, sym);
2586 if (!tmp)
2587 return NULL;
2588 snprintf(buf, sizeof(buf), "*%s", tmp);
2589 free_string(tmp);
2590 return alloc_string(buf);
2591 }
2592 }
2593
2594 while (key[0] == '*') {
2595 star_cnt++;
2596 key++;
2597 }
2598
2599 if (arg->type == EXPR_PREOP && arg->op == '&' && star_cnt) {
2600 arg = strip_expr(arg->unop);
2601 star_cnt--;
2602 }
2603
2604 if (arg->type == EXPR_PREOP && arg->op == '&') {
2605 arg = strip_expr(arg->unop);
2606 tmp = expr_to_var_sym(arg, sym);
2607 if (!tmp)
2608 return NULL;
2609 snprintf(buf, sizeof(buf), "%.*s%s.%s",
2610 star_cnt, "**********", tmp, key + 3);
2611 return alloc_string(buf);
2612 }
2613
2614 tmp = expr_to_var_sym(arg, sym);
2615 if (!tmp)
2616 return NULL;
2617 snprintf(buf, sizeof(buf), "%.*s%s%s", star_cnt, "**********", tmp, key + 1);
2618 free_string(tmp);
2619 return alloc_string(buf);
2620 }
2621
2622 char *get_chunk_from_key(struct expression *arg, char *key, struct symbol **sym, struct var_sym_list **vsl)
2623 {
2624 *vsl = NULL;
2625
2626 if (strcmp("$", key) == 0)
2627 return expr_to_chunk_sym_vsl(arg, sym, vsl);
2628 return get_variable_from_key(arg, key, sym);
2629 }
2630
2631 const char *state_name_to_param_name(const char *state_name, const char *param_name)
2632 {
2633 int star_cnt = 0;
2634 int name_len;
2635 char buf[256];
2636
2637 name_len = strlen(param_name);
2638
2639 while (state_name[0] == '*') {
2640 star_cnt++;
2641 state_name++;
2642 }
2643
2644 /* ten out of ten stars! */
2645 if (star_cnt > 10)
2646 return NULL;
2647
2648 if (strcmp(state_name, param_name) == 0) {
2649 snprintf(buf, sizeof(buf), "%.*s$", star_cnt, "**********");
2650 return alloc_sname(buf);
2651 }
2652
2653 if (state_name[name_len] == '-' && /* check for '-' from "->" */
2654 strncmp(state_name, param_name, name_len) == 0) {
2655 snprintf(buf, sizeof(buf), "%.*s$%s", star_cnt, "**********", state_name + name_len);
2656 return alloc_sname(buf);
2657 }
2658 return NULL;
2659 }
2660
2661 const char *get_param_name_var_sym(const char *name, struct symbol *sym)
2662 {
2663 if (!sym || !sym->ident)
2664 return NULL;
2665
2666 return state_name_to_param_name(name, sym->ident->name);
2667 }
2668
2669 const char *get_mtag_name_var_sym(const char *state_name, struct symbol *sym)
2670 {
2671 struct symbol *type;
2672 const char *sym_name;
2673 int name_len;
2674 static char buf[256];
2675
|