Print this page
12166 resync smatch to 0.6.1-rc1-il-3

Split Close
Expand all
Collapse all
          --- old/usr/src/tools/smatch/src/smatch_comparison.c
          +++ new/usr/src/tools/smatch/src/smatch_comparison.c
↓ open down ↓ 24 lines elided ↑ open up ↑
  25   25   * Part of the trick of it is that if x or y is modified then we need to reset
  26   26   * the state.  We need to keep a list of all the states which depend on x and
  27   27   * all the states which depend on y.  The link_id code handles this.
  28   28   *
  29   29   */
  30   30  
  31   31  #include "smatch.h"
  32   32  #include "smatch_extra.h"
  33   33  #include "smatch_slist.h"
  34   34  
  35      -static int compare_id;
       35 +int comparison_id;
  36   36  static int link_id;
  37   37  static int inc_dec_id;
  38   38  static int inc_dec_link_id;
  39   39  
  40   40  static void add_comparison(struct expression *left, int comparison, struct expression *right);
  41   41  
  42   42  /* for handling for loops */
  43   43  STATE(start);
  44   44  STATE(incremented);
  45   45  
↓ open down ↓ 489 lines elided ↑ open up ↑
 535  535  
 536  536          return UNKNOWN_COMPARISON;
 537  537  }
 538  538  
 539  539  static void pre_merge_hook(struct sm_state *cur, struct sm_state *other)
 540  540  {
 541  541          struct compare_data *data = cur->state->data;
 542  542          int extra, new;
 543  543          static bool in_recurse;
 544  544  
      545 +        // FIXME.  No data is useless
 545  546          if (!data)
 546  547                  return;
 547  548  
 548  549          if (in_recurse)
 549  550                  return;
 550  551          in_recurse = true;
 551  552          extra = comparison_from_extra(data->left, data->right);
 552  553          in_recurse = false;
 553  554          if (!extra)
 554  555                  return;
 555  556          new = comparison_intersection(extra, data->comparison);
 556  557          if (new == data->comparison)
 557  558                  return;
 558  559  
 559      -        set_state(compare_id, cur->name, NULL,
      560 +        // FIXME: we should always preserve implications
      561 +        set_state(comparison_id, cur->name, NULL,
 560  562                    alloc_compare_state(data->left, data->left_var, data->left_vsl,
 561  563                                        new,
 562  564                                        data->right, data->right_var, data->right_vsl));
 563  565  }
 564  566  
 565  567  struct smatch_state *merge_compare_states(struct smatch_state *s1, struct smatch_state *s2)
 566  568  {
 567  569          struct compare_data *data = s1->data;
 568  570          int op;
 569  571  
↓ open down ↓ 47 lines elided ↑ open up ↑
 617  619                  if (!param->ident)
 618  620                          continue;
 619  621                  snprintf(orig, sizeof(orig), "%s orig", param->ident->name);
 620  622                  snprintf(state_name, sizeof(state_name), "%s vs %s", param->ident->name, orig);
 621  623                  add_var_sym(&left_vsl, param->ident->name, param);
 622  624                  add_var_sym(&right_vsl, orig, param);
 623  625                  state = alloc_compare_state(
 624  626                                  NULL, param->ident->name, left_vsl,
 625  627                                  SPECIAL_EQUAL,
 626  628                                  NULL, alloc_sname(orig), right_vsl);
 627      -                set_state(compare_id, state_name, NULL, state);
      629 +                set_state(comparison_id, state_name, NULL, state);
 628  630  
 629  631                  link = alloc_sname(state_name);
 630  632                  links = NULL;
 631  633                  insert_string(&links, link);
 632  634                  state = alloc_link_state(links);
 633  635                  set_state(link_id, param->ident->name, param, state);
 634  636          } END_FOR_EACH_PTR(param);
 635  637  }
 636  638  
 637  639  static struct smatch_state *merge_links(struct smatch_state *s1, struct smatch_state *s2)
↓ open down ↓ 30 lines elided ↑ open up ↑
 668  670          struct string_list *links;
 669  671          struct smatch_state *state, *new;
 670  672          struct compare_data *data;
 671  673          char *tmp;
 672  674          int flip;
 673  675          int op;
 674  676  
 675  677          links = sm->state->data;
 676  678  
 677  679          FOR_EACH_PTR(links, tmp) {
 678      -                state = get_state(compare_id, tmp, NULL);
      680 +                state = get_state(comparison_id, tmp, NULL);
 679  681                  if (!state)
 680  682                          continue;
 681  683                  data = state->data;
 682  684                  if (!data)
 683  685                          continue;
 684  686  
 685  687                  flip = 0;
 686  688                  if (strncmp(sm->name, tmp, strlen(sm->name)) != 0 ||
 687  689                      tmp[strlen(sm->name)] != ' ')
 688  690                          flip = 1;
↓ open down ↓ 5 lines elided ↑ open up ↑
 694  696                  case SPECIAL_GTE:
 695  697                  case SPECIAL_UNSIGNED_GTE:
 696  698                  case '>':
 697  699                  case SPECIAL_UNSIGNED_GT:
 698  700                          if (preserve)
 699  701                                  break;
 700  702                          new = alloc_compare_state(
 701  703                                          data->left, data->left_var, data->left_vsl,
 702  704                                          flip ? '<' : '>',
 703  705                                          data->right, data->right_var, data->right_vsl);
 704      -                        set_state(compare_id, tmp, NULL, new);
      706 +                        set_state(comparison_id, tmp, NULL, new);
 705  707                          break;
 706  708                  case '<':
 707  709                  case SPECIAL_UNSIGNED_LT:
 708  710                          new = alloc_compare_state(
 709  711                                          data->left, data->left_var, data->left_vsl,
 710  712                                          flip ? SPECIAL_GTE : SPECIAL_LTE,
 711  713                                          data->right, data->right_var, data->right_vsl);
 712      -                        set_state(compare_id, tmp, NULL, new);
      714 +                        set_state(comparison_id, tmp, NULL, new);
 713  715                          break;
 714  716                  default:
 715  717                          new = alloc_compare_state(
 716  718                                          data->left, data->left_var, data->left_vsl,
 717  719                                          UNKNOWN_COMPARISON,
 718  720                                          data->right, data->right_var, data->right_vsl);
 719      -                        set_state(compare_id, tmp, NULL, new);
      721 +                        set_state(comparison_id, tmp, NULL, new);
 720  722                  }
 721  723          } END_FOR_EACH_PTR(tmp);
 722  724  }
 723  725  
 724  726  static void match_dec(struct sm_state *sm, bool preserve)
 725  727  {
 726  728          struct string_list *links;
 727  729          struct smatch_state *state;
 728  730          char *tmp;
 729  731  
 730  732          links = sm->state->data;
 731  733  
 732  734          FOR_EACH_PTR(links, tmp) {
 733  735                  struct compare_data *data;
 734  736                  struct smatch_state *new;
 735  737  
 736      -                state = get_state(compare_id, tmp, NULL);
      738 +                state = get_state(comparison_id, tmp, NULL);
 737  739                  if (!state || !state->data)
 738  740                          continue;
 739  741  
 740  742                  data = state->data;
 741  743  
 742  744                  switch (state_to_comparison(state)) {
 743  745                  case SPECIAL_EQUAL:
 744  746                  case SPECIAL_LTE:
 745  747                  case SPECIAL_UNSIGNED_LTE:
 746  748                  case '<':
 747  749                  case SPECIAL_UNSIGNED_LT: {
 748  750                          if (preserve)
 749  751                                  break;
 750  752  
 751  753                          new = alloc_compare_state(
 752  754                                          data->left, data->left_var, data->left_vsl,
 753  755                                          '<',
 754  756                                          data->right, data->right_var, data->right_vsl);
 755      -                        set_state(compare_id, tmp, NULL, new);
      757 +                        set_state(comparison_id, tmp, NULL, new);
 756  758                          break;
 757  759                          }
 758  760                  default:
 759  761                          new = alloc_compare_state(
 760  762                                          data->left, data->left_var, data->left_vsl,
 761  763                                          UNKNOWN_COMPARISON,
 762  764                                          data->right, data->right_var, data->right_vsl);
 763      -                        set_state(compare_id, tmp, NULL, new);
      765 +                        set_state(comparison_id, tmp, NULL, new);
 764  766                  }
 765  767          } END_FOR_EACH_PTR(tmp);
 766  768  }
 767  769  
 768  770  static void reset_sm(struct sm_state *sm)
 769  771  {
 770  772          struct string_list *links;
 771  773          char *tmp;
 772  774  
 773  775          links = sm->state->data;
 774  776  
 775  777          FOR_EACH_PTR(links, tmp) {
 776  778                  struct smatch_state *old, *new;
 777  779  
 778      -                old = get_state(compare_id, tmp, NULL);
      780 +                old = get_state(comparison_id, tmp, NULL);
 779  781                  if (!old || !old->data) {
 780  782                          new = &undefined;
 781  783                  } else {
 782  784                          struct compare_data *data = old->data;
 783  785  
 784  786                          new = alloc_compare_state(
 785  787                                          data->left, data->left_var, data->left_vsl,
 786  788                                          UNKNOWN_COMPARISON,
 787  789                                          data->right, data->right_var, data->right_vsl);
 788  790                  }
 789      -                set_state(compare_id, tmp, NULL, new);
      791 +                set_state(comparison_id, tmp, NULL, new);
 790  792          } END_FOR_EACH_PTR(tmp);
 791  793          set_state(link_id, sm->name, sm->sym, &undefined);
 792  794  }
 793  795  
 794  796  static bool match_add_sub_assign(struct sm_state *sm, struct expression *expr)
 795  797  {
 796  798          struct range_list *rl;
 797  799          sval_t zero = { .type = &int_ctype };
 798  800  
 799  801          if (!expr || expr->type != EXPR_ASSIGNMENT)
↓ open down ↓ 181 lines elided ↑ open up ↑
 981  983  
 982  984          if (strcmp(left, right) > 0) {
 983  985                  const char *tmp = right;
 984  986  
 985  987                  flip = 1;
 986  988                  right = left;
 987  989                  left = tmp;
 988  990          }
 989  991  
 990  992          snprintf(state_name, sizeof(state_name), "%s vs %s", left, right);
 991      -        state = get_state_stree(pre_stree, compare_id, state_name, NULL);
      993 +        state = get_state_stree(pre_stree, comparison_id, state_name, NULL);
 992  994          if (!state || !state->data)
 993  995                  return 0;
 994  996          data = state->data;
 995  997          if (flip)
 996  998                  return flip_comparison(data->comparison);
 997  999          return data->comparison;
 998 1000  
 999 1001  }
1000 1002  
1001 1003  static int have_common_var_sym(struct var_sym_list *left_vsl, struct var_sym_list *right_vsl)
↓ open down ↓ 32 lines elided ↑ open up ↑
1034 1036          struct var_sym_list *right_vsl;
1035 1037          int orig_comparison;
1036 1038          int right_comparison;
1037 1039          int true_comparison;
1038 1040          int false_comparison;
1039 1041          char *tmp;
1040 1042          char state_name[256];
1041 1043          struct var_sym *vs;
1042 1044  
1043 1045          FOR_EACH_PTR(links, tmp) {
1044      -                state = get_state_stree(pre_stree, compare_id, tmp, NULL);
     1046 +                state = get_state_stree(pre_stree, comparison_id, tmp, NULL);
1045 1047                  if (!state || !state->data)
1046 1048                          continue;
1047 1049                  data = state->data;
1048 1050                  right_comparison = data->comparison;
1049 1051                  right_expr = data->right;
1050 1052                  right_var = data->right_var;
1051 1053                  right_vsl = data->right_vsl;
1052 1054                  if (strcmp(mid_var, right_var) == 0) {
1053 1055                          right_expr = data->left;
1054 1056                          right_var = data->left_var;
↓ open down ↓ 38 lines elided ↑ open up ↑
1093 1095                          true_state = NULL;
1094 1096                  if (false_comparison)
1095 1097                          false_state = alloc_compare_state(
1096 1098                                          left_expr, left_var, left_vsl,
1097 1099                                          false_comparison,
1098 1100                                          right_expr, right_var, right_vsl);
1099 1101                  else
1100 1102                          false_state = NULL;
1101 1103  
1102 1104                  snprintf(state_name, sizeof(state_name), "%s vs %s", left_var, right_var);
1103      -                set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
     1105 +                set_true_false_states(comparison_id, state_name, NULL, true_state, false_state);
1104 1106                  FOR_EACH_PTR(left_vsl, vs) {
1105 1107                          save_link_var_sym(vs->var, vs->sym, state_name);
1106 1108                  } END_FOR_EACH_PTR(vs);
1107 1109                  FOR_EACH_PTR(right_vsl, vs) {
1108 1110                          save_link_var_sym(vs->var, vs->sym, state_name);
1109 1111                  } END_FOR_EACH_PTR(vs);
1110 1112                  if (!vsl_to_sym(left_vsl))
1111 1113                          save_link_var_sym(left_var, NULL, state_name);
1112 1114                  if (!vsl_to_sym(right_vsl))
1113 1115                          save_link_var_sym(right_var, NULL, state_name);
↓ open down ↓ 78 lines elided ↑ open up ↑
1192 1194  
1193 1195          if (get_state_chunk(inc_dec_id, expr->left) != &incremented)
1194 1196                  return;
1195 1197  
1196 1198          data = false_state->data;
1197 1199          false_state = alloc_compare_state(
1198 1200                          data->left, data->left_var, data->left_vsl,
1199 1201                          SPECIAL_EQUAL,
1200 1202                          data->right, data->right_var, data->right_vsl);
1201 1203  
1202      -        set_true_false_states(compare_id, state_name, NULL, NULL, false_state);
     1204 +        // FIXME: This doesn't handle links correct so it doesn't set "param orig"
     1205 +        set_true_false_states(comparison_id, state_name, NULL, NULL, false_state);
1203 1206  }
1204 1207  
1205 1208  static int is_plus_one(struct expression *expr)
1206 1209  {
1207 1210          sval_t sval;
1208 1211  
1209 1212          if (expr->type != EXPR_BINOP || expr->op != '+')
1210 1213                  return 0;
1211 1214          if (!get_implied_value(expr->right, &sval) || sval.value != 1)
1212 1215                  return 0;
↓ open down ↓ 120 lines elided ↑ open up ↑
1333 1336                          right_expr, right, right_vsl);
1334 1337          false_state = alloc_compare_state(
1335 1338                          left_expr, left, left_vsl,
1336 1339                          false_op,
1337 1340                          right_expr, right, right_vsl);
1338 1341  
1339 1342          pre_stree = clone_stree(__get_cur_stree());
1340 1343          update_tf_data(pre_stree, left_expr, left, left_vsl, right_expr, right, right_vsl, op, false_op);
1341 1344          free_stree(&pre_stree);
1342 1345  
1343      -        set_true_false_states(compare_id, state_name, NULL, true_state, false_state);
     1346 +        set_true_false_states(comparison_id, state_name, NULL, true_state, false_state);
1344 1347          __compare_param_limit_hook(left_expr, right_expr, state_name, true_state, false_state);
1345 1348          save_link(left_expr, state_name);
1346 1349          save_link(right_expr, state_name);
1347 1350  
1348 1351          if (_false_state)
1349 1352                  *_false_state = false_state;
1350 1353          if (_state_name)
1351 1354                  *_state_name = state_name;
1352 1355  free:
1353 1356          free_string(left);
↓ open down ↓ 81 lines elided ↑ open up ↑
1435 1438                  right_name = tmp_name;
1436 1439                  right_vsl = tmp_vsl;
1437 1440                  comparison = flip_comparison(comparison);
1438 1441          }
1439 1442          snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1440 1443          state = alloc_compare_state(
1441 1444                          left_expr, left_name, left_vsl,
1442 1445                          comparison,
1443 1446                          right_expr, right_name, right_vsl);
1444 1447  
1445      -        set_state(compare_id, state_name, NULL, state);
     1448 +        set_state(comparison_id, state_name, NULL, state);
1446 1449  
1447 1450          FOR_EACH_PTR(left_vsl, vs) {
1448 1451                  save_link_var_sym(vs->var, vs->sym, state_name);
1449 1452          } END_FOR_EACH_PTR(vs);
1450 1453          FOR_EACH_PTR(right_vsl, vs) {
1451 1454                  save_link_var_sym(vs->var, vs->sym, state_name);
1452 1455          } END_FOR_EACH_PTR(vs);
1453 1456  }
1454 1457  
1455 1458  static void add_comparison(struct expression *left, int comparison, struct expression *right)
↓ open down ↓ 29 lines elided ↑ open up ↑
1485 1488                  right_sym = tmp_sym;
1486 1489                  right_vsl = tmp_vsl;
1487 1490                  comparison = flip_comparison(comparison);
1488 1491          }
1489 1492          snprintf(state_name, sizeof(state_name), "%s vs %s", left_name, right_name);
1490 1493          state = alloc_compare_state(
1491 1494                          left, left_name, left_vsl,
1492 1495                          comparison,
1493 1496                          right, right_name, right_vsl);
1494 1497  
1495      -        set_state(compare_id, state_name, NULL, state);
     1498 +        set_state(comparison_id, state_name, NULL, state);
1496 1499          save_link(left, state_name);
1497 1500          save_link(right, state_name);
1498 1501  
1499 1502  free:
1500 1503          free_string(left_name);
1501 1504          free_string(right_name);
1502 1505  }
1503 1506  
1504 1507  static void match_assign_add(struct expression *expr)
1505 1508  {
↓ open down ↓ 96 lines elided ↑ open up ↑
1602 1605          right_var = chunk_to_var_sym(right, &right_sym);
1603 1606          if (!right_var)
1604 1607                  goto done;
1605 1608  
1606 1609          state = get_state(link_id, right_var, right_sym);
1607 1610          if (!state)
1608 1611                  return;
1609 1612          links = state->data;
1610 1613  
1611 1614          FOR_EACH_PTR(links, tmp) {
1612      -                state = get_state(compare_id, tmp, NULL);
     1615 +                state = get_state(comparison_id, tmp, NULL);
1613 1616                  if (!state || !state->data)
1614 1617                          continue;
1615 1618                  data = state->data;
1616 1619                  comparison = data->comparison;
1617 1620                  expr = data->right;
1618 1621                  var = data->right_var;
1619 1622                  vsl = data->right_vsl;
1620 1623                  if (strcmp(var, right_var) == 0) {
1621 1624                          expr = data->left;
1622 1625                          var = data->left_var;
↓ open down ↓ 48 lines elided ↑ open up ↑
1671 1674  
1672 1675          if (strcmp(one, two) > 0) {
1673 1676                  const char *tmp = one;
1674 1677  
1675 1678                  one = two;
1676 1679                  two = tmp;
1677 1680                  invert = 1;
1678 1681          }
1679 1682  
1680 1683          snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1681      -        state = get_state(compare_id, buf, NULL);
     1684 +        state = get_state(comparison_id, buf, NULL);
1682 1685          if (state)
1683 1686                  ret = state_to_comparison(state);
1684 1687  
1685 1688          if (invert)
1686 1689                  ret = flip_comparison(ret);
1687 1690  
1688 1691          return ret;
1689 1692  }
1690 1693  
1691 1694  static int get_comparison_helper(struct expression *a, struct expression *b, bool use_extra)
↓ open down ↓ 85 lines elided ↑ open up ↑
1777 1780  
1778 1781          if (strcmp(one, two) > 0) {
1779 1782                  char *tmp = one;
1780 1783  
1781 1784                  one = two;
1782 1785                  two = tmp;
1783 1786                  comparison = flip_comparison(comparison);
1784 1787          }
1785 1788  
1786 1789          snprintf(buf, sizeof(buf), "%s vs %s", one, two);
1787      -        sm = get_sm_state(compare_id, buf, NULL);
     1790 +        sm = get_sm_state(comparison_id, buf, NULL);
1788 1791          if (!sm)
1789 1792                  goto free;
1790 1793  
1791 1794          FOR_EACH_PTR(sm->possible, sm) {
1792 1795                  if (!sm->state->data)
1793 1796                          continue;
1794 1797                  saved = ((struct compare_data *)sm->state->data)->comparison;
1795 1798                  if (saved == comparison)
1796 1799                          ret = 1;
1797 1800                  if (comparison == SPECIAL_EQUAL &&
↓ open down ↓ 20 lines elided ↑ open up ↑
1818 1821          struct state_list *ret = NULL;
1819 1822          struct sm_state *sm;
1820 1823          char *tmp;
1821 1824  
1822 1825          state = get_state_chunk(link_id, expr);
1823 1826          if (!state)
1824 1827                  return NULL;
1825 1828          links = state->data;
1826 1829  
1827 1830          FOR_EACH_PTR(links, tmp) {
1828      -                sm = get_sm_state(compare_id, tmp, NULL);
     1831 +                sm = get_sm_state(comparison_id, tmp, NULL);
1829 1832                  if (!sm)
1830 1833                          continue;
1831 1834                  // FIXME have to compare name with vsl
1832 1835                  add_ptr_list(&ret, sm);
1833 1836          } END_FOR_EACH_PTR(tmp);
1834 1837  
1835 1838          return ret;
1836 1839  }
1837 1840  
1838 1841  struct state_list *get_all_possible_equal_comparisons(struct expression *expr)
↓ open down ↓ 3 lines elided ↑ open up ↑
1842 1845          struct state_list *ret = NULL;
1843 1846          struct sm_state *sm;
1844 1847          char *tmp;
1845 1848  
1846 1849          state = get_state_chunk(link_id, expr);
1847 1850          if (!state)
1848 1851                  return NULL;
1849 1852          links = state->data;
1850 1853  
1851 1854          FOR_EACH_PTR(links, tmp) {
1852      -                sm = get_sm_state(compare_id, tmp, NULL);
     1855 +                sm = get_sm_state(comparison_id, tmp, NULL);
1853 1856                  if (!sm)
1854 1857                          continue;
1855 1858                  if (!strchr(sm->state->name, '='))
1856 1859                          continue;
1857 1860                  if (strcmp(sm->state->name, "!=") == 0)
1858 1861                          continue;
1859 1862                  add_ptr_list(&ret, sm);
1860 1863          } END_FOR_EACH_PTR(tmp);
1861 1864  
1862 1865          return ret;
↓ open down ↓ 9 lines elided ↑ open up ↑
1872 1875          char *link;
1873 1876  
1874 1877          return NULL;
1875 1878  
1876 1879          state = get_state_chunk(link_id, expr);
1877 1880          if (!state)
1878 1881                  return NULL;
1879 1882          links = state->data;
1880 1883  
1881 1884          FOR_EACH_PTR(links, link) {
1882      -                sm = get_sm_state(compare_id, link, NULL);
     1885 +                sm = get_sm_state(comparison_id, link, NULL);
1883 1886                  if (!sm)
1884 1887                          continue;
1885 1888                  FOR_EACH_PTR(sm->possible, possible) {
1886 1889                          if (strcmp(possible->state->name, "!=") != 0)
1887 1890                                  continue;
1888 1891                          add_ptr_list(&ret, sm);
1889 1892                          break;
1890 1893                  } END_FOR_EACH_PTR(link);
1891 1894          } END_FOR_EACH_PTR(link);
1892 1895  
↓ open down ↓ 24 lines elided ↑ open up ↑
1917 1920          right_var = chunk_to_var_sym(right, &right_sym);
1918 1921          if (!right_var)
1919 1922                  goto done;
1920 1923  
1921 1924          state = get_state(link_id, right_var, right_sym);
1922 1925          if (!state)
1923 1926                  return;
1924 1927          links = state->data;
1925 1928  
1926 1929          FOR_EACH_PTR(links, tmp) {
1927      -                state = get_state(compare_id, tmp, NULL);
     1930 +                state = get_state(comparison_id, tmp, NULL);
1928 1931                  if (!state || !state->data)
1929 1932                          continue;
1930 1933                  data = state->data;
1931 1934                  comparison = data->comparison;
1932 1935                  expr = data->right;
1933 1936                  var = data->right_var;
1934 1937                  vsl = data->right_vsl;
1935 1938                  if (strcmp(var, right_var) == 0) {
1936 1939                          expr = data->left;
1937 1940                          var = data->left_var;
↓ open down ↓ 228 lines elided ↑ open up ↑
2166 2169                          continue;
2167 2170  
2168 2171                  links = state->data;
2169 2172                  FOR_EACH_PTR(links, link) {
2170 2173                          struct var_sym_list *right_vsl;
2171 2174                          struct var_sym *right_vs;
2172 2175  
2173 2176  
2174 2177                          if (strstr(link, " orig"))
2175 2178                                  continue;
2176      -                        sm = get_sm_state(compare_id, link, NULL);
     2179 +                        sm = get_sm_state(comparison_id, link, NULL);
2177 2180                          if (!sm)
2178 2181                                  continue;
2179 2182                          data = sm->state->data;
2180 2183                          if (!data ||
2181 2184                              data->comparison == UNKNOWN_COMPARISON ||
2182 2185                              data->comparison == IMPOSSIBLE_COMPARISON)
2183 2186                                  continue;
2184 2187                          arg_name = expr_to_var(arg);
2185 2188                          if (!arg_name)
2186 2189                                  continue;
↓ open down ↓ 34 lines elided ↑ open up ↑
2221 2224          struct compare_data *data;
2222 2225          struct var_sym *left, *right;
2223 2226          static char info_buf[256];
2224 2227          const char *right_name;
2225 2228  
2226 2229          if (strstr(printed_name, " orig"))
2227 2230                  return;
2228 2231  
2229 2232          links = link_sm->state->data;
2230 2233          FOR_EACH_PTR(links, link) {
2231      -                compare_sm = get_sm_state(compare_id, link, NULL);
     2234 +                compare_sm = get_sm_state(comparison_id, link, NULL);
2232 2235                  if (!compare_sm)
2233 2236                          continue;
2234 2237                  data = compare_sm->state->data;
2235 2238                  if (!data || !data->comparison)
2236 2239                          continue;
2237 2240  
2238 2241                  if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2239 2242                      ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2240 2243                          continue;
2241 2244                  left = first_ptr_list((struct ptr_list *)data->left_vsl);
↓ open down ↓ 72 lines elided ↑ open up ↑
2314 2317          char info_buf[258];
2315 2318          const char *tmp_name;
2316 2319  
2317 2320          print_return_value_comparison(return_id, return_ranges, expr);
2318 2321  
2319 2322          FOR_EACH_MY_SM(link_id, __get_cur_stree(), tmp) {
2320 2323                  if (get_param_num_from_sym(tmp->sym) < 0)
2321 2324                          continue;
2322 2325                  links = tmp->state->data;
2323 2326                  FOR_EACH_PTR(links, link) {
2324      -                        sm = get_sm_state(compare_id, link, NULL);
     2327 +                        sm = get_sm_state(comparison_id, link, NULL);
2325 2328                          if (!sm)
2326 2329                                  continue;
2327 2330                          data = sm->state->data;
2328 2331                          if (!data ||
2329 2332                              data->comparison == UNKNOWN_COMPARISON ||
2330 2333                              data->comparison == IMPOSSIBLE_COMPARISON)
2331 2334                                  continue;
2332 2335                          if (ptr_list_size((struct ptr_list *)data->left_vsl) != 1 ||
2333 2336                              ptr_list_size((struct ptr_list *)data->right_vsl) != 1)
2334 2337                                  continue;
↓ open down ↓ 197 lines elided ↑ open up ↑
2532 2535          right_arg = get_argument_from_call_expr(expr->args, right_param);
2533 2536          if (!right_arg)
2534 2537                  return 0;
2535 2538  
2536 2539          left_name = get_variable_from_key(left_arg, left_key, &left_sym);
2537 2540          right_name = get_variable_from_key(right_arg, right_key, &right_sym);
2538 2541          if (!left_name || !right_name)
2539 2542                  goto free;
2540 2543  
2541 2544          snprintf(buf, sizeof(buf), "%s vs %s", left_name, right_name);
2542      -        state = get_state(compare_id, buf, NULL);
     2545 +        state = get_state(comparison_id, buf, NULL);
2543 2546          if (!state)
2544 2547                  goto free;
2545 2548          state_op = state_to_comparison(state);
2546 2549          if (!state_op)
2547 2550                  goto free;
2548 2551  
2549 2552          if (!comparison_intersection(remove_unsigned_from_comparison(state_op), op))
2550 2553                  ret = 1;
2551 2554  free:
2552 2555          free_string(left_name);
↓ open down ↓ 13 lines elided ↑ open up ↑
2566 2569          if (!link_state) {
2567 2570                  if (expr->type == EXPR_BINOP &&
2568 2571                      (impossibly_high_comparison(expr->left) ||
2569 2572                       impossibly_high_comparison(expr->right)))
2570 2573                          return 1;
2571 2574                  return 0;
2572 2575          }
2573 2576  
2574 2577          links = link_state->data;
2575 2578          FOR_EACH_PTR(links, link) {
2576      -                sm = get_sm_state(compare_id, link, NULL);
     2579 +                sm = get_sm_state(comparison_id, link, NULL);
2577 2580                  if (!sm)
2578 2581                          continue;
2579 2582                  data = sm->state->data;
2580 2583                  if (!data)
2581 2584                          continue;
2582 2585                  if (!possibly_true(data->left, data->comparison, data->right))
2583 2586                          return 1;
2584 2587          } END_FOR_EACH_PTR(link);
2585 2588  
2586 2589          return 0;
↓ open down ↓ 1 lines elided ↑ open up ↑
2588 2591  
2589 2592  static void free_data(struct symbol *sym)
2590 2593  {
2591 2594          if (__inline_fn)
2592 2595                  return;
2593 2596          clear_compare_data_alloc();
2594 2597  }
2595 2598  
2596 2599  void register_comparison(int id)
2597 2600  {
2598      -        compare_id = id;
2599      -        set_dynamic_states(compare_id);
     2601 +        comparison_id = id;
     2602 +        set_dynamic_states(comparison_id);
2600 2603          add_hook(&save_start_states, AFTER_DEF_HOOK);
2601      -        add_unmatched_state_hook(compare_id, unmatched_comparison);
2602      -        add_pre_merge_hook(compare_id, &pre_merge_hook);
2603      -        add_merge_hook(compare_id, &merge_compare_states);
     2604 +        add_unmatched_state_hook(comparison_id, unmatched_comparison);
     2605 +        add_pre_merge_hook(comparison_id, &pre_merge_hook);
     2606 +        add_merge_hook(comparison_id, &merge_compare_states);
2604 2607          add_hook(&free_data, AFTER_FUNC_HOOK);
2605 2608          add_hook(&match_call_info, FUNCTION_CALL_HOOK);
2606 2609          add_split_return_callback(&print_return_comparison);
2607 2610  
2608 2611          select_return_states_hook(PARAM_COMPARE, &db_return_comparison);
2609 2612          add_hook(&match_preop, OP_HOOK);
2610 2613  }
2611 2614  
2612 2615  void register_comparison_late(int id)
2613 2616  {
↓ open down ↓ 18 lines elided ↑ open up ↑
2632 2635          add_modification_hook_late(inc_dec_id, &iter_modify);
2633 2636  }
2634 2637  
2635 2638  void register_comparison_inc_dec_links(int id)
2636 2639  {
2637 2640          inc_dec_link_id = id;
2638 2641          set_dynamic_states(inc_dec_link_id);
2639 2642          set_up_link_functions(inc_dec_id, inc_dec_link_id);
2640 2643  }
2641 2644  
     2645 +static struct sm_state *clone_partial_sm(struct sm_state *sm, int comparison)
     2646 +{
     2647 +        struct compare_data *data;
     2648 +        struct sm_state *clone;
     2649 +        struct stree *stree;
     2650 +
     2651 +        data = sm->state->data;
     2652 +
     2653 +        clone = clone_sm(sm);
     2654 +        clone->state = alloc_compare_state(data->left, data->left_var, data->left_vsl,
     2655 +                                           comparison,
     2656 +                                           data->right, data->right_var, data->right_vsl);
     2657 +        free_slist(&clone->possible);
     2658 +        add_possible_sm(clone, clone);
     2659 +
     2660 +        stree = clone_stree(sm->pool);
     2661 +        overwrite_sm_state_stree(&stree, clone);
     2662 +        clone->pool = stree;
     2663 +
     2664 +        return clone;
     2665 +}
     2666 +
     2667 +static void create_fake_history(struct sm_state *sm, int op,
     2668 +                               struct state_list **true_stack,
     2669 +                               struct state_list **false_stack)
     2670 +{
     2671 +        struct sm_state *true_sm, *false_sm;
     2672 +        struct compare_data *data;
     2673 +        int true_comparison;
     2674 +        int false_comparison;
     2675 +
     2676 +        data = sm->state->data;
     2677 +
     2678 +        if (is_merged(sm) || sm->left || sm->right)
     2679 +                return;
     2680 +
     2681 +        true_comparison = comparison_intersection(data->comparison, op);
     2682 +        false_comparison = comparison_intersection(data->comparison, negate_comparison(op));
     2683 +
     2684 +        true_sm = clone_partial_sm(sm, true_comparison);
     2685 +        false_sm = clone_partial_sm(sm, false_comparison);
     2686 +
     2687 +        sm->merged = 1;
     2688 +        sm->left = true_sm;
     2689 +        sm->right = false_sm;
     2690 +
     2691 +        add_ptr_list(true_stack, true_sm);
     2692 +        add_ptr_list(false_stack, false_sm);
     2693 +}
     2694 +
2642 2695  static void filter_by_sm(struct sm_state *sm, int op,
2643 2696                         struct state_list **true_stack,
2644      -                       struct state_list **false_stack)
     2697 +                       struct state_list **false_stack,
     2698 +                       bool *useful)
2645 2699  {
2646 2700          struct compare_data *data;
2647 2701          int is_true = 0;
2648 2702          int is_false = 0;
2649 2703  
2650 2704          if (!sm)
2651 2705                  return;
2652 2706          data = sm->state->data;
2653      -        if (!data || data->comparison == UNKNOWN_COMPARISON)
     2707 +        if (!data)
2654 2708                  goto split;
2655 2709          if (data->comparison == IMPOSSIBLE_COMPARISON)
2656 2710                  return;
2657 2711  
2658 2712          /*
2659 2713           * We want to check that "data->comparison" is totally inside "op".  So
2660 2714           * if data->comparison is < and op is <= then that's true.  Or if
2661 2715           * data->comparison is == and op is <= then that's true.  But if
2662 2716           * data->comparison is <= and op is < than that's neither true nor
2663 2717           * false.
2664 2718           */
2665 2719          if (data->comparison == comparison_intersection(data->comparison, op))
2666 2720                  is_true = 1;
2667 2721          if (data->comparison == comparison_intersection(data->comparison, negate_comparison(op)))
2668 2722                  is_false = 1;
2669 2723  
     2724 +        if (!is_true && !is_false && !is_merged(sm)) {
     2725 +                create_fake_history(sm, op, true_stack, false_stack);
     2726 +                return;
     2727 +        }
     2728 +
2670 2729          if (debug_implied()) {
2671 2730                  sm_msg("%s: %s: op = '%s' negated '%s'. true_intersect = '%s' false_insersect = '%s' sm = '%s'",
2672 2731                         __func__,
2673 2732                         sm->state->name,
2674 2733                         alloc_sname(show_comparison(op)),
2675 2734                         alloc_sname(show_comparison(negate_comparison(op))),
2676 2735                         alloc_sname(show_comparison(comparison_intersection(data->comparison, op))),
2677 2736                         alloc_sname(show_comparison(comparison_intersection(data->comparison, negate_comparison(op)))),
2678 2737                         show_sm(sm));
2679 2738          }
2680 2739  
     2740 +        *useful = true;
2681 2741          if (is_true)
2682 2742                  add_ptr_list(true_stack, sm);
2683 2743          if (is_false)
2684 2744                  add_ptr_list(false_stack, sm);
2685 2745  split:
2686      -        filter_by_sm(sm->left, op, true_stack, false_stack);
2687      -        filter_by_sm(sm->right, op, true_stack, false_stack);
     2746 +        filter_by_sm(sm->left, op, true_stack, false_stack, useful);
     2747 +        filter_by_sm(sm->right, op, true_stack, false_stack, useful);
2688 2748  }
2689 2749  
2690 2750  struct sm_state *comparison_implication_hook(struct expression *expr,
2691 2751                                  struct state_list **true_stack,
2692 2752                                  struct state_list **false_stack)
2693 2753  {
2694 2754          struct sm_state *sm;
2695 2755          char *left, *right;
2696 2756          int op;
2697 2757          static char buf[256];
     2758 +        bool useful = false;
2698 2759  
2699 2760          if (expr->type != EXPR_COMPARE)
2700 2761                  return NULL;
2701 2762  
2702 2763          op = expr->op;
2703 2764  
2704 2765          left = expr_to_var(expr->left);
2705 2766          right = expr_to_var(expr->right);
2706 2767          if (!left || !right) {
2707 2768                  free_string(left);
↓ open down ↓ 3 lines elided ↑ open up ↑
2711 2772  
2712 2773          if (strcmp(left, right) > 0) {
2713 2774                  char *tmp = left;
2714 2775  
2715 2776                  left = right;
2716 2777                  right = tmp;
2717 2778                  op = flip_comparison(op);
2718 2779          }
2719 2780  
2720 2781          snprintf(buf, sizeof(buf), "%s vs %s", left, right);
2721      -        sm = get_sm_state(compare_id, buf, NULL);
     2782 +        sm = get_sm_state(comparison_id, buf, NULL);
2722 2783          if (!sm)
2723 2784                  return NULL;
2724 2785          if (!sm->merged)
2725 2786                  return NULL;
2726 2787  
2727      -        filter_by_sm(sm, op, true_stack, false_stack);
2728      -        if (!*true_stack && !*false_stack)
     2788 +        filter_by_sm(sm, op, true_stack, false_stack, &useful);
     2789 +        if (!useful)
2729 2790                  return NULL;
2730 2791  
2731 2792          if (debug_implied())
2732 2793                  sm_msg("implications from comparison: (%s)", show_sm(sm));
2733 2794  
2734 2795          return sm;
2735 2796  }
    
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX