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

[SR-685] Can't use arc4random on Linux #43300

Closed
drewcrawford opened this issue Feb 6, 2016 · 9 comments
Closed

[SR-685] Can't use arc4random on Linux #43300

drewcrawford opened this issue Feb 6, 2016 · 9 comments

Comments

@drewcrawford
Copy link
Contributor

@drewcrawford drewcrawford commented Feb 6, 2016

Previous ID SR-685
Radar None
Original Reporter @drewcrawford
Type Bug
Status Resolved
Resolution Won't Do
Environment

swift-DEVELOPMENT-SNAPSHOT-2016-02-03
Linux x64

Additional Detail from JIRA
Votes 2
Component/s Standard Library
Labels Bug, Linux, SDKOverlay
Assignee None
Priority

md5: 69637c9b621111a39912702a44128d6d

Issue Description:

Welcome to Swift version 3.0-dev (LLVM 524cfeb800, Clang 35aeed5957, Swift 7f77733b0c). Type :help for assistance.
  1> import Glibc
  2> arc4random_uniform(2)
repl.swift:2:1: error: use of unresolved identifier 'arc4random_uniform'

I'm not aware of any workaround for this issue.

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Feb 24, 2016

Comment by Trent Nadeau (JIRA)

There is no arc4random function in glibc. That function only exists in BSD distros and the Bionic libc used in Android.

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Feb 24, 2016

Comment by Michael Thomas (JIRA)

I created a modulemap of libbsd to get arc4random on Linux, see here.

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Feb 25, 2016

Comment by Jay Buffington (JIRA)

        #if os(Linux)
            return Int(random() % (max + 1))
        #else
            return Int(arc4random_uniform(UInt32(max)))
        #endif

@swift-ci
Copy link
Collaborator

@swift-ci swift-ci commented Mar 23, 2016

Comment by Johannes Schriewer (JIRA)

It may look absurd, but this actually works:

$ swift -Xcc -fmodule-map-file=libbsd.modulemap
Welcome to Swift version 3.0-dev (LLVM 699a786c15, Clang 77080f2c03, Swift d22638766e). Type :help for assistance.
  1> import BSD
  2> print(arc4random())
2257500811

When there is a module map file for libbsd with these contents:

module BSD [system] {
        header "/usr/include/bsd/bsd.h"
        link "bsd"
        export *
}

@Coeur
Copy link
Mannequin

@Coeur Coeur mannequin commented Sep 16, 2016

Please, never apply Jay Buffington workaround, as it would mislead people on Linux and potentially create threats.

@Dante-Broggi
Copy link
Contributor

@Dante-Broggi Dante-Broggi commented Jul 16, 2018

What should happen to this bug since SE-0202 (in Swift 4.2) encourages the new `Int.random(in: Range)` etc. instead?

@drewcrawford
Copy link
Contributor Author

@drewcrawford drewcrawford commented Jul 16, 2018

I vote to close wontfix, as I don't need it in 4.2. Feel free to speak up if you still do

@belkadan
Copy link
Contributor

@belkadan belkadan commented Jul 17, 2018

I think that's correct. We also now have the SwiftPM module map packages, in case someone wants to use Johannes' solution.

@Dante-Broggi
Copy link
Contributor

@Dante-Broggi Dante-Broggi commented Sep 30, 2018

Resolving because both the reporter (@drewcrawford) and @belkadan commented that it is "resolved" as "Won't Fix".

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants