Skip to content

Commit 11431ea

Browse files
author
Daniel Kroening
committed
object_size can now do objects with variable size
1 parent 84bd584 commit 11431ea

File tree

3 files changed

+42
-16
lines changed

3 files changed

+42
-16
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: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -786,24 +786,17 @@ void bv_pointerst::do_postponed(
786786
{
787787
const exprt &expr=*it;
788788

789-
mp_integer object_size;
790-
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);
789+
if(expr.id() != ID_symbol)
790+
continue;
797791

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

801-
if(to_integer(size_expr, object_size))
802-
continue;
803-
}
804-
else
794+
if(size_expr.is_nil())
805795
continue;
806796

797+
const exprt object_size =
798+
typecast_exprt::conditional_cast(size_expr, postponed.expr.type());
799+
807800
// only compare object part
808801
bvt bv;
809802
encode(number, bv);
@@ -813,12 +806,13 @@ void bv_pointerst::do_postponed(
813806
bvt saved_bv=postponed.op;
814807
saved_bv.erase(saved_bv.begin(), saved_bv.begin()+offset_bits);
815808

809+
bvt size_bv = convert_bv(object_size);
810+
816811
POSTCONDITION(bv.size()==saved_bv.size());
817812
PRECONDITION(postponed.bv.size()>=1);
813+
PRECONDITION(size_bv.size() == postponed.bv.size());
818814

819815
literalt l1=bv_utils.equal(bv, saved_bv);
820-
821-
bvt size_bv=bv_utils.build_constant(object_size, postponed.bv.size());
822816
literalt l2=bv_utils.equal(postponed.bv, size_bv);
823817

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

0 commit comments

Comments
 (0)