Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
# CLAUDE.md

This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.

## Project Overview

PolkaVM is a general-purpose RISC-V based virtual machine targeting RV32EM. It provides secure sandboxed execution with both an interpreter and a JIT compiler backend.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not against adding this, but it should be manually edited before committing so that it makes sense. For example:

  • RV32EM is not what we target anymore (although 32-bit is still technically supported).
  • Saying which exact Rust versions are used is useless, and will go out of date eventually.
  • Incorrect or irrelevant points should be removed (e.g. 'linker.rs: Runtime linking of host functions' is incorrect and the naming scheme is mostly a historical artifact since I was originally basing this on wasmtime's API, or "Performs register allocation via regalloc2" is a minor detail and gross misrepresentation that will most likely confuse the AI, since register allocation is only done in a very specific case)
  • The environment variable section is probably useless

etc.

If you're not sure about something feel free to just remove it. :P

Copy link
Copy Markdown
Member Author

@xermicus xermicus Jan 13, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought claude may have some logic in what it puts inside this file when generating it. It did actually not too bad in creating the file for paritytech/revive. Apparently here it's just mainly AI slop.

Manually reviewed it and changed a few things. I don't know yet what exactly to put in there and what not. But the file is intended to be changed over time anyways.


## Build Commands

```bash
# Build all workspace crates
cargo build

# Build with release optimizations
cargo build --release

# Run all tests (both dev and release)
./ci/jobs/build-and-test.sh

# Run tests for a specific crate
cargo test -p polkavm
cargo test -p polkavm-linker
cargo test -p polkavm-common

# Run tests with all features
cargo test -p polkavm-common --all-features

# Run a single test
cargo test -p polkavm -- test_name

# Linting
./ci/jobs/clippy.sh
cargo clippy -p polkavm

# Formatting
cargo fmt --check --all
cargo fmt --all
```

## Guest Programs

Guest programs (programs that run inside the VM) are in `guest-programs/` and use a separate workspace with nightly Rust:

```bash
cd guest-programs

# Build guest programs (uses custom RISC-V target)
cargo build --release

# The custom target is at: crates/polkavm-linker/targets/legacy/riscv32emac-unknown-none-polkavm.json
```

Guest programs require nightly Rust (`nightly-2025-05-10`) with `rust-src` component for `-Z build-std`.

## Architecture

### Core Crates (`crates/`)

- **polkavm**: Main VM crate with `Engine`, `Module`, `Instance` API. Contains:
- `interpreter.rs`: Cross-platform interpreter backend
- `compiler.rs`: JIT compiler (x86_64 Linux/macOS/FreeBSD)
- `sandbox/`: Process isolation for compiled code
- `api.rs`: Public API types
- `linker.rs`: Runtime linking of host functions

- **polkavm-common**: Shared types and utilities
- `program.rs`: Program blob format, instruction definitions, parsing
- `assembler.rs`: Assembly/disassembly support
- `abi.rs`: VM ABI definitions (memory map, calling conventions)

- **polkavm-linker**: Offline linker that transforms ELF to PolkaVM format
- Reads RISC-V ELF files from guest compilation
- Performs register allocation via regalloc2
- Outputs `.polkavm` program blobs

- **polkavm-zygote**: Linux-only process template for sandboxed execution (separate workspace)

- **polkavm-derive**: Proc macros for guest programs (`#[polkavm_import]`, `#[polkavm_export]`)

### Tools (`tools/`)

- **polkatool**: CLI for inspecting/disassembling `.polkavm` blobs
- **spectool**: Generates test vectors for VM specification
- **gastool**: Gas metering analysis (Linux only)
- **benchtool**: Performance benchmarking suite

### Execution Backends

The VM supports two backends controlled via `Config::set_backend()` or `POLKAVM_BACKEND` env var:
- `interpreter`: Cross-platform, always available
- `compiler`: JIT to native code, requires x86_64 + (Linux | macOS with `generic-sandbox` feature | FreeBSD with `generic-sandbox` feature)

### Environment Variables

- `POLKAVM_BACKEND`: `auto`, `compiler`, `interpreter`
- `POLKAVM_SANDBOX`: `auto`, `linux`, `generic`
- `POLKAVM_ALLOW_EXPERIMENTAL`: Enable experimental features
- `POLKAVM_TRACE_EXECUTION`: Enable execution tracing (for debugging)

## Testing

```bash
# Full test suite
./ci/jobs/build-and-test.sh

# Run with execution tracing
POLKAVM_TRACE_EXECUTION=1 POLKAVM_ALLOW_INSECURE=1 cargo run -p hello-world-host

# Fuzzing (requires cargo-fuzz)
cd fuzz
cargo fuzz run fuzz_polkavm -- -runs=10000
```

## Rust Version

- Main workspace: Rust 1.86.0 (stable)
- Guest programs: nightly-2025-05-10

## Key Types

- `Engine`: VM engine instance, configured via `Config`
- `Module`: Compiled program, created from `ProgramBlob`
- `Instance`: Execution instance with memory state
- `Linker`: Links host functions to module imports
- `ProgramBlob`: Serialized program format