webassembly
by Dan Mihai Dumitriu

Why WebAssembly Is Perfect for Tiny IoT Devices

As the world becomes more interconnected, the number of internet of things (IoT) devices has exploded. These devices come in a wide range of shapes and sizes, from massive industrial machines to tiny sensors. While larger devices may run on Linux or other operating systems, smaller devices require a different approach. In this article, I’ll explain why WebAssembly (Wasm) is the perfect runtime for tiny IoT devices that are too small for Linux and may need to run on battery power.

Challenges

First, let’s take a look at the challenges of running a traditional operating system on tiny IoT devices. These devices are typically low-powered and have limited memory and storage capacity. Running a full-blown operating system such as Linux requires a significant amount of resources, which can quickly drain the battery life of the device. Additionally, these devices may not have the hardware necessary to support a full Linux OS, such as MMU (memory management unit) required for hardware virtual memory. Various RTOS (Real Time Operating Systems) exist for tiny IoT devices – some examples include FreeRTOS, ThreadX, and NuttX. As such, we cannot use Linux containers (aka Docker) as a unit of isolation on these tiny IoT devices.

Second, the traditional development cycle for embedded systems, like IoT devices, differs significantly from that of cloud software. Although making changes to the code may be uncomplicated, delivering them to customers is a complex process. Typically, there is a monthly code freeze, during which all modifications undergo thorough hardware-in-the-loop testing, potentially on many types of devices. Following successful testing of the new release, a staged rollout process is initiated to prevent issues with firmware updates, customer complaints, or support escalations. It may take several weeks or even months for a minor code change to reach most customers. This process is no longer acceptable in the era of agile development and the cloud.

WebAssembly

Enter WebAssembly. Wasm is a stack based virtual machine and bytecode format. Originally designed for web browser plugins, it is not limited to just web applications. It’s a versatile runtime that can be used for a wide range of applications, including IoT devices. Wasm is designed to be small and efficient, which makes it a great fit for tiny IoT devices. Wasm modules are typically a few kilobytes in size, which is much smaller than a typical Linux kernel or a Linux container. This small size means that Wasm can run on devices with limited memory and storage capacity, with native performance.

Another advantage of using Wasm for IoT devices is that it’s a platform-independent runtime. This means that Wasm modules can be written in any programming language and executed on any platform that supports Wasm. This flexibility makes it easy to develop applications for IoT devices, regardless of the hardware or software environment.

Developers can write code in their preferred programming language, and then compile it to Wasm, which can be executed on the target device. C, C++, Rust, JavaScript are well supported. Languages that require garbage collection are less well supported, but that is due to change as the Wasm spec evolves.

Wasm modules are executed in a sandboxed environment, which means that they’re isolated from the rest of the system. This makes it difficult for attackers to exploit vulnerabilities in the system, as they’re unable to access the underlying operating system or hardware. This is particularly important for tiny IoT devices which have no virtual memory. Additionally, Wasm modules can be verified, ahead of deployment, and signed, which adds an extra layer of security to the system.

Some Wasm runtimes support AoT (Ahead of Time) compilation, which takes the Wasm bytecode and produces machine code for the target CPU/MCU type. This is very useful, or even essential, in the context of tiny IoT devices which may not have the available CPU and memory to perform JIT (Just in Time) compilation, as we typically do in the cloud or on the desktop. The AoT compilation can be part of a cloud service which manages the lifecycle of software deployed on the IoT devices. AoT compiled code can run nearly at native speeds, and orders of magnitude faster than interpreted languages like Micropython.

Maturity

While Wasm has been around for several years, it’s still a relatively new technology. There are not as many tools and libraries available for Wasm as there are for traditional programming languages and frameworks. Some parts of the Wasm spec are still evolving, such as native support for GC required by popular languages like Python. However, this is changing rapidly, as more and more developers adopt Wasm for a variety of applications.

Another difficulty of using Wasm for IoT devices is the lack of support for hardware-specific functionality. Since Wasm is a platform-independent runtime, it doesn’t have direct access to the hardware of the device, or to peripherals such as sensors. Developers must use a combination of Wasm and native code to access the hardware of the device, and create a WASI API extension that abstracts the hardware functionality and exposes it to the Wasm module. The Wasm community is working to standardize various system interfaces in the WASI (WebAssembly System Interface) specification.

Conclusion

Overall, the benefits of using Wasm for IoT devices far outweigh the potential drawbacks. Wasm is a lightweight, efficient, and secure runtime that’s perfect for devices with limited resources. It’s also flexible and platform-independent, which makes it easy to develop applications for a wide range of hardware and software environments, and bring agile development to tiny IoT devices. As the number of IoT devices continues to grow, Wasm will become an increasingly important tool for developers who are looking to create efficient and secure applications for these devices. With the increasing importance of edge computing
and the rise of IoT, it’s clear that WebAssembly is set to play a significant role in the future of computing.

Share