Skip to content

Commit 763d005

Browse files
author
Daniel Kroening
committed
object_size can now do objects with variable size
1 parent e2315f7 commit 763d005

File tree

3 files changed

+43
-15
lines changed

3 files changed

+43
-15
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
struct S
2+
{
3+
__CPROVER_size_t size;
4+
char *p;
5+
};
6+
7+
void func(struct S *s)
8+
{
9+
char *p = s->p;
10+
__CPROVER_size_t size = __CPROVER_OBJECT_SIZE(p);
11+
__CPROVER_assert(size == s->size, "size ok");
12+
p[80] = 123; // should be safe
13+
}
14+
15+
int main()
16+
{
17+
__CPROVER_size_t buffer_size;
18+
__CPROVER_assume(buffer_size >= 100);
19+
char buffer[buffer_size];
20+
struct S s;
21+
s.size = buffer_size;
22+
s.p = buffer;
23+
func(&s);
24+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
stack_object.c
3+
--pointer-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/solvers/flattening/bv_pointers.cpp

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -786,24 +786,19 @@ void bv_pointerst::do_postponed(
786786
{
787787
const exprt &expr=*it;
788788

789-
mp_integer object_size;
789+
exprt object_size;
790790

791-
if(expr.id()==ID_symbol)
792-
{
793-
// just get the type
794-
const typet &type=ns.follow(expr.type());
795-
796-
exprt size_expr=size_of_expr(type, ns);
791+
if(expr.id() != ID_symbol)
792+
continue;
797793

798-
if(size_expr.is_nil())
799-
continue;
794+
const exprt size_expr = size_of_expr(expr.type(), ns);
800795

801-
if(to_integer(size_expr, object_size))
802-
continue;
803-
}
804-
else
796+
if(size_expr.is_nil())
805797
continue;
806798

799+
const exprt object_size =
800+
typecast_exprt::conditional_cast(size_expr, postponed.expr.type());
801+
807802
// only compare object part
808803
bvt bv;
809804
encode(number, bv);
@@ -813,12 +808,13 @@ void bv_pointerst::do_postponed(
813808
bvt saved_bv=postponed.op;
814809
saved_bv.erase(saved_bv.begin(), saved_bv.begin()+offset_bits);
815810

811+
bvt size_bv = convert_bv(object_size);
812+
816813
POSTCONDITION(bv.size()==saved_bv.size());
817814
PRECONDITION(postponed.bv.size()>=1);
815+
PRECONDITION(size_bv.size() == postponed.bv.size());
818816

819817
literalt l1=bv_utils.equal(bv, saved_bv);
820-
821-
bvt size_bv=bv_utils.build_constant(object_size, postponed.bv.size());
822818
literalt l2=bv_utils.equal(postponed.bv, size_bv);
823819

824820
prop.l_set_to(prop.limplies(l1, l2), true);

0 commit comments

Comments
 (0)