Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Using TLS in Rust: The Complexity of Async, Macros, and Madness

DZone's Guide to

Using TLS in Rust: The Complexity of Async, Macros, and Madness

Learn more about the complexity of the async I/O implementation.

· Security Zone ·
Free Resource

Discover how to provide active runtime protection for your web applications from known and unknown vulnerabilities including Remote Code Execution Attacks.

After a lot of trouble, I’m really happy that I was able to build an async I/O implementation of my protocol. However, for real code, I think that I would probably recommend using with the sync API instead since at least it's straightforward and doesn’t incur so much overhead at development time. The async stuff is still very much a “use at your own risk” kind of deal from my perspective. And I can’t imagine trying to use it in a large project and no suffering from the complexity.

As a good example, take a look at the following bit of code:

image

It doesn’t seem to be doing much, right? And it is clear what the intent of the code is.

However, if you try to compile this code, you’ll get:

image

Now, it took me a long time to figure out what is going on. The issue is that the code I’m seeing isn’t the actual code because of macro expansions.

So, let’s resolve this and see what the expanded code looks like:

fn send_results(
    receiver: Receiver<String>,
) -> impl ::futures::__rt::MyFuture<std::result::Result<(), io::Error>> + 'static {
::futures::__rt::gen(move || -> std::result::Result<(), io::Error> {
let __e: std::result::Result<(), io::Error> = {
            {
                {
let mut __stream = receiver;
loop {
let msg = {
extern crate futures_await;
let r = futures_await::Stream::poll(&mut __stream)?;
match r {
                                futures_await::Async::Ready(e) => match e {
                                    futures_await::__rt::std::option::Option::Some(e) => e,
                                    futures_await::__rt::std::option::Option::None => break,
                                },
                                futures_await::Async::NotReady => {
yield futures_await::Async::NotReady;
continue;
                                }
                            }
                        };
                        {
                            {
                                $crate::io::_print($crate::fmt::Arguments::new_v1_formatted(
&["", "\n"],
&match (&msg,) {
                                        (arg0,) => [$crate::fmt::ArgumentV1::new(
                                            arg0,
                                            $crate::fmt::Display::fmt,
                                        )],
                                    },
&[$crate::fmt::rt::v1::Argument {
                                        position: $crate::fmt::rt::v1::Position::At(0usize),
                                        format: $crate::fmt::rt::v1::FormatSpec {
                                            fill: ' ',
                                            align: $crate::fmt::rt::v1::Alignment::Unknown,
                                            flags: 0u32,
                                            precision: $crate::fmt::rt::v1::Count::Implied,
                                            width: $crate::fmt::rt::v1::Count::Implied,
                                        },
                                    }],
                                ));
                            };
                        }
                    }
                }
Ok(())
            }
        };
        #[allow(unreachable_code)]
        {
return __e;
loop {
yield ::futures::Async::NotReady
            }
        }
    })
}


This is after formatting, of course, but it certainly looks scary. Glancing at this code doesn’t tell me what the problem is, so I tried replacing the method with the expanded result, and I got the same error. But this time, I got it on a line that helped me figure it out. Here is the issue:

image

We use the ? to return early from the poll method, and the Receiver I’m using, in this case, is defined to have a Result<String, ()>, so this is the cause of the problem.

I returned my own error type as a result, giving me the ability to convert from (), but that was a really hard thing to resolve.

It might be better to have Rust also offer to show the error on the expanded code by default because it was somewhat of a chore to actually get to this.

What made this oh so confusing is that I had the exact same code, but using a Stream<String, io:Error> that worked, obviously. But it was decidedly non-obvious to see the difference between two identical pieces of code.

Find out how Waratek’s award-winning application security platform can improve the security of your new and legacy applications and platforms with no false positives, code changes or slowing your application.

Topics:
security ,tls ,rust ,macros ,async ,tutorial ,sync api

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}