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

make system random work in VM #17059

Merged
merged 13 commits into from
Feb 17, 2021
Merged

make system random work in VM #17059

merged 13 commits into from
Feb 17, 2021

Conversation

ringabout
Copy link
Member

No description provided.

@@ -158,7 +158,8 @@ elif defined(windows):
result = randomBytes(addr dest[0], size)

Copy link
Member

@timotheecour timotheecour Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how about adding this at top-level:

when defined(nimExperimentalSysrand):
  ... # all module goes here

refs timotheecour#575

until we stabilize the API

that flag can still be used in stdlib eg in random.randomize:

when defined(nimExperimentalSysrand):
  # use std/sysrandom
else:
  # use time based version

EDIT: would require when defined(nimExperimentalSysrand) or defined(booting): so that it also works with vmops

lib/std/sysrand.nim Outdated Show resolved Hide resolved
compiler/vmops.nim Outdated Show resolved Hide resolved

proc getNodeAddr*(a: VmArgs; i: Natural): PNode =
doAssert i < a.rc-1
doAssert a.slots[i+a.rb+1].kind == rkNodeAddr
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in future work we may need to revisit whether to use assert instead of doAssert in most places in this file (depends whether it's a bottleneck)

compiler/vmconv.nim Outdated Show resolved Hide resolved
@@ -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`.
Copy link
Member

@timotheecour timotheecour Feb 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is cleaner, less different code paths:

proc urandom*(size: Natural): seq[byte] {.inline.} =
  ## Returns random bytes suitable for cryptographic use.
  result = newSeq[byte](size)
  if not urandom(result):
    when not defined(js):
      raiseOSError(osLastError())

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But I can reduce a JS call.

compiler/vmops.nim Outdated Show resolved Hide resolved
Copy link
Member

@timotheecour timotheecour left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM; remaining suggestions can be done either here or in future work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants