zydis-rs
A pure Rust x86/x64 disassembler and encoder library inspired by Zydis.
一个纯 Rust 实现的 x86/x64 反汇编和编码库,灵感来源于 Zydis。
Features / 特性
Core / 核心
- Decoder / 解码器: Decode x86/x64 instruction bytes into structured representation / 将字节序列解码为结构化表示
- Encoder / 编码器: Encode instructions back to bytes / 将指令编码为字节序列
- Formatter / 格式化器: Format instructions as readable text (Intel, AT&T, MASM syntax) / 格式化为可读文本(多种语法)
Technical / 技术特性
- Zero-allocation decoding: Minimal heap allocations for performance-sensitive scenarios / 零分配解码
- no_std support: Works in embedded environments / 支持 no_std 环境
- No FFI: Pure Rust implementation, no C dependencies / 纯 Rust 实现,无外部依赖
- Thread-safe: Decoder and Encoder are stateless and can be shared across threads / 线程安全
ISA Support / 指令集支持
- General-purpose integer instructions / 通用整数指令
- SSE/SSE2/SSE3/SSSE3/SSE4.1/SSE4.2
- AVX/AVX2
- AVX-512 (F, CD, BW, DQ, VL, VNNI, BF16)
- AMX (Advanced Matrix Extensions)
- BMI1/BMI2/POPCNT/LZCNT/TZCNT
- FMA/FMA4
- AES-NI/VAES/VPCLMULQDQ
- XOP/FMA4 (AMD)
- 3DNow! (AMD)
- FPU (x87)
- And more... / 更多...
Installation / 安装
Add to your Cargo.toml:
[]
= "0.1.0"
Feature Flags / 功能开关
| Feature | Description | Default |
|---|---|---|
decoder |
Instruction decoding | Yes |
encoder |
Instruction encoding | Yes |
formatter |
Instruction formatting | Yes |
std |
Standard library support | No |
zydis-sys |
Development only - FFI bindings for compatibility testing | No |
Development Features / 开发特性
The zydis-sys feature is for development and testing only. It enables:
zydis-sys 功能仅用于开发和测试。它用于:
- Comparison testing against the original Zydis C library / 与原始 Zydis C 库的对比测试
- Compatibility verification between zydis-rs and Zydis C / zydis-rs 与 Zydis C 之间的兼容性验证
Warning / 警告: End users should NOT enable this feature. It requires the Zydis C library to be installed on your system and is intended for contributors who want to verify compatibility.
普通用户不应该启用此功能。它需要系统安装 Zydis C 库,仅适用于想要验证兼容性的贡献者。
To enable (requires Zydis C library installed) / 启用方法(需要安装 Zydis C 库):
Disable default features for no_std:
[]
= { = "0.1.0", = false, = ["decoder"] }
Quick Start / 快速开始
Decoding / 解码
use ;
// Create a 64-bit decoder
let decoder = new?;
// Decode instruction
let bytes = &; // mov rax, rbx
let instruction = decoder.decode?;
println!;
println!;
println!;
# Ok::
One-stop Disassembly / 一站式反汇编
use ;
let code = &; // mov rbp, rsp
let result = disassemble_intel?;
println!; // 0x1000: mov rbp, rsp
# Ok::
Encoding / 编码
use ;
use Mnemonic;
let encoder = new?;
// Encode MOV RAX, RBX
let request = new
.with_reg
.with_reg;
let bytes = encoder.encode?;
println!; // [0x48, 0x89, 0xD8]
# Ok::
Formatting / 格式化
use ;
let decoder = new?;
let instruction = decoder.decode?;
// Intel syntax
let intel = new;
println!; // mov rax, rbx
// AT&T syntax
let att = new;
println!; // mov %rbx, %rax
// MASM syntax
let masm = new;
println!; // mov rax, rbx
# Ok::
Memory Operands / 内存操作数
use ;
use Mnemonic;
let encoder = new?;
// MOV RAX, [RBP - 8]
let mem = base_disp;
let request = new
.with_reg
.with_mem;
let bytes = encoder.encode?;
# Ok::
AVX Instructions / AVX 指令
use ;
use Mnemonic;
let decoder = new?;
let encoder = new?;
// Decode AVX instruction
let avx_bytes = &; // VADDPS XMM0, XMM1, XMM2
let instruction = decoder.decode?;
println!; // VADDPS
// Encode AVX instruction
let request = new
.with_reg
.with_reg
.with_reg;
let encoded = encoder.encode?;
# Ok::
Examples / 示例
The examples/ directory contains comprehensive examples:
| Example | Description |
|---|---|
basic_decode.rs |
Basic decoding operations |
basic_encode.rs |
Basic encoding operations |
format_instructions.rs |
Formatting with different syntaxes |
avx_example.rs |
AVX/AVX2 instruction handling |
avx512_decode.rs |
AVX-512 instruction decoding |
custom_hooks.rs |
Custom formatting with hooks |
complete_example.rs |
Complete feature demonstration |
encoder_demo.rs |
Encoder demonstration |
roundtrip.rs |
Encode-decode roundtrip test |
memory_operand_example.rs |
Memory operand handling |
Run an example:
Benchmarks / 性能测试
Run the benchmark suite:
# All benchmarks
# Specific benchmark
Documentation / 文档
Generate and view the API documentation:
Key Documentation / 关键文档
- CHANGELOG - Version history and changes / 版本历史和变更
- Migration Guide - Migrating from Zydis C API / 从 Zydis C API 迁移
- Development Roadmap - Development progress / 开发进度
Testing / 测试
# Run all tests
# Run specific test
# Run compatibility tests
# Run doc tests
Current Status / 当前状态
| Metric | Value |
|---|---|
| Mnemonics / 助记符 | 1,971 (104.4% of Zydis) |
| ISA Sets / ISA 集 | 233 (104.5% of Zydis) |
| Registers / 寄存器 | 331 (99.4% of Zydis) |
| Instruction Encodings / 指令编码 | 2,265+ |
| Overall Coverage / 总体覆盖率 | ~90% |
Comparison with Zydis / 与 Zydis 对比
| Feature | zydis-rs | Zydis (C) |
|---|---|---|
| Language | Pure Rust | C |
| Memory Safety | Guaranteed | Manual |
| no_std | Yes | Yes |
| Decoder | Yes | Yes |
| Encoder | Yes | Yes |
| Formatter | Intel/AT&T/MASM | Intel/AT&T/MASM/NASM |
| AVX-512 | Partial | Full |
Architecture / 架构
zydis-rs
├── src/
│ ├── lib.rs # Library entry point
│ ├── error.rs # Error types
│ ├── decoder/ # Decoding module
│ │ ├── mod.rs # Decoder implementation
│ │ ├── instruction.rs
│ │ ├── operand.rs
│ │ └── prefix.rs
│ ├── encoder/ # Encoding module
│ │ ├── mod.rs # Encoder implementation
│ │ ├── request.rs
│ │ └── vex.rs, evex.rs
│ ├── formatter/ # Formatting module
│ │ ├── mod.rs # Formatter implementation
│ │ ├── intel.rs
│ │ ├── att.rs
│ │ └── masm.rs
│ ├── isa/ # ISA definitions
│ │ ├── mnemonic.rs
│ │ ├── register.rs
│ │ └── attributes.rs
│ └── data/ # Instruction tables
├── examples/ # Usage examples
├── benches/ # Performance benchmarks
└── tests/ # Integration tests
Contributing / 贡献
Contributions are welcome! Please feel free to submit a Pull Request.
欢迎贡献代码!请随时提交 Pull Request。
License / 许可证
MIT License
Acknowledgments / 致谢
This project is inspired by Zydis, an excellent C x86/x64 disassembler library.
本项目灵感来源于 Zydis,一个优秀的 C 语言 x86/x64 反汇编库。