Userspace
๐ Overview
Userspace is a Rust implementation of a standard library for userspace applications, designed to work without depending on the Rust standard library (no_std). It provides safe abstractions for low-level operations, architecture-specific functionality, memory management, and executable file format handling.
Key Features
- ๐ Memory Safety: Leverage Rust's ownership model for secure systems programming
- ๐งฉ Modular Architecture: Well-defined components with clear interfaces
- ๐ Cross-Platform: Architecture abstractions for portability (currently x86_64)
- ๐ฆ No Standard Library: Works in
no_stdenvironments - ๐ ELF Support: Parse, map, and launch ELF64 images according to the GABI
- ๐ ELF Interpreters: Load
PT_INTERPdynamic linkers and prepare Linux startup state - ๐ง Memory Management: Stack manipulation and memory allocation utilities
๐ Project Structure
userspace/
โโโ src/
โ โโโ file/ # File format handling
โ โโโ macros/ # Utility macros
โ โโโ memory/ # Memory management
โ โ โโโ alloc/ # Allocation functionality
โ โ โโโ page/ # Page management
โ โ โโโ stack/ # Stack handling
โ โโโ target/ # Architecture abstractions
โ โ โโโ architecture/ # CPU architecture specifics
โ โ โโโ operating_system/ # OS abstractions
โ โโโ traits/ # Common interfaces
โ โโโ types/ # Library-specific types
โ โโโ entry.rs # Binary entry point
โ โโโ library.rs # Main library definition
โ โโโ panic.rs # Panic handler
โ โโโ result.rs # Error handling
โโโ Cargo.toml # Project configuration
โโโ build.rs # Build script
๐ Getting Started
Prerequisites
- Rust 2024 Edition or newer
- Cargo and Rustup
Usage
With standard library
Add this to your Cargo.toml:
[]
= { ="*", =["with_std"] }
Usage Example
use userspace;
๐ ๏ธ Architecture
Userspace is designed with a layered architecture:
- Core Layer: Basic types, traits and utilities
- Target Layer: Architecture and OS abstractions
- Memory Layer: Stack, pages, and allocation
- File Layer: File format parsing and manipulation
Each layer builds upon the previous ones, providing increasingly higher-level abstractions while maintaining safety and performance.
Memory Management
The memory subsystem provides:
- Safe stack traversal and argument extraction
- Page allocation primitives
- Basic heap allocation in no_std environments
ELF Loading
The ELF loader currently targets Linux x86_64 and follows the GABI program-header view:
- Maps
PT_LOADsegments with their file and memory sizes. - Applies load bias to
ET_DYN/PIE images. - Preserves segment permissions from
p_flags. - Detects
PT_INTERPand transfers control to the system dynamic linker. - Executes static
ET_EXECand relocation-freeET_DYNimages directly, while requiring an interpreter for relocation/dependency-bearing dynamic images. - Builds the initial interpreter stack and updates the core auxiliary-vector entries.
The current loader uses a fixed 0x100000 link address for the userspace executable. ET_EXEC images linked into that range are intentionally rejected by MAP_FIXED_NOREPLACE; conventional ET_EXEC images near 0x400000 do not collide with the loader. The rebuilt initial stack is a 16 MiB writable mapping preceded by a PROT_NONE guard page. argv, envp, AT_EXECFN, fresh AT_RANDOM, AT_PLATFORM, and AT_BASE_PLATFORM data are copied into the new stack; AT_SYSINFO_EHDR remains a pointer to the existing vDSO mapping. Required loader auxv entries are synthesized when absent, and newly created image mappings are rolled back when segment loading or permission application fails.
See tests/elf_fixtures/build.sh for reproducible static, PIE, dynamic, large-BSS, and address-collision ELF fixtures. Run sh tests/host/run.sh for host-side macro-layout, auxv, and loader regression tests.
Architecture Abstraction
The target subsystem abstracts architecture details:
- Pointer types and operations
- Register access patterns
- CPU-specific features
- OS-specific functionality
Currently focused on x86_64, but designed to be extensible to other architectures.
๐งช Experimental Features
Userspace uses several experimental Rust features:
These enable advanced type-level programming required for zero-cost abstractions across architectures.
๐ Documentation
For more detailed documentation:
๐ค Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/amazing-feature) - Commit your changes (
git commit -am 'Add some amazing feature') - Push to the branch (
git push origin feature/amazing-feature) - Open a Pull Request
๐ License
This project is licensed under the terms found in the LICENSE file.
๐ฎ Future Work
- Expand the fixed initial stack and add a growth policy
- Transactional mapping ownership and rollback
- Complete auxiliary-vector semantic classification and regeneration
- Validate source stack ranges before copying pointed data
- Support for additional architectures (ARM, RISC-V)
- Enhanced file system abstractions
- Networking capabilities
- Threading and concurrency primitives
- Comprehensive test suite