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


  57         ret.type = get_type(expr);
  58         if (!ret.type)
  59                 ret.type = &int_ctype;
  60         ret.value = 123456789;
  61 
  62         return ret;
  63 }
  64 
  65 sval_t sval_type_val(struct symbol *type, long long val)
  66 {
  67         sval_t ret;
  68 
  69         if (!type)
  70                 type = &llong_ctype;
  71 
  72         ret.type = type;
  73         ret.value = val;
  74         return ret;
  75 }
  76 









  77 sval_t sval_from_val(struct expression *expr, long long val)
  78 {
  79         sval_t ret;
  80 
  81         ret = sval_blank(expr);
  82         ret.value = val;
  83         ret = sval_cast(get_type(expr), ret);
  84 
  85         return ret;
  86 }
  87 











  88 int sval_is_ptr(sval_t sval)
  89 {
  90         if (!sval.type)
  91                 return 0;
  92         return (sval.type->type == SYM_PTR || sval.type->type == SYM_ARRAY);
  93 }
  94 





  95 int sval_unsigned(sval_t sval)
  96 {
  97         if (is_ptr_type(sval.type))
  98                 return true;
  99         return type_unsigned(sval.type);
 100 }
 101 
 102 int sval_signed(sval_t sval)
 103 {
 104         return !type_unsigned(sval.type);
 105 }
 106 
 107 int sval_bits(sval_t sval)
 108 {
 109         return type_bits(sval.type);
 110 }
 111 
 112 int sval_bits_used(sval_t sval)
 113 {
 114         int i;


 117                 if (sval.uvalue & (1ULL << (i - 1)))
 118                         return i;
 119         }
 120         return 0;
 121 }
 122 
 123 int sval_is_negative(sval_t sval)
 124 {
 125         if (type_unsigned(sval.type))
 126                 return 0;
 127         if (sval.value < 0)
 128                 return 1;
 129         return 0;
 130 }
 131 
 132 int sval_is_positive(sval_t sval)
 133 {
 134         return !sval_is_negative(sval);
 135 }
 136 












 137 int sval_is_min(sval_t sval)
 138 {
 139         sval_t min = sval_type_min(sval.type);
 140 



 141         if (sval_unsigned(sval)) {
 142                 if (sval.uvalue == 0)
 143                         return 1;
 144                 return 0;
 145         }
 146         /* return true for less than min as well */
 147         return (sval.value <= min.value);
 148 }
 149 












 150 int sval_is_max(sval_t sval)
 151 {
 152         sval_t max = sval_type_max(sval.type);
 153 



 154         if (sval_unsigned(sval))
 155                 return (sval.uvalue >= max.value);
 156         return (sval.value >= max.value);
 157 }
 158 
 159 int sval_is_a_min(sval_t sval)
 160 {
 161         if (sval_is_min(sval))
 162                 return 1;




 163         if (sval_signed(sval) && sval.value == SHRT_MIN)
 164                 return 1;
 165         if (sval_signed(sval) && sval.value == INT_MIN)
 166                 return 1;
 167         if (sval_signed(sval) && sval.value == LLONG_MIN)
 168                 return 1;
 169         return 0;
 170 }
 171 
 172 int sval_is_a_max(sval_t sval)
 173 {
 174         if (sval_is_max(sval))
 175                 return 1;




 176         if (sval.uvalue == SHRT_MAX)
 177                 return 1;
 178         if (sval.uvalue == INT_MAX)
 179                 return 1;
 180         if (sval.uvalue == LLONG_MAX)
 181                 return 1;
 182         if (sval.uvalue == USHRT_MAX)
 183                 return 1;
 184         if (sval.uvalue == UINT_MAX)
 185                 return 1;
 186         if (sval_unsigned(sval) && sval.uvalue == ULLONG_MAX)
 187                 return 1;
 188         if (sval.value > valid_ptr_max - 1000 &&
 189             sval.value < valid_ptr_max + 1000)
 190                 return 1;
 191         return 0;
 192 }
 193 
 194 int sval_is_negative_min(sval_t sval)
 195 {



 196         if (!sval_is_negative(sval))
 197                 return 0;
 198         return sval_is_min(sval);
 199 }
 200 
 201 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
 202 {
 203         sval_t one_cast, two_cast;
 204 
 205         one_cast = sval_cast(type, one);
 206         two_cast = sval_cast(type, two);
 207         return sval_cmp(one_cast, two_cast);
 208 }
 209 
 210 int sval_cmp_val(sval_t one, long long val)
 211 {
 212         sval_t sval;
 213 
 214         sval = sval_type_val(&llong_ctype, val);
 215         return sval_cmp(one, sval);


 237                 return 0;
 238         if (type_signed(sval.type) &&
 239             sval.value < sval_type_min(type).value)
 240                 return 1;
 241         if (sval_cmp(sval, sval_type_min(type)) < 0)
 242                 return 1;
 243         return 0;
 244 }
 245 
 246 int sval_too_high(struct symbol *type, sval_t sval)
 247 {
 248         if (sval_is_negative(sval))
 249                 return 0;
 250         if (sval.uvalue > sval_type_max(type).uvalue)
 251                 return 1;
 252         return 0;
 253 }
 254 
 255 int sval_fits(struct symbol *type, sval_t sval)
 256 {







 257         if (sval_too_low(type, sval))
 258                 return 0;
 259         if (sval_too_high(type, sval))
 260                 return 0;
 261         return 1;
 262 }
 263 

































































 264 sval_t sval_cast(struct symbol *type, sval_t sval)
 265 {
 266         sval_t ret;
 267 
 268         if (!type)
 269                 type = &int_ctype;
 270 





 271         ret.type = type;
 272         switch (sval_bits(ret)) {
 273         case 1:
 274                 ret.value = !!sval.value;
 275                 break;
 276         case 8:
 277                 if (sval_unsigned(ret))
 278                         ret.value = (long long)(unsigned char)sval.value;
 279                 else
 280                         ret.value = (long long)(char)sval.value;
 281                 break;
 282         case 16:
 283                 if (sval_unsigned(ret))
 284                         ret.value = (long long)(unsigned short)sval.value;
 285                 else
 286                         ret.value = (long long)(short)sval.value;
 287                 break;
 288         case 32:
 289                 if (sval_unsigned(ret))
 290                         ret.value = (long long)(unsigned int)sval.value;


 609 
 610         return high_bit;
 611 }
 612 
 613 unsigned long long fls_mask(unsigned long long uvalue)
 614 {
 615         int high_bit = 0;
 616 
 617         high_bit = sm_fls64(uvalue);
 618         if (high_bit == 0)
 619                 return 0;
 620 
 621         return ((unsigned long long)-1) >> (64 - high_bit);
 622 }
 623 
 624 unsigned long long sval_fls_mask(sval_t sval)
 625 {
 626         return fls_mask(sval.uvalue);
 627 }
 628 
















 629 const char *sval_to_str(sval_t sval)
 630 {
 631         char buf[30];
 632 



 633         if (sval_is_ptr(sval) && sval.value == valid_ptr_max)
 634                 return "ptr_max";
 635         if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
 636                 return "u64max";
 637         if (sval_unsigned(sval) && sval.value == UINT_MAX)
 638                 return "u32max";
 639         if (sval.value == USHRT_MAX)
 640                 return "u16max";
 641 
 642         if (sval_signed(sval) && sval.value == LLONG_MAX)
 643                 return "s64max";
 644         if (sval.value == INT_MAX)
 645                 return "s32max";
 646         if (sval.value == SHRT_MAX)
 647                 return "s16max";
 648 
 649         if (sval_signed(sval) && sval.value == SHRT_MIN)
 650                 return "s16min";
 651         if (sval_signed(sval) && sval.value == INT_MIN)
 652                 return "s32min";


 654                 return "s64min";
 655 
 656         if (sval_unsigned(sval))
 657                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 658         else if (sval.value < 0)
 659                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 660         else
 661                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 662 
 663         return alloc_sname(buf);
 664 }
 665 
 666 const char *sval_to_str_or_err_ptr(sval_t sval)
 667 {
 668         char buf[12];
 669 
 670         if (option_project != PROJ_KERNEL ||
 671             !is_ptr_type(sval.type))
 672                 return sval_to_str(sval);
 673 
 674         if (sval.uvalue >= -4905ULL) {
 675                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 676                 return alloc_sname(buf);
 677         }
 678 
 679         return sval_to_str(sval);
 680 }
 681 
 682 const char *sval_to_numstr(sval_t sval)
 683 {
 684         char buf[30];
 685 



 686         if (sval_unsigned(sval))
 687                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 688         else if (sval.value < 0)
 689                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 690         else
 691                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 692 
 693         return alloc_sname(buf);
 694 }
 695 
 696 sval_t ll_to_sval(long long val)
 697 {
 698         sval_t ret;
 699 
 700         ret.type = &llong_ctype;
 701         ret.value = val;
 702         return ret;
 703 }
 704 
 705 static void free_svals(struct symbol *sym)


  57         ret.type = get_type(expr);
  58         if (!ret.type)
  59                 ret.type = &int_ctype;
  60         ret.value = 123456789;
  61 
  62         return ret;
  63 }
  64 
  65 sval_t sval_type_val(struct symbol *type, long long val)
  66 {
  67         sval_t ret;
  68 
  69         if (!type)
  70                 type = &llong_ctype;
  71 
  72         ret.type = type;
  73         ret.value = val;
  74         return ret;
  75 }
  76 
  77 sval_t sval_type_fval(struct symbol *type, long double fval)
  78 {
  79         sval_t ret;
  80 
  81         ret.type = &ldouble_ctype;
  82         ret.ldvalue = fval;
  83         return sval_cast(type, ret);
  84 }
  85 
  86 sval_t sval_from_val(struct expression *expr, long long val)
  87 {
  88         sval_t ret;
  89 
  90         ret = sval_blank(expr);
  91         ret.value = val;
  92         ret = sval_cast(get_type(expr), ret);
  93 
  94         return ret;
  95 }
  96 
  97 sval_t sval_from_fval(struct expression *expr, long double fval)
  98 {
  99         sval_t ret;
 100 
 101         ret.type = &ldouble_ctype;
 102         ret.ldvalue = fval;
 103         ret = sval_cast(get_type(expr), ret);
 104 
 105         return ret;
 106 }
 107 
 108 int sval_is_ptr(sval_t sval)
 109 {
 110         if (!sval.type)
 111                 return 0;
 112         return (sval.type->type == SYM_PTR || sval.type->type == SYM_ARRAY);
 113 }
 114 
 115 bool sval_is_fp(sval_t sval)
 116 {
 117         return type_is_fp(sval.type);
 118 }
 119 
 120 int sval_unsigned(sval_t sval)
 121 {
 122         if (is_ptr_type(sval.type))
 123                 return true;
 124         return type_unsigned(sval.type);
 125 }
 126 
 127 int sval_signed(sval_t sval)
 128 {
 129         return !type_unsigned(sval.type);
 130 }
 131 
 132 int sval_bits(sval_t sval)
 133 {
 134         return type_bits(sval.type);
 135 }
 136 
 137 int sval_bits_used(sval_t sval)
 138 {
 139         int i;


 142                 if (sval.uvalue & (1ULL << (i - 1)))
 143                         return i;
 144         }
 145         return 0;
 146 }
 147 
 148 int sval_is_negative(sval_t sval)
 149 {
 150         if (type_unsigned(sval.type))
 151                 return 0;
 152         if (sval.value < 0)
 153                 return 1;
 154         return 0;
 155 }
 156 
 157 int sval_is_positive(sval_t sval)
 158 {
 159         return !sval_is_negative(sval);
 160 }
 161 
 162 static bool fp_is_min(sval_t sval)
 163 {
 164         if (sval.type == &float_ctype)
 165                 return sval.fvalue == -FLT_MAX;
 166         if (sval.type == &double_ctype)
 167                 return sval.dvalue == -DBL_MAX;
 168         if (sval.type == &ldouble_ctype)
 169                 return sval.ldvalue == -LDBL_MAX;
 170         sm_perror("%s: bad type: '%s'", __func__, type_to_str(sval.type));
 171         return false;
 172 }
 173 
 174 int sval_is_min(sval_t sval)
 175 {
 176         sval_t min = sval_type_min(sval.type);
 177 
 178         if (sval_is_fp(sval))
 179                 return fp_is_min(sval);
 180 
 181         if (sval_unsigned(sval)) {
 182                 if (sval.uvalue == 0)
 183                         return 1;
 184                 return 0;
 185         }
 186         /* return true for less than min as well */
 187         return (sval.value <= min.value);
 188 }
 189 
 190 static bool fp_is_max(sval_t sval)
 191 {
 192         if (sval.type == &float_ctype)
 193                 return sval.fvalue == FLT_MAX;
 194         if (sval.type == &double_ctype)
 195                 return sval.dvalue == DBL_MAX;
 196         if (sval.type == &ldouble_ctype)
 197                 return sval.ldvalue == LDBL_MAX;
 198         sm_perror("%s: bad type: '%s'", __func__, type_to_str(sval.type));
 199         return false;
 200 }
 201 
 202 int sval_is_max(sval_t sval)
 203 {
 204         sval_t max = sval_type_max(sval.type);
 205 
 206         if (sval_is_fp(sval))
 207                 return fp_is_max(sval);
 208 
 209         if (sval_unsigned(sval))
 210                 return (sval.uvalue >= max.value);
 211         return (sval.value >= max.value);
 212 }
 213 
 214 int sval_is_a_min(sval_t sval)
 215 {
 216         if (sval_is_min(sval))
 217                 return 1;
 218 
 219         if (sval_is_fp(sval))
 220                 return 0;
 221 
 222         if (sval_signed(sval) && sval.value == SHRT_MIN)
 223                 return 1;
 224         if (sval_signed(sval) && sval.value == INT_MIN)
 225                 return 1;
 226         if (sval_signed(sval) && sval.value == LLONG_MIN)
 227                 return 1;
 228         return 0;
 229 }
 230 
 231 int sval_is_a_max(sval_t sval)
 232 {
 233         if (sval_is_max(sval))
 234                 return 1;
 235 
 236         if (sval_is_fp(sval))
 237                 return 0;
 238 
 239         if (sval.uvalue == SHRT_MAX)
 240                 return 1;
 241         if (sval.uvalue == INT_MAX)
 242                 return 1;
 243         if (sval.uvalue == LLONG_MAX)
 244                 return 1;
 245         if (sval.uvalue == USHRT_MAX)
 246                 return 1;
 247         if (sval.uvalue == UINT_MAX)
 248                 return 1;
 249         if (sval_unsigned(sval) && sval.uvalue == ULLONG_MAX)
 250                 return 1;
 251         if (sval.value > valid_ptr_max - 1000 &&
 252             sval.value < valid_ptr_max + 1000)
 253                 return 1;
 254         return 0;
 255 }
 256 
 257 int sval_is_negative_min(sval_t sval)
 258 {
 259         if (sval_is_fp(sval))
 260                 return 0;
 261 
 262         if (!sval_is_negative(sval))
 263                 return 0;
 264         return sval_is_min(sval);
 265 }
 266 
 267 int sval_cmp_t(struct symbol *type, sval_t one, sval_t two)
 268 {
 269         sval_t one_cast, two_cast;
 270 
 271         one_cast = sval_cast(type, one);
 272         two_cast = sval_cast(type, two);
 273         return sval_cmp(one_cast, two_cast);
 274 }
 275 
 276 int sval_cmp_val(sval_t one, long long val)
 277 {
 278         sval_t sval;
 279 
 280         sval = sval_type_val(&llong_ctype, val);
 281         return sval_cmp(one, sval);


 303                 return 0;
 304         if (type_signed(sval.type) &&
 305             sval.value < sval_type_min(type).value)
 306                 return 1;
 307         if (sval_cmp(sval, sval_type_min(type)) < 0)
 308                 return 1;
 309         return 0;
 310 }
 311 
 312 int sval_too_high(struct symbol *type, sval_t sval)
 313 {
 314         if (sval_is_negative(sval))
 315                 return 0;
 316         if (sval.uvalue > sval_type_max(type).uvalue)
 317                 return 1;
 318         return 0;
 319 }
 320 
 321 int sval_fits(struct symbol *type, sval_t sval)
 322 {
 323         /* everything fits into floating point */
 324         if (type_is_fp(type))
 325                 return 1;
 326         /* floating points don't fit into int */
 327         if (type_is_fp(sval.type))
 328                 return 0;
 329 
 330         if (sval_too_low(type, sval))
 331                 return 0;
 332         if (sval_too_high(type, sval))
 333                 return 0;
 334         return 1;
 335 }
 336 
 337 static sval_t cast_to_fp(struct symbol *type, sval_t sval)
 338 {
 339         sval_t ret = {};
 340 
 341         ret.type = type;
 342         if (type == &float_ctype) {
 343                 if (!sval_is_fp(sval)) {
 344                         if (sval_unsigned(sval))
 345                                 ret.fvalue = sval.uvalue;
 346                         else
 347                                 ret.fvalue = sval.value;
 348                 } else if (sval.type == &float_ctype)
 349                         ret.fvalue = sval.fvalue;
 350                 else if (sval.type == &double_ctype)
 351                         ret.fvalue = sval.dvalue;
 352                 else
 353                         ret.fvalue = sval.ldvalue;
 354         } else if (type == &double_ctype) {
 355                 if (!sval_is_fp(sval)) {
 356                         if (sval_unsigned(sval))
 357                                 ret.dvalue = sval.uvalue;
 358                         else
 359                                 ret.dvalue = sval.value;
 360                 } else if (sval.type == &float_ctype)
 361                         ret.dvalue = sval.fvalue;
 362                 else if (sval.type == &double_ctype)
 363                         ret.dvalue = sval.dvalue;
 364                 else
 365                         ret.dvalue = sval.ldvalue;
 366         } else if (type == &ldouble_ctype) {
 367                 if (!sval_is_fp(sval)) {
 368                         if (sval_unsigned(sval))
 369                                 ret.ldvalue = (long double)sval.uvalue;
 370                         else
 371                                 ret.ldvalue = (long double)sval.value;
 372                 } else if (sval.type == &float_ctype)
 373                         ret.ldvalue = sval.fvalue;
 374                 else if (sval.type == &double_ctype)
 375                         ret.ldvalue = sval.dvalue;
 376                 else
 377                         ret.ldvalue = sval.ldvalue;
 378         } else {
 379                 sm_perror("%s: bad type: %s", __func__, type_to_str(type));
 380         }
 381 
 382         return ret;
 383 }
 384 
 385 static sval_t cast_from_fp(struct symbol *type, sval_t sval)
 386 {
 387         sval_t ret = {};
 388 
 389         ret.type = &llong_ctype;
 390         if (sval.type == &float_ctype)
 391                 ret.value = sval.fvalue;
 392         else if (sval.type == &double_ctype)
 393                 ret.value = sval.dvalue;
 394         else if (sval.type == &ldouble_ctype)
 395                 ret.value = sval.ldvalue;
 396         else
 397                 sm_perror("%s: bad type: %s", __func__, type_to_str(type));
 398 
 399         return sval_cast(type, ret);
 400 }
 401 
 402 sval_t sval_cast(struct symbol *type, sval_t sval)
 403 {
 404         sval_t ret;
 405 
 406         if (!type)
 407                 type = &int_ctype;
 408 
 409         if (type_is_fp(type))
 410                 return cast_to_fp(type, sval);
 411         if (type_is_fp(sval.type))
 412                 return cast_from_fp(type, sval);
 413 
 414         ret.type = type;
 415         switch (sval_bits(ret)) {
 416         case 1:
 417                 ret.value = !!sval.value;
 418                 break;
 419         case 8:
 420                 if (sval_unsigned(ret))
 421                         ret.value = (long long)(unsigned char)sval.value;
 422                 else
 423                         ret.value = (long long)(char)sval.value;
 424                 break;
 425         case 16:
 426                 if (sval_unsigned(ret))
 427                         ret.value = (long long)(unsigned short)sval.value;
 428                 else
 429                         ret.value = (long long)(short)sval.value;
 430                 break;
 431         case 32:
 432                 if (sval_unsigned(ret))
 433                         ret.value = (long long)(unsigned int)sval.value;


 752 
 753         return high_bit;
 754 }
 755 
 756 unsigned long long fls_mask(unsigned long long uvalue)
 757 {
 758         int high_bit = 0;
 759 
 760         high_bit = sm_fls64(uvalue);
 761         if (high_bit == 0)
 762                 return 0;
 763 
 764         return ((unsigned long long)-1) >> (64 - high_bit);
 765 }
 766 
 767 unsigned long long sval_fls_mask(sval_t sval)
 768 {
 769         return fls_mask(sval.uvalue);
 770 }
 771 
 772 static char *fp_to_str(sval_t sval)
 773 {
 774         char buf[32];
 775 
 776         if (sval.type == &float_ctype)
 777                 snprintf(buf, sizeof(buf), "%f", sval.fvalue);
 778         else if (sval.type == &double_ctype)
 779                 snprintf(buf, sizeof(buf), "%e", sval.dvalue);
 780         else if (sval.type == &ldouble_ctype) {
 781                 snprintf(buf, sizeof(buf), "%Lf", sval.ldvalue);
 782         } else
 783                 snprintf(buf, sizeof(buf), "nan");
 784 
 785         return alloc_sname(buf);
 786 }
 787 
 788 const char *sval_to_str(sval_t sval)
 789 {
 790         char buf[32];
 791 
 792         if (sval_is_fp(sval))
 793                 return fp_to_str(sval);
 794 
 795         if (sval_is_ptr(sval) && sval.value == valid_ptr_max)
 796                 return "ptr_max";
 797         if (sval_unsigned(sval) && sval.value == ULLONG_MAX)
 798                 return "u64max";
 799         if (sval_unsigned(sval) && sval.value == UINT_MAX)
 800                 return "u32max";
 801         if (sval.value == USHRT_MAX)
 802                 return "u16max";
 803 
 804         if (sval_signed(sval) && sval.value == LLONG_MAX)
 805                 return "s64max";
 806         if (sval.value == INT_MAX)
 807                 return "s32max";
 808         if (sval.value == SHRT_MAX)
 809                 return "s16max";
 810 
 811         if (sval_signed(sval) && sval.value == SHRT_MIN)
 812                 return "s16min";
 813         if (sval_signed(sval) && sval.value == INT_MIN)
 814                 return "s32min";


 816                 return "s64min";
 817 
 818         if (sval_unsigned(sval))
 819                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 820         else if (sval.value < 0)
 821                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 822         else
 823                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 824 
 825         return alloc_sname(buf);
 826 }
 827 
 828 const char *sval_to_str_or_err_ptr(sval_t sval)
 829 {
 830         char buf[12];
 831 
 832         if (option_project != PROJ_KERNEL ||
 833             !is_ptr_type(sval.type))
 834                 return sval_to_str(sval);
 835 
 836         if (!sval_is_fp(sval) && sval.uvalue >= -4905ULL) {
 837                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 838                 return alloc_sname(buf);
 839         }
 840 
 841         return sval_to_str(sval);
 842 }
 843 
 844 const char *sval_to_numstr(sval_t sval)
 845 {
 846         char buf[30];
 847 
 848         if (type_is_fp(sval.type))
 849                 return fp_to_str(sval);
 850 
 851         if (sval_unsigned(sval))
 852                 snprintf(buf, sizeof(buf), "%llu", sval.value);
 853         else if (sval.value < 0)
 854                 snprintf(buf, sizeof(buf), "(%lld)", sval.value);
 855         else
 856                 snprintf(buf, sizeof(buf), "%lld", sval.value);
 857 
 858         return alloc_sname(buf);
 859 }
 860 
 861 sval_t ll_to_sval(long long val)
 862 {
 863         sval_t ret;
 864 
 865         ret.type = &llong_ctype;
 866         ret.value = val;
 867         return ret;
 868 }
 869 
 870 static void free_svals(struct symbol *sym)