Skip to main content

Crate zydis_rs

Crate zydis_rs 

Source
Expand description

§zydis-rs

一个纯 Rust 实现的 x86/x64 反汇编和编码库,灵感来源于 Zydis。

A pure Rust x86/x64 disassembler and encoder library inspired by Zydis.

§功能特性 / Features

  • 零内存分配解码 - 高效的指令解码,适合性能敏感场景
  • Intel 和 AT&T 语法格式化 - 灵活的输出格式选择
  • 指令编码 - 从结构化表示生成 x86/x64 指令机器码
  • 无外部依赖 - 纯 Rust 实现,无需 FFI 调用
  • no_std 兼容 - 可在嵌入式环境中使用
  • AVX/AVX2/AVX-512 支持 - 完整的现代 SIMD 指令集支持

§功能开关 / Feature Flags

  • decoder (默认) - 指令解码支持
  • formatter (默认) - 指令格式化(Intel/AT&T 语法)
  • encoder - 指令编码支持
  • std - 标准库支持(默认禁用,no_std 环境)

§快速开始 / Quick Start

§一站式反汇编 / One-stop Disassembly

最简单的使用方式是使用一站式函数:

use zydis_rs::{disassemble_intel, disassemble_all_intel, MachineMode, StackWidth};

let code = &[0x48, 0x89, 0xE5]; // mov rbp, rsp
let result = disassemble_intel(code, 0x1000, MachineMode::Long64, StackWidth::Width64)?;

println!("{}", result.text); // "mov rbp, rsp"
println!("0x{:X}", result.address); // 0x1000
println!("{}", result.length); // 3

§底层解码 / Low-level Decoding

如需更多控制,可直接使用 DecoderFormatter

use zydis_rs::{Decoder, Formatter, Syntax, MachineMode, StackWidth};

let code = &[0x48, 0x89, 0xE5]; // mov rbp, rsp
let decoder = Decoder::new(MachineMode::Long64, StackWidth::Width64)?;

let instruction = decoder.decode(code)?;

let formatter = Formatter::new(Syntax::Intel);
println!("{}", formatter.format_instruction(&instruction)); // "mov rbp, rsp"

§指令编码 / Instruction Encoding

使用编码器生成 x86/x64 机器码:

use zydis_rs::{Encoder, EncoderRequest, MachineMode, Mnemonic, Register};

let encoder = Encoder::new(MachineMode::Long64, 64)?;

// 编码 MOV RAX, 0x12345678
// Encode MOV RAX, 0x12345678
let request = EncoderRequest::new(Mnemonic::MOV)
    .with_reg(Register::RAX)
    .with_imm(0x12345678);
let bytes = encoder.encode(&request)?;

println!("{:?}", bytes); // [0x48, 0xB8, 0x78, 0x56, 0x34, 0x12, 0x00, 0x00, 0x00, 0x00]

§支持的指令集 / Supported Instruction Sets

  • 通用整数指令(ADD, SUB, MOV, JMP, 等)
  • SSE/SSE2/SSE3/SSSE3/SSE4 指令集
  • AVX/AVX2 指令集
  • AVX-512 指令集(F, CD, BW, DQ, VL)
  • AMD XOP 指令集

§示例 / Examples

查看 examples/ 目录获取更多使用示例:

  • basic_disasm.rs - 基础反汇编
  • encoder_demo.rs - 编码器使用
  • roundtrip.rs - 编码后验证解码

§架构说明 / Architecture Notes

§解码器架构 / Decoder Architecture

解码器采用流水线式处理:

  1. 前缀解析(Legacy, REX, VEX, EVEX, XOP)
  2. 操作码查找
  3. ModR/M 和 SIB 解析
  4. 偏移量和立即数提取
  5. 操作数构建

§格式化器架构 / Formatter Architecture

格式化器支持多种语法风格:

  • Intel: mov rax, rbx - 标准 Intel 语法
  • AT&T: mov %rbx, %rax - GNU 工具使用的 AT&T 语法
  • MASM: mov dword ptr [rax], 0x12345678 - Microsoft MASM 语法

Re-exports§

pub use error::Error;
pub use error::Result;
pub use isa::MachineMode;
pub use isa::Mnemonic;
pub use isa::Register;
pub use isa::RegisterClass;
pub use isa::StackWidth;
pub use decoder::BranchType;
pub use decoder::ConditionCode;
pub use decoder::DecodedInstruction;
pub use decoder::Decoder;
pub use decoder::ExceptionClass;
pub use decoder::OpcodeMap;
pub use decoder::Operand;
pub use formatter::Formatter;
pub use formatter::OutputBuffer;
pub use formatter::Syntax;
pub use encoder::Encoder;
pub use encoder::EncoderOperand;
pub use encoder::EncoderRequest;
pub use encoder::EvexZeroingMode;
pub use encoder::MemoryOperand;

Modules§

data
Internal data definitions
decoder
Decoder Module
encoder
x86/x64 指令编码器模块 / x86/x64 instruction encoder module
error
Error types for zydis-rs
formatter
Formatter Module
isa
Instruction Set Architecture definitions

Structs§

DisassembledInstruction
Result of a one-stop disassembly operation.
DisassemblyIterator
Iterator for streaming disassembly.

Constants§

VERSION
库版本信息

Functions§

disassemble_all_intel
Disassemble multiple instructions from a byte slice.
disassemble_att
Disassemble a single instruction in AT&T syntax.
disassemble_intel
Disassemble a single instruction in Intel syntax.