Skip to content

Commit 75a66dc

Browse files
author
Sriya Pratipati
committed
[libc] Internal getrandom implementation
Implemented an internal getrandom to avoid calls to the public one in table.h
1 parent a637584 commit 75a66dc

File tree

5 files changed

+58
-9
lines changed

5 files changed

+58
-9
lines changed

libc/src/__support/HashTable/randomness.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
#include "src/__support/macros/attributes.h"
1515
#include "src/__support/macros/config.h"
1616
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
17+
#include "src/__support/OSUtil/getrandom.h"
1718
#include "src/__support/libc_errno.h"
18-
#include "src/sys/random/getrandom.h"
1919
#endif
2020

2121
namespace LIBC_NAMESPACE_DECL {
@@ -39,7 +39,7 @@ LIBC_INLINE uint64_t next_random_seed() {
3939
size_t count = sizeof(entropy);
4040
uint8_t *buffer = reinterpret_cast<uint8_t *>(entropy);
4141
while (count > 0) {
42-
ssize_t len = getrandom(buffer, count, 0);
42+
ssize_t len = internal::getrandom(buffer, count, 0);
4343
if (len == -1) {
4444
if (libc_errno == ENOSYS)
4545
break;
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//===------------ Implementation of getrandom function ----------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
10+
#define LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H
11+
12+
#include "src/__support/macros/config.h"
13+
#include <sys/random.h>
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
namespace internal {
17+
18+
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags);
19+
20+
} // namespace internal
21+
} // namespace LIBC_NAMESPACE_DECL
22+
23+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H

libc/src/__support/OSUtil/linux/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ add_object_library(
99
SRCS
1010
exit.cpp
1111
fcntl.cpp
12+
getrandom.cpp
1213
HDRS
1314
io.h
1415
syscall.h
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===------------ Linux implementation of getrandom -------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#include "src/__support/OSUtil/getrandom.h"
10+
#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
11+
#include "src/__support/common.h"
12+
#include "src/__support/libc_errno.h"
13+
#include "src/__support/macros/config.h"
14+
#include <sys/syscall.h> // For syscall numbers.
15+
16+
namespace LIBC_NAMESPACE_DECL {
17+
namespace internal {
18+
19+
ssize_t getrandom(void *buf, size_t buflen, unsigned int flags) {
20+
ssize_t ret =
21+
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
22+
if (ret < 0) {
23+
libc_errno = static_cast<int>(-ret);
24+
return -1;
25+
}
26+
return ret;
27+
}
28+
29+
} // namespace internal
30+
} // namespace LIBC_NAMESPACE_DECL

libc/src/sys/random/linux/getrandom.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1212
#include "src/__support/common.h"
13+
#include "src/__support/OSUtil/getrandom.h"
1314

1415
#include "src/__support/libc_errno.h"
1516
#include "src/__support/macros/config.h"
@@ -19,13 +20,7 @@ namespace LIBC_NAMESPACE_DECL {
1920

2021
LLVM_LIBC_FUNCTION(ssize_t, getrandom,
2122
(void *buf, size_t buflen, unsigned int flags)) {
22-
ssize_t ret =
23-
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
24-
if (ret < 0) {
25-
libc_errno = static_cast<int>(-ret);
26-
return -1;
27-
}
28-
return ret;
23+
return internal::getrandom(buf, buflen, flags);
2924
}
3025

3126
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)