The fastest RISC-V sandbox
libriscv is a simple, slim and complete sandbox that is highly embeddable and configurable. It is a specialty emulator that specializes in low-latency, low-footprint emulation. libriscv may be the only one of its kind. Where other solutions routinely require ~50-150ns to call a VM function and return, libriscv requires 3ns. libriscv is also routinely faster than other interpreters, JIT-compilers and binary translators. libriscv has specialized APIs that make passing data in and out of the sandbox safe and low-latency.
There is also a CLI that you can use to run RISC-V programs and step through instructions one by one, like a simulator, or to connect with GDB in order to remotely live-debug programs. The CLI has many build and run-time options, so please check out the README.
For discussions & help, visit Discord.
libriscv is an ultra-low latency emulator, designed specifically to have very low overheads when sandboxing certain programs, such as game scripts. All the listed goals reflect current implementation.
Goals:
Non goals:
STREAM benchmark CoreMark: 38223 vs 41382 native (~92%).
Run D00M 1 in libriscv and see for yourself. It should use around 2% CPU at 60 fps.
Benchmark between binary translated libriscv vs LuaJIT, interpreted libriscv vs LuaJIT and also interpreted libriscv vs Luau. Most benchmarks are hand-picked for the purposes of game engine scripting, but there are still some classic benchmarks.
Register vs stack machines (interpreted)
RISC-V is a register machine architecture, which makes it very easy to reach good interpreter performance without needing a register allocator.
We can see that libriscv in interpreter mode is substantially faster than other interpreters.
libriscv can call the above function 5.25-7.25 times before wasmtime has called it once. libriscv beats all established emulators for short-running functions due to its substantially lower latency.
See the example project for directly embedding libriscv using CMake. You can also use libriscv as a packaged artifact, through the package example, although you will need to install the package first.
On Windows you can use Clang-cl in Visual Studio. See the example CMake project. It requires Clang and Git installed.
docker build . -t libriscv
docker run -v $PWD/binaries:/app/binaries --rm -i -t libriscv binaries/A fib(256000000) program for testing is built automatically. You can test-run it like so:
docker run -v $PWD/binaries:/app/binaries --rm -i -t libriscv fibYou can enter the docker container instead of using it from the outside. A 64-bit RISC-V compiler is installed in the container, and it can be used to build RISC-V programs.
docker run -v $PWD/binaries:/app/binaries --entrypoint='' -i -t libriscv /bin/bashInside the container you have access to rvlinux, and the compilers riscv64-linux-gnu-gcc-13 and riscv64-linux-gnu-g++-13. There is also rvlinux-fast which uses binary translation to make emulation a lot faster, but needs time to compile beforehand.
On Ubuntu and Linux distributions like it, you can install a 64-bit RISC-V GCC compiler for running Linux programs with a one-liner:
sudo apt install gcc-14-riscv64-linux-gnu g++-14-riscv64-linux-gnuDepending on your distro you may have access to GCC versions 10 to 14. Now you have a full Linux C/C++ compiler for 64-bit RISC-V.
To build smaller and leaner programs you will want a (limited) Linux userspace environment. Check out the guide on how to build a Newlib compiler.
cd emulator
./build.sh
./rvlinux Check out the CLI documentation.
If you have built the emulator, you can use ./rvlinux --gdb /path/to/program to enable GDB to connect. Most distros have gdb-multiarch, which is a separate program from the default gdb. It will have RISC-V support already built in. Start your GDB like so: gdb-multiarch /path/to/program. Make sure your program is built with -O0 and with debuginfo present. Then, once in GDB connect with target remote :2159. Now you can step through the code.
Most modern languages embed their own pretty printers for debuginfo which enables you to go line by line in your favorite language.
The emulator currently supports the RVA23U64 profile, -march=rva23u64 in GCC 16+. More specifically, RV32GCVB, RV64GCVB (imafdcbv_zicsr_zifence_zicond_zfa_zcb) and RV128G.
The 128-bit ISA support is experimental, and the specification is not yet complete.
Load a Linux program built for RISC-V and run through main:
…In order to have the machine not throw an exception when the instruction limit is reached, you can call simulate with the template argument false, instead:
machine.simulate(5'000'000ull);If the machine runs out of instructions, it will now simply stop running. Use machine.instruction_limit_reached() to check if the machine stopped running because it hit the instruction limit.
You can limit the amount of (virtual) memory the machine can use like so:
const uint64_t memsize = 1024 * 1024 * 64ul;
riscv::Machine machine { binary, { .memory_max = memsize } };You can find the MachineOptions structure in common.hpp.
You can find details on the Linux system call ABI online as well as in the docs. You can use these examples to handle system calls in your RISC-V programs. The system calls emulate normal Linux system calls, and is compatible with a normal Linux RISC-V compiler.
Check out the C API and the test project.
You can create your own custom instruction loop if you want to do things manually by yourself:
…Pausing and resuming is a first-class feature in libriscv. Using the same example as above with an additional outer loop, we can keep stopping as many times as we want to until the program ends normally.
…The function machine.instruction_limit_reached() only returns true when the instruction limit was reached, and not if the machine stops normally. Using that we can keep going until either the machine stops, or an exception is thrown.
You can create a machine without a binary, with no ELF loader invoked:
Machine machine;
machine.setup_minimal_syscalls();
std::vector my_program {
0x29a00513, // li a0,666
0x05d00893, // li a7,93
0x00000073, // ecall
};
// Set main execute segment (12 instruction bytes)
const uint32_t dst = 0x1000;
machine.cpu.init_execute_area(my_program.data(), dst, 12);
// Jump to the start instruction
machine.cpu.jump(dst);
// Geronimo!
machine.simulate(1'000ull);The fuzzing program does this, so have a look at that. There are also unit tests.
See this unit test for an example on how to add your own instructions. They work in all simulation modes.
Integrating libriscv into your projects
Using an RSP server:
Using the debugging wrapper:
The binary translation feature (accessible by enabling the RISCV_BINARY_TRANSLATION CMake option) can greatly improve performance in most cases, but requires compiling the program on the first run. The RISC-V binary is scanned for code blocks that are safe to translate, and then a C compiler is invoked on the generated code.
No open issues yet, or sync has not completed.