Rust Esp32 Iot
Rust Esp32 Iot
https://www.rust-lang.org/
Rust
“Rust is a language for systems programming.”
Jim Blandy & Jason Orendorff, Programming Rust
http://rigaux.org/language-study/diagram.html
History of Rust
• Started 2006 by Mozilla employee Graydon Hoare
• Announced 2010 by the Mozilla Foundation
• Self-compiled 2011
• Getting things right, before moving on
• A community to grow the language, not only use it
“Rust was the third-most-loved
programming language in the 2015
Stack Overflow annual survey, and took
first place in 2016, 2017, 2018, and
2019.” – Wikipedia
Fixing stuff at compile time
• Have a compiler which understands your code
• Have language rules which prevent bugs
• Eliminate “undefined behavior”
• Reduce “unexpected behavior”
What is undefined behavior?
Undefined behavior: behavior, upon use of a nonportable or
erroneous program construct or of erroneous data, for which this
International Standard imposes no requirements
§3.4.3, Cx11
Code Output
fn main() { Compiling playground v0.0.1 (/playground)
error[E0597]: `s2` does not live long enough
--> src/main.rs:8:13
let mut s1 = "Foo"; |
8 | x = &mut s2;
let mut x = &mut s1; | ^^^^^^^ borrowed value does not live long enough
assert_eq!(*x, "Foo"); 9 | }
| - `s2` dropped here while still borrowed
10 | assert_eq!(*x, "Bar");
{ | ---------------------- borrow later used here
|
let mut s2 = "Bar"; = note: this error originates in a macro outside of the current crate (in
x = &mut s2; Nightly builds, run with -Z external-macro-backtrace for more info)
} error: aborting due to previous error
assert_eq!(*x, "Bar"); For more information about this error, try `rustc --explain E0597`.
error: Could not compile `playground`.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cc5f9c6bbb709ac1c0e292b3042a4b40
The cost of a bug over time…
Bugs
100
90
80
70
60
50
40
30
20
10
0
Compile Test Release
Bugs
…with Rust
Bugs
100
90
80
70
60
50
40
30
20
10
0
Compile Test Release
Bugs
Dependencies
• “Crates” are the “JAR files of
Rust” … but contain code, not
binaries!
• Then “crates.io” is “Maven My App
Central for Rust”
• “cargo” manages
dependencies, and foo bar baz
orchestrates the build
and test libbaz.so
std
baz.dll
The problem with „std“
• „std“ provides all kinds of functionality
• Files, Streams, …
• Network, Sockets, …
• …
• But also requires a POSIX-like operating system
• So, what about embedded systems? Like the ESP32?
#![no_std], “core” & “alloc”
• You can disable the usage of “std” and switch to “core” instead
• If you can provide an allocator, you can also use “alloc” for
dynamic memory allocations (like String, Vec, …)
• Some crates support this by using “features”, which
enable/disable features of the crate at compile time
• e.g. “serde” with “serde-json-core”
https://crates.io/keywords/nostd
Unsafe Superpowers
• How?
• “unsafe {}” block
• “unsafe” keyword
• Why?
• Call other „unsafe“ methods
• Dereference raw pointers
• …
ESP-IDF & Rust
My App
serde / JSON
Rust
core alloc „timer“ „http“
https://github.com/ctron/rust-esp32-hono
End-to-end example
IDE Integration
Eclipse Corrosion
IntelliJ + Rust
One more thing…
How to compile for the Xtensa
architecture?
Forked LLVM, forked rustc and a bunch of
scripts
• Execute ~50 different commands
• Hope you have the right OS, in the right version, with the right
packages
• Wait ~3½ hours
• Enjoy … or try again
Containerized
• docker run quay.io/ctron/rust-esp
• –ti –v $PWD:/home/project
• Runs on Windows, Linux, (and should on Mac OS)
What lies ahead?
• Rust Runtime for AWS Lambda
• https://aws.amazon.com/blogs/opensource/rust-runtime-for-aws-
lambda/
• Rust Embedded Book
• https://rust-embedded.github.io/book/
• Linux kernel experiments with Rust
• https://lwn.net/Articles/797828/
• Microsoft
• https://msrc-blog.microsoft.com/2019/07/16/a-proactive-approach-
to-more-secure-code/
• „~70% of the vulnerabilities Microsoft assigns a CVE each
year continue to be memory safety issues”
Questions?
… and answers!
A few links
• Rust • Rust for ESP32
• https://www.rust-lang.org/ • https://github.com/ctron/rust-
esp-container/
• Rust Embedded Book
• https://rust- • Rust, ESP32, ESP-IDF, Hono
embedded.github.io/book/ • https://github.com/ctron/rust-
esp32-hono
• Programming Rust
• O'Reilly Media • LLVM for Xtensa
• https://github.com/espressif/llv
• Eclipse Corrosion m-xtensa
• https://marketplace.eclipse.or
g/content/corrosion-rust- • Rust fork for Xtensa
edition-eclipse-ide • https://github.com/MabezDev/r
ust-xtensa
Thank you!