Skip to content

Commit 99b4ea8

Browse files
committed
Adds an over-approximation model for sysconf
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent da48fba commit 99b4ea8

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <errno.h>
2+
#include <unistd.h>
3+
4+
int main()
5+
{
6+
errno = 0;
7+
long result = sysconf(_SC_ARG_MAX);
8+
__CPROVER_assert(
9+
!(errno == EINVAL) || (result == -1),
10+
"Non-deterministically set errno to EINVAL");
11+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^\[main.assertion.\d+\] .* Non-deterministically set errno to EINVAL: SUCCESS$
7+
^\*\* 0 of 1 failed
8+
--
9+
^WARNING: no body for function sysconf

src/ansi-c/library/unistd.c

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,39 @@ ret_type _read(int fildes, void *buf, size_type nbyte)
319319
__CPROVER_HIDE:;
320320
return read(fildes, buf, nbyte);
321321
}
322+
323+
/* FUNCTION: sysconf */
324+
325+
#ifndef __CPROVER_ERRNO_H_INCLUDED
326+
# include <errno.h>
327+
# define __CPROVER_ERRNO_H_INCLUDED
328+
#endif
329+
330+
long __VERIFIER_nondet_long(void);
331+
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool(void);
332+
333+
long sysconf(int name);
334+
335+
// This overapproximation is based on the sysconf specification available at
336+
// https://pubs.opengroup.org/onlinepubs/9699919799/functions/sysconf.html.
337+
long sysconf(int name)
338+
{
339+
__CPROVER_HIDE:;
340+
(void)name;
341+
long retval = __VERIFIER_nondet_long();
342+
__CPROVER_assume(retval >= -1);
343+
344+
// Spec states "On error, -1 is returned and errno is set to indicate
345+
// the error (for example, EINVAL, indicating that name is invalid).
346+
// If name corresponds to a maximum or minimum limit, and that
347+
// limit is indeterminate, -1 is returned and errno is not changed."
348+
if(retval == -1 && __VERIFIER_nondet___CPROVER_bool())
349+
{
350+
// Spec only lists EINVAL as possible errors.
351+
errno = EINVAL;
352+
}
353+
354+
// Spec states "some returned values may be huge; they are not suitable
355+
// for allocating memory", so keeping this non-deterministic.
356+
return retval;
357+
}

0 commit comments

Comments
 (0)