Skip to content

Commit da3c311

Browse files
committed
feat: std::random_device works, and it took four layers to make it so
使用者报 `_LIBCPP_HAS_RANDOM_DEVICE 0` 让 5 个 TU 编不过,本地改成 1 即修好。 ⚠️ 那个 0 不是保守设置,是当时事实的准确记录 —— 改成 1 只让编译期通过, 链接会缺符号。现在 openkal 有了随机源接口,这四层可以逐一拆掉。 **① 宏改为 1** —— 只在 `generic`;`freestanding` 保持 0,因为裸机后端 只有板子有源时才提供 `openkal.random`,而 6.1 条在链接期报告缺席。 **② `random.cpp` 曾被 sources 显式排除**,与那个宏配对。排除删掉。 **③ ⚠️ 默认后端在这里走不通,而它不会报错。** libc++ 对不认识的目标 落到 `_LIBCPP_USING_DEV_RANDOM`,那条要 `open("/dev/urandom")` —— 而能力型文件系统刻意不发绝对路径。必须在 `__config_site` 里**显式**选 `_LIBCPP_USING_GETENTROPY`:它不要描述符也不要路径。 **④ 声明找不到** —— musl 把 `getentropy` 放在 `<unistd.h>`,glibc 放在 `<sys/random.h>`,而 libc++ 只 include 后者。修在 openkal-musl 的 overlay (见 mcpplibs/openkal-musl#6)。 ⭐ 第三层最要紧:**那条默认路径在 openkal 上走不通,而它不报错, 只会在运行期打不开设备。** 实测,完整链路全程走接口层: std::random_device → getentropy → getrandom → kal_random_fill → 后端 x86_64-linux random_device: 3415089597 356442272 differ = true x86_64-windows random_device: 1945321362 2880234856 differ = true (wine) `entropy() = 0` 是 libc++ GETENTROPY 后端的既定行为,不是缺陷。
1 parent 8111a2e commit da3c311

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

llvm-generated/generic/__config_site

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,22 @@
4747
/* #undef _LIBCPP_NO_VCRUNTIME */
4848
/* #undef _LIBCPP_TYPEINFO_COMPARISON_IMPLEMENTATION */
4949
#define _LIBCPP_HAS_FILESYSTEM 1
50-
#define _LIBCPP_HAS_RANDOM_DEVICE 0
50+
#define _LIBCPP_HAS_RANDOM_DEVICE 1
51+
52+
/* ⚠️ AND THE BACKEND, EXPLICITLY, BECAUSE THE DEFAULT ONE CANNOT WORK HERE.
53+
*
54+
* libc++ picks `_LIBCPP_USING_DEV_RANDOM` for anything it does not recognise,
55+
* and that backend opens `/dev/urandom` by absolute path. A capability-oriented
56+
* filesystem hands a program its roots rather than the whole namespace, so
57+
* openkal.fs deliberately cannot open it --- the refusal is the model working.
58+
*
59+
* `getentropy` needs no descriptor and no path. Beneath it musl calls
60+
* `getrandom`, which this port routes to `kal_random_fill` rather than to a
61+
* system call, so the whole chain stays inside the interface layer:
62+
*
63+
* std::random_device → getentropy → getrandom → openkal.random → backend
64+
*/
65+
#define _LIBCPP_USING_GETENTROPY 1
5166
#define _LIBCPP_HAS_LOCALIZATION 1
5267
#define _LIBCPP_HAS_UNICODE 1
5368
#define _LIBCPP_HAS_WIDE_CHARACTERS 1

mcpp.toml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -253,12 +253,29 @@ sources = [
253253

254254
# The standard library.
255255
"llvm/libcxx/src/*.cpp",
256-
# openkal has no source of entropy and this port does not invent one, so the
257-
# configuration sets _LIBCPP_HAS_RANDOM_DEVICE to 0 --- and the source that
258-
# defines std::random_device must then not be compiled, which is what libc++'s
259-
# own build does with the same switch. A program that names random_device is
260-
# told by the linker, which is clause 6.1's report.
261-
"!llvm/libcxx/src/random.cpp",
256+
# ⭐ AND `random.cpp` IS BUILT, BECAUSE openkal NOW HAS A SOURCE OF ENTROPY.
257+
#
258+
# It used to be excluded, paired with `_LIBCPP_HAS_RANDOM_DEVICE 0`, and the
259+
# note here said openkal had no source and this port would not invent one.
260+
# That was accurate: a clock reading is unpredictable to a reader of the
261+
# source and not to an adversary, and openkal.fs deliberately cannot open
262+
# `/dev/urandom` --- a capability-oriented filesystem hands over roots rather
263+
# than the whole namespace.
264+
#
265+
# ⚠️ Neither bypassing the interface layer nor inventing entropy was
266+
# acceptable, so the layer gained an interface: `openkal.random`. The chain
267+
# now runs entirely inside it:
268+
#
269+
# std::random_device → getentropy → getrandom → kal_random_fill → backend
270+
#
271+
# ⚠️ THE BACKEND IS SELECTED EXPLICITLY IN `__config_site`. libc++ falls back
272+
# to `_LIBCPP_USING_DEV_RANDOM` for anything it does not recognise, and that
273+
# one opens `/dev/urandom` by absolute path. `_LIBCPP_USING_GETENTROPY` needs
274+
# no descriptor and no path.
275+
#
276+
# ⚠️ The freestanding configuration keeps the switch at 0: a bare-metal
277+
# backend provides `openkal.random` only if its board has a source, and
278+
# clause 6.1 reports the absence at link time rather than here.
262279
"llvm/libcxx/src/filesystem/*.cpp",
263280
"llvm/libcxx/src/ryu/*.cpp",
264281
"llvm/libcxx/src/support/runtime/*.cpp",

0 commit comments

Comments
 (0)