precedence: make test stricter
[smatch.git] / check_dev_queue_xmit.c
blobf3d069ecdce9825cffa9428c9a8474b3297d0511
1 /*
2 * sparse/check_dev_queue_xmit.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * According to an email on lkml you are not allowed to reuse the skb
12 * passed to dev_queue_xmit()
16 #include "smatch.h"
18 static int my_id;
20 STATE(do_not_use);
22 static void ok_to_use(const char *name, struct symbol *sym, struct expression *expr, void *unused)
24 delete_state(my_id, name, sym);
27 static int valid_use()
29 struct expression *tmp;
30 int i = 0;
31 int dot_ops = 0;
33 FOR_EACH_PTR_REVERSE(big_expression_stack, tmp) {
34 if (!i++)
35 continue;
36 if (tmp->type == EXPR_PREOP && tmp->op == '(')
37 continue;
38 if (tmp->op == '.' && !dot_ops++)
39 continue;
40 // if (tmp->type == EXPR_POSTOP)
41 // return 1;
42 if (tmp->type == EXPR_CALL && sym_name_is("kfree_skb", tmp->fn))
43 return 1;
44 return 0;
45 } END_FOR_EACH_PTR_REVERSE(tmp);
46 return 0;
49 /* match symbol is expensive. only turn it on after we match the xmit function */
50 static int match_symbol_active;
51 static void match_symbol(struct expression *expr)
53 struct sm_state *sm;
54 char *name;
56 sm = get_sm_state_expr(my_id, expr);
57 if (!sm)
58 return;
59 if (valid_use())
60 return;
61 name = get_variable_from_expr(expr, NULL);
62 sm_msg("error: '%s' was already used up by dev_queue_xmit()", name);
63 free_string(name);
66 static void match_kfree_skb(const char *fn, struct expression *expr, void *param)
68 struct expression *arg;
70 arg = get_argument_from_call_expr(expr->args, 0);
71 if (!arg)
72 return;
73 delete_state_expr(my_id, arg);
76 static void match_xmit(const char *fn, struct expression *expr, void *param)
78 struct expression *arg;
80 arg = get_argument_from_call_expr(expr->args, (int)param);
81 if (!arg)
82 return;
83 set_state_expr(my_id, arg, &do_not_use);
84 add_modification_hook_expr(arg, ok_to_use, NULL);
85 if (!match_symbol_active++) {
86 add_hook(&match_symbol, SYM_HOOK);
87 add_function_hook("kfree_skb", &match_kfree_skb, NULL);
91 static void register_funcs_from_file(void)
93 struct token *token;
94 const char *func;
95 int arg;
97 token = get_tokens_file("kernel.dev_queue_xmit");
98 if (!token)
99 return;
100 if (token_type(token) != TOKEN_STREAMBEGIN)
101 return;
102 token = token->next;
103 while (token_type(token) != TOKEN_STREAMEND) {
104 if (token_type(token) != TOKEN_IDENT)
105 return;
106 func = show_ident(token->ident);
107 token = token->next;
108 if (token_type(token) != TOKEN_NUMBER)
109 return;
110 arg = atoi(token->number);
111 add_function_hook(func, &match_xmit, INT_PTR(arg));
112 token = token->next;
114 clear_token_alloc();
117 void check_dev_queue_xmit(int id)
119 if (option_project != PROJ_KERNEL || !option_rare)
120 return;
121 my_id = id;
122 register_funcs_from_file();