Skip to main content

Python bindings for the RaspSim C++ library, a cycle-accurate X86-64 simulator based on PTLSim.

Project description

RASPsim

RASPsim is a cycle-accurate x86-64 simulator, originally developed by Alexis Engelke as a fork of PTLsim by Matt Yourst. This package provides Python bindings to RASPsim, allowing you to configure the virtual address space, set initial register values, and run simulations of ELF binaries directly from Python.

Features

  • Cycle-accurate simulation of x86-64 instructions.
  • ELF Binary Loading: Parse and load ELF binaries, including segments and debugging information.
  • Custom Memory Management: Map memory segments with specific permissions.
  • Register Access: Read and write CPU registers during simulation.
  • Exception Handling: Detailed exceptions for various fault conditions.
  • Python Binding: Interface directly with the simulator using Python.

Key Components

Raspsim Class

The main class used to interact with the simulator. It provides methods to:

  • Load ELF binaries into the simulator.
  • Run the simulation.
  • Access and modify CPU registers.
  • Handle exceptions and retrieve simulation statistics.

ELF Class

Represents an ELF binary and provides methods to:

  • Parse ELF files from bytes and extract segments.
  • Handle debugging information like line mappings.
  • Add trampolines to binaries for controlled execution flow.

Segment Class

Represents a memory segment with attributes:

  • vaddr: Virtual address.
  • prot: Memory protection (read, write, execute).
  • size: Size of the segment.
  • data: Optional data contained in the segment.
  • stack: Indicates if the segment is a stack.

iN Classes (i8, i16, i32, i64)

Represents N-bit integers with proper wrapping and arithmetic operations. Useful for working with register values and ensuring correct bit-width behavior.

Utility Functions

  • rscompile: Context manager to compile code into an ELF binary using appropriate flags.
  • asm_preamble: Generates assembly code preamble with an entry label.
  • asm_stop_sim: Generates assembly instruction to stop the simulator (int 0x80).

Usage

Compiling and Loading an ELF Binary

from raspsim.utils import rscompile, asm_preamble, asm_stop_sim
from raspsim.elf import ELF
from raspsim.raspsim import Raspsim

# Write your assembly code
code = asm_preamble() + """
    mov rax, -1
""" + asm_stop_sim()

# Compile the code into an ELF binary
with rscompile(code=code) as f:
    elf = ELF.from_file(f)

# Create a simulator instance and load the ELF binary
sim = Raspsim()
sim.load_elf(elf)

# Run the simulation
sim.run()

# Access register values
print(f"RAX: {sim.registers.rax:#x}")

Accessing Memory

# Map a memory segment
address = sim.memmap(start=0x1000, prot=Prot.RW, length=0x1000)

# Write to memory
address[0x0] = b'\x01\x02\x03\x04'

# Read from memory
data = address.read(size=4)
print(f"Data: {data.hex()}")

Working with Registers

# Set register values
sim.registers.rax = 0xdeadbeef

# Get register values
rip = sim.registers["rip"]
print(f"RIP: {rip:#x}")

Handling Exceptions

The simulator provides detailed exceptions for various fault conditions, such as PageFaultException, InvalidOpcodeException, etc. These can be caught and handled appropriately. In case that the address space of the simulator was populated with an ELF binary that contains debugging information, the exception will contain the file and linenoand line attributes that can be used to provide more context about the fault. Either all three attributes are present or none of them.

try:
    sim.run()
except RaspsimException as e:
    print(f"Simulation error: {e}")
    if hasattr(e, 'lineno'):
        print(f"At {e.file}:{e.lineno}: {e.line}")

Command-Line Interface

You can use raspsim as a command-line tool to simulate code directly from the terminal.

python -m raspsim << EOF
.global _start
.intel_syntax noprefix
.text
_start:
    mov rax, 1
    int 0x80
EOF

Contributing

Contributions are welcome! Please submit pull requests or open issues for any bugs or feature requests at the GitHub repository.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

raspsim-0.1.0.6.tar.gz (4.3 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

raspsim-0.1.0.6-py3-none-any.whl (4.3 MB view details)

Uploaded Python 3

File details

Details for the file raspsim-0.1.0.6.tar.gz.

File metadata

  • Download URL: raspsim-0.1.0.6.tar.gz
  • Upload date:
  • Size: 4.3 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for raspsim-0.1.0.6.tar.gz
Algorithm Hash digest
SHA256 035337af5ed9cb4b53e1e0aae261ac4dfc3ec7d6cddf023be10c92199413dac9
MD5 f844cdf208137d7640f5435973766127
BLAKE2b-256 393daf33974b4f9cefee0a9593a67e9e554921ee446f7548d45600e7f40a23d9

See more details on using hashes here.

File details

Details for the file raspsim-0.1.0.6-py3-none-any.whl.

File metadata

  • Download URL: raspsim-0.1.0.6-py3-none-any.whl
  • Upload date:
  • Size: 4.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for raspsim-0.1.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 0ae59ea6c6bb4385f56ac2f631513661ddd6170d60d72642e006ed4ff62ca9f3
MD5 364a3387553a54d5cef7ef2c3f113e2f
BLAKE2b-256 b07448a4f6d0f7b8740ca6f965b36bf8f757bb834c46d202a82a11d945bc6a4a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page