Overview
Thememory_manager class (defined in memory_manager.hpp:50) sits between Windows syscalls and the CPU backend’s memory interface:
It maintains Windows-specific metadata (region types, permissions, committed vs. reserved) while delegating actual memory operations to the backend.
Memory Regions
Region Types
Windows distinguishes between several memory region types (frommemory_manager.hpp:20):
- private_allocation: Standard heap/stack memory, can be freed with
VirtualFree - section_view: File-backed memory, must be unmapped with
UnmapViewOfSection - section_image: Executable images, may have relocations and import tables
- mmio: Special memory with read/write callbacks (e.g., KUSER_SHARED_DATA)
Reserved vs. Committed
Windows has a two-phase allocation model:- Reserve: Allocate address space but no physical memory
- Commit: Allocate actual memory within reserved region
memory_manager.hpp:66-72.
Region Info
Applications query memory viaNtQueryVirtualMemory, which returns:
Memory Permissions
Windows uses fine-grained memory protection flags:Memory Operations
Allocation
Frommemory_manager.cpp:
Finding Free Space
When applications request memory without specifying an address:memory_manager.hpp:13-16.
Committing Memory
Decommitting Memory
Protection Changes
Special Memory Regions
KUSER_SHARED_DATA
Windows exposes read-only kernel data at a fixed address (0x7FFE0000). Sogen implements this as MMIO:
Process Environment Block (PEB)
The PEB is allocated in a special segment:Thread Environment Block (TEB)
Each thread has a TEB accessed via the GS segment register:Memory-Mapped Files
Sections can be mapped into memory:Memory Statistics
The memory manager tracks usage:Layout Versioning
The memory manager maintains a version counter for the memory layout:Next Steps
- Architecture - Overall emulator design
- Syscall Emulation - Memory-related syscalls
- Threading - Thread stack allocation
- Exception Handling - Access violations and guard pages