Skip to content

Commit dc6b6a7

Browse files
committed
Adds an over-approximation for syscall function
Signed-off-by: Felipe R. Monteiro <[email protected]>
1 parent a531056 commit dc6b6a7

File tree

3 files changed

+71
-0
lines changed

3 files changed

+71
-0
lines changed
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <sys/syscall.h>
2+
3+
#include <assert.h>
4+
#include <errno.h>
5+
#include <unistd.h>
6+
7+
int main()
8+
{
9+
long int rc;
10+
errno = 0;
11+
rc = syscall(SYS_chmod, "/etc/passwd", 0444);
12+
assert(!(rc != -1) || (errno == 0));
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
CORE unix
2+
main.c
3+
--pointer-check --bounds-check --conversion-check
4+
^\[main.assertion.\d+\] line \d+ assertion !\(rc != -1\) || \(errno == 0\): SUCCESS$
5+
^\*\* 1 of \d+ failed .*$
6+
^VERIFICATION SUCCESSFUL$
7+
^EXIT=0$
8+
^SIGNAL=0$
9+
--
10+
^\*\*\*\* WARNING: no body for function syscall

src/ansi-c/library/unistd.c

+48
Original file line numberDiff line numberDiff line change
@@ -385,3 +385,51 @@ __CPROVER_HIDE:;
385385
// Thus, modelling it as non-deterministic.
386386
return retval;
387387
}
388+
389+
/* FUNCTION: syscall */
390+
391+
#ifndef __CPROVER_ERRNO_H_INCLUDED
392+
# include <errno.h>
393+
# define __CPROVER_ERRNO_H_INCLUDED
394+
#endif
395+
396+
long int __VERIFIER_nondet_long_int(void);
397+
int __VERIFIER_nondet_int(void);
398+
399+
// This overapproximation is based on the syscall specification available at
400+
// https://man7.org/linux/man-pages/man2/syscall.2.html and
401+
// https://www.gnu.org/software/libc/manual/html_node/System-Calls.html.
402+
//
403+
// sysno is the system call number. The remaining arguments are the arguments
404+
// for the system call. Each kind of system call has a definite number of
405+
// arguments, from zero to five. If you code more arguments than the system
406+
// call takes, the extra ones to the right are ignored.
407+
#ifdef __APPLE__
408+
int syscall(int sysno, ...);
409+
int syscall(int sysno, ...)
410+
#else
411+
long int syscall(long int sysno, ...);
412+
long int syscall(long int sysno, ...)
413+
#endif
414+
{
415+
__CPROVER_HIDE:;
416+
(void)sysno;
417+
418+
#ifdef __APPLE__
419+
int retval = __VERIFIER_nondet_int();
420+
#else
421+
long int retval = __VERIFIER_nondet_long_int();
422+
#endif
423+
424+
if(retval == -1)
425+
{
426+
// We should keep errno as non-deterministic as possible, since this model
427+
// never takes into account any input.
428+
errno = __VERIFIER_nondet_int();
429+
}
430+
431+
// The return value is the return value from the system call, unless the
432+
// system call failed. This over-approximation doesn't take into account
433+
// any system call operation, so we leave the return value as non-det.
434+
return retval;
435+
}

0 commit comments

Comments
 (0)