News for hackers. Stuff that matters.
Re:WebAssembly Micro Runtime by janekm on Friday May 10, 2019 @04:35AM (#19876108)
I was wondering if it would be suitable for micro controllers (with let's say a few hundred kB of memory, not MB). I can't immediately find the answer but there appears to be a port to Zephyr OS in the repo so that is promising.

There's also the beginnings of a sensor manager and API so clearly small embedded use cases are intended.

It's still a very small project (looks like one contributor so far), but that's no bad thing...

Re:WebAssembly Micro Runtime by justinclift on Friday May 10, 2019 @04:41AM (#19876130)
TinyGo (Go for embedded devices - AVR, Cortex, Bluepill, STM32F4, etc) compiles to Wasm as well:

https://github.com/tinygo-org/tinygo/

No garbage collector yet, but it's on the ToDo list and probably not too far off. :)

Re:WebAssembly Micro Runtime by snarfy on Friday May 10, 2019 @09:02AM (#19877297)
Do you really want garbage collection when your device has 2KB of ram?
Re:WebAssembly Micro Runtime by justinclift on Friday May 10, 2019 @09:34AM (#19877521)
It depends. There's no one-size-fits-everything approach for this stuff. :)
Re:WebAssembly Micro Runtime by kbumsik on Friday May 10, 2019 @10:19AM (#19877922)
No, you can't run GC at all. The minimum size of WASM heap is 64KiB by its spec. Otherwise it should run in stack-only mode. So running GC for microcontroller with less than that size doesn't make much sense.
Re:WebAssembly Micro Runtime by sitkack on Friday May 10, 2019 @07:28AM (#19876774)
We already have Lua [1] and MicroPython [2] as well as a myriad of Forths, Scheme, etc running on MCUs. So arguably Wasm would be a better fit for an MCU over these two as it doesn't need to be parsed and the interpreter doesn't need to handle dynamic types.

[1] http://www.eluaproject.net/

[2] https://micropython.org/

Re:WebAssembly Micro Runtime by radarsat1 on Friday May 10, 2019 @08:19AM (#19877013)
What would be the advantage of running WASM on a microcontroller instead of compiling the code directly for the microcontroller's architecture?
Re:WebAssembly Micro Runtime by jononor on Friday May 10, 2019 @10:28AM (#19878016)
I'm interested in WASM for microcontrollers to have the application guaranteed free of side-effects (due to the sandboxing). This allows to be test the logic thoroughly in isolation, and have reasonably high certainty that it does not do anything funky - like write to some hardware register in some edge-case.

Such code would not be able to do I/O directly, but must receive it from outside the sandbox. It could be a pure function implementing a state machine on form `state, outputs = next(state, inputs)`, where state,inputs and outputs are plain data. This structure is amendable to generating testcases, be it via property testing or fuzzing. Or regression tests by capturing a stream of state transitions in serialized form.

There are languages which makes this possible, like Haskell, or maybe even Rust or Ada. However in the embedded world, C is what everyone knows and uses, so there would be a benefit to staying within that ecosystem.

Re:WebAssembly Micro Runtime by unwind on Friday May 10, 2019 @10:57AM (#19878374)
Cool, but aren't side-effects just what you want when using a microcontroller to, you know, control things?

I work in embedded development and the amount of pure logic is really small in our software but I guess that varies with the niche you're in.

Re:WebAssembly Micro Runtime by pault on Friday May 10, 2019 @11:02AM (#19878426)
All software is a model of side-effects, but if you delegate the execution of your side-effects to a third party (usually the language's runtime or an application framework) all of your code can be side-effect free. If neither of those things is available, you can at least isolate your side-effect peforming code to a small footprint.
Re:WebAssembly Micro Runtime by jononor on Friday May 10, 2019 @01:02PM (#19879775)
Yes, you are very right. The function of most microcontroller/embedded systems are almost all input/output (sensing/actuating). The challenge is that combined software+hardware systems can be hard to test, especially together.

The traditional way to simplify QA is to use a Hardware Abstraction Layer in the code, which hardware side-effects happen through. So you have a classic layer cake like:

    | App Logic (imperative, stateful)         |
    | Hardware Abstraction Layer (interface)   |
    | Input/Output (implementations)           |
Then during testing of the software application logic, a mocked implementation of the HAL can be used. When done with due care, this works OK. In this basic model it is OK to read input and cause outputs "anywhere" in the application logic. That is easy and convenient, but (I argue) that this causes pure logic to be rare. Which is unfortunate, since it would be much easier to test. State also tends to be spread across the code-base, which can hide stateful behavior that is critical to test.

In the proposed model, the HAL functionality is split into two distinct parts: input and output drivers. And the application logic does not call the HAL, but gets called with Input and produces a description of Output. So the layer cake kinda tips sideways:

    HW input driver > Input (data) >  App Logic (pure) > Output (data) > HW output driver
                                   |                   |
                                   <      State        <
During test of application logic we have:

    input generator -> App Logic -> output capture
and can then perform validations on who sequences of Input/Output pairs. And debugging can trivially access whole sequences of State changes.

Those familiar with dataflow programming might find this very familiar. Frontend people might see parallels to unidirectional flow of data in reactive UI frameworks like React. Simulation minded people might see that this structure is very amendable to Discrete-Event Simulation.

I have used this model to good effect across many (relatively simple) embedded/IoT systems over the last few years. One thing I really like, is that it makes temporal logic very easy. Because in this model the current time (be it ticks or wall-time) is just a type of input. So it is easy to stimulate, visualize and make assertions across whole timelines of behavior, and one can see many such timelines at the same time. Similar to what Brett Victor showed with Mario in Inventing on Principle.

Have been meaning to write more about this for a long time, so apologies for the mini blogpost :) Some very related writing in http://www.jonnor.com/2017/03/host-based-simulation-for-embe...

PS: the comment by pault is to the point, and right on the money.

Re:WebAssembly Micro Runtime by chubot on Friday May 10, 2019 @04:55PM (#19882141)
It's great to see these software ideas being used in embedded systems. From my limited experience with embedded code, most engineers in the domain treat their software more like hardware. They test it WITH specific hardware and don't abstract it, abstraction being more of a software thing.

I would like to read a blog post about what you're doing. It would also be great to connect it to the other software communities doing similar things with different names.

I think this encountered these ideas in the Java world around 2005, with their strong focus on testing. It's basically "dependency injection" or dependency inversion.

I just did some Googling and found this good overview:

https://gist.github.com/kbilsted/abdc017858cad68c3e7926b0364...

It's very much an idea in the "enterprise" software world. I've never been in that world but it does seem like they are grappling with complex problems and systems, and this architecture has proven itsef in that domain. It's not surprising to me that it's also useful in the embedded domain.

I would say it's just "modularity". If your dependencies are hard-coded, then you have no modularity in your software at all. The whole thing is one big ball of mud which you can either take or leave. "Functions" aren't really modular if they have nontrivial hard-coded dependencies! (i.e. particularly ones that do I/O or depend on state).

Other names:

- capability based security / Object capabilities (WASM is influenced by these ideas, which originated from Eros OS as far as I know. The E language was an influential object capability language.) The idea of "ambient authority" is useful.

- https://news.ycombinator.com/item?id=14523728 -- a thread where Go programmers are rediscovering dependency injection. You can "invert" state or I/O. Those are independent decisions, but the same concept. In my larger programs, I tend to abstract both of them.

- In Haskell, you use State and IO monads. They are parameters, not things you go "looking for".

- Good overview of enterprise world: https://blog.cleancoder.com/uncle-bob/2012/08/13/the-clean-a... (Clean architecture, ports and adapters, onion architecture, hexagonal architecture, functional core / imperative shell, etc.)

I think Java eventually went overboard with "DI frameworks", which I never used. I just wire up the dependencies manually in main() and it works fine.

-----

Also, I bought a somewhat obscure many years about dataflow programming and hardware, which I think it somewhat related:

https://www.amazon.com/Programming-SIMPL-Way-John-Collins/dp...

At its most fundamental, SIMPL is a set of library functions which allow the passing of encapsulated messages between cooperating processes. These processes may be local to one host computer or spread over a network.

Re:WebAssembly Micro Runtime by yorwba on Friday May 10, 2019 @08:28AM (#19877052)
I assume this is meant for cases where there's no LLVM backend for the target architecture and you're forced to use the vendor's proprietary C compiler. Then you can use that to compile this runtime and run any program written in a WASM-targeting language on top of it.
Re:WebAssembly Micro Runtime by janekm on Friday May 10, 2019 @08:58AM (#19877263)
I'm assuming safer loadable modules (there's no memory protection on most mcus) and choice of languages that have wasm backends. In some circumstances being able to run the same code in a browser in a test environment as on the device could also be great (for education for example).
Re:WebAssembly Micro Runtime by zaarn on Friday May 10, 2019 @05:11AM (#19876243)
I would imagine a scaled down and cut version of WASM could be viable on MCU's as a form of interpreted program to be loaded from an external memory device, the WASM compiler/interpreter would likely already take much of a small MCU's memory.
Re:WebAssembly Micro Runtime by baybal2 on Friday May 10, 2019 @06:46AM (#19876620)
It will be much more ahead of interpreters running on MCUs (overhead of parsing alone is enough to disqualify them for production use,) but still hopelessly far behind native code.

WASM was made with running on regular desktop CPUs in mind. Wasm is not a truly a state machine, nor stack machine, but a horrible hybrid in between them thanks to design by committee.

Changeable locals, accessible undefineds, use of stack pointer protection technique that kills both pipelining and speculative execution. Yes, that's combined downsides of both stack and register VMs.

There were MCU that were naively running Java long time ago, but all failed on the market. The idea proved non feasible

Re:WebAssembly Micro Runtime by Zardoz84 on Friday May 10, 2019 @06:59AM (#19876662)
So BASIC Stamp, was a dream... I know that was popular to do some automatization on industrial machines.
Re:WebAssembly Micro Runtime by boomlinde on Friday May 10, 2019 @09:03AM (#19877302)
> It will be much more ahead of interpreters running on MCUs (overhead of parsing alone is enough to disqualify them for production use,) but still hopelessly far behind native code.

To be clear, this is also interpreted, so it doesn't have an advantage there.

Re:WebAssembly Micro Runtime by rjsw on Friday May 10, 2019 @09:17AM (#19877399)
> There were MCU that were naively running Java long time ago, but all failed on the market. The idea proved non feasible

Not sure they failed for technical reasons, embedded CPUs just got fast enough and typical memory big enough to run a JIT.

Re:WebAssembly Micro Runtime by baybal2 on Friday May 10, 2019 @10:25AM (#19877985)
To begin with, why to run JIT if you can run optimised to the limit native code?

Really no value there. The positive connotation of "web native" label is not so positive in the world of dour and severe people which "hardcore embedded developers" are.

Re:WebAssembly Micro Runtime by rjsw on Friday May 10, 2019 @10:48AM (#19878266)
The main limiting factor for embedded is cost. If you start from the position that you want to run something that was written in Java then there will be a selection of ways to do that. Most Java applications will require external RAM to the CPU, the lowest cost DRAM chips will be whatever is used at that time in high-volume consumer electronics, when ARM chips with Jazelle were introduced the DRAM capacity that represented this optimal price had increased by a lot from when those ARM chips were designed, increased to the point that the increased memory requirements of a JIT were no longer a problem.
Re:WebAssembly Micro Runtime by i4t on Friday May 10, 2019 @05:43AM (#19876360)
Yes, definitely aimed at embedded devices.