Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

libriscv

> 数据库
Open source

The fastest RISC-V sandbox

1.1K stars0 likes0 views
WebsiteGitHub

About

The fastest RISC-V sandbox

RISC-V sandboxing library

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.

Ultra-Low latency emulation

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:

  • Lowest possible latency
    • Calling a guest VM function can finish 1-2 orders of magnitude before other emulators begin executing the first instruction
  • Lightning-fast interpreter mode
  • Modern, type-safe VM call and system call interfaces
    • The safe interfaces prevents all kinds of footguns that the author has personally suffered, and consequently blocked off forever :-)
  • Secure speculation-safe sandbox
  • Low attack surface, only 20k LOC (35k with RVA23 and binary translation)
  • Platform-independent and super-easy to embed
    • Supports all architectures and all platforms, C++17 or later
  • Pause and resume is a first-class citizen
    • Pause on any instruction
    • Make preemptive function calls
    • Pause and serialize VM, deserialize and resume on another host/platform
  • Just-in-time compilation for development usage
    • libtcc can be used to instantly improve emulation of RISC-V programs
    • This is currently enabled by default
  • High-performance binary translation on end-user systems through DLLs (eg. Windows)
    • Cross-compile RISC-V programs to a binary translated .dll executable in libriscv on end-user systems (where shared libraries are allowed)
  • Maximum final-build performance on all platforms, including Consoles, Mobiles, production systems
    • Supports embeddable high-performance binary translations that can be baked into final builds (eg. where shared libraries are not allowed, or static builds are preferred)
  • Tiny memory footprint
    • Less than 40kB total memory usage for fibonacci program
  • High scalability with unique CoW-support and shared memories
    • Serve requests using ephemeral VMs in ~1us in production
    • Execute segments are automatically shared among all instances (with or without forking)
  • Dynamic linking and run-time dlopen() support (see CLI)
  • Supports sandboxing language-runtimes that use JIT-compilation, eg. V8 JavaScript and LuaJIT
    • JIT-compiled segments will be detected, processed and can use the fast-path dispatch

Non goals:

  • Wide support for Linux system calls
  • Higher performance at the cost of call latency

Benchmarks

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)

Benchmarks against other emulators

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.

Embedding the emulator in a project

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.

Emulator using Docker CLI

bash
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:

bash
docker run -v $PWD/binaries:/app/binaries --rm -i -t libriscv fib

You 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.

bash
docker run -v $PWD/binaries:/app/binaries --entrypoint='' -i -t libriscv /bin/bash

Inside 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.

Installing a RISC-V GCC compiler

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:

bash
sudo apt install gcc-14-riscv64-linux-gnu g++-14-riscv64-linux-gnu

Depending 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.

Running a RISC-V program

bash
cd emulator
./build.sh
./rvlinux 

Check out the CLI documentation.

Remote debugging using GDB

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.

Instruction set support

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.

Example usage when embedded into a project

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:

cpp
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:

cpp
	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.

Example C API usage

Check out the C API and the test project.

Handling instructions one by one

You can create your own custom instruction loop if you want to do things manually by yourself:

…

Pausing and resuming

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.

Setting up your own machine environment

You can create a machine without a binary, with no ELF loader invoked:

cpp
	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.

Adding your own instructions

See this unit test for an example on how to add your own instructions. They work in all simulation modes.

Documentation

Integrating libriscv into your projects

Fast custom RISC-V compiler

System calls

Freestanding environments

Function calls into the VM

Debugging with libriscv

Example programs

Unit tests

Remote GDB using RSP server

Using an RSP server:

  • Step through the code using GDB and your programs embedded pretty printers

Build your own interpreter loop

  • Using CPU::step_one(), one can step one instruction
  • Precise simulation with custom conditions

Using the DebugMachine wrapper

Using the debugging wrapper:

  • Simulate one instruction at a time
  • Verbose instruction logging
  • Debugger CLI with commands
  • Pause simulation on arbitrary conditions

Binary translation

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.

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

C++binary-translationcmakecppcpp20

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category数据库
PricingOpen source

> Related tools

P
PostgreSQL
功能强大的开源关系型数据库
R
Redis
内存数据结构存储,常用作缓存与队列
M
MySQL
广泛使用的开源关系型数据库