struct_assignment: use assigned expression
[smatch.git] / check_dev_queue_xmit.c
blob1ee247a2321674d4e6a5ae93a93fe29a0845a2ec
1 /*
2 * Copyright (C) 2010 Dan Carpenter.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
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.
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
19 * According to an email on lkml you are not allowed to reuse the skb
20 * passed to dev_queue_xmit()
24 #include "smatch.h"
25 #include "smatch_slist.h"
27 static int my_id;
29 STATE(do_not_use);
31 static int valid_use(void)
33 struct expression *tmp;
34 int i = 0;
35 int dot_ops = 0;
37 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
38 if (!i++)
39 continue;
40 if (tmp->type == EXPR_PREOP && tmp->op == '(')
41 continue;
42 if (tmp->op == '.' && !dot_ops++)
43 continue;
44 // if (tmp->type == EXPR_POSTOP)
45 // return 1;
46 if (tmp->type == EXPR_CALL && sym_name_is("kfree_skb", tmp->fn))
47 return 1;
48 return 0;
49 } END_FOR_EACH_PTR_REVERSE(tmp);
50 return 0;
53 /* match symbol is expensive. only turn it on after we match the xmit function */
54 static int match_symbol_active;
55 static void match_symbol(struct expression *expr)
57 struct sm_state *sm;
58 char *name;
60 sm = get_sm_state_expr(my_id, expr);
61 if (!sm || !slist_has_state(sm->possible, &do_not_use))
62 return;
63 if (valid_use())
64 return;
65 name = expr_to_var(expr);
66 sm_error("'%s' was already used up by dev_queue_xmit()", name);
67 free_string(name);
70 static void match_kfree_skb(const char *fn, struct expression *expr, void *param)
72 struct expression *arg;
74 arg = get_argument_from_call_expr(expr->args, 0);
75 if (!arg)
76 return;
77 set_state_expr(my_id, arg, &undefined);
80 static void match_xmit(const char *fn, struct expression *expr, void *param)
82 struct expression *arg;
84 arg = get_argument_from_call_expr(expr->args, PTR_INT(param));
85 if (!arg)
86 return;
87 set_state_expr(my_id, arg, &do_not_use);
88 if (!match_symbol_active++) {
89 add_hook(&match_symbol, SYM_HOOK);
90 add_function_hook("kfree_skb", &match_kfree_skb, NULL);
94 static void register_funcs_from_file(void)
96 struct token *token;
97 const char *func;
98 int arg;
100 token = get_tokens_file("kernel.dev_queue_xmit");
101 if (!token)
102 return;
103 if (token_type(token) != TOKEN_STREAMBEGIN)
104 return;
105 token = token->next;
106 while (token_type(token) != TOKEN_STREAMEND) {
107 if (token_type(token) != TOKEN_IDENT)
108 return;
109 func = show_ident(token->ident);
110 token = token->next;
111 if (token_type(token) != TOKEN_NUMBER)
112 return;
113 arg = atoi(token->number);
114 add_function_hook(func, &match_xmit, INT_PTR(arg));
115 token = token->next;
117 clear_token_alloc();
120 void check_dev_queue_xmit(int id)
122 if (option_project != PROJ_KERNEL)
123 return;
124 my_id = id;
125 add_modification_hook(my_id, &set_undefined);
126 register_funcs_from_file();