Skip to content

Commit 62e7e34

Browse files
author
Daniel Kroening
authored
Merge pull request #2852 from diffblue/dynamic_object_size
object_size can now do objects with dynamic size
2 parents ed558c0 + 11431ea commit 62e7e34

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-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/ansi-c/c_typecheck_expr.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,21 @@ exprt c_typecheck_baset::do_special_functions(
21642164

21652165
return typecast_exprt::conditional_cast(pointer_offset_expr, expr.type());
21662166
}
2167+
else if(identifier == CPROVER_PREFIX "OBJECT_SIZE")
2168+
{
2169+
if(expr.arguments().size() != 1)
2170+
{
2171+
err_location(f_op);
2172+
error() << "object_size expects one operand" << eom;
2173+
throw 0;
2174+
}
2175+
2176+
unary_exprt object_size_expr(
2177+
ID_object_size, expr.arguments()[0], size_type());
2178+
object_size_expr.add_source_location() = source_location;
2179+
2180+
return object_size_expr;
2181+
}
21672182
else if(identifier==CPROVER_PREFIX "POINTER_OBJECT")
21682183
{
21692184
if(expr.arguments().size()!=1)

src/ansi-c/cprover_builtin_headers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ void CBMC_trace(int lvl, const char *event, ...);
3737
// pointers
3838
__CPROVER_size_t __CPROVER_POINTER_OBJECT(const void *);
3939
__CPROVER_ssize_t __CPROVER_POINTER_OFFSET(const void *);
40+
__CPROVER_size_t __CPROVER_OBJECT_SIZE(const void *);
4041
__CPROVER_bool __CPROVER_DYNAMIC_OBJECT(const void *);
4142
void __CPROVER_allocated_memory(__CPROVER_size_t address, __CPROVER_size_t extent);
4243

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)