make system random work in VM (#17059)

* make system random work in VM
This commit is contained in:
flywind 2021-02-17 04:52:46 -06:00 • committed by GitHub
commit 4f118721be
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 90 additions and 19 deletions

View file

@ -158,7 +158,8 @@ elif defined(windows):
result = randomBytes(addr dest[0], size)
elif defined(linux):
let SYS_getrandom {.importc: "SYS_getrandom", header: "<sys/syscall.h>".}: clong
# TODO using let, pending bootstrap >= 1.4.0
var SYS_getrandom {.importc: "SYS_getrandom", header: "<sys/syscall.h>".}: clong
const syscallHeader = """#include <unistd.h>
#include <sys/syscall.h>"""
@ -209,7 +210,7 @@ elif defined(freebsd):
# errno is set to indicate the error.
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
result = getrandom(p, csize_t(batchSize), 0)
result = getrandom(p, csize_t(size), 0)
elif defined(ios):
{.passL: "-framework Security".}
@ -284,7 +285,7 @@ proc urandomInternalImpl(dest: var openArray[byte]): int {.inline.} =
proc urandom*(dest: var openArray[byte]): bool =
## Fills `dest` with random bytes suitable for cryptographic use.
## If succeed, returns `true`.
## If the call succeeds, returns `true`.
##
## If `dest` is empty, `urandom` immediately returns success,
## without calling underlying operating system api.
@ -305,4 +306,4 @@ proc urandom*(size: Natural): seq[byte] {.inline.} =
when defined(js): discard urandomInternalImpl(result)
else:
if not urandom(result):
raiseOsError(osLastError())
raiseOSError(osLastError())