manual: link lock-example1.c

This commit is contained in:
Daniel Kroening 2018-11-08 20:04:35 +00:00
parent 2e9b1812de
commit 37c2a68017
2 changed files with 63 additions and 14 deletions

View File

@ -209,40 +209,48 @@ obtained with the option `--property`.
### Unbounded Loops ### Unbounded Loops
CBMC can also be used for programs with unbounded loops. In this case, CBMC can also be used for programs with unbounded loops. In this case, CBMC
CBMC is used for bug hunting only; CBMC does not attempt to find all is used for bug hunting only; CBMC does not attempt to find all bugs. The
bugs. The following program (lock-example.c) is an example of a program following program
with a user-specified property: ([lock-example.c](https://raw.githubusercontent.com/diffblue/cbmc/develop/doc/cprover-manual/lock-example.c))
is an example of a program with a user-specified property:
```C ```C
_Bool nondet_bool(); _Bool nondet_bool();
_Bool LOCK = 0; _Bool LOCK = 0;
_Bool lock() { _Bool lock()
if(nondet_bool()) { {
if(nondet_bool())
{
assert(!LOCK); assert(!LOCK);
LOCK=1; LOCK = 1;
return 1; } return 1;
}
return 0; return 0;
} }
void unlock() { void unlock()
{
assert(LOCK); assert(LOCK);
LOCK=0; LOCK = 0;
} }
int main() { int main()
{
unsigned got_lock = 0; unsigned got_lock = 0;
int times; int times;
while(times > 0) { while(times > 0)
if(lock()) { {
if(lock())
{
got_lock++; got_lock++;
/* critical section */ /* critical section */
} }
if(got_lock!=0) if(got_lock != 0)
unlock(); unlock();
got_lock--; got_lock--;

View File

@ -0,0 +1,41 @@
_Bool nondet_bool();
_Bool LOCK = 0;
_Bool lock()
{
if(nondet_bool())
{
assert(!LOCK);
LOCK = 1;
return 1;
}
return 0;
}
void unlock()
{
assert(LOCK);
LOCK = 0;
}
int main()
{
unsigned got_lock = 0;
int times;
while(times > 0)
{
if(lock())
{
got_lock++;
/* critical section */
}
if(got_lock != 0)
unlock();
got_lock--;
times--;
}
}