Skip to content

Commit 3cf84be

Browse files
committed
[embedded] Add an arc4random_buf stub for Linux for embedded/dependencies-random.swift test
1 parent 43554c4 commit 3cf84be

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <stdlib.h>
2+
#include <stdio.h>
3+
#include <errno.h>
4+
#include <stdint.h>
5+
6+
#ifdef __linux__
7+
8+
ssize_t getrandom(void *buf, size_t len, unsigned int flags);
9+
10+
void arc4random_buf(void *buf, size_t nbytes) {
11+
while (nbytes > 0) {
12+
ssize_t actual_nbytes = 0;
13+
do {
14+
actual_nbytes = getrandom(buf, nbytes, 0);
15+
} while (actual_nbytes == -1 && errno == EINTR);
16+
17+
if (actual_nbytes == -1) {
18+
abort();
19+
}
20+
21+
buf = (uint8_t *)(buf) + actual_nbytes;
22+
nbytes -= actual_nbytes;
23+
}
24+
}
25+
26+
#endif

test/embedded/dependencies-random.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
// DEP: _posix_memalign
2121

2222
// RUN: %target-clang -x c -c %S/Inputs/print.c -o %t/print.o
23-
// RUN: %target-clang %t/a.o %t/print.o -o %t/a.out
23+
// RUN: %target-clang -x c -c %S/Inputs/linux-rng-support.c -o %t/linux-rng-support.o
24+
// RUN: %target-clang %t/a.o %t/print.o %t/linux-rng-support.o -o %t/a.out
2425
// RUN: %target-run %t/a.out | %FileCheck %s
2526

2627
// REQUIRES: swift_in_compiler

0 commit comments

Comments
 (0)