Skip to content

Commit 092c01d

Browse files
committed
C library: implement getrandom
Adds a model of getrandom as available on Linux.
1 parent 8a9ab0c commit 092c01d

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifdef __linux__
2+
# include <sys/random.h>
3+
4+
# include <assert.h>
5+
6+
int main()
7+
{
8+
char zero_bytes[6] = {0};
9+
ssize_t res = getrandom(zero_bytes, 5, 0);
10+
assert(res <= 5);
11+
assert(zero_bytes[5] == 0);
12+
return 0;
13+
}
14+
#else
15+
int main()
16+
{
17+
}
18+
#endif
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
--pointer-check --bounds-check --signed-overflow-check
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/ansi-c/library/random.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/* FUNCTION: getrandom */
2+
3+
#ifdef __linux__
4+
5+
# ifndef __CPROVER_SYS_RANDOM_H_INCLUDED
6+
# include <sys/random.h>
7+
# define __CPROVER_SYS_RANDOM_H_INCLUDED
8+
# endif
9+
10+
# ifndef GRND_NONBLOCK
11+
# define GRND_NONBLOCK 0
12+
# endif
13+
14+
__CPROVER_bool __VERIFIER_nondet___CPROVER_bool();
15+
16+
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags)
17+
{
18+
if(flags & GRND_NONBLOCK && __VERIFIER_nondet___CPROVER_bool())
19+
return -1;
20+
21+
char bytes[buflen];
22+
__CPROVER_array_replace(buf, bytes);
23+
24+
size_t actual_bytes;
25+
__CPROVER_assume(actual_bytes <= buflen);
26+
return (ssize_t)actual_bytes;
27+
}
28+
29+
#endif

0 commit comments

Comments
 (0)