Skip to content

Commit

Permalink
Adds an over-approximation model for sysconf
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe R. Monteiro <[email protected]>
  • Loading branch information
feliperodri committed Jun 28, 2023
1 parent da48fba commit 99b4ea8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 0 deletions.
11 changes: 11 additions & 0 deletions regression/cbmc-library/sysconf-01/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <errno.h>
#include <unistd.h>

int main()
{
errno = 0;
long result = sysconf(_SC_ARG_MAX);
__CPROVER_assert(
!(errno == EINVAL) || (result == -1),
"Non-deterministically set errno to EINVAL");
}
9 changes: 9 additions & 0 deletions regression/cbmc-library/sysconf-01/test.desc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
CORE
main.c

^EXIT=0$
^SIGNAL=0$
^\[main.assertion.\d+\] .* Non-deterministically set errno to EINVAL: SUCCESS$
^\*\* 0 of 1 failed
--
^WARNING: no body for function sysconf
36 changes: 36 additions & 0 deletions src/ansi-c/library/unistd.c
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,39 @@ ret_type _read(int fildes, void *buf, size_type nbyte)
__CPROVER_HIDE:;
return read(fildes, buf, nbyte);
}

/* FUNCTION: sysconf */

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

long __VERIFIER_nondet_long(void);
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);

long sysconf(int name);

// This overapproximation is based on the sysconf specification available at
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html.
long sysconf(int name)
{
__CPROVER_HIDE:;
(void)name;
long retval = __VERIFIER_nondet_long();
__CPROVER_assume(retval >= -1);

// Spec states "On error, -1 is returned and errno is set to indicate
// the error (for example, EINVAL, indicating that name is invalid).
// If name corresponds to a maximum or minimum limit, and that
// limit is indeterminate, -1 is returned and errno is not changed."
if(retval == -1 && __VERIFIER_nondet___CPROVER_bool())
{
// Spec only lists EINVAL as possible errors.
errno = EINVAL;
}

// Spec states "some returned values may be huge; they are not suitable
// for allocating memory", so keeping this non-deterministic.
return retval;
}

0 comments on commit 99b4ea8

Please sign in to comment.