Skip to content

Conversation

@kubamracek
Copy link
Contributor

  • Make SystemRandomNumberGenerator, .random(), .random(in: ...), .randomElement() APIs available in embedded Swift.
  • Add tests exercising those.
  • Add a test that checks the symbol dependencies of program that use the random APIs, the only added dependency should be on a (relatively standard and commmon) arc4random_buf.

@kubamracek kubamracek requested a review from a team as a code owner December 14, 2023 01:49
@kubamracek kubamracek added the embedded Embedded Swift label Dec 14, 2023
Copy link
Contributor

Choose a reason for hiding this comment

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

are these functions weak? e.g. if I implement swift_stdlib_random in a platform definition to a non-cryptographic-secure-pseudo-random source but want to not claim arc4random_buf is implemented would that work?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is a good point, and I was previously thinking that we would just have Embedded Swift have a small fixed set of well-known basically-always-present-anyway dependencies, but I do see your point that it would really be useful to have more influence and control over things like this.

I don't think Swift has a notion of weak / override-able functions... does it? Though TBH even if it did, the linker based weak function replacement is arguably quite non-obvious and "too magical". I was thinking that an explicit solution would be better, something along the lines of:

public enum EmbeddedConfiguration {
  // default is arc4random_buf...
  public static var randomnessSource: (@convention(c) (UnsafeMutableRawPointer, Int) -> ()) = arc4random_buf
}

public func swift_stdlib_random(_ buf: UnsafeMutableRawPointer, _ nbytes: Int) {
  EmbeddedConfiguration.randomnessSource(buf, nbytes)
}

// ...but the user can change it by simply doing this in the firmware startup code:
EmbeddedConfiguration.randomnessSource = my_other_randomness_provider

@kubamracek kubamracek force-pushed the embedded-random branch 3 times, most recently from 1dd82e3 to 0a55f36 Compare January 4, 2024 17:43
@kubamracek
Copy link
Contributor Author

@swift-ci please test

1 similar comment
@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek
Copy link
Contributor Author

@swift-ci please test

Copy link
Member

@rauhul rauhul left a comment

Choose a reason for hiding this comment

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

good enough for now, I'd like the link time error to be missing "swift_stdllib_random" but we can improve this later

@kubamracek
Copy link
Contributor Author

@swift-ci please test

@kubamracek kubamracek enabled auto-merge January 24, 2024 18:15
@kubamracek kubamracek merged commit 302e106 into swiftlang:main Jan 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

embedded Embedded Swift

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants