Print this page
12724 update smatch to 0.6.1-rc1-il-5

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/smatch_fresh_alloc.c
          +++ new/usr/src/tools/smatch/src/smatch_fresh_alloc.c
↓ open down ↓ 15 lines elided ↑ open up ↑
  16   16   */
  17   17  
  18   18  /*
  19   19   * There are a bunch of allocation functions where we allocate some memory,
  20   20   * set up some struct members and then return the allocated memory.  One
  21   21   * nice thing about this is that we just one pointer to the allocated memory
  22   22   * so what we can do is we can generate a mtag alias for it in the caller.
  23   23   */
  24   24  
  25   25  #include "smatch.h"
       26 +#include "smatch_extra.h"
  26   27  #include "smatch_slist.h"
  27   28  
  28   29  static int my_id;
  29   30  
  30   31  STATE(fresh);
  31   32  
  32   33  struct alloc_info *alloc_funcs;
  33   34  
  34   35  struct alloc_info kernel_allocation_funcs[] = {
  35   36          {"kmalloc", 0},
↓ open down ↓ 22 lines elided ↑ open up ↑
  58   59  };
  59   60  
  60   61  struct alloc_info general_allocation_funcs[] = {
  61   62          {"malloc", 0},
  62   63          {"calloc", 0, 1},
  63   64          {"memdup", 1},
  64   65          {"realloc", 1},
  65   66          {},
  66   67  };
  67   68  
       69 +static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
       70 +{
       71 +        struct smatch_state *state;
       72 +        sval_t sval;
       73 +
       74 +        state = get_state(SMATCH_EXTRA, cur->name, cur->sym);
       75 +        if (estate_get_single_value(state, &sval) && sval.value == 0)
       76 +                set_state(my_id, cur->name, cur->sym, &undefined);
       77 +}
       78 +
  68   79  static int fresh_callback(void *fresh, int argc, char **argv, char **azColName)
  69   80  {
  70   81          *(int *)fresh = 1;
  71   82          return 0;
  72   83  }
  73   84  
  74   85  static int fresh_from_db(struct expression *call)
  75   86  {
  76   87          int fresh = 0;
  77   88  
↓ open down ↓ 65 lines elided ↑ open up ↑
 143  154  
 144  155  static void match_call(struct expression *expr)
 145  156  {
 146  157          struct expression *arg;
 147  158  
 148  159          FOR_EACH_PTR(expr->args, arg) {
 149  160                  set_unfresh(arg);
 150  161          } END_FOR_EACH_PTR(arg);
 151  162  }
 152  163  
      164 +static struct expression *handled;
 153  165  static void set_fresh(struct expression *expr)
 154  166  {
      167 +        struct range_list *rl;
      168 +
 155  169          expr = strip_expr(expr);
 156  170          if (expr->type != EXPR_SYMBOL)
 157  171                  return;
      172 +        if (expr == handled)
      173 +                return;
      174 +
      175 +        get_absolute_rl(expr, &rl);
      176 +        rl = rl_intersection(rl, valid_ptr_rl);
      177 +        if (!rl)
      178 +                return;
 158  179          set_state_expr(my_id, expr, &fresh);
      180 +        handled = expr;
 159  181  }
 160  182  
 161  183  static void returns_fresh_alloc(struct expression *expr, int param, char *key, char *value)
 162  184  {
 163  185          if (param != -1 || !key || strcmp(key, "$") != 0)
 164  186                  return;
 165  187          if (expr->type != EXPR_ASSIGNMENT)
 166  188                  return;
 167  189  
 168  190          set_fresh(expr->left);
↓ open down ↓ 16 lines elided ↑ open up ↑
 185  207                  alloc_funcs = general_allocation_funcs;
 186  208  
 187  209          i = -1;
 188  210          while (alloc_funcs[++i].fn)
 189  211                  add_function_assign_hook(alloc_funcs[i].fn, &match_alloc, 0);
 190  212  
 191  213          add_split_return_callback(&record_alloc_func);
 192  214          select_return_states_hook(FRESH_ALLOC, &returns_fresh_alloc);
 193  215          add_hook(&match_assign, ASSIGNMENT_HOOK);
 194  216          add_hook(&match_call, FUNCTION_CALL_HOOK);
      217 +
      218 +        add_pre_merge_hook(my_id, &pre_merge_hook);
 195  219  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX