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

Use #[repr(transparent)] newtype to hold FFI error codes #696

Merged
merged 1 commit into from
Oct 22, 2018

Conversation

ecstatic-morse
Copy link
Contributor

@ecstatic-morse ecstatic-morse commented Oct 6, 2018

Resolves #451.

This PR changes the definition of some foreign functions to return a bssl::ErrorCode instead of c::int.

Converting to a Result is now done through a conversion function rather than bssl::map_result. It also defines marker traits for the two widely used return code conventions in this library:

  • 1 for success (used in most BoringSSL functions)
  • 0 for success

I agree to license my contributions to each file under the terms given
at the top of each file I changed.

Copy link
Owner

@briansmith briansmith left a comment

Choose a reason for hiding this comment

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

In general I like the idea of using a #[repr(transparent)] type for the return types of the non-Rust code but I disagree with several of the details.

GFp_aes_gcm_seal(ctx.as_ptr(), in_out.as_mut_ptr(), in_out.len(), tag,
nonce, ad.as_ptr(), ad.len())
})
}.into()
Copy link
Owner

Choose a reason for hiding this comment

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

I don't find this as clear as the existing code but I guess people would learn it fast enough.

Would Result::from be a workable improvement?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Using Result::from everywhere is fine. You have to explicitly qualify it anyways when using the ? operator if the Ok type returned by the function is not ().

Another option is to add a method like into_result to ErrorCode which solves the type deduction problem but doesn't require wrapping the whole unsafe block in another set of parens.

src/bssl.rs Outdated
use {c, error};

/// The meaning of the error code returned by a foreign function.
pub trait Semantics {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't see the advantage of factoring this out into a trait. One of the design principles of ring is to avoid unnecessary abstractions, and at least the way the code is currently, this abstraction isn't necessary.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Just two separate structs here then?

I had imagined that this would make it easy to share code later as features were added, but that's kind of the definition of overabstraction :)

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Oct 7, 2018

Choose a reason for hiding this comment

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

Also, how do you feel about using Posix to mean "returns zero on failure". It was the best, shortest thing I could think of, but bssl::PosixResult reads a bit strangely.

src/bssl.rs Outdated
1 => true,
c => {
// Be pedantic in debug mode to catch some cases where function signatures are
// wrongly defined.
Copy link
Owner

Choose a reason for hiding this comment

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

What are examples of such wrongly-defined functions?

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Oct 7, 2018

Choose a reason for hiding this comment

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

By wrongly defined I meant that an author adding a new extern declaration might mistakenly use the wrong return type (e.g. fn() -> bssl::ErrorCode for a function which returns a negative value on error).

Copy link
Owner

@briansmith briansmith Oct 7, 2018

Choose a reason for hiding this comment

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

In that case I would say "any cases where [whatever type name we decide on] might be misused."

src/bssl.rs Outdated
#[must_use]
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
pub struct ErrorCode<S = BoringSsl> {
Copy link
Owner

Choose a reason for hiding this comment

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

I don't think ErrorCode is a very good name for this boolean flag. I think Result would make more sense since BoringSSL's boolean results are basically Result<(), ()>s.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Agreed. I avoided it initially since modname::Result is usually a type alias for std::result::Result with the error type defined.

src/bssl.rs Outdated
pub enum BoringSsl {}

impl Semantics for BoringSsl {
fn is_success(code: c::int) -> bool {
Copy link
Owner

Choose a reason for hiding this comment

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

I would say, rather than defining an is_success() -> bool function, just inline this logic directly into the From implementation that does the conversion to Result<(), error::Unspecified>.

fn GFp_AES_set_encrypt_key(key: *const u8, bits: usize,
aes_key: *mut AES_KEY) -> c::int;
aes_key: *mut AES_KEY) -> bssl::PosixStyleErrorCode;
Copy link
Owner

Choose a reason for hiding this comment

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

Is this the only function like this you've found? I'd rather change the C code to use the regular boolean conventions and rename it, than add this complexity.

Copy link
Contributor Author

@ecstatic-morse ecstatic-morse Oct 7, 2018

Choose a reason for hiding this comment

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

There are also system APIs being used which return 0 on success, but to my knowledge this is the only currently used function from BoringSSL which has that convention.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Oct 7, 2018

There's also a WIN32 function which returns one on success, but uses a c::win32::BOOLEAN instead of a c::int.

Based on your feedback as well as this new information, I'd like to make the following sets of changes:

  • Define a new module called c::result to make clear that the newly added types should only be used for ffi.
  • Define transparent newtypes c::result::BoringSsl, c::result::Posix and c::result::Win32Bool to handle various commonly used return types and conventions.
  • Publicly alias either c::Result or bssl::Result to c::result::BoringSsl.
  • Add size/alignment tests for the newtypes via the define_test_metrics macro.

I could also eliminate the POSIX-style and Win32Bool wrappers, change AES_set_encrypt_key to return a BoringSSL style result (or nothing at all), and add a #[must_use] declaration to the various system APIs which return an error code. Would you prefer this approach?

I was just perusing the list of good-first-bugss and saw this one. Sorry if this took more time for you to review than it would to have implemented it. Thanks for all the work you've done in the Rust community!

@briansmith
Copy link
Owner

For that one single Win32 function, why not just change the FFI declaration of the function to use function-level #[must_use]?:

        #[link_name = "SystemFunction036"]
        #[must_use]
        fn RtlGenRandom(random_buffer: *mut u8,
                        random_buffer_length: c::win32::ULONG)
                        -> c::win32::BOOLEAN;

I suggest we skip the Posix case for now as I think it's trickier and also I hope we can eliminate the use of "POSIX" semantics from non-Posix functions like that AES API (in a separate PR).

For the GFp/BoringSSL API, maybe we should "just" use #[must_use] instead too? What are the pros/cons from your perspective?

[ ] Publicly alias either c::Result or bssl::Result to c::result::BoringSsl.

I suggest we just skip this, regardless.

Add size/alignment tests for the newtypes via the define_test_metrics macro.

Sounds good to me, if we need the newtypes.

@ecstatic-morse
Copy link
Contributor Author

ecstatic-morse commented Oct 7, 2018

Both approaches statically guarantee that error codes are being checked, so the issue is more about the conversion to Result currently done by bssl::map_result.

With the free function, the consumer must know the return code semantics at each call site, whereas with the newtype, the return code semantics must only be known at the declaration, and conversion can be done thoughtlessly via Result::from. Having to know the return code semantics at the call site isn't a large burden, especially if we normalize GFp_AES_set_encrypt_key to be consistent¸ but there will still be functions such as GFp_memcmp which return a c::int which doesn't indicate an error.

The presence or absence of #[must_use] shouldn't be used to indicate whether a function returns an error. GFp_memcmp could be marked #[must_use] as well so the compiler could warn when the result of a comparison is unused just as it does with a == b.

I understand the proliferation of return type wrappers being undesirable, especially given the efforts to oxidize ring, but I think it'd be good to provide one for the common case.

@briansmith
Copy link
Owner

OK, I think in this PR we should go with your approach for the GFp API, the approach I suggested above for the single Win32 function, and punt Posix to later. That should allow us to make progress here.

@ecstatic-morse
Copy link
Contributor Author

Got it! I'll make the changes later today. Do you want the path to the type to be bssl::Result, c::Result or some spelling of c::result::BoringSsl`?

appveyor.yml Outdated
@@ -11,7 +11,7 @@ platform:
environment:
matrix:
- TOOLCHAIN_VERSION: 14.0
RUST: 1.26.0
RUST: 1.28.0
Copy link
Owner

Choose a reason for hiding this comment

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

This should be done as a separate PR and we should be doing Rust 1.29.1.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is there a reason this isn't just "stable" instead of an explicit version number?

@@ -172,6 +172,9 @@ mod tests {
}

extern "C" {
// AES_set_encrypt_key returns 0 on success, unlike most BoringSSL functions.
// https://github.com/google/boringssl/blob/28babde159253bfa9003a445242605806fff5f1f/include/openssl/aes.h#L83
Copy link
Owner

Choose a reason for hiding this comment

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

Please remove the link to the BoringSSL code. If the comment being linked to is missing in ring then we should add the comment to ring.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I added a comment to ring. I'm going to fix this issue next, making it irrelevant.

src/bssl.rs Outdated
1 => Ok(()),
_ => Err(error::Unspecified),
/// A `c::int` returned from a foreign function containing **1** if the function was successful
/// or **0** if an error occurred. This is the convention used by BoringSSL.
Copy link
Owner

Choose a reason for hiding this comment

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

Let's remove the "This is the convention used by BoringSSL." I don't want to conflate ring with BoringSSL.

src/bssl.rs Outdated
/// or **0** if an error occurred. This is the convention used by BoringSSL.
#[must_use]
#[repr(transparent)]
#[derive(Debug, Clone, Copy)]
Copy link
Owner

Choose a reason for hiding this comment

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

Let's put #[derive(...)] first unless there's some reason not to. I usually sort these alphabetically when I remember to.

src/bssl.rs Outdated
c => {
// Be pedantic in debug mode to catch any cases where functions are mistakenly
// declared to return a BoringSSL-style error code when they really have
// different semantics.
Copy link
Owner

Choose a reason for hiding this comment

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

The comment is not necessary; let's just remove it.

src/bssl.rs Outdated
// Be pedantic in debug mode to catch any cases where functions are mistakenly
// declared to return a BoringSSL-style error code when they really have
// different semantics.
debug_assert_eq!(c, 0, "BoringSSL functions should return 0 or 1");
Copy link
Owner

Choose a reason for hiding this comment

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

I suggest "bssl::Result value must be be 0 or 1" instead.

src/bssl.rs Outdated
mod tests {
mod result {
use {bssl, c};
use std::mem;
Copy link
Owner

Choose a reason for hiding this comment

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

use core::mem instead of std::mem. In general we use core instead of std whenever possible.

Copy link
Owner

@briansmith briansmith left a comment

Choose a reason for hiding this comment

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

Thanks! This looks good, modulo a few nits.

@briansmith
Copy link
Owner

@ecstatic-morse This looks great. Please squash this PR into a single commit that ends with the "I agree ..." statement. The commit message should be wrapped to ~72 characters.

I suggest this as the commit message (wrapped to ~72 characters):

Replace the `bssl::map_result` function with a new `bssl::Result` type.

I agree to ....

This commit introduces a `#[repr(transparent)]` newtype to represent
the return type of foreign functions which return 1 on success and 0 on
failure. This type is `#[must_use]`, so these return codes must be
checked by the caller.

It also adds `#[must_use]` to foreign functions which use a different
convention to return an error.

I agree to license my contributions to each file under the terms given
at the top of each file I changed.
@ecstatic-morse
Copy link
Contributor Author

Squashed and rebased.

@briansmith briansmith merged commit 408089d into briansmith:master Oct 22, 2018
@briansmith
Copy link
Owner

Thanks!

@ecstatic-morse ecstatic-morse deleted the ffi-return-type branch October 23, 2018 02:25
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.

2 participants