Contents

C Memory Safety and IOT

Contents

C Type Safety

The major issues with coding in C include memory safety bugs such as use-after-free or use-before-initialization bugs, null pointer exceptions, and manual memory management. Around 70 percent of the vulnerabilities addressed in Chrome and Microsoft Windows over the last decade can be attributed to memory management issues. C happily allows the developer ultimate control over memory, leading to potential memory bugs and vulnerabilities that can be invisible to the developer or non-trivial to hunt down. In IoT devices, we rely heavily on systems programming languages for direct hardware access, which can sometimes lead to disastrous outcomes. Now, more than ever, we need the compiler to be our ally, helping us prevent the majority of bugs from making it to production in the IoT devices that will soon be found nearly everywhere.

Take the following C example: /images/posts/c_rust_memory_safety/image1.png

In this C program, we are allocating memory, freeing it, and then trying to use it. This is a use-after-free bug. These can be disastrous as the memory area might have been repurposed and might hold different data, leading to unreliable program behavior.

And a similar example in Rust: /images/posts/c_rust_memory_safety/image2.png

In the Rust version, our variable b is moved into the drop function which consumes it, deallocating the memory. if we attempt to compile this example we will get a compile-time error: “value borrowed after move”. Rust’s ownership system prevents use-after-free and other memory bugs at compile time, leading to safer code. Take a look at the beautiful error message that the rust compiler gives to us if we try to compile an example like this:

/images/posts/c_rust_memory_safety/image3.png

/images/posts/c_rust_memory_safety/image4.png

/images/posts/c_rust_memory_safety/image5.png

Embedded systems and IoT devices cannot afford use after free bugs and adopting languages like Rust could be a great strategy to combat the pervasive memory management bugs. These devices are frequently deployed in critical applications, and writing resilient code is essential. Rust’s main strength is its type system and ownership model that provide compile-time guarantees about the memory safety of the program. For instance, Rust’s borrow checker ensures that references do not outlive the data they refer to, effectively preventing use-after-free bugs. IoT devices, which handle sensitive data and need to respond in real time, require robust security and performance. Rust, designed with a focus on “zero-cost abstractions”, “move semantics”," guaranteed memory safety", and “threads without data races”, provides the ideal features needed for IoT development. In fact, Rust’s ownership model makes it stand out compared to other systems programming languages. It delegates the responsibility of managing memory from the programmer to the compiler through “ownership” with rules that are checked at compile-time.