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

Add SOLID target support #235

Merged
merged 6 commits into from
Nov 30, 2021
Merged

Conversation

kawadakk
Copy link
Contributor

This PR adds support for *-kmc-solid_*, a new Tier 3 target.

Copy link
Member

@newpavlov newpavlov left a comment

Choose a reason for hiding this comment

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

Thank you! Looks good apart from small nits.

@@ -30,6 +30,7 @@
//! | WASI | `wasm32‑wasi` | [`random_get`][17]
//! | Web Browser | `wasm32‑*‑unknown` | [`Crypto.getRandomValues()`][14], see [WebAssembly support][16]
//! | Node.js | `wasm32‑*‑unknown` | [`crypto.randomBytes`][15], see [WebAssembly support][16]
//! | SOLID | `*-kmc-solid_*` | `SOLID_RNG_SampleRandomBytes`
Copy link
Member

Choose a reason for hiding this comment

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

It would be nice to link a relevant documentation if it's available publicly.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, it's not available publicly (yet).

Copy link
Member

Choose a reason for hiding this comment

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

@newpavlov are you OK merging pre-release OS support? Or do you want to wait for external documentation?

Copy link
Member

@josephlr josephlr Oct 27, 2021

Choose a reason for hiding this comment

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

It looks like the OS is documented here: http://solid.kmckk.com/doc/skit/current/solid_os.html (in-browser Google translate is a must if you do not speak Japanese).

I found references to functions like:

However, there's no reference to SOLID_RNG_SampleRandomBytes. @kawadakk is this some sort of extension to the base SOLID system?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's optional but indeed included in the base system. It's added quite recently, and the documentation hasn't been updated yet.

Copy link
Member

Choose a reason for hiding this comment

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

Awesome! If that's the case I'd be fine with adding this support now.

Copy link
Member

Choose a reason for hiding this comment

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

@kawadakk is there documentation for SOLID_RNG_SampleRandomBytes anywhere? I'd love to update out documentation for this.

Copy link
Contributor Author

@kawadakk kawadakk Jan 31, 2023

Choose a reason for hiding this comment

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

@josephlr Sorry, it's still unavailable. I'd be happy to provide information if you have something specific in mind.

Copy link
Member

Choose a reason for hiding this comment

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

Oh I just wanted to:

  • add a link to our docs when one is available
  • make sure that the signature for SOLID_RNG_SampleRandomBytes is correct.
    • We currently have the signature declaration as: fn(*mut u8, usize) -> i32
    • Similar functions in the SOLID docs have signatures like fn(*mut c_void, usize) -> c_int
    • I realize this signatures are API compatible, but we generally want to have our declarations exactly match those in the relevant OS docs.

src/solid.rs Outdated
pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let er = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr(), dest.len()) };
if let Some(er) = NonZeroU32::new(er as u32) {
Err(er.into())
Copy link
Member

Choose a reason for hiding this comment

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

For consistency with other targets it would be nice to use ret instead of er here. Also is there a meaningful information in those error code? If not, it may be worth to return a fixed constant as we do for example with iOS.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it contains meaningful information, such as -1001 (SOLID_ERR_NOTSUPPORTED) when the support is lacking.

src/solid.rs Show resolved Hide resolved
src/solid.rs Outdated

pub fn getrandom_inner(dest: &mut [u8]) -> Result<(), Error> {
let ret = unsafe { SOLID_RNG_SampleRandomBytes(dest.as_mut_ptr(), dest.len()) };
// ITRON error numbers are always negative, so we negate it so that it
Copy link
Member

Choose a reason for hiding this comment

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

Do these ITRON error numbers correspond to errno values?

Specifically, if the return value ret is negative, do we have that ret == -raw_os_error_code, where raw_os_error_code would be an errno value or something you would pass to io::Error::from_raw_os_error?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not exactly errno, but it can be meaningfully passed to io::Error::from_raw_os_error, provided that the negative sign is preserved.

Actually I just now realized that there exists impl From<Error> for io::Error, which passes the value returned by crate::Error::raw_os_error to io::Error::from_raw_os_error. I pushed the commit 730b326 to negate the error code back in crate::Error::raw_os_error. I hope it's not too much clutter to introduce outside the platform-specific code.

src/solid.rs Outdated
Comment on lines 21 to 25
if let Some(ret) = NonZeroU32::new((-ret) as u32) {
Err(ret.into())
} else {
Ok(())
}
Copy link
Member

@josephlr josephlr Oct 27, 2021

Choose a reason for hiding this comment

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

Instead of just trusting that this function returns 0i32 or a negative i32, could we explicitly check (like we do on other OSes). Something like:

if ret == 0 {
    Ok(())
} else if ret < 0 {
    // ITRON error numbers are always negative, so we negate it so that it
    // falls in the dedicated OS error range (1..INTERNAL_START).
    Err(NonZeroU32::new((-ret) as u32).unwrap())
} else {
    Err(Error::ERRNO_NOT_POSITIVE)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

After scouring documents and pondering for a bit, since all non-negative ITRON error codes are supposed to represent success, I think we can just check if it's negative here. (Implemented in 16f30f0)

@josephlr
Copy link
Member

@kawadakk can you also add support for one of the *-kmc-solid_asp3 targets in our CI configuration? No need for all of them, just aarch64-kmc-solid_asp3 would be fine.

Copy link
Member

@josephlr josephlr left a comment

Choose a reason for hiding this comment

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

Looks good to me, will let @newpavlov also approve before merging.

@newpavlov newpavlov merged commit f5e3300 into rust-random:master Nov 30, 2021
@kawadakk kawadakk deleted the feat-solid-support branch February 1, 2022 02:56
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