9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 */
17
18 #include "parse.h"
19 #include "smatch.h"
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
22
23 ALLOCATOR(data_info, "smatch extra data");
24 ALLOCATOR(data_range, "data range");
25 __DO_ALLOCATOR(struct data_range, sizeof(struct data_range), __alignof__(struct data_range),
26 "permanent ranges", perm_data_range);
27 __DECLARE_ALLOCATOR(struct ptr_list, rl_ptrlist);
28
29 char *show_rl(struct range_list *list)
30 {
31 struct data_range *tmp;
32 char full[512];
33 int i = 0;
34
35 full[0] = '\0';
36 full[sizeof(full) - 1] = '\0';
37 FOR_EACH_PTR(list, tmp) {
38 if (i++)
39 strncat(full, ",", 254 - strlen(full));
40 if (sval_cmp(tmp->min, tmp->max) == 0) {
41 strncat(full, sval_to_str(tmp->min), 254 - strlen(full));
42 continue;
43 }
44 strncat(full, sval_to_str(tmp->min), 254 - strlen(full));
45 strncat(full, "-", 254 - strlen(full));
46 strncat(full, sval_to_str(tmp->max), 254 - strlen(full));
47 } END_FOR_EACH_PTR(tmp);
48 if (strlen(full) == sizeof(full) - 1)
49 full[sizeof(full) - 2] = '+';
50 return alloc_sname(full);
51 }
52
53 void free_all_rl(void)
54 {
55 clear_rl_ptrlist_alloc();
56 }
57
58 static int sval_too_big(struct symbol *type, sval_t sval)
59 {
60 if (type_bits(type) >= 32 &&
61 type_bits(sval.type) <= type_bits(type))
62 return 0;
63 if (sval.uvalue <= ((1ULL << type_bits(type)) - 1))
64 return 0;
65 if (type_signed(sval.type)) {
66 if (type_unsigned(type)) {
67 unsigned long long neg = ~sval.uvalue;
68 if (neg <= sval_type_max(type).uvalue)
69 return 0;
70 }
71 if (sval.value < sval_type_min(type).value)
72 return 1;
73 if (sval.value > sval_type_max(type).value)
74 return 1;
75 return 0;
76 }
77 if (sval.uvalue > sval_type_max(type).uvalue)
78 return 1;
79 return 0;
80 }
81
82 static void add_range_t(struct symbol *type, struct range_list **rl, sval_t min, sval_t max)
83 {
84 /* If we're just adding a number, cast it and add it */
85 if (sval_cmp(min, max) == 0) {
86 add_range(rl, sval_cast(type, min), sval_cast(type, max));
87 return;
88 }
89
90 /* If the range is within the type range then add it */
91 if (sval_fits(type, min) && sval_fits(type, max)) {
92 add_range(rl, sval_cast(type, min), sval_cast(type, max));
93 return;
94 }
95
96 /*
97 * If the range we are adding has more bits than the range type then
98 * add the whole range type. Eg:
99 * 0x8000000000000000 - 0xf000000000000000 -> cast to int
100 * This isn't totally the right thing to do. We could be more granular.
101 */
102 if (sval_too_big(type, min) || sval_too_big(type, max)) {
103 add_range(rl, sval_type_min(type), sval_type_max(type));
104 return;
105 }
106
107 /* Cast negative values to high positive values */
108 if (sval_is_negative(min) && type_unsigned(type)) {
109 if (sval_is_positive(max)) {
110 if (sval_too_high(type, max)) {
111 add_range(rl, sval_type_min(type), sval_type_max(type));
112 return;
113 }
114 add_range(rl, sval_type_val(type, 0), sval_cast(type, max));
115 max = sval_type_max(type);
116 } else {
117 max = sval_cast(type, max);
118 }
119 min = sval_cast(type, min);
120 add_range(rl, min, max);
121 }
122
123 /* Cast high positive numbers to negative */
124 if (sval_unsigned(max) && sval_is_negative(sval_cast(type, max))) {
125 if (!sval_is_negative(sval_cast(type, min))) {
126 add_range(rl, sval_cast(type, min), sval_type_max(type));
127 min = sval_type_min(type);
128 } else {
129 min = sval_cast(type, min);
130 }
131 max = sval_cast(type, max);
132 add_range(rl, min, max);
133 }
134
135 add_range(rl, sval_cast(type, min), sval_cast(type, max));
136 return;
137 }
138
139 static int str_to_comparison_arg_helper(const char *str,
140 struct expression *call, int *comparison,
141 struct expression **arg, char **endp)
142 {
143 int param;
144 char *c = (char *)str;
145
146 if (*c != '[')
147 return 0;
148 c++;
149
150 if (*c == '<') {
151 c++;
152 if (*c == '=') {
153 *comparison = SPECIAL_LTE;
154 c++;
155 } else {
156 *comparison = '<';
157 }
158 } else if (*c == '=') {
159 c++;
160 c++;
161 *comparison = SPECIAL_EQUAL;
162 } else if (*c == '>') {
163 c++;
164 if (*c == '=') {
165 *comparison = SPECIAL_GTE;
166 c++;
167 } else {
168 *comparison = '>';
169 }
170 } else if (*c == '!') {
171 c++;
172 c++;
173 *comparison = SPECIAL_NOTEQUAL;
174 } else {
175 return 0;
176 }
177
178 if (*c != '$')
179 return 0;
180 c++;
181
182 param = strtoll(c, &c, 10);
183 if (*c == ']')
184 c++; /* skip the ']' character */
185 if (endp)
186 *endp = (char *)c;
187
188 if (!call)
189 return 0;
190 *arg = get_argument_from_call_expr(call->args, param);
191 if (!*arg)
192 return 0;
193 if (*c == '-' && *(c + 1) == '>') {
194 char buf[256];
195 int n;
196
197 n = snprintf(buf, sizeof(buf), "$%s", c);
198 if (n >= sizeof(buf))
199 return 0;
200 if (buf[n - 1] == ']')
201 buf[n - 1] = '\0';
202 *arg = gen_expression_from_key(*arg, buf);
203 while (*c && *c != ']')
204 c++;
205 }
206 return 1;
207 }
208
209 int str_to_comparison_arg(const char *str, struct expression *call, int *comparison, struct expression **arg)
210 {
211 while (1) {
212 if (!*str)
213 return 0;
214 if (*str == '[')
215 break;
216 str++;
217 }
218 return str_to_comparison_arg_helper(str, call, comparison, arg, NULL);
219 }
220
221 static int get_val_from_key(int use_max, struct symbol *type, char *c, struct expression *call, char **endp, sval_t *sval)
222 {
223 struct expression *arg;
224 int comparison;
225 sval_t ret, tmp;
226
227 if (use_max)
228 ret = sval_type_max(type);
229 else
230 ret = sval_type_min(type);
231
232 if (!str_to_comparison_arg_helper(c, call, &comparison, &arg, endp)) {
233 *sval = ret;
234 return 0;
235 }
236
237 if (use_max && get_implied_max(arg, &tmp)) {
238 ret = tmp;
239 if (comparison == '<') {
240 tmp.value = 1;
241 ret = sval_binop(ret, '-', tmp);
301 case SPECIAL_UNSIGNED_GTE:
302 if (!sval_is_min(rl_min(right_orig)))
303 ret_rl = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
304 break;
305 case '>':
306 case SPECIAL_UNSIGNED_GT:
307 ret_rl = remove_range(left_orig, min, rl_min(right_orig));
308 break;
309 case SPECIAL_NOTEQUAL:
310 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
311 ret_rl = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
312 break;
313 default:
314 sm_perror("unhandled comparison %s", show_special(comparison));
315 return;
316 }
317
318 *rl = cast_rl(rl_type(*rl), ret_rl);
319 }
320
321 static struct range_list *filter_by_comparison_call(char *c, struct expression *call, char **endp, struct range_list *start_rl)
322 {
323 struct symbol *type;
324 struct expression *arg;
325 struct range_list *casted_start, *right_orig;
326 int comparison;
327
328 if (!str_to_comparison_arg_helper(c, call, &comparison, &arg, endp))
329 return start_rl;
330
331 if (!get_implied_rl(arg, &right_orig))
332 return start_rl;
333
334 type = &int_ctype;
335 if (type_positive_bits(rl_type(start_rl)) > type_positive_bits(type))
336 type = rl_type(start_rl);
337 if (type_positive_bits(rl_type(right_orig)) > type_positive_bits(type))
338 type = rl_type(right_orig);
339
340 casted_start = cast_rl(type, start_rl);
341 right_orig = cast_rl(type, right_orig);
342
343 filter_by_comparison(&casted_start, comparison, right_orig);
344 return cast_rl(rl_type(start_rl), casted_start);
345 }
346
347 static sval_t parse_val(int use_max, struct expression *call, struct symbol *type, char *c, char **endp)
348 {
349 char *start = c;
350 sval_t ret;
351
352 if (!strncmp(start, "max", 3)) {
353 ret = sval_type_max(type);
354 c += 3;
355 } else if (!strncmp(start, "u64max", 6)) {
356 ret = sval_type_val(type, ULLONG_MAX);
357 c += 6;
358 } else if (!strncmp(start, "s64max", 6)) {
359 ret = sval_type_val(type, LLONG_MAX);
360 c += 6;
361 } else if (!strncmp(start, "u32max", 6)) {
362 ret = sval_type_val(type, UINT_MAX);
363 c += 6;
364 } else if (!strncmp(start, "s32max", 6)) {
365 ret = sval_type_val(type, INT_MAX);
366 c += 6;
367 } else if (!strncmp(start, "u16max", 6)) {
368 ret = sval_type_val(type, USHRT_MAX);
369 c += 6;
381 c += 6;
382 } else if (!strncmp(start, "s16min", 6)) {
383 ret = sval_type_val(type, SHRT_MIN);
384 c += 6;
385 } else if (!strncmp(start, "long_min", 8)) {
386 ret = sval_type_val(type, LONG_MIN);
387 c += 8;
388 } else if (!strncmp(start, "long_max", 8)) {
389 ret = sval_type_val(type, LONG_MAX);
390 c += 8;
391 } else if (!strncmp(start, "ulong_max", 9)) {
392 ret = sval_type_val(type, ULONG_MAX);
393 c += 9;
394 } else if (!strncmp(start, "ptr_max", 7)) {
395 ret = sval_type_val(type, valid_ptr_max);
396 c += 7;
397 } else if (start[0] == '[') {
398 /* this parses [==p0] comparisons */
399 get_val_from_key(1, type, start, call, &c, &ret);
400 } else if (type_positive_bits(type) == 64) {
401 ret = sval_type_val(type, strtoull(start, &c, 0));
402 } else {
403 ret = sval_type_val(type, strtoll(start, &c, 0));
404 }
405 *endp = c;
406 return ret;
407 }
408
409 static char *jump_to_call_math(char *value)
410 {
411 char *c = value;
412
413 while (*c && *c != '[')
414 c++;
415
416 if (!*c)
417 return NULL;
418 c++;
419 if (*c == '<' || *c == '=' || *c == '>' || *c == '!')
420 return NULL;
421
422 return c;
423 }
424
425 static void str_to_rl_helper(struct expression *call, struct symbol *type, char *str, char **endp, struct range_list **rl)
426 {
427 struct range_list *rl_tmp = NULL;
428 sval_t min, max;
429 char *c;
430
431 min = sval_type_min(type);
432 max = sval_type_max(type);
433 c = str;
434 while (*c != '\0' && *c != '[') {
435 if (*c == '+') {
436 if (sval_cmp(min, sval_type_min(type)) != 0)
437 min = max;
438 max = sval_type_max(type);
439 add_range_t(type, &rl_tmp, min, max);
440 break;
441 }
442 if (*c == '(')
443 c++;
444 min = parse_val(0, call, type, c, &c);
445 if (!sval_fits(type, min))
446 min = sval_type_min(type);
447 max = min;
448 if (*c == ')')
449 c++;
450 if (*c == '\0' || *c == '[') {
451 add_range_t(type, &rl_tmp, min, min);
452 break;
453 }
454 if (*c == ',') {
455 add_range_t(type, &rl_tmp, min, min);
456 c++;
457 continue;
458 }
459 if (*c == '+') {
460 min = sval_type_max(type);
461 c++;
462 }
463 if (*c != '-') {
464 sm_msg("debug XXX: trouble parsing %s c = %s", str, c);
465 break;
466 }
467 c++;
468 if (*c == '(')
469 c++;
470 max = parse_val(1, call, type, c, &c);
471 if (!sval_fits(type, max))
472 max = sval_type_max(type);
473 if (*c == '+') {
474 max = sval_type_max(type);
475 c++;
476 }
477 add_range_t(type, &rl_tmp, min, max);
478 if (*c == ')')
479 c++;
480 if (*c == ',')
481 c++;
482 }
483
484 *rl = rl_tmp;
485 *endp = c;
486 }
487
488 static void str_to_dinfo(struct expression *call, struct symbol *type, char *value, struct data_info *dinfo)
489 {
490 struct range_list *math_rl;
491 char *call_math;
492 char *c;
493 struct range_list *rl = NULL;
494
495 if (!type)
496 type = &llong_ctype;
497
498 if (strcmp(value, "empty") == 0)
499 return;
500
501 if (strncmp(value, "[==$", 4) == 0) {
502 struct expression *arg;
503 int comparison;
504
505 if (!str_to_comparison_arg(value, call, &comparison, &arg))
506 return;
507 if (!get_implied_rl(arg, &rl))
508 return;
509 goto cast;
510 }
511
512 str_to_rl_helper(call, type, value, &c, &rl);
516 call_math = jump_to_call_math(value);
517 if (call_math && parse_call_math_rl(call, call_math, &math_rl)) {
518 rl = rl_intersection(rl, math_rl);
519 goto cast;
520 }
521
522 /*
523 * For now if we already tried to handle the call math and couldn't
524 * figure it out then bail.
525 */
526 if (jump_to_call_math(c) == c + 1)
527 goto cast;
528
529 rl = filter_by_comparison_call(c, call, &c, rl);
530
531 cast:
532 rl = cast_rl(type, rl);
533 dinfo->value_ranges = rl;
534 }
535
536 void str_to_rl(struct symbol *type, char *value, struct range_list **rl)
537 {
538 struct data_info dinfo = {};
539
540 str_to_dinfo(NULL, type, value, &dinfo);
541 *rl = dinfo.value_ranges;
542 }
543
544 void call_results_to_rl(struct expression *expr, struct symbol *type, char *value, struct range_list **rl)
545 {
546 struct data_info dinfo = {};
547
548 str_to_dinfo(strip_expr(expr), type, value, &dinfo);
549 *rl = dinfo.value_ranges;
550 }
551
552 int is_whole_rl(struct range_list *rl)
553 {
554 struct data_range *drange;
555
556 if (ptr_list_empty(rl))
557 return 0;
558 drange = first_ptr_list((struct ptr_list *)rl);
559 if (sval_is_min(drange->min) && sval_is_max(drange->max))
560 return 1;
561 return 0;
562 }
563
564 int is_whole_rl_non_zero(struct range_list *rl)
565 {
566 struct data_range *drange;
567
568 if (ptr_list_empty(rl))
569 return 0;
570 drange = first_ptr_list((struct ptr_list *)rl);
571 if (sval_unsigned(drange->min) &&
572 drange->min.value == 1 &&
573 sval_is_max(drange->max))
574 return 1;
575 if (!sval_is_min(drange->min) || drange->max.value != -1)
576 return 0;
577 drange = last_ptr_list((struct ptr_list *)rl);
578 if (drange->min.value != 1 || !sval_is_max(drange->max))
579 return 0;
580 return 1;
581 }
582
583 sval_t rl_min(struct range_list *rl)
655 {
656 struct range_list *rl = NULL;
657
658 if (sval_cmp(min, max) > 0)
659 return alloc_whole_rl(min.type);
660
661 add_range(&rl, min, max);
662 return rl;
663 }
664
665 struct range_list *alloc_whole_rl(struct symbol *type)
666 {
667 if (!type || type_positive_bits(type) < 0)
668 type = &llong_ctype;
669 if (type->type == SYM_ARRAY)
670 type = &ptr_ctype;
671
672 return alloc_rl(sval_type_min(type), sval_type_max(type));
673 }
674
675 extern int rl_ptrlist_hack;
676 void add_range(struct range_list **list, sval_t min, sval_t max)
677 {
678 struct data_range *tmp;
679 struct data_range *new = NULL;
680 int check_next = 0;
681
682 /*
683 * There is at least on valid reason why the types might be confusing
684 * and that's when you have a void pointer and on some paths you treat
685 * it as a u8 pointer and on other paths you treat it as a u16 pointer.
686 * This case is hard to deal with.
687 *
688 * There are other cases where we probably should be more specific about
689 * the types than we are. For example, we end up merging a lot of ulong
690 * with pointers and I have not figured out why we do that.
691 *
692 * But this hack works for both cases, I think. We cast it to pointers
693 * or we use the bigger size.
694 *
695 */
696 if (*list && rl_type(*list) != min.type) {
697 if (rl_type(*list)->type == SYM_PTR) {
698 min = sval_cast(rl_type(*list), min);
699 max = sval_cast(rl_type(*list), max);
700 } else if (min.type->type == SYM_PTR) {
701 *list = cast_rl(min.type, *list);
702 } else if (type_bits(rl_type(*list)) >= type_bits(min.type)) {
703 min = sval_cast(rl_type(*list), min);
704 max = sval_cast(rl_type(*list), max);
705 } else {
706 *list = cast_rl(min.type, *list);
707 }
708 }
709
710 if (sval_cmp(min, max) > 0) {
711 min = sval_type_min(min.type);
712 max = sval_type_max(min.type);
713 }
714
715 /*
716 * FIXME: This has a problem merging a range_list like: min-0,3-max
717 * with a range like 1-2. You end up with min-2,3-max instead of
718 * just min-max.
719 */
720 FOR_EACH_PTR(*list, tmp) {
721 if (check_next) {
722 /* Sometimes we overlap with more than one range
723 so we have to delete or modify the next range. */
724 if (!sval_is_max(max) && max.value + 1 == tmp->min.value) {
725 /* join 2 ranges here */
726 new->max = tmp->max;
727 DELETE_CURRENT_PTR(tmp);
728 return;
729 }
730
731 /* Doesn't overlap with the next one. */
732 if (sval_cmp(max, tmp->min) < 0)
733 return;
734
1202 if (!type || type == rl_type(rl))
1203 return rl;
1204
1205 FOR_EACH_PTR(rl, tmp) {
1206 min = tmp->min;
1207 max = tmp->max;
1208 if (type_bits(type) < type_bits(rl_type(rl))) {
1209 min.uvalue = tmp->min.uvalue & ((1ULL << type_bits(type)) - 1);
1210 max.uvalue = tmp->max.uvalue & ((1ULL << type_bits(type)) - 1);
1211 }
1212 if (sval_cmp(min, max) > 0) {
1213 min = sval_cast(type, min);
1214 max = sval_cast(type, max);
1215 }
1216 add_range_t(type, &ret, min, max);
1217 } END_FOR_EACH_PTR(tmp);
1218
1219 return ret;
1220 }
1221
1222 static int rl_is_sane(struct range_list *rl)
1223 {
1224 struct data_range *tmp;
1225 struct symbol *type;
1226
1227 type = rl_type(rl);
1228 FOR_EACH_PTR(rl, tmp) {
1229 if (!sval_fits(type, tmp->min))
1230 return 0;
1231 if (!sval_fits(type, tmp->max))
1232 return 0;
1233 if (sval_cmp(tmp->min, tmp->max) > 0)
1234 return 0;
1235 } END_FOR_EACH_PTR(tmp);
1236
1237 return 1;
1238 }
1239
1240 static int rl_type_consistent(struct range_list *rl)
1241 {
1242 struct data_range *tmp;
1243 struct symbol *type;
1244
1245 type = rl_type(rl);
1246 FOR_EACH_PTR(rl, tmp) {
1247 if (type != tmp->min.type || type != tmp->max.type)
1248 return 0;
1249 } END_FOR_EACH_PTR(tmp);
1250 return 1;
1251 }
1252
1253 static struct range_list *cast_to_bool(struct range_list *rl)
1254 {
1255 struct data_range *tmp;
1256 struct range_list *ret = NULL;
1293 if (!type)
1294 return rl;
1295 if (!rl_is_sane(rl))
1296 return alloc_whole_rl(type);
1297 if (type == rl_type(rl) && rl_type_consistent(rl))
1298 return rl;
1299
1300 if (type == &bool_ctype)
1301 return cast_to_bool(rl);
1302
1303 FOR_EACH_PTR(rl, tmp) {
1304 add_range_t(type, &ret, tmp->min, tmp->max);
1305 } END_FOR_EACH_PTR(tmp);
1306
1307 if (!ret)
1308 return alloc_whole_rl(type);
1309
1310 return ret;
1311 }
1312
1313 struct range_list *rl_invert(struct range_list *orig)
1314 {
1315 struct range_list *ret = NULL;
1316 struct data_range *tmp;
1317 sval_t gap_min, abs_max, sval;
1318
1319 if (!orig)
1320 return NULL;
1321 if (type_bits(rl_type(orig)) < 0) /* void type mostly */
1322 return NULL;
1323
1324 gap_min = sval_type_min(rl_min(orig).type);
1325 abs_max = sval_type_max(rl_max(orig).type);
1326
1327 FOR_EACH_PTR(orig, tmp) {
1328 if (sval_cmp(tmp->min, gap_min) > 0) {
1329 sval = sval_type_val(tmp->min.type, tmp->min.value - 1);
1330 add_range(&ret, gap_min, sval);
1331 }
1332 if (sval_cmp(tmp->max, abs_max) == 0)
1333 return ret;
1334 gap_min = sval_type_val(tmp->max.type, tmp->max.value + 1);
1335 } END_FOR_EACH_PTR(tmp);
1336
1337 if (sval_cmp(gap_min, abs_max) <= 0)
1338 add_range(&ret, gap_min, abs_max);
1339
1340 return ret;
1341 }
1342
1343 struct range_list *rl_filter(struct range_list *rl, struct range_list *filter)
1344 {
1345 struct data_range *tmp;
1346
1347 FOR_EACH_PTR(filter, tmp) {
1348 rl = remove_range(rl, tmp->min, tmp->max);
1349 } END_FOR_EACH_PTR(tmp);
1350
1351 return rl;
1352 }
1353
1354 struct range_list *rl_intersection(struct range_list *one, struct range_list *two)
1355 {
1356 struct range_list *one_orig;
1357 struct range_list *two_orig;
1358 struct range_list *ret;
1359 struct symbol *ret_type;
1360 struct symbol *small_type;
1361 struct symbol *large_type;
1362
1363 if (!two)
1364 return NULL;
1365 if (!one)
1366 return NULL;
1367
1368 one_orig = one;
1369 two_orig = two;
1370
1371 ret_type = rl_type(one);
1372 small_type = rl_type(one);
1373 large_type = rl_type(two);
1374
1375 if (type_bits(rl_type(two)) < type_bits(small_type)) {
1376 small_type = rl_type(two);
1377 large_type = rl_type(one);
1378 }
1379
1380 one = cast_rl(large_type, one);
1381 two = cast_rl(large_type, two);
1382
1383 ret = one;
1384 one = rl_invert(one);
1385 two = rl_invert(two);
1386
1387 ret = rl_filter(ret, one);
1388 ret = rl_filter(ret, two);
1389
1390 one = cast_rl(small_type, one_orig);
1391 two = cast_rl(small_type, two_orig);
1392
1393 one = rl_invert(one);
1394 two = rl_invert(two);
1395
1396 ret = cast_rl(small_type, ret);
1397 ret = rl_filter(ret, one);
1398 ret = rl_filter(ret, two);
1399
1400 return cast_rl(ret_type, ret);
1401 }
1402
1403 static struct range_list *handle_mod_rl(struct range_list *left, struct range_list *right)
1404 {
1405 sval_t zero;
1406 sval_t max;
1407
1408 max = rl_max(right);
1409 if (sval_is_max(max))
1410 return left;
1411 if (max.value == 0)
1412 return NULL;
1413 max.value--;
1414 if (sval_is_negative(max))
1415 return NULL;
1416 if (sval_cmp(rl_max(left), max) < 0)
1417 return left;
1418 zero = max;
1419 zero.value = 0;
1503 struct range_list *ret;
1504
1505 if (is_whole_rl(right))
1506 return NULL;
1507
1508 left_neg = get_neg_rl(left);
1509 left_pos = get_pos_rl(left);
1510 right_neg = get_neg_rl(right);
1511 right_pos = get_pos_rl(right);
1512
1513 neg_neg = divide_rl_helper(left_neg, right_neg);
1514 neg_pos = divide_rl_helper(left_neg, right_pos);
1515 pos_neg = divide_rl_helper(left_pos, right_neg);
1516 pos_pos = divide_rl_helper(left_pos, right_pos);
1517
1518 ret = rl_union(neg_neg, neg_pos);
1519 ret = rl_union(ret, pos_neg);
1520 return rl_union(ret, pos_pos);
1521 }
1522
1523 static struct range_list *handle_add_mult_rl(struct range_list *left, int op, struct range_list *right)
1524 {
1525 sval_t min, max;
1526
1527 if (sval_binop_overflows(rl_min(left), op, rl_min(right)))
1528 return NULL;
1529 min = sval_binop(rl_min(left), op, rl_min(right));
1530
1531 if (sval_binop_overflows(rl_max(left), op, rl_max(right)))
1532 return NULL;
1533 max = sval_binop(rl_max(left), op, rl_max(right));
1534
1535 return alloc_rl(min, max);
1536 }
1537
1538 static struct range_list *handle_sub_rl(struct range_list *left_orig, struct range_list *right_orig)
1539 {
1540 struct range_list *left_rl, *right_rl;
1541 struct symbol *type;
1542 sval_t min, max;
1543 sval_t min_ll, max_ll, res_ll;
1544 sval_t tmp;
1545
1546 /* TODO: These things should totally be using dranges where possible */
1628
1629 static struct range_list *handle_XOR_rl(struct range_list *left, struct range_list *right)
1630 {
1631 unsigned long long left_set, left_maybe;
1632 unsigned long long right_set, right_maybe;
1633 sval_t zero, max;
1634
1635 left_set = rl_bits_always_set(left);
1636 left_maybe = rl_bits_maybe_set(left);
1637
1638 right_set = rl_bits_always_set(right);
1639 right_maybe = rl_bits_maybe_set(right);
1640
1641 zero = max = rl_min(left);
1642 zero.uvalue = 0;
1643 max.uvalue = fls_mask((left_maybe | right_maybe) ^ (left_set & right_set));
1644
1645 return cast_rl(rl_type(left), alloc_rl(zero, max));
1646 }
1647
1648 static struct range_list *handle_AND_rl(struct range_list *left, struct range_list *right)
1649 {
1650 unsigned long long left_set, left_maybe;
1651 unsigned long long right_set, right_maybe;
1652 sval_t zero, max;
1653
1654 return NULL;
1655
1656 left_set = rl_bits_always_set(left);
1657 left_maybe = rl_bits_maybe_set(left);
1658
1659 right_set = rl_bits_always_set(right);
1660 right_maybe = rl_bits_maybe_set(right);
1661
1662 zero = max = rl_min(left);
1663 zero.uvalue = 0;
1664 max.uvalue = fls_mask((left_maybe | right_maybe) ^ (left_set & right_set));
1665
1666 return cast_rl(rl_type(left), alloc_rl(zero, max));
1667 }
1668
1669 struct range_list *rl_binop(struct range_list *left, int op, struct range_list *right)
1670 {
1671 struct symbol *cast_type;
1672 sval_t left_sval, right_sval;
1673 struct range_list *ret = NULL;
1674
1675 cast_type = rl_type(left);
1676 if (sval_type_max(rl_type(left)).uvalue < sval_type_max(rl_type(right)).uvalue)
1677 cast_type = rl_type(right);
1678 if (sval_type_max(cast_type).uvalue < INT_MAX)
1679 cast_type = &int_ctype;
1680
1681 left = cast_rl(cast_type, left);
1682 right = cast_rl(cast_type, right);
1683
1684 if (!left && !right)
1685 return NULL;
1686
1687 if (rl_to_sval(left, &left_sval) && rl_to_sval(right, &right_sval)) {
1688 sval_t val = sval_binop(left_sval, op, right_sval);
1695 break;
1696 case '/':
1697 ret = handle_divide_rl(left, right);
1698 break;
1699 case '*':
1700 case '+':
1701 ret = handle_add_mult_rl(left, op, right);
1702 break;
1703 case '|':
1704 ret = handle_OR_rl(left, right);
1705 break;
1706 case '^':
1707 ret = handle_XOR_rl(left, right);
1708 break;
1709 case '&':
1710 ret = handle_AND_rl(left, right);
1711 break;
1712 case '-':
1713 ret = handle_sub_rl(left, right);
1714 break;
1715 /* FIXME: Do the rest as well */
1716 case SPECIAL_RIGHTSHIFT:
1717 case SPECIAL_LEFTSHIFT:
1718 break;
1719 }
1720
1721 return ret;
1722 }
1723
1724 void free_data_info_allocs(void)
1725 {
1726 struct allocator_struct *desc = &data_info_allocator;
1727 struct allocation_blob *blob = desc->blobs;
1728
1729 free_all_rl();
1730 clear_math_cache();
1731
1732 desc->blobs = NULL;
1733 desc->allocations = 0;
1734 desc->total_bytes = 0;
1735 desc->useful_bytes = 0;
1736 desc->freelist = NULL;
1737 while (blob) {
1738 struct allocation_blob *next = blob->next;
|
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see http://www.gnu.org/copyleft/gpl.txt
16 */
17
18 #include "parse.h"
19 #include "smatch.h"
20 #include "smatch_extra.h"
21 #include "smatch_slist.h"
22
23 ALLOCATOR(data_info, "smatch extra data");
24 ALLOCATOR(data_range, "data range");
25 __DO_ALLOCATOR(struct data_range, sizeof(struct data_range), __alignof__(struct data_range),
26 "permanent ranges", perm_data_range);
27 __DECLARE_ALLOCATOR(struct ptr_list, rl_ptrlist);
28
29 static bool is_err_ptr(sval_t sval)
30 {
31 if (option_project != PROJ_KERNEL)
32 return false;
33 if (!type_is_ptr(sval.type))
34 return false;
35 if (sval.uvalue < -4095ULL)
36 return false;
37 return true;
38 }
39
40 static char *get_err_pointer_str(struct data_range *drange)
41 {
42 static char buf[20];
43
44 /*
45 * The kernel has error pointers where you do essentially:
46 *
47 * return (void *)(unsigned long)-12;
48 *
49 * But what I want here is to print -12 instead of the unsigned version
50 * of that.
51 *
52 */
53 if (!is_err_ptr(drange->min))
54 return NULL;
55
56 if (drange->min.value == drange->max.value)
57 snprintf(buf, sizeof(buf), "(%lld)", drange->min.value);
58 else
59 snprintf(buf, sizeof(buf), "(%lld)-(%lld)", drange->min.value, drange->max.value);
60 return buf;
61 }
62
63 char *show_rl(struct range_list *list)
64 {
65 struct data_range *prev_drange = NULL;
66 struct data_range *tmp;
67 char full[255];
68 char *p = full;
69 char *prev = full;
70 char *err_ptr;
71 int remain;
72 int i = 0;
73
74 full[0] = '\0';
75
76 FOR_EACH_PTR(list, tmp) {
77 remain = full + sizeof(full) - p;
78 if (remain < 48) {
79 snprintf(prev, full + sizeof(full) - prev, ",%s-%s",
80 sval_to_str(prev_drange->min),
81 sval_to_str(sval_type_max(prev_drange->min.type)));
82 break;
83 }
84 prev_drange = tmp;
85 prev = p;
86
87 err_ptr = get_err_pointer_str(tmp);
88 if (err_ptr) {
89 p += snprintf(p, remain, "%s%s", i++ ? "," : "", err_ptr);
90 } else if (sval_cmp(tmp->min, tmp->max) == 0) {
91 p += snprintf(p, remain, "%s%s", i++ ? "," : "",
92 sval_to_str(tmp->min));
93 } else {
94 p += snprintf(p, remain, "%s%s-%s", i++ ? "," : "",
95 sval_to_str(tmp->min),
96 sval_to_str(tmp->max));
97 }
98 } END_FOR_EACH_PTR(tmp);
99
100 return alloc_sname(full);
101 }
102
103 void free_all_rl(void)
104 {
105 clear_rl_ptrlist_alloc();
106 }
107
108 static int sval_too_big(struct symbol *type, sval_t sval)
109 {
110 if (type_bits(type) >= 32 &&
111 type_bits(sval.type) <= type_bits(type))
112 return 0;
113 if (sval.uvalue <= ((1ULL << type_bits(type)) - 1))
114 return 0;
115 if (type_signed(sval.type)) {
116 if (type_unsigned(type)) {
117 unsigned long long neg = ~sval.uvalue;
118 if (neg <= sval_type_max(type).uvalue)
119 return 0;
120 }
121 if (sval.value < sval_type_min(type).value)
122 return 1;
123 if (sval.value > sval_type_max(type).value)
124 return 1;
125 return 0;
126 }
127 if (sval.uvalue > sval_type_max(type).uvalue)
128 return 1;
129 return 0;
130 }
131
132 static int truncates_nicely(struct symbol *type, sval_t min, sval_t max)
133 {
134 unsigned long long mask;
135 int bits = type_bits(type);
136
137 if (bits >= type_bits(min.type))
138 return 0;
139
140 mask = -1ULL << bits;
141 return (min.uvalue & mask) == (max.uvalue & mask);
142 }
143
144 static void add_range_t(struct symbol *type, struct range_list **rl, sval_t min, sval_t max)
145 {
146 /* If we're just adding a number, cast it and add it */
147 if (sval_cmp(min, max) == 0) {
148 add_range(rl, sval_cast(type, min), sval_cast(type, max));
149 return;
150 }
151
152 /* If the range is within the type range then add it */
153 if (sval_fits(type, min) && sval_fits(type, max)) {
154 add_range(rl, sval_cast(type, min), sval_cast(type, max));
155 return;
156 }
157
158 if (truncates_nicely(type, min, max)) {
159 add_range(rl, sval_cast(type, min), sval_cast(type, max));
160 return;
161 }
162
163 /*
164 * If the range we are adding has more bits than the range type then
165 * add the whole range type. Eg:
166 * 0x8000000000000000 - 0xf000000000000000 -> cast to int
167 *
168 */
169 if (sval_too_big(type, min) || sval_too_big(type, max)) {
170 add_range(rl, sval_type_min(type), sval_type_max(type));
171 return;
172 }
173
174 /* Cast negative values to high positive values */
175 if (sval_is_negative(min) && type_unsigned(type)) {
176 if (sval_is_positive(max)) {
177 if (sval_too_high(type, max)) {
178 add_range(rl, sval_type_min(type), sval_type_max(type));
179 return;
180 }
181 add_range(rl, sval_type_val(type, 0), sval_cast(type, max));
182 max = sval_type_max(type);
183 } else {
184 max = sval_cast(type, max);
185 }
186 min = sval_cast(type, min);
187 add_range(rl, min, max);
188 }
189
190 /* Cast high positive numbers to negative */
191 if (sval_unsigned(max) && sval_is_negative(sval_cast(type, max))) {
192 if (!sval_is_negative(sval_cast(type, min))) {
193 add_range(rl, sval_cast(type, min), sval_type_max(type));
194 min = sval_type_min(type);
195 } else {
196 min = sval_cast(type, min);
197 }
198 max = sval_cast(type, max);
199 add_range(rl, min, max);
200 }
201
202 add_range(rl, sval_cast(type, min), sval_cast(type, max));
203 return;
204 }
205
206 static int str_to_comparison_arg_helper(const char *str,
207 struct expression *call, int *comparison,
208 struct expression **arg, const char **endp)
209 {
210 int param;
211 const char *c = str;
212
213 if (*c != '[')
214 return 0;
215 c++;
216
217 if (*c == '<') {
218 c++;
219 if (*c == '=') {
220 *comparison = SPECIAL_LTE;
221 c++;
222 } else {
223 *comparison = '<';
224 }
225 } else if (*c == '=') {
226 c++;
227 c++;
228 *comparison = SPECIAL_EQUAL;
229 } else if (*c == '>') {
230 c++;
231 if (*c == '=') {
232 *comparison = SPECIAL_GTE;
233 c++;
234 } else {
235 *comparison = '>';
236 }
237 } else if (*c == '!') {
238 c++;
239 c++;
240 *comparison = SPECIAL_NOTEQUAL;
241 } else if (*c == '$') {
242 *comparison = SPECIAL_EQUAL;
243 } else {
244 return 0;
245 }
246
247 if (*c != '$')
248 return 0;
249 c++;
250
251 param = strtoll(c, (char **)&c, 10);
252 if (*c == ',' || *c == ']')
253 c++; /* skip the ']' character */
254 if (endp)
255 *endp = (char *)c;
256
257 if (!call)
258 return 0;
259 *arg = get_argument_from_call_expr(call->args, param);
260 if (!*arg)
261 return 0;
262 if (*c == '-' && *(c + 1) == '>') {
263 char buf[256];
264 int n;
265
266 n = snprintf(buf, sizeof(buf), "$%s", c);
267 if (n >= sizeof(buf))
268 return 0;
269 if (buf[n - 1] == ']')
270 buf[n - 1] = '\0';
271 *arg = gen_expression_from_key(*arg, buf);
272 while (*c && *c != ']')
273 c++;
274 }
275 return 1;
276 }
277
278 int str_to_comparison_arg(const char *str, struct expression *call, int *comparison, struct expression **arg)
279 {
280 while (1) {
281 if (!*str)
282 return 0;
283 if (*str == '[')
284 break;
285 str++;
286 }
287 return str_to_comparison_arg_helper(str, call, comparison, arg, NULL);
288 }
289
290 static int get_val_from_key(int use_max, struct symbol *type, const char *c, struct expression *call, const char **endp, sval_t *sval)
291 {
292 struct expression *arg;
293 int comparison;
294 sval_t ret, tmp;
295
296 if (use_max)
297 ret = sval_type_max(type);
298 else
299 ret = sval_type_min(type);
300
301 if (!str_to_comparison_arg_helper(c, call, &comparison, &arg, endp)) {
302 *sval = ret;
303 return 0;
304 }
305
306 if (use_max && get_implied_max(arg, &tmp)) {
307 ret = tmp;
308 if (comparison == '<') {
309 tmp.value = 1;
310 ret = sval_binop(ret, '-', tmp);
370 case SPECIAL_UNSIGNED_GTE:
371 if (!sval_is_min(rl_min(right_orig)))
372 ret_rl = remove_range(left_orig, min, sub_one(rl_min(right_orig)));
373 break;
374 case '>':
375 case SPECIAL_UNSIGNED_GT:
376 ret_rl = remove_range(left_orig, min, rl_min(right_orig));
377 break;
378 case SPECIAL_NOTEQUAL:
379 if (sval_cmp(rl_min(right_orig), rl_max(right_orig)) == 0)
380 ret_rl = remove_range(left_orig, rl_min(right_orig), rl_min(right_orig));
381 break;
382 default:
383 sm_perror("unhandled comparison %s", show_special(comparison));
384 return;
385 }
386
387 *rl = cast_rl(rl_type(*rl), ret_rl);
388 }
389
390 static struct range_list *filter_by_comparison_call(const char *c, struct expression *call, const char **endp, struct range_list *start_rl)
391 {
392 struct symbol *type;
393 struct expression *arg;
394 struct range_list *casted_start, *right_orig;
395 int comparison;
396
397 if (!str_to_comparison_arg_helper(c, call, &comparison, &arg, endp))
398 return start_rl;
399
400 if (!get_implied_rl(arg, &right_orig))
401 return start_rl;
402
403 type = &int_ctype;
404 if (type_positive_bits(rl_type(start_rl)) > type_positive_bits(type))
405 type = rl_type(start_rl);
406 if (type_positive_bits(rl_type(right_orig)) > type_positive_bits(type))
407 type = rl_type(right_orig);
408
409 casted_start = cast_rl(type, start_rl);
410 right_orig = cast_rl(type, right_orig);
411
412 filter_by_comparison(&casted_start, comparison, right_orig);
413 return cast_rl(rl_type(start_rl), casted_start);
414 }
415
416 static sval_t parse_val(int use_max, struct expression *call, struct symbol *type, const char *c, const char **endp)
417 {
418 const char *start = c;
419 sval_t ret;
420
421 if (!strncmp(start, "max", 3)) {
422 ret = sval_type_max(type);
423 c += 3;
424 } else if (!strncmp(start, "u64max", 6)) {
425 ret = sval_type_val(type, ULLONG_MAX);
426 c += 6;
427 } else if (!strncmp(start, "s64max", 6)) {
428 ret = sval_type_val(type, LLONG_MAX);
429 c += 6;
430 } else if (!strncmp(start, "u32max", 6)) {
431 ret = sval_type_val(type, UINT_MAX);
432 c += 6;
433 } else if (!strncmp(start, "s32max", 6)) {
434 ret = sval_type_val(type, INT_MAX);
435 c += 6;
436 } else if (!strncmp(start, "u16max", 6)) {
437 ret = sval_type_val(type, USHRT_MAX);
438 c += 6;
450 c += 6;
451 } else if (!strncmp(start, "s16min", 6)) {
452 ret = sval_type_val(type, SHRT_MIN);
453 c += 6;
454 } else if (!strncmp(start, "long_min", 8)) {
455 ret = sval_type_val(type, LONG_MIN);
456 c += 8;
457 } else if (!strncmp(start, "long_max", 8)) {
458 ret = sval_type_val(type, LONG_MAX);
459 c += 8;
460 } else if (!strncmp(start, "ulong_max", 9)) {
461 ret = sval_type_val(type, ULONG_MAX);
462 c += 9;
463 } else if (!strncmp(start, "ptr_max", 7)) {
464 ret = sval_type_val(type, valid_ptr_max);
465 c += 7;
466 } else if (start[0] == '[') {
467 /* this parses [==p0] comparisons */
468 get_val_from_key(1, type, start, call, &c, &ret);
469 } else if (type_positive_bits(type) == 64) {
470 ret = sval_type_val(type, strtoull(start, (char **)&c, 0));
471 } else {
472 ret = sval_type_val(type, strtoll(start, (char **)&c, 0));
473 }
474 *endp = c;
475 return ret;
476 }
477
478 static const char *jump_to_call_math(const char *value)
479 {
480 const char *c = value;
481
482 while (*c && *c != '[')
483 c++;
484
485 if (!*c)
486 return NULL;
487 c++;
488 if (*c == '<' || *c == '=' || *c == '>' || *c == '!')
489 return NULL;
490
491 return c;
492 }
493
494 static void str_to_rl_helper(struct expression *call, struct symbol *type, const char *str, const char **endp, struct range_list **rl)
495 {
496 struct range_list *rl_tmp = NULL;
497 sval_t prev_min, min, max;
498 const char *c;
499
500 prev_min = sval_type_min(type);
501 min = sval_type_min(type);
502 max = sval_type_max(type);
503 c = str;
504 while (*c != '\0' && *c != '[') {
505 if (*c == '+') {
506 if (sval_cmp(min, sval_type_min(type)) != 0)
507 min = max;
508 max = sval_type_max(type);
509 add_range_t(type, &rl_tmp, min, max);
510 break;
511 }
512 if (*c == '(')
513 c++;
514 min = parse_val(0, call, type, c, &c);
515 if (!sval_fits(type, min))
516 min = sval_type_min(type);
517 max = min;
518 if (*c == ')')
519 c++;
520 if (*c == '\0' || *c == '[') {
521 add_range_t(type, &rl_tmp, min, min);
522 break;
523 }
524 if (*c == ',') {
525 add_range_t(type, &rl_tmp, min, min);
526 c++;
527 continue;
528 }
529 if (*c == '+') {
530 min = prev_min;
531 max = sval_type_max(type);
532 add_range_t(type, &rl_tmp, min, max);
533 c++;
534 if (*c == '[' || *c == '\0')
535 break;
536 }
537 if (*c != '-') {
538 sm_msg("debug XXX: trouble parsing %s c = %s", str, c);
539 break;
540 }
541 c++;
542 if (*c == '(')
543 c++;
544 max = parse_val(1, call, type, c, &c);
545 if (!sval_fits(type, max))
546 max = sval_type_max(type);
547 if (*c == '+') {
548 max = sval_type_max(type);
549 add_range_t(type, &rl_tmp, min, max);
550 c++;
551 if (*c == '[' || *c == '\0')
552 break;
553 }
554 prev_min = max;
555 add_range_t(type, &rl_tmp, min, max);
556 if (*c == ')')
557 c++;
558 if (*c == ',')
559 c++;
560 }
561
562 *rl = rl_tmp;
563 *endp = c;
564 }
565
566 static void str_to_dinfo(struct expression *call, struct symbol *type, const char *value, struct data_info *dinfo)
567 {
568 struct range_list *math_rl;
569 const char *call_math;
570 const char *c;
571 struct range_list *rl = NULL;
572
573 if (!type)
574 type = &llong_ctype;
575
576 if (strcmp(value, "empty") == 0)
577 return;
578
579 if (strncmp(value, "[==$", 4) == 0) {
580 struct expression *arg;
581 int comparison;
582
583 if (!str_to_comparison_arg(value, call, &comparison, &arg))
584 return;
585 if (!get_implied_rl(arg, &rl))
586 return;
587 goto cast;
588 }
589
590 str_to_rl_helper(call, type, value, &c, &rl);
594 call_math = jump_to_call_math(value);
595 if (call_math && parse_call_math_rl(call, call_math, &math_rl)) {
596 rl = rl_intersection(rl, math_rl);
597 goto cast;
598 }
599
600 /*
601 * For now if we already tried to handle the call math and couldn't
602 * figure it out then bail.
603 */
604 if (jump_to_call_math(c) == c + 1)
605 goto cast;
606
607 rl = filter_by_comparison_call(c, call, &c, rl);
608
609 cast:
610 rl = cast_rl(type, rl);
611 dinfo->value_ranges = rl;
612 }
613
614 static int rl_is_sane(struct range_list *rl)
615 {
616 struct data_range *tmp;
617 struct symbol *type;
618
619 type = rl_type(rl);
620 FOR_EACH_PTR(rl, tmp) {
621 if (!sval_fits(type, tmp->min))
622 return 0;
623 if (!sval_fits(type, tmp->max))
624 return 0;
625 if (sval_cmp(tmp->min, tmp->max) > 0)
626 return 0;
627 } END_FOR_EACH_PTR(tmp);
628
629 return 1;
630 }
631
632 void str_to_rl(struct symbol *type, char *value, struct range_list **rl)
633 {
634 struct data_info dinfo = {};
635
636 str_to_dinfo(NULL, type, value, &dinfo);
637 if (!rl_is_sane(dinfo.value_ranges))
638 dinfo.value_ranges = alloc_whole_rl(type);
639 *rl = dinfo.value_ranges;
640 }
641
642 void call_results_to_rl(struct expression *expr, struct symbol *type, const char *value, struct range_list **rl)
643 {
644 struct data_info dinfo = {};
645
646 str_to_dinfo(strip_expr(expr), type, value, &dinfo);
647 *rl = dinfo.value_ranges;
648 }
649
650 int is_whole_rl(struct range_list *rl)
651 {
652 struct data_range *drange;
653
654 if (ptr_list_empty(rl))
655 return 0;
656 drange = first_ptr_list((struct ptr_list *)rl);
657 if (sval_is_min(drange->min) && sval_is_max(drange->max))
658 return 1;
659 return 0;
660 }
661
662 int is_unknown_ptr(struct range_list *rl)
663 {
664 struct data_range *drange;
665 int cnt = 0;
666
667 if (is_whole_rl(rl))
668 return 1;
669
670 FOR_EACH_PTR(rl, drange) {
671 if (++cnt >= 3)
672 return 0;
673 if (sval_cmp(drange->min, valid_ptr_min_sval) == 0 &&
674 sval_cmp(drange->max, valid_ptr_max_sval) == 0)
675 return 1;
676 } END_FOR_EACH_PTR(drange);
677
678 return 0;
679 }
680
681 int is_whole_rl_non_zero(struct range_list *rl)
682 {
683 struct data_range *drange;
684
685 if (ptr_list_empty(rl))
686 return 0;
687 drange = first_ptr_list((struct ptr_list *)rl);
688 if (sval_unsigned(drange->min) &&
689 drange->min.value == 1 &&
690 sval_is_max(drange->max))
691 return 1;
692 if (!sval_is_min(drange->min) || drange->max.value != -1)
693 return 0;
694 drange = last_ptr_list((struct ptr_list *)rl);
695 if (drange->min.value != 1 || !sval_is_max(drange->max))
696 return 0;
697 return 1;
698 }
699
700 sval_t rl_min(struct range_list *rl)
772 {
773 struct range_list *rl = NULL;
774
775 if (sval_cmp(min, max) > 0)
776 return alloc_whole_rl(min.type);
777
778 add_range(&rl, min, max);
779 return rl;
780 }
781
782 struct range_list *alloc_whole_rl(struct symbol *type)
783 {
784 if (!type || type_positive_bits(type) < 0)
785 type = &llong_ctype;
786 if (type->type == SYM_ARRAY)
787 type = &ptr_ctype;
788
789 return alloc_rl(sval_type_min(type), sval_type_max(type));
790 }
791
792 static bool collapse_pointer_rl(struct range_list **rl, sval_t min, sval_t max)
793 {
794 struct range_list *new_rl = NULL;
795 struct data_range *tmp;
796 static bool recurse;
797 bool ret = false;
798 int cnt = 0;
799
800 /*
801 * With the mtag work, then we end up getting huge lists of mtags.
802 * That seems cool, but the problem is that we can only store about
803 * 8-10 mtags in the DB before we truncate the list. Also the mtags
804 * aren't really used at all so it's a waste of resources for now...
805 * In the future, we maybe will revisit this code.
806 *
807 */
808
809 if (recurse)
810 return false;
811 recurse = true;
812 if (!type_is_ptr(min.type))
813 goto out;
814
815 if (ptr_list_size((struct ptr_list *)*rl) < 8)
816 goto out;
817 FOR_EACH_PTR(*rl, tmp) {
818 if (!is_err_ptr(tmp->min))
819 cnt++;
820 } END_FOR_EACH_PTR(tmp);
821 if (cnt < 8)
822 goto out;
823
824 FOR_EACH_PTR(*rl, tmp) {
825 if (sval_cmp(tmp->min, valid_ptr_min_sval) >= 0 &&
826 sval_cmp(tmp->max, valid_ptr_max_sval) <= 0)
827 add_range(&new_rl, valid_ptr_min_sval, valid_ptr_max_sval);
828 else
829 add_range(&new_rl, tmp->min, tmp->max);
830 } END_FOR_EACH_PTR(tmp);
831
832 add_range(&new_rl, min, max);
833
834 *rl = new_rl;
835 ret = true;
836 out:
837 recurse = false;
838 return ret;
839 }
840
841 extern int rl_ptrlist_hack;
842 void add_range(struct range_list **list, sval_t min, sval_t max)
843 {
844 struct data_range *tmp;
845 struct data_range *new = NULL;
846 int check_next = 0;
847
848 /*
849 * There is at least on valid reason why the types might be confusing
850 * and that's when you have a void pointer and on some paths you treat
851 * it as a u8 pointer and on other paths you treat it as a u16 pointer.
852 * This case is hard to deal with.
853 *
854 * There are other cases where we probably should be more specific about
855 * the types than we are. For example, we end up merging a lot of ulong
856 * with pointers and I have not figured out why we do that.
857 *
858 * But this hack works for both cases, I think. We cast it to pointers
859 * or we use the bigger size.
860 *
861 */
862 if (*list && rl_type(*list) != min.type) {
863 if (rl_type(*list)->type == SYM_PTR) {
864 min = sval_cast(rl_type(*list), min);
865 max = sval_cast(rl_type(*list), max);
866 } else if (min.type->type == SYM_PTR) {
867 *list = cast_rl(min.type, *list);
868 } else if (type_bits(rl_type(*list)) >= type_bits(min.type)) {
869 min = sval_cast(rl_type(*list), min);
870 max = sval_cast(rl_type(*list), max);
871 } else {
872 *list = cast_rl(min.type, *list);
873 }
874 }
875
876 if (sval_cmp(min, max) > 0) {
877 min = sval_type_min(min.type);
878 max = sval_type_max(min.type);
879 }
880
881 if (collapse_pointer_rl(list, min, max))
882 return;
883
884 /*
885 * FIXME: This has a problem merging a range_list like: min-0,3-max
886 * with a range like 1-2. You end up with min-2,3-max instead of
887 * just min-max.
888 */
889 FOR_EACH_PTR(*list, tmp) {
890 if (check_next) {
891 /* Sometimes we overlap with more than one range
892 so we have to delete or modify the next range. */
893 if (!sval_is_max(max) && max.value + 1 == tmp->min.value) {
894 /* join 2 ranges here */
895 new->max = tmp->max;
896 DELETE_CURRENT_PTR(tmp);
897 return;
898 }
899
900 /* Doesn't overlap with the next one. */
901 if (sval_cmp(max, tmp->min) < 0)
902 return;
903
1371 if (!type || type == rl_type(rl))
1372 return rl;
1373
1374 FOR_EACH_PTR(rl, tmp) {
1375 min = tmp->min;
1376 max = tmp->max;
1377 if (type_bits(type) < type_bits(rl_type(rl))) {
1378 min.uvalue = tmp->min.uvalue & ((1ULL << type_bits(type)) - 1);
1379 max.uvalue = tmp->max.uvalue & ((1ULL << type_bits(type)) - 1);
1380 }
1381 if (sval_cmp(min, max) > 0) {
1382 min = sval_cast(type, min);
1383 max = sval_cast(type, max);
1384 }
1385 add_range_t(type, &ret, min, max);
1386 } END_FOR_EACH_PTR(tmp);
1387
1388 return ret;
1389 }
1390
1391 int rl_fits_in_type(struct range_list *rl, struct symbol *type)
1392 {
1393 if (type_bits(rl_type(rl)) <= type_bits(type))
1394 return 1;
1395 if (sval_cmp(rl_max(rl), sval_type_max(type)) > 0)
1396 return 0;
1397 if (sval_is_negative(rl_min(rl)) &&
1398 sval_cmp(rl_min(rl), sval_type_min(type)) < 0)
1399 return 0;
1400 return 1;
1401 }
1402
1403 static int rl_type_consistent(struct range_list *rl)
1404 {
1405 struct data_range *tmp;
1406 struct symbol *type;
1407
1408 type = rl_type(rl);
1409 FOR_EACH_PTR(rl, tmp) {
1410 if (type != tmp->min.type || type != tmp->max.type)
1411 return 0;
1412 } END_FOR_EACH_PTR(tmp);
1413 return 1;
1414 }
1415
1416 static struct range_list *cast_to_bool(struct range_list *rl)
1417 {
1418 struct data_range *tmp;
1419 struct range_list *ret = NULL;
1456 if (!type)
1457 return rl;
1458 if (!rl_is_sane(rl))
1459 return alloc_whole_rl(type);
1460 if (type == rl_type(rl) && rl_type_consistent(rl))
1461 return rl;
1462
1463 if (type == &bool_ctype)
1464 return cast_to_bool(rl);
1465
1466 FOR_EACH_PTR(rl, tmp) {
1467 add_range_t(type, &ret, tmp->min, tmp->max);
1468 } END_FOR_EACH_PTR(tmp);
1469
1470 if (!ret)
1471 return alloc_whole_rl(type);
1472
1473 return ret;
1474 }
1475
1476 struct range_list *rl_filter(struct range_list *rl, struct range_list *filter)
1477 {
1478 struct data_range *tmp;
1479
1480 FOR_EACH_PTR(filter, tmp) {
1481 rl = remove_range(rl, tmp->min, tmp->max);
1482 } END_FOR_EACH_PTR(tmp);
1483
1484 return rl;
1485 }
1486
1487 struct range_list *do_intersection(struct range_list *one_rl, struct range_list *two_rl)
1488 {
1489 struct data_range *one, *two;
1490 struct range_list *ret = NULL;
1491
1492
1493 PREPARE_PTR_LIST(one_rl, one);
1494 PREPARE_PTR_LIST(two_rl, two);
1495
1496 while (true) {
1497 if (!one || !two)
1498 break;
1499 if (sval_cmp(one->max, two->min) < 0) {
1500 NEXT_PTR_LIST(one);
1501 continue;
1502 }
1503 if (sval_cmp(one->min, two->min) < 0 && sval_cmp(one->max, two->max) <= 0) {
1504 add_range(&ret, two->min, one->max);
1505 NEXT_PTR_LIST(one);
1506 continue;
1507 }
1508 if (sval_cmp(one->min, two->min) >= 0 && sval_cmp(one->max, two->max) <= 0) {
1509 add_range(&ret, one->min, one->max);
1510 NEXT_PTR_LIST(one);
1511 continue;
1512 }
1513 if (sval_cmp(one->min, two->min) < 0 && sval_cmp(one->max, two->max) > 0) {
1514 add_range(&ret, two->min, two->max);
1515 NEXT_PTR_LIST(two);
1516 continue;
1517 }
1518 if (sval_cmp(one->min, two->max) <= 0 && sval_cmp(one->max, two->max) > 0) {
1519 add_range(&ret, one->min, two->max);
1520 NEXT_PTR_LIST(two);
1521 continue;
1522 }
1523 if (sval_cmp(one->min, two->max) <= 0) {
1524 sm_fatal("error calculating intersection of '%s' and '%s'", show_rl(one_rl), show_rl(two_rl));
1525 return NULL;
1526 }
1527 NEXT_PTR_LIST(two);
1528 }
1529
1530 FINISH_PTR_LIST(two);
1531 FINISH_PTR_LIST(one);
1532
1533 return ret;
1534 }
1535
1536 struct range_list *rl_intersection(struct range_list *one, struct range_list *two)
1537 {
1538 struct range_list *ret;
1539 struct symbol *ret_type;
1540 struct symbol *small_type;
1541 struct symbol *large_type;
1542
1543 if (!one || !two)
1544 return NULL;
1545
1546 ret_type = rl_type(one);
1547 small_type = rl_type(one);
1548 large_type = rl_type(two);
1549
1550 if (type_bits(rl_type(two)) < type_bits(small_type)) {
1551 small_type = rl_type(two);
1552 large_type = rl_type(one);
1553 }
1554
1555 one = cast_rl(large_type, one);
1556 two = cast_rl(large_type, two);
1557
1558 ret = do_intersection(one, two);
1559 return cast_rl(ret_type, ret);
1560 }
1561
1562 static struct range_list *handle_mod_rl(struct range_list *left, struct range_list *right)
1563 {
1564 sval_t zero;
1565 sval_t max;
1566
1567 max = rl_max(right);
1568 if (sval_is_max(max))
1569 return left;
1570 if (max.value == 0)
1571 return NULL;
1572 max.value--;
1573 if (sval_is_negative(max))
1574 return NULL;
1575 if (sval_cmp(rl_max(left), max) < 0)
1576 return left;
1577 zero = max;
1578 zero.value = 0;
1662 struct range_list *ret;
1663
1664 if (is_whole_rl(right))
1665 return NULL;
1666
1667 left_neg = get_neg_rl(left);
1668 left_pos = get_pos_rl(left);
1669 right_neg = get_neg_rl(right);
1670 right_pos = get_pos_rl(right);
1671
1672 neg_neg = divide_rl_helper(left_neg, right_neg);
1673 neg_pos = divide_rl_helper(left_neg, right_pos);
1674 pos_neg = divide_rl_helper(left_pos, right_neg);
1675 pos_pos = divide_rl_helper(left_pos, right_pos);
1676
1677 ret = rl_union(neg_neg, neg_pos);
1678 ret = rl_union(ret, pos_neg);
1679 return rl_union(ret, pos_pos);
1680 }
1681
1682 static struct range_list *ptr_add_mult(struct range_list *left, int op, struct range_list *right)
1683 {
1684 struct range_list *ret;
1685 sval_t l_sval, r_sval, res;
1686
1687 /*
1688 * This function is sort of the wrong API because it takes two pointer
1689 * and adds them together. The caller is expected to figure out
1690 * alignment. Neither of those are the correct things to do.
1691 *
1692 * Really this function is quite bogus...
1693 */
1694
1695 if (rl_to_sval(left, &l_sval) && rl_to_sval(right, &r_sval)) {
1696 res = sval_binop(l_sval, op, r_sval);
1697 return alloc_rl(res, res);
1698 }
1699
1700 if (rl_min(left).value != 0 || rl_max(right).value != 0) {
1701 ret = alloc_rl(valid_ptr_min_sval, valid_ptr_max_sval);
1702 return cast_rl(rl_type(left), ret);
1703 }
1704
1705 return alloc_whole_rl(rl_type(left));
1706 }
1707
1708 static struct range_list *handle_add_mult_rl(struct range_list *left, int op, struct range_list *right)
1709 {
1710 sval_t min, max;
1711
1712 if (type_is_ptr(rl_type(left)) || type_is_ptr(rl_type(right)))
1713 return ptr_add_mult(left, op, right);
1714
1715 if (sval_binop_overflows(rl_min(left), op, rl_min(right)))
1716 return NULL;
1717 min = sval_binop(rl_min(left), op, rl_min(right));
1718
1719 if (sval_binop_overflows(rl_max(left), op, rl_max(right)))
1720 return NULL;
1721 max = sval_binop(rl_max(left), op, rl_max(right));
1722
1723 return alloc_rl(min, max);
1724 }
1725
1726 static struct range_list *handle_sub_rl(struct range_list *left_orig, struct range_list *right_orig)
1727 {
1728 struct range_list *left_rl, *right_rl;
1729 struct symbol *type;
1730 sval_t min, max;
1731 sval_t min_ll, max_ll, res_ll;
1732 sval_t tmp;
1733
1734 /* TODO: These things should totally be using dranges where possible */
1816
1817 static struct range_list *handle_XOR_rl(struct range_list *left, struct range_list *right)
1818 {
1819 unsigned long long left_set, left_maybe;
1820 unsigned long long right_set, right_maybe;
1821 sval_t zero, max;
1822
1823 left_set = rl_bits_always_set(left);
1824 left_maybe = rl_bits_maybe_set(left);
1825
1826 right_set = rl_bits_always_set(right);
1827 right_maybe = rl_bits_maybe_set(right);
1828
1829 zero = max = rl_min(left);
1830 zero.uvalue = 0;
1831 max.uvalue = fls_mask((left_maybe | right_maybe) ^ (left_set & right_set));
1832
1833 return cast_rl(rl_type(left), alloc_rl(zero, max));
1834 }
1835
1836 static sval_t sval_lowest_set_bit(sval_t sval)
1837 {
1838 sval_t ret = { .type = sval.type };
1839 int i;
1840
1841 for (i = 0; i < 64; i++) {
1842 if (sval.uvalue & 1ULL << i) {
1843 ret.uvalue = (1ULL << i);
1844 return ret;
1845 }
1846 }
1847 return ret;
1848 }
1849
1850 static struct range_list *handle_AND_rl_sval(struct range_list *rl, sval_t sval)
1851 {
1852 struct range_list *known_rl;
1853 sval_t zero = { 0 };
1854 sval_t min;
1855
1856 zero.type = sval.type;
1857 zero.value = 0;
1858
1859 if (sm_fls64(rl_max(rl).uvalue) < find_first_zero_bit(sval.uvalue) &&
1860 sm_fls64(rl_min(rl).uvalue) < find_first_zero_bit(sval.uvalue))
1861 return rl;
1862
1863 min = sval_lowest_set_bit(sval);
1864
1865 if (min.value != 0) {
1866 sval_t max, mod;
1867
1868 max = rl_max(rl);
1869 mod = sval_binop(max, '%', min);
1870 if (mod.value) {
1871 max = sval_binop(max, '-', mod);
1872 max.value++;
1873 if (max.value > 0 && sval_cmp(max, rl_max(rl)) < 0)
1874 rl = remove_range(rl, max, rl_max(rl));
1875 }
1876 }
1877
1878 known_rl = alloc_rl(min, sval);
1879
1880 rl = rl_intersection(rl, known_rl);
1881 zero = rl_min(rl);
1882 zero.value = 0;
1883 add_range(&rl, zero, zero);
1884
1885 return rl;
1886 }
1887
1888 static struct range_list *fudge_AND_rl(struct range_list *rl)
1889 {
1890 struct range_list *ret;
1891 sval_t min;
1892
1893 min = sval_lowest_set_bit(rl_min(rl));
1894 ret = clone_rl(rl);
1895 add_range(&ret, min, rl_min(rl));
1896
1897 return ret;
1898 }
1899
1900 static struct range_list *handle_AND_rl(struct range_list *left, struct range_list *right)
1901 {
1902 sval_t sval, zero;
1903 struct range_list *rl;
1904
1905 if (rl_to_sval(left, &sval))
1906 return handle_AND_rl_sval(right, sval);
1907 if (rl_to_sval(right, &sval))
1908 return handle_AND_rl_sval(left, sval);
1909
1910 left = fudge_AND_rl(left);
1911 right = fudge_AND_rl(right);
1912
1913 rl = rl_intersection(left, right);
1914 zero = rl_min(rl);
1915 zero.value = 0;
1916 add_range(&rl, zero, zero);
1917
1918 return rl;
1919 }
1920
1921 static struct range_list *handle_lshift(struct range_list *left_orig, struct range_list *right_orig)
1922 {
1923 struct range_list *left;
1924 struct data_range *tmp;
1925 struct range_list *ret = NULL;
1926 sval_t zero = { .type = rl_type(left_orig), };
1927 sval_t shift, min, max;
1928 bool add_zero = false;
1929
1930 if (!rl_to_sval(right_orig, &shift) || sval_is_negative(shift))
1931 return NULL;
1932 if (shift.value == 0)
1933 return left_orig;
1934
1935 /* Cast to unsigned for easier left shift math */
1936 if (type_positive_bits(rl_type(left_orig)) < 32)
1937 left = cast_rl(&uint_ctype, left_orig);
1938 else if(type_positive_bits(rl_type(left_orig)) == 63)
1939 left = cast_rl(&ullong_ctype, left_orig);
1940 else
1941 left = left_orig;
1942
1943 FOR_EACH_PTR(left, tmp) {
1944 min = tmp->min;
1945 max = tmp->max;
1946
1947 if (min.value == 0 || max.value > sval_type_max(max.type).uvalue >> shift.uvalue)
1948 add_zero = true;
1949 if (min.value == 0 && max.value == 0)
1950 continue;
1951 if (min.value == 0)
1952 min.value = 1;
1953 min = sval_binop(min, SPECIAL_LEFTSHIFT, shift);
1954 max = sval_binop(max, SPECIAL_LEFTSHIFT, shift);
1955 add_range(&ret, min, max);
1956 } END_FOR_EACH_PTR(tmp);
1957
1958 if (!rl_fits_in_type(ret, rl_type(left_orig)))
1959 add_zero = true;
1960 ret = cast_rl(rl_type(left_orig), ret);
1961 if (add_zero)
1962 add_range(&ret, zero, zero);
1963
1964 return ret;
1965 }
1966
1967 static struct range_list *handle_rshift(struct range_list *left_orig, struct range_list *right_orig)
1968 {
1969 struct data_range *tmp;
1970 struct range_list *ret = NULL;
1971 sval_t shift, min, max;
1972
1973 if (!rl_to_sval(right_orig, &shift) || sval_is_negative(shift))
1974 return NULL;
1975 if (shift.value == 0)
1976 return left_orig;
1977
1978 FOR_EACH_PTR(left_orig, tmp) {
1979 min = sval_binop(tmp->min, SPECIAL_RIGHTSHIFT, shift);
1980 max = sval_binop(tmp->max, SPECIAL_RIGHTSHIFT, shift);
1981 add_range(&ret, min, max);
1982 } END_FOR_EACH_PTR(tmp);
1983
1984 return ret;
1985 }
1986
1987 struct range_list *rl_binop(struct range_list *left, int op, struct range_list *right)
1988 {
1989 struct symbol *cast_type;
1990 sval_t left_sval, right_sval;
1991 struct range_list *ret = NULL;
1992
1993 cast_type = rl_type(left);
1994 if (sval_type_max(rl_type(left)).uvalue < sval_type_max(rl_type(right)).uvalue)
1995 cast_type = rl_type(right);
1996 if (sval_type_max(cast_type).uvalue < INT_MAX)
1997 cast_type = &int_ctype;
1998
1999 left = cast_rl(cast_type, left);
2000 right = cast_rl(cast_type, right);
2001
2002 if (!left && !right)
2003 return NULL;
2004
2005 if (rl_to_sval(left, &left_sval) && rl_to_sval(right, &right_sval)) {
2006 sval_t val = sval_binop(left_sval, op, right_sval);
2013 break;
2014 case '/':
2015 ret = handle_divide_rl(left, right);
2016 break;
2017 case '*':
2018 case '+':
2019 ret = handle_add_mult_rl(left, op, right);
2020 break;
2021 case '|':
2022 ret = handle_OR_rl(left, right);
2023 break;
2024 case '^':
2025 ret = handle_XOR_rl(left, right);
2026 break;
2027 case '&':
2028 ret = handle_AND_rl(left, right);
2029 break;
2030 case '-':
2031 ret = handle_sub_rl(left, right);
2032 break;
2033 case SPECIAL_RIGHTSHIFT:
2034 return handle_rshift(left, right);
2035 case SPECIAL_LEFTSHIFT:
2036 return handle_lshift(left, right);
2037 }
2038
2039 return ret;
2040 }
2041
2042 void free_data_info_allocs(void)
2043 {
2044 struct allocator_struct *desc = &data_info_allocator;
2045 struct allocation_blob *blob = desc->blobs;
2046
2047 free_all_rl();
2048 clear_math_cache();
2049
2050 desc->blobs = NULL;
2051 desc->allocations = 0;
2052 desc->total_bytes = 0;
2053 desc->useful_bytes = 0;
2054 desc->freelist = NULL;
2055 while (blob) {
2056 struct allocation_blob *next = blob->next;
|