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

Implement Crystal::System::Random for win32 #5539

Merged
merged 1 commit into from
Jan 5, 2018

Conversation

RX14
Copy link
Contributor

@RX14 RX14 commented Jan 5, 2018

Nice and short implementation on this one!

module Crystal::System::Random
def self.random_bytes(buf : Bytes) : Nil
raise NotImplementedError.new("Crystal::System::Random.random_bytes")
unless LibC.RtlGenRandom(buf, buf.size)
Copy link
Contributor

@bew bew Jan 5, 2018

Choose a reason for hiding this comment

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

Will this work even though it can't be nil?
Put another way: It'll never enter the unless's body

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oops, fixed.

@RX14 RX14 force-pushed the feature/windows-system-random branch from 64ec074 to 7ffa7e4 Compare January 5, 2018 00:55
@asterite
Copy link
Member

asterite commented Jan 5, 2018

Just a question: is win32 just 32 bits of windows?

@RX14
Copy link
Contributor Author

RX14 commented Jan 5, 2018

@asterite no, it's just the name of the ABI, for example there's x86_64-windows-gnu and x86_64-windows-cygwin, but we want to support the win32 API directly, so that's the flag name we use. See this comment from ysbaddaden.

@asterite
Copy link
Member

asterite commented Jan 5, 2018

So Windows API is called Win 32? Even if it's running in a 64 bits machine? At least that's what I understand from the comment above, and why we can't just use windows as a flag name.


@[Link("advapi32")]
lib LibC
fun RtlGenRandom = SystemFunction036(random_buffer : Void*, random_buffer_length : ULong) : BOOLEAN
Copy link
Member

Choose a reason for hiding this comment

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

Why the alias?

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, I guess it's a #define

Copy link
Contributor Author

Choose a reason for hiding this comment

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

RtlGenRandom is what it's called in the windows documentation. The point of keeping the windows names is to be able to find the documentation easilly, so I think this is correct. It's much easier to read this way anyway.

Copy link
Contributor

Choose a reason for hiding this comment

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

Have a look to libsodium sysrandom. The function is known as RtlGenRandom, but the symbol is SystemFunction036 and kind of private API, but everybody uses it (libsodium, go, etc) because it avoids bundling the whole crypto API, which is apparently quite big.

An alias, here, is expected, yes :-)

@RX14
Copy link
Contributor Author

RX14 commented Jan 5, 2018

@asterite I think @ysbaddaden explained it perfectly in the comment I linked...

Cygwin and mingw also have flag?(:windows) set, even if they're using the gnu or cygwin ABIs.

@RX14 RX14 added this to the Next milestone Jan 5, 2018
@RX14 RX14 merged commit 6611a66 into crystal-lang:master Jan 5, 2018
@ysbaddaden
Copy link
Contributor

Yup, we must be pass windows in the target for LLVM to generate a Windows PE EXEcutable, but there are different ABI on Windows, and the microsoft one is... WIN32. Yes, even for Windows 64.

@straight-shoota straight-shoota added this to Todo in Windows via automation Jan 8, 2019
@straight-shoota straight-shoota moved this from Todo to Done in Windows Jan 8, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Windows
  
Done
Development

Successfully merging this pull request may close these issues.

None yet

4 participants