Print this page
smatch: check libld_* allocation functions


  29  *
  30  * The work around is that for now what this check only
  31  * checks simple expressions and doesn't check whether
  32  * foo->bar is leaked.
  33  * 
  34  */
  35 
  36 #include <fcntl.h>
  37 #include <unistd.h>
  38 #include "parse.h"
  39 #include "smatch.h"
  40 #include "smatch_slist.h"
  41 
  42 static int my_id;
  43 
  44 STATE(allocated);
  45 STATE(ok);
  46 
  47 static void set_parent(struct expression *expr, struct smatch_state *state);
  48 
  49 static const char *allocation_funcs[] = {
  50         "malloc",
  51         "kmalloc",
  52         "kzalloc",
  53         "kmemdup",
  54 };
  55 
  56 static char *alloc_parent_str(struct symbol *sym)
  57 {
  58         static char buf[256];
  59 
  60         if (!sym || !sym->ident)
  61                 return NULL;
  62 
  63         snprintf(buf, 255, "%s", sym->ident->name);
  64         buf[255] = '\0';
  65         return alloc_string(buf);
  66 }
  67 
  68 static char *get_parent_from_expr(struct expression *expr, struct symbol **sym)
  69 {
  70         char *name;
  71 
  72         expr = strip_expr(expr);
  73 
  74         name = expr_to_str_sym(expr, sym);
  75         free_string(name);


 236         } END_FOR_EACH_SM(tmp);
 237 }
 238 
 239 static void match_return(struct expression *ret_value)
 240 {
 241         if (__inline_fn)
 242                 return;
 243         set_parent(ret_value, &ok);
 244         check_for_allocated();
 245 }
 246 
 247 static void match_end_func(struct symbol *sym)
 248 {
 249         if (__inline_fn)
 250                 return;
 251         check_for_allocated();
 252 }
 253 
 254 void check_leaks(int id)
 255 {
 256         int i;
 257 
 258         my_id = id;
 259 
 260         for (i = 0; i < ARRAY_SIZE(allocation_funcs); i++)
 261                 add_function_assign_hook(allocation_funcs[i], &match_alloc, NULL);












 262 
 263         add_hook(&match_condition, CONDITION_HOOK);
 264 
 265         add_hook(&match_function_call, FUNCTION_CALL_HOOK);
 266         add_hook(&match_assign, ASSIGNMENT_HOOK);
 267 
 268         add_hook(&match_return, RETURN_HOOK);
 269         add_hook(&match_end_func, END_FUNC_HOOK);
 270 }


  29  *
  30  * The work around is that for now what this check only
  31  * checks simple expressions and doesn't check whether
  32  * foo->bar is leaked.
  33  * 
  34  */
  35 
  36 #include <fcntl.h>
  37 #include <unistd.h>
  38 #include "parse.h"
  39 #include "smatch.h"
  40 #include "smatch_slist.h"
  41 
  42 static int my_id;
  43 
  44 STATE(allocated);
  45 STATE(ok);
  46 
  47 static void set_parent(struct expression *expr, struct smatch_state *state);
  48 







  49 static char *alloc_parent_str(struct symbol *sym)
  50 {
  51         static char buf[256];
  52 
  53         if (!sym || !sym->ident)
  54                 return NULL;
  55 
  56         snprintf(buf, 255, "%s", sym->ident->name);
  57         buf[255] = '\0';
  58         return alloc_string(buf);
  59 }
  60 
  61 static char *get_parent_from_expr(struct expression *expr, struct symbol **sym)
  62 {
  63         char *name;
  64 
  65         expr = strip_expr(expr);
  66 
  67         name = expr_to_str_sym(expr, sym);
  68         free_string(name);


 229         } END_FOR_EACH_SM(tmp);
 230 }
 231 
 232 static void match_return(struct expression *ret_value)
 233 {
 234         if (__inline_fn)
 235                 return;
 236         set_parent(ret_value, &ok);
 237         check_for_allocated();
 238 }
 239 
 240 static void match_end_func(struct symbol *sym)
 241 {
 242         if (__inline_fn)
 243                 return;
 244         check_for_allocated();
 245 }
 246 
 247 void check_leaks(int id)
 248 {


 249         my_id = id;
 250 
 251         switch (option_project) {
 252         case PROJ_KERNEL:
 253                 add_function_assign_hook("kmalloc", &match_alloc, NULL);
 254                 add_function_assign_hook("kzalloc", &match_alloc, NULL);
 255                 add_function_assign_hook("kmemdup", &match_alloc, NULL);
 256                 break;
 257         case PROJ_ILLUMOS_USER:
 258                 add_function_assign_hook("libld_malloc", &match_alloc, NULL);
 259                 add_function_assign_hook("libld_calloc", &match_alloc, NULL);
 260                 /* FALLTHROUGH */
 261         default:
 262                 add_function_assign_hook("malloc", &match_alloc, NULL);
 263                 add_function_assign_hook("calloc", &match_alloc, NULL);
 264         }
 265         
 266         add_hook(&match_condition, CONDITION_HOOK);
 267 
 268         add_hook(&match_function_call, FUNCTION_CALL_HOOK);
 269         add_hook(&match_assign, ASSIGNMENT_HOOK);
 270 
 271         add_hook(&match_return, RETURN_HOOK);
 272         add_hook(&match_end_func, END_FUNC_HOOK);
 273 }