Skip to content

Commit 6a17a99

Browse files
author
Daniel Kroening
committed
__CPROVER_r_ok and __CPROVER_w_ok added to ANSI-C front-end
1 parent 44ef8d5 commit 6a17a99

File tree

6 files changed

+38
-0
lines changed

6 files changed

+38
-0
lines changed

doc/cbmc-user-manual.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2190,6 +2190,15 @@ to the program. If the expression evaluates to false, the execution
21902190
aborts without failure. More detail on the use of assumptions is in the
21912191
section on [Assumptions and Assertions](modeling-assertions.shtml).
21922192

2193+
#### \_\_CPROVER\_r_ok, \_\_CPROVER\_w_ok
2194+
2195+
void __CPROVER_r_ok(const void *, size_t size);
2196+
void __CPROVER_w_ok(cosnt void *, size_t size);
2197+
2198+
The function **\_\_CPROVER\_r_ok** returns true if reading the piece of
2199+
memory starting at the given pointer with the given size is safe.
2200+
**\_\_CPROVER\_w_ok** does the same with writing.
2201+
21932202
#### \_\_CPROVER\_same\_object, \_\_CPROVER\_POINTER\_OBJECT, \_\_CPROVER\_POINTER\_OFFSET, \_\_CPROVER\_DYNAMIC\_OBJECT
21942203

21952204
_Bool __CPROVER_same_object(const void *, const void *);

src/ansi-c/c_typecheck_expr.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2334,6 +2334,23 @@ exprt c_typecheck_baset::do_special_functions(
23342334

23352335
return malloc_expr;
23362336
}
2337+
else if(
2338+
identifier == CPROVER_PREFIX "r_ok" || identifier == CPROVER_PREFIX "w_ok")
2339+
{
2340+
if(expr.arguments().size() != 2)
2341+
{
2342+
err_location(f_op);
2343+
error() << identifier << " expects two operands" << eom;
2344+
throw 0;
2345+
}
2346+
2347+
irep_idt id = identifier == CPROVER_PREFIX "r_ok" ? ID_r_ok : ID_w_ok;
2348+
2349+
predicate_exprt ok_expr(id, expr.arguments()[0], expr.arguments()[1]);
2350+
ok_expr.add_source_location() = source_location;
2351+
2352+
return ok_expr;
2353+
}
23372354
else if(identifier==CPROVER_PREFIX "isinff" ||
23382355
identifier==CPROVER_PREFIX "isinfd" ||
23392356
identifier==CPROVER_PREFIX "isinfld" ||

src/ansi-c/cprover_builtin_headers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ __CPROVER_bool __CPROVER_invalid_pointer(const void *);
99
__CPROVER_bool __CPROVER_is_zero_string(const void *);
1010
__CPROVER_size_t __CPROVER_zero_string_length(const void *);
1111
__CPROVER_size_t __CPROVER_buffer_size(const void *);
12+
__CPROVER_bool __CPROVER_r_ok(const void *, __CPROVER_size_t);
13+
__CPROVER_bool __CPROVER_w_ok(const void *, __CPROVER_size_t);
1214

1315
// bitvector analysis
1416
__CPROVER_bool __CPROVER_get_flag(const void *, const char *);

src/ansi-c/expr2c.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3515,6 +3515,12 @@ std::string expr2ct::convert_with_precedence(
35153515
return convert_function(src, "__builtin_popcount", precedence=16);
35163516
}
35173517

3518+
else if(src.id() == ID_r_ok)
3519+
return convert_function(src, "R_OK", precedence = 16);
3520+
3521+
else if(src.id() == ID_w_ok)
3522+
return convert_function(src, "W_OK", precedence = 16);
3523+
35183524
else if(src.id()==ID_invalid_pointer)
35193525
return convert_function(src, "INVALID-POINTER", precedence=16);
35203526

src/ansi-c/library/cprover.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ void __CPROVER_precondition(__CPROVER_bool assertion, const char *description);
2424
__CPROVER_bool __CPROVER_is_zero_string(const void *);
2525
__CPROVER_size_t __CPROVER_zero_string_length(const void *);
2626
__CPROVER_size_t __CPROVER_buffer_size(const void *);
27+
__CPROVER_bool __CPROVER_r_ok(const void *, __CPROVER_size_t);
28+
__CPROVER_bool __CPROVER_w_ok(const void *, __CPROVER_size_t);
2729

2830
#if 0
2931
__CPROVER_bool __CPROVER_equal();

src/util/irep_ids.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,8 @@ IREP_ID_TWO(C_unnamed_object, #unnamed_object)
691691
IREP_ID_TWO(C_temporary_avoided, #temporary_avoided)
692692
IREP_ID_TWO(C_qualifier, #qualifier)
693693
IREP_ID_TWO(C_array_ini, #array_ini)
694+
IREP_ID_ONE(r_ok)
695+
IREP_ID_ONE(w_ok)
694696

695697
// Projects depending on this code base that wish to extend the list of
696698
// available ids should provide a file local_irep_ids.def in their source tree

0 commit comments

Comments
 (0)