Repository navigation
Expand file tree
/
Copy pathrandom.wave
More file actions
91 lines (83 loc) · 2.73 KB
/
Copy pathrandom.wave
File metadata and controls
91 lines (83 loc) · 2.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// This file is part of the Wave language project.
// Copyright (c) 2024–2026 Wave Foundation
// Copyright (c) 2024–2026 LunaStev and contributors
// SPDX-License-Identifier: Apache-2.0
// Provider contract: positive bytes filled, or a negative error. Zero for a
// nonempty request is an I/O failure. No provider may substitute weak entropy.
#[target(os="linux")]
import("std::sys::linux::syscall")::{syscall3};
#[target(os="macos")]
import("std::sys::macos::fs")::{open, read, close, FS_O_RDONLY};
#[target(os="linux", arch="x86_64")]
const GETRANDOM: i64 = 318;
#[target(os="linux", arch="aarch64")]
const GETRANDOM: i64 = 278;
#[target(os="linux", arch="riscv64")]
const GETRANDOM: i64 = 278;
#[target(os="linux", arch="loongarch64")]
const GETRANDOM: i64 = 278;
#[target(os="linux")]
pub fun sys_random_available() -> bool {
return true;
}
#[target(os="linux")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
return syscall3(GETRANDOM, buffer as i64, size, 0);
}
#[target(os="macos")]
pub fun sys_random_available() -> bool {
return true;
}
#[target(os="macos")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
var fd: i64 = open("/dev/urandom", FS_O_RDONLY, 0);
if (fd < 0) {
return fd;
}
var result: i64 = read(fd, buffer, size);
close(fd);
return result;
}
#[target(os="windows")]
pub import("std::sys::windows::random")::{sys_random_available, sys_random_read};
#[target(os="freebsd", arch="x86_64")]
pub import("std::sys::freebsd::amd64::random")::{sys_random_available, sys_random_read};
// Other FreeBSD architectures and WASI retain unavailable providers.
// WASI support requires an explicit host randomness capability (issue #367).
#[target(os="freebsd", arch="aarch64")]
pub fun sys_random_available() -> bool {
return false;
}
#[target(os="wasi")]
pub fun sys_random_available() -> bool {
return false;
}
#[target(os="unknown")]
pub fun sys_random_available() -> bool {
return false;
}
#[target(os="none")]
pub fun sys_random_available() -> bool {
return false;
}
#[target(os="freebsd", arch="aarch64")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
return -38;
}
#[target(os="wasi")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
// WASI Preview 1 NOSYS; bare WebAssembly retains its existing convention.
return -52;
}
#[target(os="unknown")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
return -38;
}
#[target(os="none")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 {
return -38;
}
#[target(os="freebsd", arch="riscv64")]
pub fun sys_random_available() -> bool { return false; }
#[target(os="freebsd", arch="riscv64")]
pub fun sys_random_read(buffer: ptr<u8>, size: i64) -> i64 { return -38; }