59 static struct timeval fn_start_time;
60 static struct timeval outer_fn_start_time;
61 char *get_function(void) { return cur_func; }
62 int get_lineno(void) { return __smatch_lineno; }
63 int inside_loop(void) { return !!loop_count; }
64 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
65 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
66 int in_expression_statement(void) { return !!__expr_stmt_count; }
67
68 static void split_symlist(struct symbol_list *sym_list);
69 static void split_declaration(struct symbol_list *sym_list);
70 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
71 static void add_inline_function(struct symbol *sym);
72 static void parse_inline(struct expression *expr);
73
74 int option_assume_loops = 0;
75 int option_two_passes = 0;
76 struct symbol *cur_func_sym = NULL;
77 struct stree *global_states;
78
79 long long valid_ptr_min = 4096;
80 long long valid_ptr_max = 2117777777;
81 sval_t valid_ptr_min_sval = {
82 .type = &ptr_ctype,
83 {.value = 4096},
84 };
85 sval_t valid_ptr_max_sval = {
86 .type = &ptr_ctype,
87 {.value = LONG_MAX - 100000},
88 };
89 struct range_list *valid_ptr_rl;
90
91 static void set_valid_ptr_max(void)
92 {
93 if (type_bits(&ptr_ctype) == 32)
94 valid_ptr_max = 2117777777;
95 else if (type_bits(&ptr_ctype) == 64)
96 valid_ptr_max = 2117777777777777777LL;
97
98 valid_ptr_max_sval.value = valid_ptr_max;
99 }
100
101 static void alloc_valid_ptr_rl(void)
102 {
103 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
104 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
105 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
106 }
107
108 int outside_of_function(void)
109 {
110 return cur_func_sym == NULL;
111 }
112
113 const char *get_filename(void)
114 {
115 if (option_info && option_full_path)
116 return full_base_file;
117 if (option_info)
118 return base_file;
119 if (option_full_path)
120 return full_filename;
121 return filename;
122 }
487 break;
488 case EXPR_SLICE:
489 expr_set_parent_expr(expr->base, expr);
490
491 __split_expr(expr->base);
492 break;
493 case EXPR_CAST:
494 case EXPR_FORCE_CAST:
495 expr_set_parent_expr(expr->cast_expression, expr);
496
497 __pass_to_client(expr, CAST_HOOK);
498 __split_expr(expr->cast_expression);
499 break;
500 case EXPR_SIZEOF:
501 if (expr->cast_expression)
502 __pass_to_client(strip_parens(expr->cast_expression),
503 SIZEOF_HOOK);
504 break;
505 case EXPR_OFFSETOF:
506 case EXPR_ALIGNOF:
507 evaluate_expression(expr);
508 break;
509 case EXPR_CONDITIONAL:
510 case EXPR_SELECT:
511 expr_set_parent_expr(expr->conditional, expr);
512 expr_set_parent_expr(expr->cond_true, expr);
513 expr_set_parent_expr(expr->cond_false, expr);
514
515 if (known_condition_true(expr->conditional)) {
516 __split_expr(expr->cond_true);
517 break;
518 }
519 if (known_condition_false(expr->conditional)) {
520 __split_expr(expr->cond_false);
521 break;
522 }
523 __pass_to_client(expr, SELECT_HOOK);
524 __split_whole_condition(expr->conditional);
525 __split_expr(expr->cond_true);
526 __push_true_states();
527 __use_false_states();
861 if (!tmp)
862 break;
863 rl = rl_union(rl, tmp);
864 if (!stmt->case_expression)
865 __set_default();
866 stmt = stmt->case_statement;
867 }
868
869 __merge_switches(top_expression(switch_expr_stack), rl);
870
871 if (!stmt->case_expression)
872 __set_default();
873 __split_stmt(stmt->case_statement);
874 }
875
876 int time_parsing_function(void)
877 {
878 return ms_since(&fn_start_time) / 1000;
879 }
880
881 static int taking_too_long(void)
882 {
883 if ((ms_since(&outer_fn_start_time) / 1000) > 60 * 5) /* five minutes */
884 return 1;
885 return 0;
886 }
887
888 static int is_last_stmt(struct statement *cur_stmt)
889 {
890 struct symbol *fn;
891 struct statement *stmt;
892
893 if (!cur_func_sym)
894 return 0;
895 fn = get_base_type(cur_func_sym);
896 if (!fn)
897 return 0;
898 stmt = fn->stmt;
899 if (!stmt)
900 stmt = fn->inline_stmt;
901 if (!stmt || stmt->type != STMT_COMPOUND)
902 return 0;
903 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1891
1892 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1893 sm_outfd = fopen(buf, "w");
1894 if (!sm_outfd)
1895 sm_fatal("Cannot open %s", buf);
1896
1897 if (!option_info)
1898 return;
1899
1900 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1901 sql_outfd = fopen(buf, "w");
1902 if (!sql_outfd)
1903 sm_fatal("Error: Cannot open %s", buf);
1904
1905 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1906 caller_info_fd = fopen(buf, "w");
1907 if (!caller_info_fd)
1908 sm_fatal("Error: Cannot open %s", buf);
1909 }
1910
1911 void smatch(int argc, char **argv)
1912 {
1913 struct string_list *filelist = NULL;
1914 struct symbol_list *sym_list;
1915 struct timeval stop, start;
1916 char *path;
1917 int len;
1918
1919 gettimeofday(&start, NULL);
1920
1921 sparse_initialize(argc, argv, &filelist);
1922 set_valid_ptr_max();
1923 alloc_valid_ptr_rl();
1924 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1925 path = getcwd(NULL, 0);
1926 free(full_base_file);
1927 if (path) {
1928 len = strlen(path) + 1 + strlen(base_file) + 1;
1929 full_base_file = malloc(len);
1930 snprintf(full_base_file, len, "%s/%s", path, base_file);
1931 } else {
1932 full_base_file = alloc_string(base_file);
1933 }
1934 if (option_file_output)
1935 open_output_files(base_file);
1936 sym_list = sparse_keep_tokens(base_file);
1937 split_c_file_functions(sym_list);
1938 } END_FOR_EACH_PTR_NOTAG(base_file);
1939
1940 gettimeofday(&stop, NULL);
1941
1942 set_position(last_pos);
1943 if (option_time)
1944 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1945 if (option_mem)
1946 sm_msg("mem: %luKb", get_max_memory());
1947 }
|
59 static struct timeval fn_start_time;
60 static struct timeval outer_fn_start_time;
61 char *get_function(void) { return cur_func; }
62 int get_lineno(void) { return __smatch_lineno; }
63 int inside_loop(void) { return !!loop_count; }
64 int definitely_inside_loop(void) { return !!(loop_count & ~0x08000000); }
65 struct expression *get_switch_expr(void) { return top_expression(switch_expr_stack); }
66 int in_expression_statement(void) { return !!__expr_stmt_count; }
67
68 static void split_symlist(struct symbol_list *sym_list);
69 static void split_declaration(struct symbol_list *sym_list);
70 static void split_expr_list(struct expression_list *expr_list, struct expression *parent);
71 static void add_inline_function(struct symbol *sym);
72 static void parse_inline(struct expression *expr);
73
74 int option_assume_loops = 0;
75 int option_two_passes = 0;
76 struct symbol *cur_func_sym = NULL;
77 struct stree *global_states;
78
79 const unsigned long valid_ptr_min = 4096;
80 unsigned long valid_ptr_max = ULONG_MAX & ~(MTAG_OFFSET_MASK);
81 const sval_t valid_ptr_min_sval = {
82 .type = &ptr_ctype,
83 {.value = 4096},
84 };
85 sval_t valid_ptr_max_sval = {
86 .type = &ptr_ctype,
87 {.value = ULONG_MAX & ~(MTAG_OFFSET_MASK)},
88 };
89 struct range_list *valid_ptr_rl;
90
91 void alloc_valid_ptr_rl(void)
92 {
93 valid_ptr_max = sval_type_max(&ulong_ctype).value & ~(MTAG_OFFSET_MASK);
94 valid_ptr_max_sval.value = valid_ptr_max;
95
96 valid_ptr_rl = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
97 valid_ptr_rl = cast_rl(&ptr_ctype, valid_ptr_rl);
98 valid_ptr_rl = clone_rl_permanent(valid_ptr_rl);
99 }
100
101 int outside_of_function(void)
102 {
103 return cur_func_sym == NULL;
104 }
105
106 const char *get_filename(void)
107 {
108 if (option_info && option_full_path)
109 return full_base_file;
110 if (option_info)
111 return base_file;
112 if (option_full_path)
113 return full_filename;
114 return filename;
115 }
480 break;
481 case EXPR_SLICE:
482 expr_set_parent_expr(expr->base, expr);
483
484 __split_expr(expr->base);
485 break;
486 case EXPR_CAST:
487 case EXPR_FORCE_CAST:
488 expr_set_parent_expr(expr->cast_expression, expr);
489
490 __pass_to_client(expr, CAST_HOOK);
491 __split_expr(expr->cast_expression);
492 break;
493 case EXPR_SIZEOF:
494 if (expr->cast_expression)
495 __pass_to_client(strip_parens(expr->cast_expression),
496 SIZEOF_HOOK);
497 break;
498 case EXPR_OFFSETOF:
499 case EXPR_ALIGNOF:
500 break;
501 case EXPR_CONDITIONAL:
502 case EXPR_SELECT:
503 expr_set_parent_expr(expr->conditional, expr);
504 expr_set_parent_expr(expr->cond_true, expr);
505 expr_set_parent_expr(expr->cond_false, expr);
506
507 if (known_condition_true(expr->conditional)) {
508 __split_expr(expr->cond_true);
509 break;
510 }
511 if (known_condition_false(expr->conditional)) {
512 __split_expr(expr->cond_false);
513 break;
514 }
515 __pass_to_client(expr, SELECT_HOOK);
516 __split_whole_condition(expr->conditional);
517 __split_expr(expr->cond_true);
518 __push_true_states();
519 __use_false_states();
853 if (!tmp)
854 break;
855 rl = rl_union(rl, tmp);
856 if (!stmt->case_expression)
857 __set_default();
858 stmt = stmt->case_statement;
859 }
860
861 __merge_switches(top_expression(switch_expr_stack), rl);
862
863 if (!stmt->case_expression)
864 __set_default();
865 __split_stmt(stmt->case_statement);
866 }
867
868 int time_parsing_function(void)
869 {
870 return ms_since(&fn_start_time) / 1000;
871 }
872
873 /*
874 * This defaults to 60 * 5 == 5 minutes, so we'll just multiply
875 * whatever we're given by 5.
876 */
877 bool taking_too_long(void)
878 {
879 if (option_timeout &&
880 (ms_since(&outer_fn_start_time) / 1000) > option_timeout * 5)
881 return 1;
882 return 0;
883 }
884
885 static int is_last_stmt(struct statement *cur_stmt)
886 {
887 struct symbol *fn;
888 struct statement *stmt;
889
890 if (!cur_func_sym)
891 return 0;
892 fn = get_base_type(cur_func_sym);
893 if (!fn)
894 return 0;
895 stmt = fn->stmt;
896 if (!stmt)
897 stmt = fn->inline_stmt;
898 if (!stmt || stmt->type != STMT_COMPOUND)
899 return 0;
900 stmt = last_ptr_list((struct ptr_list *)stmt->stmts);
1888
1889 snprintf(buf, sizeof(buf), "%s.smatch", base_file);
1890 sm_outfd = fopen(buf, "w");
1891 if (!sm_outfd)
1892 sm_fatal("Cannot open %s", buf);
1893
1894 if (!option_info)
1895 return;
1896
1897 snprintf(buf, sizeof(buf), "%s.smatch.sql", base_file);
1898 sql_outfd = fopen(buf, "w");
1899 if (!sql_outfd)
1900 sm_fatal("Error: Cannot open %s", buf);
1901
1902 snprintf(buf, sizeof(buf), "%s.smatch.caller_info", base_file);
1903 caller_info_fd = fopen(buf, "w");
1904 if (!caller_info_fd)
1905 sm_fatal("Error: Cannot open %s", buf);
1906 }
1907
1908 void smatch(struct string_list *filelist)
1909 {
1910 struct symbol_list *sym_list;
1911 struct timeval stop, start;
1912 char *path;
1913 int len;
1914
1915 gettimeofday(&start, NULL);
1916
1917 FOR_EACH_PTR_NOTAG(filelist, base_file) {
1918 path = getcwd(NULL, 0);
1919 free(full_base_file);
1920 if (path) {
1921 len = strlen(path) + 1 + strlen(base_file) + 1;
1922 full_base_file = malloc(len);
1923 snprintf(full_base_file, len, "%s/%s", path, base_file);
1924 } else {
1925 full_base_file = alloc_string(base_file);
1926 }
1927 if (option_file_output)
1928 open_output_files(base_file);
1929 sym_list = sparse_keep_tokens(base_file);
1930 split_c_file_functions(sym_list);
1931 } END_FOR_EACH_PTR_NOTAG(base_file);
1932
1933 gettimeofday(&stop, NULL);
1934
1935 set_position(last_pos);
1936 final_pass = 1;
1937 if (option_time)
1938 sm_msg("time: %lu", stop.tv_sec - start.tv_sec);
1939 if (option_mem)
1940 sm_msg("mem: %luKb", get_max_memory());
1941 }
|