Print this page
11972 resync smatch
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/tools/smatch/src/check_readl_infinite_loops.c
+++ new/usr/src/tools/smatch/src/check_readl_infinite_loops.c
1 1 /*
2 2 * Copyright (C) 2014 Oracle.
3 3 *
4 4 * This program is free software; you can redistribute it and/or
5 5 * modify it under the terms of the GNU General Public License
6 6 * as published by the Free Software Foundation; either version 2
7 7 * of the License, or (at your option) any later version.
8 8 *
9 9 * This program is distributed in the hope that it will be useful,
10 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 * GNU General Public License for more details.
13 13 *
14 14 * You should have received a copy of the GNU General Public License
15 15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 16 */
17 17
18 18 #include "smatch.h"
19 19 #include "smatch_extra.h"
20 20
21 21 static int my_id;
22 22
23 23 STATE(readl);
24 24 STATE(readl_ff);
25 25 STATE(readl_00);
26 26
27 27 DECLARE_PTR_LIST(state_stack, struct smatch_state);
28 28 struct state_stack *state_at_start;
29 29
30 30 static int readl_has_been_called;
31 31 static int returned;
32 32
33 33 static int is_readl_call(struct expression *expr)
34 34 {
35 35 struct symbol *sym;
36 36
37 37 expr = strip_expr(expr);
38 38 if (expr->type != EXPR_CALL)
39 39 return 0;
40 40 if (expr->fn->type != EXPR_SYMBOL)
41 41 return 0;
42 42 sym = expr->fn->symbol;
43 43 if (!sym || !sym->ident)
44 44 return 0;
45 45 if (strcmp(sym->ident->name, "readl") != 0)
46 46 return 0;
47 47 return 1;
48 48 }
49 49
50 50 static int is_readl(struct expression *expr)
51 51 {
52 52 if (is_readl_call(expr))
53 53 return 1;
54 54 if (get_state_expr(my_id, expr) == &readl)
55 55 return 1;
56 56 return 0;
57 57 }
58 58
59 59 static void match_assign(struct expression *expr)
60 60 {
61 61 if (is_readl(expr->right))
62 62 set_state_expr(my_id, expr->left, &readl);
63 63 else if (get_state_expr(my_id, expr->left))
64 64 set_state_expr(my_id, expr->left, &undefined);
65 65 }
66 66
67 67 static int condition_depends_on_readl(struct expression *expr)
68 68 {
69 69 if (expr->type == EXPR_BINOP) {
70 70 if (condition_depends_on_readl(expr->left))
71 71 return 1;
72 72 if (condition_depends_on_readl(expr->right))
73 73 return 1;
74 74 return 0;
75 75 }
76 76 if (is_readl(expr))
77 77 return 1;
78 78 return 0;
79 79 }
80 80
81 81 static void check_condition(struct expression *expr)
82 82 {
83 83 if (expr->op != '&')
84 84 return;
85 85 if (!condition_depends_on_readl(expr))
86 86 return;
87 87 readl_has_been_called = 1;
88 88 set_true_false_states(my_id, "depends on", NULL, &readl_ff, &readl_00);
89 89 }
90 90
91 91 static void match_return(struct expression *expr)
92 92 {
93 93
94 94 if (__inline_fn)
95 95 return;
96 96 returned = 1;
97 97 #if 0
98 98 struct smatch_state *tmp;
99 99
100 100 if (!readl_has_been_called)
101 101 return;
102 102
103 103 FOR_EACH_PTR(state_at_start, tmp) {
104 104 REPLACE_CURRENT_PTR(tmp, NULL);
105 105 }
106 106 #endif
107 107 }
108 108
109 109 static void push_state_at_start(struct smatch_state *state)
110 110 {
111 111 add_ptr_list(&state_at_start, state);
112 112 }
113 113
114 114 static struct smatch_state *pop_state_at_start(void)
115 115 {
116 116 struct smatch_state *state;
117 117
118 118 state = last_ptr_list((struct ptr_list *)state_at_start);
↓ open down ↓ |
118 lines elided |
↑ open up ↑ |
119 119 delete_ptr_list_last((struct ptr_list **)&state_at_start);
120 120 return state;
121 121 }
122 122
123 123 static void before_loop(struct statement *stmt)
124 124 {
125 125 struct smatch_state *state;
126 126
127 127 if (!stmt || stmt->type != STMT_ITERATOR)
128 128 return;
129 - if (ptr_list_empty(state_at_start))
129 + if (ptr_list_empty((struct ptr_list *)state_at_start))
130 130 returned = 0;
131 131 state = get_state(my_id, "depends on", NULL);
132 132 push_state_at_start(state);
133 133 }
134 134
135 135 static void after_loop(struct statement *stmt)
136 136 {
137 137 struct smatch_state *old_state;
138 138
139 139 if (!stmt || stmt->type != STMT_ITERATOR)
140 140 return;
141 141 old_state = pop_state_at_start();
142 142 if (old_state == &readl_00)
143 143 return;
144 144 if (returned)
145 145 return;
146 146 if (get_state(my_id, "depends on", NULL) != &readl_00)
147 147 return;
148 148 sm_warning("this loop depends on readl() succeeding");
149 149 }
150 150
151 151 void check_readl_infinite_loops(int id)
152 152 {
153 153 if (option_project != PROJ_KERNEL)
154 154 return;
155 155
156 156 my_id = id;
157 157
158 158 add_hook(match_assign, ASSIGNMENT_HOOK);
159 159 add_hook(check_condition, CONDITION_HOOK);
160 160
161 161 add_hook(&match_return, RETURN_HOOK);
162 162
163 163 add_hook(before_loop, STMT_HOOK);
164 164 add_hook(after_loop, STMT_HOOK_AFTER);
165 165 }
↓ open down ↓ |
26 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX