Skip to content

Commit 8a5094f

Browse files
authored
Merge pull request #7901 from tautschnig/features/usleep
C library: model usleep
2 parents d832f93 + 6a120a4 commit 8a5094f

File tree

7 files changed

+86
-4
lines changed

7 files changed

+86
-4
lines changed
Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#include <assert.h>
2-
#include <unistd.h>
2+
#ifndef _WIN32
3+
# include <unistd.h>
4+
#else
5+
unsigned _sleep(unsigned);
6+
#endif
37

48
int main()
59
{
6-
_sleep();
7-
assert(0);
10+
assert(_sleep(42) <= 42);
811
return 0;
912
}

regression/cbmc-library/_sleep-01/test.desc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
KNOWNBUG
1+
CORE
22
main.c
33
--pointer-check --bounds-check
44
^EXIT=0$
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
#ifndef _WIN32
3+
# include <unistd.h>
4+
#else
5+
int _usleep(unsigned);
6+
#endif
7+
8+
int main()
9+
{
10+
unsigned input;
11+
int retval = _usleep(input);
12+
assert(retval == 0 || retval == -1);
13+
return 0;
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#include <assert.h>
2+
#ifndef _WIN32
3+
# include <unistd.h>
4+
#else
5+
int usleep(unsigned);
6+
#endif
7+
8+
int main()
9+
{
10+
unsigned input;
11+
int retval = usleep(input);
12+
assert(retval == 0 || retval == -1);
13+
return 0;
14+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/library/unistd.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,41 @@ unsigned int _sleep(unsigned int seconds)
2222
return sleep(seconds);
2323
}
2424

25+
/* FUNCTION: usleep */
26+
27+
#ifndef __CPROVER_ERRNO_H_INCLUDED
28+
# include <errno.h>
29+
# define __CPROVER_ERRNO_H_INCLUDED
30+
#endif
31+
32+
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
33+
34+
int usleep(unsigned int usec)
35+
{
36+
__CPROVER_HIDE:;
37+
// do nothing, but return nondet value
38+
__CPROVER_bool error = __VERIFIER_nondet___CPROVER_bool();
39+
if(error)
40+
{
41+
if(usec >= 1000000)
42+
errno = EINVAL;
43+
else
44+
errno = EINTR;
45+
return -1;
46+
}
47+
return 0;
48+
}
49+
50+
/* FUNCTION: _usleep */
51+
52+
int usleep(unsigned int);
53+
54+
int _usleep(unsigned int usec)
55+
{
56+
__CPROVER_HIDE:;
57+
return usleep(usec);
58+
}
59+
2560
/* FUNCTION: unlink */
2661

2762
int __VERIFIER_nondet_int(void);

0 commit comments

Comments
 (0)