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_buf_size.c
          +++ new/usr/src/tools/smatch/src/smatch_buf_size.c
↓ open down ↓ 261 lines elided ↑ open up ↑
 262  262          rl = size_from_db_symbol(expr);
 263  263          if (rl)
 264  264                  return rl;
 265  265          return size_from_db_type(expr);
 266  266  }
 267  267  
 268  268  static void db_returns_buf_size(struct expression *expr, int param, char *unused, char *math)
 269  269  {
 270  270          struct expression *call;
 271  271          struct range_list *rl;
      272 +        sval_t sval;
 272  273  
 273  274          if (expr->type != EXPR_ASSIGNMENT)
 274  275                  return;
 275  276          call = strip_expr(expr->right);
 276  277  
 277  278          call_results_to_rl(call, &int_ctype, math, &rl);
 278  279          rl = cast_rl(&int_ctype, rl);
      280 +        if (rl_to_sval(rl, &sval) && sval.value == 0)
      281 +                return;
 279  282          set_state_expr(my_size_id, expr->left, alloc_estate_rl(rl));
 280  283  }
 281  284  
 282  285  static int get_real_array_size_from_type(struct symbol *type)
 283  286  {
 284  287          sval_t sval;
 285  288  
 286  289          if (!type)
 287  290                  return 0;
 288  291          if (!type || type->type != SYM_ARRAY)
↓ open down ↓ 155 lines elided ↑ open up ↑
 444  447          base_sym = get_real_base_type(base_sym);
 445  448          if (!base_sym || base_sym->type != SYM_STRUCT)
 446  449                  return 0;
 447  450  
 448  451          if (!is_last_member_of_struct(base_sym, expr->member))
 449  452                  return 0;
 450  453          if (!last_member_is_resizable(base_sym))
 451  454                  return 0;
 452  455  
 453  456          state = get_state(my_size_id, sym->ident->name, sym);
 454      -        if (!estate_to_size(state))
      457 +        if (!estate_to_size(state) || estate_to_size(state) == -1)
 455  458                  return 0;
 456  459  
 457  460          return estate_to_size(state) - type_bytes(base_sym) + type_bytes(get_type(expr));
 458  461  }
 459  462  
 460  463  static struct range_list *alloc_int_rl(int value)
 461  464  {
 462  465          sval_t sval = {
 463  466                  .type = &int_ctype,
 464  467                  {.value = value},
↓ open down ↓ 32 lines elided ↑ open up ↑
 497  500                  bytes = type_bytes(type);
 498  501                  if (bytes == 0)
 499  502                          return NULL;
 500  503                  offset.value *= bytes;
 501  504                  size = get_array_size_bytes(expr->left);
 502  505                  if (size <= 0)
 503  506                          return NULL;
 504  507                  return alloc_int_rl(size - offset.value);
 505  508          }
 506  509  
      510 +        /* buf = malloc(1024); */
      511 +        ret = get_stored_size_bytes(expr);
      512 +        if (ret)
      513 +                return ret;
      514 +
 507  515          size = get_stored_size_end_struct_bytes(expr);
 508  516          if (size)
 509  517                  return alloc_int_rl(size);
 510  518  
 511  519          /* buf[4] */
 512  520          size = get_real_array_size(expr);
 513  521          if (size)
 514  522                  return alloc_int_rl(elements_to_bytes(expr, size));
 515  523  
 516      -        /* buf = malloc(1024); */
 517      -        ret = get_stored_size_bytes(expr);
 518      -        if (ret)
 519      -                return ret;
 520      -
 521  524          /* char *foo = "BAR" */
 522  525          size = get_size_from_initializer(expr);
 523  526          if (size)
 524  527                  return alloc_int_rl(elements_to_bytes(expr, size));
 525  528  
 526  529          size = get_bytes_from_address(expr);
 527  530          if (size)
 528  531                  return alloc_int_rl(size);
 529  532  
 530  533          ret = size_from_db(expr);
↓ open down ↓ 98 lines elided ↑ open up ↑
 629  632          free_string(name);
 630  633  }
 631  634  
 632  635  static void store_alloc(struct expression *expr, struct range_list *rl)
 633  636  {
 634  637          struct symbol *type;
 635  638  
 636  639          rl = clone_rl(rl); // FIXME!!!
 637  640          if (!rl)
 638  641                  rl = size_to_rl(UNKNOWN_SIZE);
 639      -        set_state_expr(my_size_id, expr, alloc_estate_rl(rl));
 640  642  
      643 +        if (rl_min(rl).value != UNKNOWN_SIZE ||
      644 +            rl_max(rl).value != UNKNOWN_SIZE ||
      645 +            get_state_expr(my_size_id, expr))
      646 +                set_state_expr(my_size_id, expr, alloc_estate_rl(rl));
      647 +
 641  648          type = get_type(expr);
 642  649          if (!type)
 643  650                  return;
 644  651          if (type->type != SYM_PTR)
 645  652                  return;
 646  653          type = get_real_base_type(type);
 647  654          if (!type)
 648  655                  return;
 649  656          if (type == &void_ctype)
 650  657                  return;
 651  658          if (type->type != SYM_BASETYPE && type->type != SYM_PTR)
 652  659                  return;
 653  660  
 654  661          info_record_alloction(expr, rl);
 655  662  }
 656  663  
      664 +static bool is_array_base(struct expression *expr)
      665 +{
      666 +        struct symbol *type;
      667 +
      668 +        type = get_type(expr);
      669 +        if (type && type->type == SYM_ARRAY)
      670 +                return true;
      671 +        return false;
      672 +}
      673 +
 657  674  static void match_array_assignment(struct expression *expr)
 658  675  {
 659  676          struct expression *left;
 660  677          struct expression *right;
 661  678          char *left_member, *right_member;
 662  679          struct range_list *rl;
 663  680          sval_t sval;
 664  681  
 665  682          if (expr->op != '=')
 666  683                  return;
      684 +
 667  685          left = strip_expr(expr->left);
 668  686          right = strip_expr(expr->right);
 669  687          right = strip_ampersands(right);
 670  688  
 671  689          if (!is_pointer(left))
 672  690                  return;
      691 +        /* char buf[24] = "str"; */
      692 +        if (is_array_base(left))
      693 +                return;
 673  694          if (is_allocation_function(right))
 674  695                  return;
 675  696  
 676  697          left_member = get_member_name(left);
 677  698          right_member = get_member_name(right);
 678  699          if (left_member && right_member && strcmp(left_member, right_member) == 0) {
 679  700                  free_string(left_member);
 680  701                  free_string(right_member);
 681  702                  return;
 682  703          }
↓ open down ↓ 20 lines elided ↑ open up ↑
 703  724          struct expression *arg;
 704  725          struct range_list *rl;
 705  726  
 706  727          right = strip_expr(expr->right);
 707  728          arg = get_argument_from_call_expr(right->args, size_arg);
 708  729          get_absolute_rl(arg, &rl);
 709  730          rl = cast_rl(&int_ctype, rl);
 710  731          store_alloc(expr->left, rl);
 711  732  }
 712  733  
 713      -static void match_calloc(const char *fn, struct expression *expr, void *unused)
      734 +static void match_calloc(const char *fn, struct expression *expr, void *_param)
 714  735  {
 715  736          struct expression *right;
 716  737          struct expression *size, *nr, *mult;
 717  738          struct range_list *rl;
      739 +        int param = PTR_INT(_param);
 718  740  
 719  741          right = strip_expr(expr->right);
 720      -        nr = get_argument_from_call_expr(right->args, 0);
 721      -        size = get_argument_from_call_expr(right->args, 1);
      742 +        nr = get_argument_from_call_expr(right->args, param);
      743 +        size = get_argument_from_call_expr(right->args, param + 1);
 722  744          mult = binop_expression(nr, '*', size);
 723  745          if (get_implied_rl(mult, &rl))
 724  746                  store_alloc(expr->left, rl);
 725  747          else
 726  748                  store_alloc(expr->left, size_to_rl(UNKNOWN_SIZE));
 727  749  }
 728  750  
 729  751  static void match_page(const char *fn, struct expression *expr, void *_unused)
 730  752  {
 731  753          sval_t page_size = {
↓ open down ↓ 73 lines elided ↑ open up ↑
 805  827  
 806  828          i = -1;
 807  829          FOR_EACH_PTR(expr->args, arg) {
 808  830                  i++;
 809  831                  type = get_type(arg);
 810  832                  if (!type || (type->type != SYM_PTR && type->type != SYM_ARRAY))
 811  833                          continue;
 812  834                  rl = get_array_size_bytes_rl(arg);
 813  835                  if (!rl)
 814  836                          continue;
      837 +                if (rl_min(rl).value == UNKNOWN_SIZE &&
      838 +                    rl_max(rl).value == UNKNOWN_SIZE)
      839 +                        continue;
 815  840                  if (is_whole_rl(rl))
 816  841                          continue;
 817  842                  if (is_type_bytes(rl, arg))
 818  843                          continue;
 819  844                  sql_insert_caller_info(expr, BUF_SIZE, i, "$", show_rl(rl));
 820  845          } END_FOR_EACH_PTR(arg);
 821  846  }
 822  847  
 823  848  static void struct_member_callback(struct expression *call, int param, char *printed_name, struct sm_state *sm)
 824  849  {
↓ open down ↓ 77 lines elided ↑ open up ↑
 902  927          add_allocation_function("malloc", &match_alloc, 0);
 903  928          add_allocation_function("calloc", &match_calloc, 0);
 904  929          add_allocation_function("memdup", &match_alloc, 1);
 905  930          add_allocation_function("realloc", &match_alloc, 1);
 906  931          if (option_project == PROJ_KERNEL) {
 907  932                  add_allocation_function("kmalloc", &match_alloc, 0);
 908  933                  add_allocation_function("kmalloc_node", &match_alloc, 0);
 909  934                  add_allocation_function("kzalloc", &match_alloc, 0);
 910  935                  add_allocation_function("kzalloc_node", &match_alloc, 0);
 911  936                  add_allocation_function("vmalloc", &match_alloc, 0);
      937 +                add_allocation_function("vzalloc", &match_alloc, 0);
 912  938                  add_allocation_function("__vmalloc", &match_alloc, 0);
 913  939                  add_allocation_function("kvmalloc", &match_alloc, 0);
 914  940                  add_allocation_function("kcalloc", &match_calloc, 0);
 915  941                  add_allocation_function("kmalloc_array", &match_calloc, 0);
 916      -                add_allocation_function("drm_malloc_ab", &match_calloc, 0);
 917      -                add_allocation_function("drm_calloc_large", &match_calloc, 0);
      942 +                add_allocation_function("devm_kmalloc_array", &match_calloc, 1);
 918  943                  add_allocation_function("sock_kmalloc", &match_alloc, 1);
 919  944                  add_allocation_function("kmemdup", &match_alloc, 1);
 920  945                  add_allocation_function("kmemdup_user", &match_alloc, 1);
 921  946                  add_allocation_function("dma_alloc_attrs", &match_alloc, 1);
 922  947                  add_allocation_function("pci_alloc_consistent", &match_alloc, 1);
 923  948                  add_allocation_function("pci_alloc_coherent", &match_alloc, 1);
 924  949                  add_allocation_function("devm_kmalloc", &match_alloc, 1);
 925  950                  add_allocation_function("devm_kzalloc", &match_alloc, 1);
 926  951                  add_allocation_function("krealloc", &match_alloc, 1);
 927  952                  add_allocation_function("__alloc_bootmem", &match_alloc, 0);
↓ open down ↓ 32 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX