-
Notifications
You must be signed in to change notification settings - Fork 69
docs: add Rust toolchain section #2046
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
980e64c
chore: add Rust toolchain section
yi-sun 1976005
feat: add information about host and guest
yi-sun 43278df
feat: add memory allocator details
yi-sun d0b79a7
feat: mention tier 3 target
jonathanpwang d6caf5d
feat: explicate openvm rt features
jonathanpwang 8a2b13d
Update docs/vocs/docs/pages/book/writing-apps/compiling-a-program.mdx
jonathanpwang f989232
docs: add PAL ABI details
jonathanpwang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 3 additions & 7 deletions
10
docs/vocs/docs/pages/book/writing-apps/compiling-a-program.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# OpenVM Rust Frontend | ||
|
||
OpenVM supports a Rust frontend via compilation to a 32-bit RISC-V target which is | ||
then transpiled into an OpenVM executable with instructions from the RV32IM VM extension. | ||
We implement this by **cross-compiling** for a platform which differs from the machine | ||
performing the build. This involves the following: | ||
|
||
- **Host**: The target and compiler toolchain used to run the program build and proving binaries. | ||
- **Guest**: The target and compiler toolchain used to build the program to be proven. | ||
|
||
We detail the host and guest target and toolchain as well as the guest runtime for the | ||
OpenVM Rust frontend below. | ||
|
||
## Host and Guest Target and Toolchain | ||
|
||
The OpenVM Rust frontend supports the following host and guest target and toolchains: | ||
|
||
- **Host**: We support `aarch64-apple-darwin` and `x86_64-unknown-linux-gnu` with Rust 1.86.0: `rustc 1.86.0 (05f9846f8 2025-03-31)`. | ||
For reproducible builds, we recommend using the `x86_64-unknown-linux-gnu` platform. | ||
- **Guest**: `riscv32im-risc0-zkvm-elf` with Rust `nightly-2025-02-14`: `rustc 1.86.0-nightly (a567209da 2025-02-13)`. | ||
|
||
The `riscv32im-risc0-zkvm-elf` guest target incorporates special support for | ||
zkVMs and has official support by the | ||
[Rust toolchain](https://doc.rust-lang.org/rustc/platform-support/riscv32im-risc0-zkvm-elf.html). | ||
We anticipate upstreaming an OpenVM-specific target to Rust in the future. | ||
|
||
:::info | ||
We use a nightly Rust toolchain for the guest target in order to compile the Rust standard library for this target because it is a [Tier 3](https://doc.rust-lang.org/beta/rustc/target-tier-policy.html#tier-3-target-policy) target. | ||
::: | ||
|
||
## Guest Runtime | ||
|
||
The OpenVM Rust runtime supports `no_std` Rust by default, with optional `std` support available | ||
through the `"std"` feature. This section documents the different features of the runtime. | ||
|
||
### Memory Allocator | ||
|
||
OpenVM supports 512MB of guest memory, with stack growing down from `STACK_TOP = 0x0020_0400`. | ||
program loading starting at `TEXT_START = 0x0020_0800`, and heap starting right afterwards. | ||
We support two allocators: | ||
|
||
- A **bump** allocator which increments a heap pointer for each successive allocation without | ||
deallocating. This is the default allocator. | ||
- A **linked-list** allocator from the `embedded-alloc` crate which supports deallocation | ||
at the cost of additional allocation overhead. | ||
|
||
The linked-list allocator can be selected by enabling the `heap-embedded-alloc` feature on the | ||
`openvm` crate. | ||
|
||
### System Calls | ||
|
||
OpenVM currently does not support any system calls via the RISC-V `ecall` instruction. Instead, OpenVM supports [custom RISC-V instruction set extensions](/specs/reference/riscv-custom-code) directly via VM extensions. | ||
|
||
In particular, [support](#std-support) for the Rust `std` library is implemented via custom RISC-V instructions. | ||
|
||
### OpenVM Intrinsics | ||
|
||
OpenVM supports [custom RISC-V instructions](/specs/reference/riscv-custom-code#classification-of-custom-risc-v-machine-code), known as **intrinsic instructions**, within the RISC-V ELF binary. These instructions may be inserted directly from the Rust program code using the Rust [`asm!` macro](https://doc.rust-lang.org/reference/inline-assembly.html) and the [`.insn` directive](https://doc.rust-lang.org/reference/inline-assembly.html#r-asm.directives.supported-directives). | ||
|
||
For convenience, we define two procedural macros `custom_insn_i!` and `custom_insn_r!` that provide more streamlined interfaces for calling intrinsic instructions within Rust code. These macros are defined in the `openvm-custom-insn` crate and are re-exported in the `openvm-platform` crate. They may be accessed from the `openvm` crate via `openvm::platform::custom_insn_i!` and `openvm::platform::custom_insn_r!`. | ||
|
||
### OpenVM Kernels | ||
|
||
OpenVM also supports insertion of [custom kernel code](/specs/reference/riscv-custom-code#classification-of-custom-risc-v-machine-code) into the RISC-V ELF. Kernel code is used as a means to statically link foreign OpenVM assembly code into the ELF without a custom linker. Custom kernel code should be inserted from Rust using the [`asm!` macro](https://doc.rust-lang.org/reference/inline-assembly.html) and the [`.insn` directive](https://doc.rust-lang.org/reference/inline-assembly.html#r-asm.directives.supported-directives). We recommend that the kernel code is generated in a separate build script and then directly included within the `asm!` macro invocation using `include_str!`. | ||
|
||
### `std` Support | ||
|
||
OpenVM supports compilation of guest code using the Rust `std` library when the `openvm` crate is imported with the `"std"` feature enabled. When the `"std"` feature is enabled, the `openvm` crate defines `extern "C"` functions that are compatible with the platform abstraction layer (PAL) ABI defined in the Rust standard library for the guest target. These ABI definitions are statically linked with the Rust standard library at compile time. | ||
|
||
The PAL ABI is implemented in `openvm` using Rust and direct calls to [OpenVM intrinsics](#openvm-intrinsics) without the use of system calls. | ||
|
||
Users should be aware of the limitations of the Rust `std` support within OpenVM, which are documented in the [book](/book/writing-apps/writing-a-program#rust-std-library-support). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.