Skip to content

C library: model usleep #7901

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions regression/cbmc-library/_sleep-01/main.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#include <assert.h>
#include <unistd.h>
#ifndef _WIN32
# include <unistd.h>
#else
unsigned _sleep(unsigned);
#endif

int main()
{
_sleep();
assert(0);
assert(_sleep(42) <= 42);
return 0;
}
2 changes: 1 addition & 1 deletion regression/cbmc-library/_sleep-01/test.desc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
KNOWNBUG
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
Expand Down
14 changes: 14 additions & 0 deletions regression/cbmc-library/_usleep-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <assert.h>
#ifndef _WIN32
# include <unistd.h>
#else
int _usleep(unsigned);
#endif

int main()
{
unsigned input;
int retval = _usleep(input);
assert(retval == 0 || retval == -1);
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/_usleep-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
14 changes: 14 additions & 0 deletions regression/cbmc-library/usleep-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <assert.h>
#ifndef _WIN32
# include <unistd.h>
#else
int usleep(unsigned);
#endif

int main()
{
unsigned input;
int retval = usleep(input);
assert(retval == 0 || retval == -1);
return 0;
}
8 changes: 8 additions & 0 deletions regression/cbmc-library/usleep-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CORE
main.c
--pointer-check --bounds-check
^EXIT=0$
^SIGNAL=0$
^VERIFICATION SUCCESSFUL$
--
^warning: ignoring
35 changes: 35 additions & 0 deletions src/ansi-c/library/unistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,41 @@ unsigned int _sleep(unsigned int seconds)
return sleep(seconds);
}

/* FUNCTION: usleep */

#ifndef __CPROVER_ERRNO_H_INCLUDED
# include <errno.h>
# define __CPROVER_ERRNO_H_INCLUDED
#endif

__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);

int usleep(unsigned int usec)
{
__CPROVER_HIDE:;
// do nothing, but return nondet value
__CPROVER_bool error = __VERIFIER_nondet___CPROVER_bool();
if(error)
{
if(usec >= 1000000)
errno = EINVAL;
else
errno = EINTR;
return -1;
}
return 0;
}

/* FUNCTION: _usleep */

int usleep(unsigned int);

int _usleep(unsigned int usec)
{
__CPROVER_HIDE:;
return usleep(usec);
}

/* FUNCTION: unlink */

int __VERIFIER_nondet_int(void);
Expand Down