extra: handle MOD_ASSIGN better
[smatch.git] / check_kernel.c
blob96886f2c338998cc01e3a10dd734a986926cfcf9
1 /*
2 * sparse/check_kernel.c
4 * Copyright (C) 2010 Dan Carpenter.
6 * Licensed under the Open Software License version 1.1
8 */
11 * This is kernel specific stuff for smatch_extra.
14 #include "smatch.h"
15 #include "smatch_extra.h"
17 static int implied_copy_return(struct expression *call, void *unused, struct range_list **rl)
19 struct expression *arg;
20 sval_t max;
22 arg = get_argument_from_call_expr(call->args, 2);
23 get_absolute_max(arg, &max);
24 *rl = alloc_rl(ll_to_sval(0), max);
25 return 1;
28 static void match_param_nonnull(const char *fn, struct expression *call_expr,
29 struct expression *assign_expr, void *_param)
31 int param = PTR_INT(_param);
32 struct expression *arg;
33 struct smatch_state *pre_state;
34 struct smatch_state *true_state;
36 arg = get_argument_from_call_expr(call_expr->args, param);
37 pre_state = get_state_expr(SMATCH_EXTRA, arg);
38 true_state = estate_filter_sval(pre_state, ll_to_sval(0));
39 set_extra_expr_nomod(arg, true_state);
42 static void match_container_of(const char *fn, struct expression *expr, void *unused)
44 set_extra_expr_mod(expr->left, alloc_estate_range(valid_ptr_min_sval, valid_ptr_max_sval));
47 void check_kernel(int id)
49 if (option_project != PROJ_KERNEL)
50 return;
52 return_implies_state("tomoyo_memory_ok", 1, 1, &match_param_nonnull, (void *)0);
53 add_macro_assign_hook_extra("container_of", &match_container_of, NULL);
55 add_implied_return_hook("copy_to_user", &implied_copy_return, NULL);
56 add_implied_return_hook("__copy_to_user", &implied_copy_return, NULL);
57 add_implied_return_hook("copy_from_user", &implied_copy_return, NULL);
58 add_implied_return_hook("__copy_fom_user", &implied_copy_return, NULL);
59 add_implied_return_hook("clear_user", &implied_copy_return, NULL);