News for hackers. Stuff that matters.
Re:Writing Rust the Elixir Way by rapsey on Monday November 30, 2020 @09:35AM (#25254091)
Wait why does calling a crashing c function not crash the process?
Re:Writing Rust the Elixir Way by nahuel0x on Monday November 30, 2020 @09:48AM (#25254251)
the C code is compiled to WASM bytecodes, they are interpreted in a WASM VM. If the C function crash, it does it inside the VM, so the crash is contained.
Re:Writing Rust the Elixir Way by rapsey on Monday November 30, 2020 @09:55AM (#25254330)
Well then that is not comparable to NIFs at all. In fact it is an extremely misleading example.
Re:Writing Rust the Elixir Way by kibwen on Monday November 30, 2020 @11:39AM (#25255661)
Why is it misleading? If BEAM could isolate crashes in NIFs, it absolutely would. Either way the point is that you are able to leverage C code (though in Rust's case the point is just to leverage existing code, since you don't need to drop into C for performance).
Re:Writing Rust the Elixir Way by rapsey on Monday November 30, 2020 @12:01PM (#25255955)
Because you are not actually calling a native compiled C lib you are calling wasm code. A lot of the time you can not compile the C code to wasm and what do you do then? How do you interface with OS libraries?
Re:Writing Rust the Elixir Way by kibwen on Monday November 30, 2020 @02:42PM (#25258002)
You may be focusing too closely on the C aspect; the point of the demonstration in the OP is to show that Lunatic can gracefully sandbox and interoperate with any code that can be compiled to WASM, of which C code is just one example. Ideally you wouldn't need to access any OS libs from within the sandbox (indeed, much of the point of the sandbox is to provide alternatives to OS facilities), and even if you did you could still access those libs in an un-sandboxed form from within your ordinary Rust code (and yes, crashing at that point would take down the process, but it's still strictly better to have an option to run C code that doesn't take down the process when crashing).
Re:Writing Rust the Elixir Way by identity0 on Monday November 30, 2020 @01:29PM (#25257139)
Agreed. It's not a "foreign" function if everything is in WASM. All the VM sees is WASM code; it doesn't make a difference if it was originally written in C or Rust.
Re:Writing Rust the Elixir Way by kibwen on Monday November 30, 2020 @02:48PM (#25258068)
"Foreignness" is an incidental property, the actual goal is interoperation. Being able to interoperate with C code from within the sandbox is both useful and something that BEAM doesn't do. In the meantime, there's nothing preventing anyone from doing regular FFI from within the part of the Rust program that lives outside of Lunatic, if for whatever reason the sandbox is insufficient.
Re:Writing Rust the Elixir Way by akashakya on Monday November 30, 2020 @10:12AM (#25254524)
I don't know much about WASM, but how does this work with shared libraries? Is it even possible to call a shared library without any safety guarantees?
Re:Writing Rust the Elixir Way by brabel on Monday November 30, 2020 @10:20AM (#25254638)
WASM runs in a sandbox, it's not possible to call a shared library directly in the same way as you do in C... all sys calls are "imported" functions that a host exposes to the WASM code running... this host has the ability to do anything around a sys call, which is likely how they manage to forbid access to network/filesystem for example.

The host can be the browser when running on the browser, or it can be one of the WASM runtimes (wasmtime, wasmer, Lucet)