1 /*
   2  * Copyright (C) 2010 Dan Carpenter.
   3  *
   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.
   8  *
   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.
  13  *
  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
  16  */
  17 
  18 #include "smatch.h"
  19 #include "smatch_slist.h"
  20 
  21 STATE(no_unmap);
  22 
  23 extern int check_assigned_expr_id;
  24 static int my_id;
  25 
  26 static void check_assignment(void *data)
  27 {
  28         struct expression *expr = (struct expression *)data;
  29         char *fn;
  30 
  31         if (!expr)
  32                 return;
  33         if (expr->type != EXPR_CALL)
  34                 return;
  35         fn = expr_to_var(expr->fn);
  36         if (!fn)
  37                 return;
  38         if (!strcmp(fn, "kmap"))
  39                 sm_warning("passing the wrong variable to kunmap()");
  40         free_string(fn);
  41 }
  42 
  43 static void match_kmap_atomic(const char *fn, struct expression *expr, void *data)
  44 {
  45         struct expression *arg;
  46 
  47         arg = get_argument_from_call_expr(expr->args, 0);
  48         set_state_expr(my_id, arg, &no_unmap);
  49 }
  50 
  51 static void match_kunmap_atomic(const char *fn, struct expression *expr, void *data)
  52 {
  53         struct expression *arg;
  54         struct sm_state *sm;
  55 
  56         arg = get_argument_from_call_expr(expr->args, 0);
  57         sm = get_sm_state_expr(my_id, arg);
  58         if (!sm)
  59                 return;
  60         if (slist_has_state(sm->possible, &no_unmap))
  61                 sm_warning("passing the wrong variable to kmap_atomic()");
  62 }
  63 
  64 static void match_kunmap(const char *fn, struct expression *expr, void *data)
  65 {
  66         struct expression *arg;
  67         struct sm_state *sm;
  68         struct sm_state *tmp;
  69 
  70         arg = get_argument_from_call_expr(expr->args, 0);
  71         sm = get_sm_state_expr(check_assigned_expr_id, arg);
  72         if (!sm)
  73                 return;
  74         FOR_EACH_PTR(sm->possible, tmp) {
  75                 check_assignment(tmp->state->data);
  76         } END_FOR_EACH_PTR(tmp);
  77 }
  78 
  79 void check_kunmap(int id)
  80 {
  81         my_id = id;
  82         if (option_project != PROJ_KERNEL)
  83                 return;
  84         add_function_hook("kunmap", &match_kunmap, NULL);
  85         add_function_hook("kmap_atomic", &match_kmap_atomic, NULL);
  86         add_function_hook("kunmap_atomic", &match_kunmap_atomic, NULL);
  87 }