Print this page
smatch: check libld_* allocation functions

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/smatch_constraints_required.c
          +++ new/usr/src/tools/smatch/src/smatch_constraints_required.c
↓ open down ↓ 18 lines elided ↑ open up ↑
  19   19  #include "smatch_extra.h"
  20   20  
  21   21  static int my_id;
  22   22  
  23   23  struct allocator {
  24   24          const char *func;
  25   25          int param;
  26   26          int param2;
  27   27  };
  28   28  
       29 +static struct allocator illumos_user_allocator_table[] = {
       30 +        {"libld_malloc", 0},
       31 +        {"libld_realloc", 1},
       32 +};
       33 +
  29   34  static struct allocator generic_allocator_table[] = {
  30   35          {"malloc", 0},
  31   36          {"memdup", 1},
  32   37          {"realloc", 1},
  33   38  };
  34   39  
  35   40  static struct allocator kernel_allocator_table[] = {
  36   41          {"kmalloc", 0},
  37   42          {"kzalloc", 0},
  38   43          {"vmalloc", 0},
↓ open down ↓ 3 lines elided ↑ open up ↑
  42   47          {"kmemdup", 1},
  43   48          {"kmemdup_user", 1},
  44   49          {"dma_alloc_attrs", 1},
  45   50          {"pci_alloc_consistent", 1},
  46   51          {"pci_alloc_coherent", 1},
  47   52          {"devm_kmalloc", 1},
  48   53          {"devm_kzalloc", 1},
  49   54          {"krealloc", 1},
  50   55  };
  51   56  
  52      -static struct allocator calloc_table[] = {
       57 +static struct allocator illumos_user_calloc_table[] = {
       58 +        {"libld_calloc", 0, 1},
  53   59          {"calloc", 0, 1},
  54   60          {"kcalloc", 0, 1},
  55   61          {"kmalloc_array", 0, 1},
  56   62          {"devm_kcalloc", 1, 2},
  57   63  };
  58   64  
       65 +static struct allocator generic_calloc_table[] = {
       66 +        {"calloc", 0, 1},
       67 +};
       68 +
       69 +static struct allocator kernel_calloc_table[] = {
       70 +        {"kcalloc", 0, 1},
       71 +        {"kmalloc_array", 0, 1},
       72 +        {"devm_kcalloc", 1, 2},
       73 +};
       74 +
  59   75  static int bytes_per_element(struct expression *expr)
  60   76  {
  61   77          struct symbol *type;
  62   78  
  63   79          type = get_type(expr);
  64   80          if (!type)
  65   81                  return 0;
  66   82  
  67   83          if (type->type != SYM_PTR && type->type != SYM_ARRAY)
  68   84                  return 0;
↓ open down ↓ 248 lines elided ↑ open up ↑
 317  333                  return;
 318  334  
 319  335          for (i = 0; i < ARRAY_SIZE(generic_allocator_table); i++) {
 320  336                  if (strcmp(right->fn->symbol->ident->name,
 321  337                             generic_allocator_table[i].func) == 0) {
 322  338                          size_arg = generic_allocator_table[i].param;
 323  339                          goto found;
 324  340                  }
 325  341          }
 326  342  
      343 +        for (i = 0; i < ARRAY_SIZE(generic_calloc_table); i++) {
      344 +                if (strcmp(right->fn->symbol->ident->name,
      345 +                           generic_calloc_table[i].func) == 0) {
      346 +                        size_arg = generic_calloc_table[i].param;
      347 +                        size_arg2 = generic_calloc_table[i].param2;
      348 +                        goto found;
      349 +                }
      350 +        }       
      351 +
      352 +        if (option_project == PROJ_ILLUMOS_USER) {
      353 +                if (strcmp(right->fn->symbol->ident->name,
      354 +                           illumos_user_allocator_table[i].func) == 0) {
      355 +                        size_arg = illumos_user_allocator_table[i].param;
      356 +                        goto found;
      357 +                }
      358 +
      359 +                for (i = 0; i < ARRAY_SIZE(illumos_user_calloc_table); i++) {
      360 +                        if (strcmp(right->fn->symbol->ident->name,
      361 +                            illumos_user_calloc_table[i].func) == 0) {
      362 +                                size_arg = illumos_user_calloc_table[i].param;
      363 +                                size_arg2 = illumos_user_calloc_table[i].param2;
      364 +                                goto found;
      365 +                        }
      366 +                }
      367 +        }
      368 +
 327  369          if (option_project != PROJ_KERNEL)
 328  370                  return;
 329  371  
 330  372          for (i = 0; i < ARRAY_SIZE(kernel_allocator_table); i++) {
 331  373                  if (strcmp(right->fn->symbol->ident->name,
 332  374                             kernel_allocator_table[i].func) == 0) {
 333  375                          size_arg = kernel_allocator_table[i].param;
 334  376                          goto found;
 335  377                  }
 336  378          }
 337  379  
 338      -        for (i = 0; i < ARRAY_SIZE(calloc_table); i++) {
      380 +        for (i = 0; i < ARRAY_SIZE(kernel_calloc_table); i++) {
 339  381                  if (strcmp(right->fn->symbol->ident->name,
 340      -                           calloc_table[i].func) == 0) {
 341      -                        size_arg = calloc_table[i].param;
 342      -                        size_arg2 = calloc_table[i].param2;
      382 +                           kernel_calloc_table[i].func) == 0) {
      383 +                        size_arg = kernel_calloc_table[i].param;
      384 +                        size_arg2 = kernel_calloc_table[i].param2;
 343  385                          goto found;
 344  386                  }
 345  387          }
 346  388  
 347  389          return;
 348  390  
 349  391  found:
 350  392          arg = get_argument_from_call_expr(right->args, size_arg);
 351  393          match_alloc_helper(expr->left, arg, 1);
 352  394          if (size_arg2 == -1)
↓ open down ↓ 107 lines elided ↑ open up ↑
 460  502          add_hook(&match_assign_has_buf_comparison, ASSIGNMENT_HOOK);
 461  503  
 462  504          add_hook(&match_assign_ARRAY_SIZE, ASSIGNMENT_HOOK);
 463  505          add_hook(&match_assign_ARRAY_SIZE, GLOBAL_ASSIGNMENT_HOOK);
 464  506          add_hook(&match_assign_buf_comparison, ASSIGNMENT_HOOK);
 465  507          add_hook(&match_assign_constraint, ASSIGNMENT_HOOK);
 466  508  
 467  509          add_allocation_function("malloc", &match_alloc, 0);
 468  510          add_allocation_function("memdup", &match_alloc, 1);
 469  511          add_allocation_function("realloc", &match_alloc, 1);
 470      -        add_allocation_function("realloc", &match_calloc, 0);
      512 +        add_allocation_function("calloc", &match_calloc, 0);
      513 +        if (option_project == PROJ_ILLUMOS_USER) {
      514 +                add_allocation_function("libld_malloc", &match_alloc, 0);
      515 +                add_allocation_function("libld_realloc", &match_alloc, 1);
      516 +                add_allocation_function("libld_calloc", &match_calloc, 0);
      517 +        }
 471  518          if (option_project == PROJ_KERNEL) {
 472  519                  add_allocation_function("kmalloc", &match_alloc, 0);
 473  520                  add_allocation_function("kzalloc", &match_alloc, 0);
 474  521                  add_allocation_function("vmalloc", &match_alloc, 0);
 475  522                  add_allocation_function("__vmalloc", &match_alloc, 0);
 476  523                  add_allocation_function("vzalloc", &match_alloc, 0);
 477  524                  add_allocation_function("sock_kmalloc", &match_alloc, 1);
 478  525                  add_allocation_function("kmemdup", &match_alloc, 1);
 479  526                  add_allocation_function("kmemdup_user", &match_alloc, 1);
 480  527                  add_allocation_function("dma_alloc_attrs", &match_alloc, 1);
 481  528                  add_allocation_function("pci_alloc_consistent", &match_alloc, 1);
 482  529                  add_allocation_function("pci_alloc_coherent", &match_alloc, 1);
 483  530                  add_allocation_function("devm_kmalloc", &match_alloc, 1);
 484  531                  add_allocation_function("devm_kzalloc", &match_alloc, 1);
 485  532                  add_allocation_function("kcalloc", &match_calloc, 0);
 486  533                  add_allocation_function("kmalloc_array", &match_calloc, 0);
 487  534                  add_allocation_function("devm_kcalloc", &match_calloc, 1);
 488  535                  add_allocation_function("krealloc", &match_alloc, 1);
 489  536          }
 490  537  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX