Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "make system random work in VM" #17377

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions compiler/vmconv.nim
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ proc toLit*[T](a: T): PNode =
elif T is tuple:
result = newTree(nkTupleConstr)
for ai in fields(a): result.add toLit(ai)
elif T is seq:
result = newNode(nkBracket)
for ai in a:
result.add toLit(ai)
elif T is object:
result = newTree(nkObjConstr)
result.add(newNode(nkEmpty))
Expand Down
7 changes: 0 additions & 7 deletions compiler/vmhooks.nim
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,3 @@ proc getNode*(a: VmArgs; i: Natural): PNode =
doAssert i < a.rc-1
doAssert a.slots[i+a.rb+1].kind == rkNode
result = a.slots[i+a.rb+1].node

proc getNodeAddr*(a: VmArgs; i: Natural): PNode =
doAssert i < a.rc-1
doAssert a.slots[i+a.rb+1].kind == rkNodeAddr
let nodeAddr = a.slots[i+a.rb+1].nodeAddr
doAssert nodeAddr != nil
result = nodeAddr[]
54 changes: 9 additions & 45 deletions compiler/vmops.nim
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,25 @@

# Unfortunately this cannot be a module yet:
#import vmdeps, vm
from std/math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
from math import sqrt, ln, log10, log2, exp, round, arccos, arcsin,
arctan, arctan2, cos, cosh, hypot, sinh, sin, tan, tanh, pow, trunc,
floor, ceil, `mod`, cbrt, arcsinh, arccosh, arctanh, erf, erfc, gamma,
lgamma

when declared(math.copySign):
from std/math import copySign
from math import copySign

when declared(math.signbit):
from std/math import signbit

from std/os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir,
getAppFilename, raiseOSError, osLastError

from std/md5 import getMD5
from std/times import cpuTime
from std/hashes import hash
from std/osproc import nil
from std/sysrand import urandom
from math import signbit

from os import getEnv, existsEnv, dirExists, fileExists, putEnv, walkDir, getAppFilename
from md5 import getMD5
from sighashes import symBodyDigest
from times import cpuTime

from hashes import hash
from osproc import nil

# There are some useful procs in vmconv.
import vmconv

template mathop(op) {.dirty.} =
Expand Down Expand Up @@ -317,35 +313,3 @@ proc registerAdditionalOps*(c: PCtx) =
let fn = getNode(a, 0)
setResult(a, (fn.typ != nil and tfNoSideEffect in fn.typ.flags) or
(fn.kind == nkSym and fn.sym.kind == skFunc))

if vmopsDanger in c.config.features:
proc urandomImpl(a: VmArgs) =
doAssert a.numArgs == 1
let kind = a.slots[a.rb+1].kind
case kind
of rkInt:
setResult(a, urandom(a.getInt(0)).toLit)
of rkNode, rkNodeAddr:
let n =
if kind == rkNode:
a.getNode(0)
else:
a.getNodeAddr(0)

let length = n.len

## TODO refactor using vmconv.fromLit
var res = newSeq[uint8](length)
for i in 0 ..< length:
res[i] = byte(n[i].intVal)

let isSuccess = urandom(res)

for i in 0 ..< length:
n[i].intVal = BiggestInt(res[i])

setResult(a, isSuccess)
else:
doAssert false, $kind

registerCallback c, "stdlib.sysrand.urandom", urandomImpl
9 changes: 4 additions & 5 deletions lib/std/sysrand.nim
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,7 @@ elif defined(windows):
result = randomBytes(addr dest[0], size)

elif defined(linux):
# TODO using let, pending bootstrap >= 1.4.0
var SYS_getrandom {.importc: "SYS_getrandom", header: "<sys/syscall.h>".}: clong
let SYS_getrandom {.importc: "SYS_getrandom", header: "<sys/syscall.h>".}: clong
const syscallHeader = """#include <unistd.h>
#include <sys/syscall.h>"""

Expand Down Expand Up @@ -212,7 +211,7 @@ elif defined(freebsd):
# errno is set to indicate the error.

proc getRandomImpl(p: pointer, size: int): int {.inline.} =
result = getrandom(p, csize_t(size), 0)
result = getrandom(p, csize_t(batchSize), 0)

elif defined(ios):
{.passL: "-framework Security".}
Expand Down Expand Up @@ -287,7 +286,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 the call succeeds, returns `true`.
## If succeed, returns `true`.
##
## If `dest` is empty, `urandom` immediately returns success,
## without calling underlying operating system api.
Expand All @@ -308,4 +307,4 @@ proc urandom*(size: Natural): seq[byte] {.inline.} =
when defined(js): discard urandomInternalImpl(result)
else:
if not urandom(result):
raiseOSError(osLastError())
raiseOsError(osLastError())
35 changes: 6 additions & 29 deletions tests/stdlib/tsysrand.nim
Original file line number Diff line number Diff line change
@@ -1,36 +1,13 @@
discard """
targets: "c cpp js"
matrix: "--experimental:vmopsDanger"
"""

import std/sysrand


template main() =
block:
var x = array[5, byte].default
doAssert urandom(x)

block:
var x = newSeq[byte](5)
doAssert urandom(x)

block:
var x = @[byte(0), 0, 0, 0, 0]
doAssert urandom(x)

block:
var x = @[byte(1), 2, 3, 4, 5]
doAssert urandom(x)

block:
doAssert urandom(0).len == 0
doAssert urandom(10).len == 10
doAssert urandom(20).len == 20
doAssert urandom(120).len == 120
doAssert urandom(113).len == 113
doAssert urandom(1234) != urandom(1234) # unlikely to fail in practice


static: main()
main()
doAssert urandom(0).len == 0
doAssert urandom(10).len == 10
doAssert urandom(20).len == 20
doAssert urandom(120).len == 120
doAssert urandom(113).len == 113
doAssert urandom(1234) != urandom(1234) # unlikely to fail in practice