Print this page
11506 smatch resync


  26 #endif
  27 #include <limits.h>
  28 #include "parse.h"
  29 #include "smatch.h"
  30 #include "smatch_slist.h"
  31 #include "smatch_extra.h"
  32 
  33 struct smatch_state *merge_estates(struct smatch_state *s1, struct smatch_state *s2)
  34 {
  35         struct smatch_state *tmp;
  36         struct range_list *value_ranges;
  37         struct related_list *rlist;
  38 
  39         if (estates_equiv(s1, s2))
  40                 return s1;
  41 
  42         value_ranges = rl_union(estate_rl(s1), estate_rl(s2));
  43         tmp = alloc_estate_rl(value_ranges);
  44         rlist = get_shared_relations(estate_related(s1), estate_related(s2));
  45         set_related(tmp, rlist);
  46         if (estate_has_hard_max(s1) && estate_has_hard_max(s2))


  47                 estate_set_hard_max(tmp);
  48 
  49         estate_set_fuzzy_max(tmp, sval_max(estate_get_fuzzy_max(s1), estate_get_fuzzy_max(s2)));
  50 



  51         return tmp;
  52 }
  53 
  54 struct data_info *get_dinfo(struct smatch_state *state)
  55 {
  56         if (!state)
  57                 return NULL;
  58         return (struct data_info *)state->data;
  59 }
  60 
  61 struct range_list *estate_rl(struct smatch_state *state)
  62 {
  63         if (!state)
  64                 return NULL;
  65         return get_dinfo(state)->value_ranges;
  66 }
  67 
  68 struct related_list *estate_related(struct smatch_state *state)
  69 {
  70         if (!state)


 117 }
 118 
 119 void estate_set_hard_max(struct smatch_state *state)
 120 {
 121         get_dinfo(state)->hard_max = 1;
 122 }
 123 
 124 void estate_clear_hard_max(struct smatch_state *state)
 125 {
 126         get_dinfo(state)->hard_max = 0;
 127 }
 128 
 129 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
 130 {
 131         if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
 132                 return 0;
 133         *sval = rl_max(estate_rl(state));
 134         return 1;
 135 }
 136 















 137 sval_t estate_min(struct smatch_state *state)
 138 {
 139         return rl_min(estate_rl(state));
 140 }
 141 
 142 sval_t estate_max(struct smatch_state *state)
 143 {
 144         return rl_max(estate_rl(state));
 145 }
 146 
 147 struct symbol *estate_type(struct smatch_state *state)
 148 {
 149         return rl_max(estate_rl(state)).type;
 150 }
 151 
 152 static int rlists_equiv(struct related_list *one, struct related_list *two)
 153 {
 154         struct relation *one_rel;
 155         struct relation *two_rel;
 156 


 165                         return 0;
 166                 if (strcmp(one_rel->name, two_rel->name))
 167                         return 0;
 168                 NEXT_PTR_LIST(one_rel);
 169                 NEXT_PTR_LIST(two_rel);
 170         }
 171         FINISH_PTR_LIST(two_rel);
 172         FINISH_PTR_LIST(one_rel);
 173 
 174         return 1;
 175 }
 176 
 177 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
 178 {
 179         if (!one || !two)
 180                 return 0;
 181         if (one == two)
 182                 return 1;
 183         if (!rlists_equiv(estate_related(one), estate_related(two)))
 184                 return 0;


 185         if (strcmp(one->name, two->name) == 0)
 186                 return 1;
 187         return 0;
 188 }
 189 
 190 int estate_is_whole(struct smatch_state *state)
 191 {
 192         return is_whole_rl(estate_rl(state));
 193 }
 194 
 195 int estate_is_empty(struct smatch_state *state)
 196 {
 197         return state && !estate_rl(state);
 198 }
 199 
 200 int estate_is_unknown(struct smatch_state *state)
 201 {
 202         if (!estate_is_whole(state))
 203                 return 0;
 204         if (estate_related(state))


 255         ret->related = clone_related_list(dinfo->related);
 256         ret->value_ranges = clone_rl(dinfo->value_ranges);
 257         ret->hard_max = dinfo->hard_max;
 258         ret->fuzzy_max = dinfo->fuzzy_max;
 259         return ret;
 260 }
 261 
 262 struct smatch_state *clone_estate(struct smatch_state *state)
 263 {
 264         struct smatch_state *ret;
 265 
 266         if (!state)
 267                 return NULL;
 268 
 269         ret = __alloc_smatch_state(0);
 270         ret->name = state->name;
 271         ret->data = clone_dinfo(get_dinfo(state));
 272         return ret;
 273 }
 274 



















 275 struct smatch_state *alloc_estate_empty(void)
 276 {
 277         struct smatch_state *state;
 278         struct data_info *dinfo;
 279 
 280         dinfo = alloc_dinfo();
 281         state = __alloc_smatch_state(0);
 282         state->data = dinfo;
 283         state->name = "";
 284         return state;
 285 }
 286 
 287 struct smatch_state *alloc_estate_whole(struct symbol *type)
 288 {
 289         return alloc_estate_rl(alloc_whole_rl(type));
 290 }
 291 
 292 struct smatch_state *extra_empty(void)
 293 {
 294         struct smatch_state *ret;


 348         ret = __alloc_smatch_state(0);
 349         ret->name = show_rl(dinfo->value_ranges);
 350         ret->data = dinfo;
 351 
 352         return ret;
 353 }
 354 
 355 struct smatch_state *get_implied_estate(struct expression *expr)
 356 {
 357         struct smatch_state *state;
 358         struct range_list *rl;
 359 
 360         state = get_state_expr(SMATCH_EXTRA, expr);
 361         if (state)
 362                 return state;
 363         if (!get_implied_rl(expr, &rl))
 364                 rl = alloc_whole_rl(get_type(expr));
 365         return alloc_estate_rl(rl);
 366 }
 367 
 368 struct smatch_state *estate_filter_range(struct smatch_state *orig,
 369                                  sval_t filter_min, sval_t filter_max)
 370 {
 371         struct range_list *rl;
 372         struct smatch_state *state;
 373 
 374         if (!orig)
 375                 orig = alloc_estate_whole(filter_min.type);
 376 
 377         rl = remove_range(estate_rl(orig), filter_min, filter_max);
 378         state = alloc_estate_rl(rl);
 379         if (estate_has_hard_max(orig))
 380                 estate_set_hard_max(state);
 381         if (estate_has_fuzzy_max(orig))
 382                 estate_set_fuzzy_max(state, estate_get_fuzzy_max(orig));
 383         return state;
 384 }
 385 
 386 struct smatch_state *estate_filter_sval(struct smatch_state *orig, sval_t sval)
 387 {
 388         return estate_filter_range(orig, sval, sval);
 389 }
 390 
 391 /*
 392  * One of the complications is that smatch tries to free a bunch of data at the
 393  * end of every function.
 394  */
 395 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
 396 {
 397         struct data_info *ret;
 398 
 399         ret = malloc(sizeof(*ret));
 400         memset(ret, 0, sizeof(*ret));
 401         ret->related = NULL;
 402         ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
 403         ret->hard_max = 0;
 404         ret->fuzzy_max = dinfo->fuzzy_max;
 405         return ret;
 406 }
 407 
 408 struct smatch_state *clone_estate_perm(struct smatch_state *state)
 409 {
 410         struct smatch_state *ret;


  26 #endif
  27 #include <limits.h>
  28 #include "parse.h"
  29 #include "smatch.h"
  30 #include "smatch_slist.h"
  31 #include "smatch_extra.h"
  32 
  33 struct smatch_state *merge_estates(struct smatch_state *s1, struct smatch_state *s2)
  34 {
  35         struct smatch_state *tmp;
  36         struct range_list *value_ranges;
  37         struct related_list *rlist;
  38 
  39         if (estates_equiv(s1, s2))
  40                 return s1;
  41 
  42         value_ranges = rl_union(estate_rl(s1), estate_rl(s2));
  43         tmp = alloc_estate_rl(value_ranges);
  44         rlist = get_shared_relations(estate_related(s1), estate_related(s2));
  45         set_related(tmp, rlist);
  46 
  47         if ((estate_has_hard_max(s1) && (!estate_rl(s2) || estate_has_hard_max(s2))) ||
  48             (estate_has_hard_max(s2) && (!estate_rl(s1) || estate_has_hard_max(s1))))
  49                 estate_set_hard_max(tmp);
  50 
  51         estate_set_fuzzy_max(tmp, sval_max(estate_get_fuzzy_max(s1), estate_get_fuzzy_max(s2)));
  52 
  53         if (estate_capped(s1) && estate_capped(s2))
  54                 estate_set_capped(tmp);
  55 
  56         return tmp;
  57 }
  58 
  59 struct data_info *get_dinfo(struct smatch_state *state)
  60 {
  61         if (!state)
  62                 return NULL;
  63         return (struct data_info *)state->data;
  64 }
  65 
  66 struct range_list *estate_rl(struct smatch_state *state)
  67 {
  68         if (!state)
  69                 return NULL;
  70         return get_dinfo(state)->value_ranges;
  71 }
  72 
  73 struct related_list *estate_related(struct smatch_state *state)
  74 {
  75         if (!state)


 122 }
 123 
 124 void estate_set_hard_max(struct smatch_state *state)
 125 {
 126         get_dinfo(state)->hard_max = 1;
 127 }
 128 
 129 void estate_clear_hard_max(struct smatch_state *state)
 130 {
 131         get_dinfo(state)->hard_max = 0;
 132 }
 133 
 134 int estate_get_hard_max(struct smatch_state *state, sval_t *sval)
 135 {
 136         if (!state || !get_dinfo(state)->hard_max || !estate_rl(state))
 137                 return 0;
 138         *sval = rl_max(estate_rl(state));
 139         return 1;
 140 }
 141 
 142 bool estate_capped(struct smatch_state *state)
 143 {
 144         if (!state)
 145                 return false;
 146         /* impossible states are capped */
 147         if (!estate_rl(state))
 148                 return true;
 149         return get_dinfo(state)->capped;
 150 }
 151 
 152 void estate_set_capped(struct smatch_state *state)
 153 {
 154         get_dinfo(state)->capped = true;
 155 }
 156 
 157 sval_t estate_min(struct smatch_state *state)
 158 {
 159         return rl_min(estate_rl(state));
 160 }
 161 
 162 sval_t estate_max(struct smatch_state *state)
 163 {
 164         return rl_max(estate_rl(state));
 165 }
 166 
 167 struct symbol *estate_type(struct smatch_state *state)
 168 {
 169         return rl_max(estate_rl(state)).type;
 170 }
 171 
 172 static int rlists_equiv(struct related_list *one, struct related_list *two)
 173 {
 174         struct relation *one_rel;
 175         struct relation *two_rel;
 176 


 185                         return 0;
 186                 if (strcmp(one_rel->name, two_rel->name))
 187                         return 0;
 188                 NEXT_PTR_LIST(one_rel);
 189                 NEXT_PTR_LIST(two_rel);
 190         }
 191         FINISH_PTR_LIST(two_rel);
 192         FINISH_PTR_LIST(one_rel);
 193 
 194         return 1;
 195 }
 196 
 197 int estates_equiv(struct smatch_state *one, struct smatch_state *two)
 198 {
 199         if (!one || !two)
 200                 return 0;
 201         if (one == two)
 202                 return 1;
 203         if (!rlists_equiv(estate_related(one), estate_related(two)))
 204                 return 0;
 205         if (estate_capped(one) != estate_capped(two))
 206                 return 0;
 207         if (strcmp(one->name, two->name) == 0)
 208                 return 1;
 209         return 0;
 210 }
 211 
 212 int estate_is_whole(struct smatch_state *state)
 213 {
 214         return is_whole_rl(estate_rl(state));
 215 }
 216 
 217 int estate_is_empty(struct smatch_state *state)
 218 {
 219         return state && !estate_rl(state);
 220 }
 221 
 222 int estate_is_unknown(struct smatch_state *state)
 223 {
 224         if (!estate_is_whole(state))
 225                 return 0;
 226         if (estate_related(state))


 277         ret->related = clone_related_list(dinfo->related);
 278         ret->value_ranges = clone_rl(dinfo->value_ranges);
 279         ret->hard_max = dinfo->hard_max;
 280         ret->fuzzy_max = dinfo->fuzzy_max;
 281         return ret;
 282 }
 283 
 284 struct smatch_state *clone_estate(struct smatch_state *state)
 285 {
 286         struct smatch_state *ret;
 287 
 288         if (!state)
 289                 return NULL;
 290 
 291         ret = __alloc_smatch_state(0);
 292         ret->name = state->name;
 293         ret->data = clone_dinfo(get_dinfo(state));
 294         return ret;
 295 }
 296 
 297 struct smatch_state *clone_partial_estate(struct smatch_state *state, struct range_list *rl)
 298 {
 299         struct smatch_state *ret;
 300 
 301         if (!state)
 302                 return NULL;
 303 
 304         rl = cast_rl(estate_type(state), rl);
 305 
 306         ret = alloc_estate_rl(rl);
 307         set_related(ret, clone_related_list(estate_related(state)));
 308         if (estate_has_hard_max(state))
 309                 estate_set_hard_max(ret);
 310         if (estate_has_fuzzy_max(state))
 311                 estate_set_fuzzy_max(ret, estate_get_fuzzy_max(state));
 312 
 313         return ret;
 314 }
 315 
 316 struct smatch_state *alloc_estate_empty(void)
 317 {
 318         struct smatch_state *state;
 319         struct data_info *dinfo;
 320 
 321         dinfo = alloc_dinfo();
 322         state = __alloc_smatch_state(0);
 323         state->data = dinfo;
 324         state->name = "";
 325         return state;
 326 }
 327 
 328 struct smatch_state *alloc_estate_whole(struct symbol *type)
 329 {
 330         return alloc_estate_rl(alloc_whole_rl(type));
 331 }
 332 
 333 struct smatch_state *extra_empty(void)
 334 {
 335         struct smatch_state *ret;


 389         ret = __alloc_smatch_state(0);
 390         ret->name = show_rl(dinfo->value_ranges);
 391         ret->data = dinfo;
 392 
 393         return ret;
 394 }
 395 
 396 struct smatch_state *get_implied_estate(struct expression *expr)
 397 {
 398         struct smatch_state *state;
 399         struct range_list *rl;
 400 
 401         state = get_state_expr(SMATCH_EXTRA, expr);
 402         if (state)
 403                 return state;
 404         if (!get_implied_rl(expr, &rl))
 405                 rl = alloc_whole_rl(get_type(expr));
 406         return alloc_estate_rl(rl);
 407 }
 408 























 409 /*
 410  * One of the complications is that smatch tries to free a bunch of data at the
 411  * end of every function.
 412  */
 413 struct data_info *clone_dinfo_perm(struct data_info *dinfo)
 414 {
 415         struct data_info *ret;
 416 
 417         ret = malloc(sizeof(*ret));
 418         memset(ret, 0, sizeof(*ret));
 419         ret->related = NULL;
 420         ret->value_ranges = clone_rl_permanent(dinfo->value_ranges);
 421         ret->hard_max = 0;
 422         ret->fuzzy_max = dinfo->fuzzy_max;
 423         return ret;
 424 }
 425 
 426 struct smatch_state *clone_estate_perm(struct smatch_state *state)
 427 {
 428         struct smatch_state *ret;