-
-
Notifications
You must be signed in to change notification settings - Fork 751
getrandom() backwards compatibility shim
#10741
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1774,19 +1774,65 @@ else | |
|
|
||
| version (linux) | ||
| { | ||
| // `getrandom()` was introduced in Linux 3.17. | ||
| version (linux_legacy_emulate_getrandom) | ||
| { | ||
| /+ | ||
| Emulates `getrandom()` for backwards compatibility | ||
| with outdated kernels and legacy libc versions. | ||
|
|
||
| `getrandom()` was added to the GNU C Library in v2.25. | ||
| +/ | ||
| pragma(msg, "`getrandom()` emulation for legacy Linux targets is enabled."); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is obviously a bit spammy. Maybe we should comment it out? |
||
|
|
||
| /+ | ||
| On modern kernels (5.6+), `/dev/random` would behave more similar | ||
| to `getrandom()`. | ||
| However, this emulator was specifically written for systems older | ||
| than that. Hence, `/dev/urandom` is the CSPRNG of choice. | ||
|
|
||
| <https://web.archive.org/web/20200914181930/https://www.2uo.de/myths-about-urandom/> | ||
| +/ | ||
| private static immutable _pathLinuxSystemCSPRNG = "/dev/urandom"; | ||
|
|
||
| import core.sys.posix.sys.types : ssize_t; | ||
|
|
||
| /+ | ||
| Linux `getrandom()` emulation built upon `/dev/urandom`. | ||
| Parameter `flags` is happily ignored. | ||
| +/ | ||
| private ssize_t getrandom( | ||
| void* buf, | ||
| size_t buflen, | ||
| uint flags, | ||
| ) @system nothrow @nogc | ||
| { | ||
| import core.stdc.stdio : fclose, fopen, fread; | ||
|
|
||
| // Shim for missing bindings in druntime | ||
| version (none) | ||
| import core.sys.linux.sys.random : getrandom; | ||
| auto blockDev = fopen(_pathLinuxSystemCSPRNG.ptr, "r"); | ||
| if (blockDev is null) | ||
| assert(false, "System CSPRNG unavailable: `fopen(\"" ~ _pathLinuxSystemCSPRNG ~ "\")` failed."); | ||
| scope (exit) fclose(blockDev); | ||
|
|
||
| const bytesRead = fread(buf, 1, buflen, blockDev); | ||
| return bytesRead; | ||
| } | ||
| } | ||
| else | ||
| { | ||
| import core.sys.posix.sys.types : ssize_t; | ||
| extern extern(C) ssize_t getrandom( | ||
| void* buf, | ||
| size_t buflen, | ||
| uint flags, | ||
| ) @system nothrow @nogc; | ||
| // `getrandom()` was introduced in Linux 3.17. | ||
|
|
||
| // Shim for missing bindings in druntime | ||
| version (none) | ||
| import core.sys.linux.sys.random : getrandom; | ||
| else | ||
| { | ||
| import core.sys.posix.sys.types : ssize_t; | ||
| private extern extern(C) ssize_t getrandom( | ||
| void* buf, | ||
| size_t buflen, | ||
| uint flags, | ||
| ) @system nothrow @nogc; | ||
| } | ||
| } | ||
| } | ||
|
|
||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ibuclaw Is this something you’d like me to incorporate?
Or was it more like a note to yourself?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would have been nice if this was an enum, as that can survive in the installed sources after the library has been built, and there's already other tests whose results get written to this module at configure time.
https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libphobos/libdruntime/gcc/config.d.in;h=e5fcd5073a96a2ffdee25c33348fc73bdddb6bbb;hb=HEAD
Have added this, though minor annoyance that it's its own thing rather than using the existing "features" module.
https://gcc.gnu.org/git/?p=gcc.git;a=blobdiff;f=libphobos/m4/druntime/os.m4;h=ef8ca434407149c176e5d93ab4012d53511048e3;hp=15cde3b04b8d47a66f83fa6ae56057359b66adac;hb=b905ce8caf04253e02e153d60d6ea8f99d300af6;hpb=882d3b319dbf50ae64080731a1398031c100b7c7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fortunately, this is a function, and not a template. Otherwise this shim would be a non-starter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Anything urgent here?
Otherwise I’d rather wait to see this getting hopefully replaced by something like #10748 in the near future anyway.