Skip to content

Commit 4b95215

Browse files
authored
Merge pull request #7780 from feliperodri/sysconf-model
Adds an over-approximation model for sysconf
2 parents 64bf1ca + 830a60f commit 4b95215

File tree

3 files changed

+70
-0
lines changed

3 files changed

+70
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#include <errno.h>
2+
#include <unistd.h>
3+
4+
main()
5+
{
6+
long result;
7+
int name;
8+
9+
errno = 0;
10+
// Testing sysconf as an over-apporximation.
11+
// We expect to cover all branches, thus, all assertions should fail.
12+
if((result = sysconf(name)) == -1)
13+
{
14+
if(errno == 0)
15+
{
16+
__CPROVER_assert(0, "ARG_MAX is not supported");
17+
}
18+
else
19+
{
20+
__CPROVER_assert(0, "sysconf() error");
21+
}
22+
}
23+
else
24+
{
25+
__CPROVER_assert(0, "ARG_MAX is supported");
26+
}
27+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CORE unix
2+
main.c
3+
4+
^\[main.assertion.\d+\] line \d+ ARG\_MAX is not supported: FAILURE$
5+
^\[main.assertion.\d+\] line \d+ sysconf\(\) error: FAILURE$
6+
^\[main.assertion.\d+\] line \d+ ARG\_MAX is supported: FAILURE$
7+
^\*\* 3 of 3 failed .*$
8+
^VERIFICATION FAILED$
9+
^EXIT=10$
10+
^SIGNAL=0$
11+
--
12+
^WARNING: no body for function sysconf

src/ansi-c/library/unistd.c

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,3 +319,34 @@ 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+
unsigned int __VERIFIER_nondet_unsigned_int(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+
343+
// We should keep errno as non-determinist as possible, since this model
344+
// nver takes into account the name input.
345+
errno = __VERIFIER_nondet_unsigned_int();
346+
347+
// Spec states "some returned values may be huge; they are not suitable
348+
// for allocating memory". There aren't also guarantees about return
349+
// values being strickly equal or greater to -1.
350+
// Thus, modelling it as non-deterministic.
351+
return retval;
352+
}

0 commit comments

Comments
 (0)