Skip to content

Commit 6f4e4ea

Browse files
sribee8Sriya Pratipati
andauthored
[libc] Internal getrandom implementation (#144427)
Implemented an internal getrandom to avoid calls to the public one in table.h --------- Co-authored-by: Sriya Pratipati <[email protected]>
1 parent 835d303 commit 6f4e4ea

File tree

5 files changed

+63
-17
lines changed

5 files changed

+63
-17
lines changed

libc/src/__support/HashTable/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ if (NOT ${getrandom_index} EQUAL -1)
1515
message(STATUS "Using getrandom for hashtable randomness")
1616
set(randomness_compile_flags -DLIBC_HASHTABLE_USE_GETRANDOM)
1717
set(randomness_extra_depends
18-
libc.src.sys.random.getrandom libc.src.errno.errno)
18+
libc.src.__support.OSUtil.linux.getrandom
19+
libc.hdr.errno_macros)
1920
endif()
2021

2122

libc/src/__support/HashTable/randomness.h

Lines changed: 7 additions & 9 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/libc_errno.h"
18-
#include "src/sys/random/getrandom.h"
17+
#include "hdr/errno_macros.h"
18+
#include "src/__support/OSUtil/linux/getrandom.h"
1919
#endif
2020

2121
namespace LIBC_NAMESPACE_DECL {
@@ -35,20 +35,18 @@ LIBC_INLINE uint64_t next_random_seed() {
3535
entropy[0] = reinterpret_cast<uint64_t>(&entropy);
3636
entropy[1] = reinterpret_cast<uint64_t>(&state);
3737
#if defined(LIBC_HASHTABLE_USE_GETRANDOM)
38-
int errno_backup = libc_errno;
3938
size_t count = sizeof(entropy);
4039
uint8_t *buffer = reinterpret_cast<uint8_t *>(entropy);
4140
while (count > 0) {
42-
ssize_t len = getrandom(buffer, count, 0);
43-
if (len == -1) {
44-
if (libc_errno == ENOSYS)
41+
auto len = internal::getrandom(buffer, count, 0);
42+
if (!len.has_value()) {
43+
if (len.error() == ENOSYS)
4544
break;
4645
continue;
4746
}
48-
count -= len;
49-
buffer += len;
47+
count -= len.value();
48+
buffer += len.value();
5049
}
51-
libc_errno = errno_backup;
5250
#endif
5351
state.update(&entropy, sizeof(entropy));
5452
}

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ add_object_library(
2424
libc.include.sys_syscall
2525
)
2626

27+
add_header_library(
28+
getrandom
29+
HDRS
30+
getrandom.h
31+
DEPENDS
32+
libc.src.__support.OSUtil.osutil
33+
libc.src.__support.common
34+
libc.src.__support.error_or
35+
libc.src.__support.macros.config
36+
libc.hdr.types.ssize_t
37+
libc.include.sys_syscall
38+
)
39+
2740
add_header_library(
2841
vdso_sym
2942
HDRS
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 "hdr/types/ssize_t.h"
13+
#include "src/__support/OSUtil/linux/syscall.h" // syscall_impl
14+
#include "src/__support/common.h"
15+
#include "src/__support/error_or.h"
16+
#include "src/__support/macros/config.h"
17+
#include <sys/syscall.h> // For syscall numbers
18+
19+
namespace LIBC_NAMESPACE_DECL {
20+
namespace internal {
21+
22+
LIBC_INLINE static ErrorOr<ssize_t> getrandom(void *buf, size_t buflen,
23+
unsigned int flags) {
24+
ssize_t ret =
25+
LIBC_NAMESPACE::syscall_impl<ssize_t>(SYS_getrandom, buf, buflen, flags);
26+
if (ret < 0) {
27+
return Error(-static_cast<int>(ret));
28+
}
29+
return ret;
30+
}
31+
32+
} // namespace internal
33+
} // namespace LIBC_NAMESPACE_DECL
34+
35+
#endif // LLVM_LIBC_SRC___SUPPORT_OSUTIL_GETRANDOM_H

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

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,23 @@
88

99
#include "src/sys/random/getrandom.h"
1010

11+
#include "src/__support/OSUtil/linux/getrandom.h"
1112
#include "src/__support/OSUtil/syscall.h" // For internal syscall function.
1213
#include "src/__support/common.h"
13-
14+
#include "src/__support/error_or.h"
1415
#include "src/__support/libc_errno.h"
1516
#include "src/__support/macros/config.h"
16-
#include <sys/syscall.h> // For syscall numbers.
1717

1818
namespace LIBC_NAMESPACE_DECL {
1919

2020
LLVM_LIBC_FUNCTION(ssize_t, getrandom,
2121
(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);
22+
auto rand = internal::getrandom(buf, buflen, flags);
23+
if (!rand.has_value()) {
24+
libc_errno = static_cast<int>(rand.error());
2625
return -1;
2726
}
28-
return ret;
27+
return rand.value();
2928
}
3029

3130
} // namespace LIBC_NAMESPACE_DECL

0 commit comments

Comments
 (0)