check_kernel_printf.c: handle new definition of KERN_CONT
[smatch.git] / smatch_address.c
blobe9c1f9c820d5f248b9fc6754e29395450409e97a
1 /*
2 * Copyright (C) 2015 Oracle.
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
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.
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
18 #include "smatch.h"
19 #include "smatch_slist.h"
20 #include "smatch_extra.h"
22 static bool is_non_null_array(struct expression *expr)
24 struct symbol *type;
25 struct symbol *sym;
26 struct symbol *tmp;
27 int i;
29 type = get_type(expr);
30 if (!type || type->type != SYM_ARRAY)
31 return 0;
32 if (expr->type == EXPR_SYMBOL)
33 return 1;
34 if (implied_not_equal(expr, 0))
35 return 1;
37 /* verify that it's not the first member of the struct */
38 if (expr->type != EXPR_DEREF || !expr->member)
39 return 0;
40 sym = expr_to_sym(expr);
41 if (!sym)
42 return 0;
43 type = get_real_base_type(sym);
44 if (!type || type->type != SYM_PTR)
45 return 0;
46 type = get_real_base_type(type);
47 if (type->type != SYM_STRUCT)
48 return 0;
50 i = 0;
51 FOR_EACH_PTR(type->symbol_list, tmp) {
52 i++;
53 if (!tmp->ident)
54 continue;
55 if (strcmp(expr->member->name, tmp->ident->name) == 0) {
56 if (i == 1)
57 return 0;
58 return 1;
60 } END_FOR_EACH_PTR(tmp);
62 return 0;
65 int get_member_offset(struct symbol *type, char *member_name)
67 struct symbol *tmp;
68 int offset;
70 if (type->type != SYM_STRUCT)
71 return -1;
73 offset = 0;
74 FOR_EACH_PTR(type->symbol_list, tmp) {
75 if (!type->ctype.attribute->is_packed)
76 offset = ALIGN(offset, tmp->ctype.alignment);
77 if (tmp->ident &&
78 strcmp(member_name, tmp->ident->name) == 0) {
79 return offset;
81 offset += type_bytes(tmp);
82 } END_FOR_EACH_PTR(tmp);
83 return -1;
86 int get_member_offset_from_deref(struct expression *expr)
88 struct symbol *type;
89 struct ident *member;
90 int offset;
92 if (expr->type != EXPR_DEREF) /* hopefully, this doesn't happen */
93 return -1;
95 if (expr->member_offset >= 0)
96 return expr->member_offset;
98 member = expr->member;
99 if (!member)
100 return -1;
102 type = get_type(expr->deref);
103 if (!type || type->type != SYM_STRUCT)
104 return -1;
106 offset = get_member_offset(type, member->name);
107 if (offset >= 0)
108 expr->member_offset = offset;
109 return offset;
112 static void add_offset_to_min(struct range_list **rl, int offset)
114 sval_t sval, max;
115 struct range_list *orig = *rl;
116 struct range_list *offset_rl;
117 struct range_list *big_rl;
118 struct range_list *tmp;
121 * I don't know. I guess I want to preserve the upper value because
122 * that has no information. Only the lower value is interesting.
125 if (!orig)
126 return;
127 sval = rl_min(orig); /* get the type */
128 sval.value = offset;
130 offset_rl = alloc_rl(sval, sval);
131 tmp = rl_binop(orig, '+', offset_rl);
133 max = rl_max(orig);
134 /* if we actually "know" the max then preserve it. */
135 if (max.value < 100000) {
136 *rl = tmp;
137 return;
139 sval.value = 0;
140 big_rl = alloc_rl(sval, max);
142 *rl = rl_intersection(tmp, big_rl);
145 static struct range_list *where_allocated_rl(struct symbol *sym)
147 if (!sym)
148 return NULL;
150 if (sym->ctype.modifiers & (MOD_TOPLEVEL | MOD_STATIC)) {
151 if (sym->initializer)
152 return alloc_rl(data_seg_min, data_seg_max);
153 else
154 return alloc_rl(bss_seg_min, bss_seg_max);
156 return alloc_rl(stack_seg_min, stack_seg_max);
159 int get_address_rl(struct expression *expr, struct range_list **rl)
161 expr = strip_expr(expr);
162 if (!expr)
163 return 0;
165 if (expr->type == EXPR_STRING) {
166 *rl = alloc_rl(text_seg_min, text_seg_max);
167 return 1;
170 if (expr->type == EXPR_PREOP && expr->op == '&') {
171 struct expression *unop;
173 unop = strip_expr(expr->unop);
174 if (unop->type == EXPR_SYMBOL) {
175 *rl = where_allocated_rl(unop->symbol);
176 return 1;
179 if (unop->type == EXPR_DEREF) {
180 int offset = get_member_offset_from_deref(unop);
182 unop = strip_expr(unop->unop);
183 if (unop->type == EXPR_SYMBOL) {
184 *rl = where_allocated_rl(unop->symbol);
185 } else if (unop->type == EXPR_PREOP && unop->op == '*') {
186 unop = strip_expr(unop->unop);
187 get_absolute_rl(unop, rl);
188 } else {
189 return 0;
192 add_offset_to_min(rl, offset);
193 return 1;
196 return 0;
199 if (is_non_null_array(expr)) {
200 *rl = alloc_rl(array_min_sval, array_max_sval);
201 return 1;
204 return 0;