Skip to content

Commit defd0d9

Browse files
[libc] implement unistd/getentropy (#122692)
Implement GNU extension getentropy. This function is used by many programs to acquire entropy without handling the loop of getrandom.
1 parent 2a044f8 commit defd0d9

File tree

16 files changed

+207
-2
lines changed

16 files changed

+207
-2
lines changed

libc/config/linux/aarch64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ set(TARGET_LIBC_ENTRYPOINTS
322322
libc.src.unistd.fsync
323323
libc.src.unistd.ftruncate
324324
libc.src.unistd.getcwd
325+
libc.src.unistd.getentropy
325326
libc.src.unistd.geteuid
326327
libc.src.unistd.getpid
327328
libc.src.unistd.getppid

libc/config/linux/x86_64/entrypoints.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ set(TARGET_LIBC_ENTRYPOINTS
321321
libc.src.unistd.fsync
322322
libc.src.unistd.ftruncate
323323
libc.src.unistd.getcwd
324+
libc.src.unistd.getentropy
324325
libc.src.unistd.geteuid
325326
libc.src.unistd.getpid
326327
libc.src.unistd.getppid

libc/config/windows/entrypoints.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,9 @@ set(TARGET_LIBC_ENTRYPOINTS
101101
# time.h entrypoints
102102
libc.src.time.time
103103
libc.src.time.clock_getres
104+
105+
# unistd.h entrypoints
106+
libc.src.unistd.getentropy
104107
)
105108

106109
set(TARGET_LIBM_ENTRYPOINTS

libc/config/windows/headers.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ set(TARGET_PUBLIC_HEADERS
66
libc.include.errno
77
libc.include.fenv
88
libc.include.math
9+
libc.include.unistd
910
)

libc/include/sys/random.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,10 @@ functions:
1515
- type: void *
1616
- type: size_t
1717
- type: unsigned int
18+
- name: getentropy
19+
standards:
20+
- GNUExtensions
21+
return_type: int
22+
arguments:
23+
- type: void *
24+
- type: size_t

libc/include/unistd.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,13 @@ functions:
128128
arguments:
129129
- type: char *
130130
- type: size_t
131+
- name: getentropy
132+
standards:
133+
- GNUExtensions
134+
return_type: int
135+
arguments:
136+
- type: void *
137+
- type: size_t
131138
- name: geteuid
132139
standards:
133140
- POSIX

libc/src/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ add_subdirectory(string)
1515
add_subdirectory(strings)
1616
add_subdirectory(wchar)
1717
add_subdirectory(time)
18+
add_subdirectory(unistd)
1819

1920
if(${LIBC_TARGET_OS} STREQUAL "linux")
2021
add_subdirectory(dirent)
@@ -23,7 +24,6 @@ if(${LIBC_TARGET_OS} STREQUAL "linux")
2324
add_subdirectory(sched)
2425
add_subdirectory(sys)
2526
add_subdirectory(termios)
26-
add_subdirectory(unistd)
2727
endif()
2828

2929
if(NOT LLVM_LIBC_FULL_BUILD)

libc/src/unistd/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -350,3 +350,10 @@ add_entrypoint_object(
350350
DEPENDS
351351
libc.src.__support.threads.identifier
352352
)
353+
354+
add_entrypoint_object(
355+
getentropy
356+
ALIAS
357+
DEPENDS
358+
.${LIBC_TARGET_OS}.getentropy
359+
)

libc/src/unistd/getentropy.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//===-- Implementation header for getentropy ------------------------------===//
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 "hdr/types/size_t.h"
10+
#include "src/__support/common.h"
11+
12+
#ifndef LLVM_LIBC_SRC_UNISTD_GETENTROPY_H
13+
#define LLVM_LIBC_SRC_UNISTD_GETENTROPY_H
14+
15+
namespace LIBC_NAMESPACE_DECL {
16+
int getentropy(void *buffer, size_t length);
17+
}
18+
19+
#endif // LLVM_LIBC_SRC_UNISTD_GETENTROPY_H

libc/src/unistd/linux/CMakeLists.txt

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,3 +570,18 @@ add_entrypoint_object(
570570
libc.src.__support.OSUtil.osutil
571571
libc.src.errno.errno
572572
)
573+
574+
add_entrypoint_object(
575+
getentropy
576+
SRCS
577+
getentropy.cpp
578+
HDRS
579+
../getentropy.h
580+
DEPENDS
581+
libc.hdr.types.size_t
582+
libc.hdr.types.ssize_t
583+
libc.hdr.errno_macros
584+
libc.include.sys_syscall
585+
libc.src.__support.OSUtil.osutil
586+
libc.src.errno.errno
587+
)

0 commit comments

Comments
 (0)