Address Space Overview
The Thru virtual machine uses a 48-bit segmented address space designed to provide memory isolation and efficient access to different types of data during program execution. The address format follows a three-component structure that enables both type safety and performance optimization.Address Format
Each virtual address is composed of three fields packed into a 48-bit value:The address construction macro:
TN_VM_ADDR(seg_type, seg_idx, offset) = (seg_type << 40) | (seg_idx << 24) | offsetAddress Components
- Segment Type (8 bits): Determines the memory region type and access permissions
- Segment Index (16 bits): Identifies the specific segment within the type
- Offset (24 bits): Byte offset within the segment (up to 16MB per segment)
Memory Segment Types
0x00: Read-Only Data Segments
Contains immutable data that programs can read but never modify.| Segment Index | Name | Purpose | Notes |
|---|---|---|---|
| 0x0000 | NULL | Invalid/null segment | Always causes access violation |
| 0x0001 | TXN_DATA | Transaction data | Contains serialized transaction |
| 0x0002 | SHADOW_STACK | VM shadow stack | Call frame metadata (public portion) |
| 0x0003 | RESERVED | Unused | Reserved for future use |
| 0x0004 | BLOCK_CTX | Block context | Block metadata and timing info |
Write operations to read-only segments cause access violations and will fail.
0x02: Account Metadata
Provides access to account metadata structures.| Field | Description | Access Notes |
|---|---|---|
| seg_idx | Account index in transaction | Must be < transaction account count |
| offset | Byte offset in metadata | Limited to sizeof(tn_account_meta_t) |
- Size: Fixed at
sizeof(tn_account_meta_t)bytes - Access: Read-only from VM perspective
- Non-existent accounts: Returns pointer to zeroed metadata structure
- Invalid access: Access violations occur for out-of-bounds or invalid account indices
0x03: Account Data
Page-based access to account data with copy-on-write semantics. Programs execute directly from this segment type, using position-independent code at their account’s address.| Feature | Details |
|---|---|
| Page Size | 4096 bytes |
| Alignment | Enforced at page boundaries for cross-page access |
| Write Access | Requires account to be writable by current program |
| COW Pages | Automatic copy-on-write for modifications |
| Program Execution | Programs stored as account data execute from 0x03XXXX000000 where XXXX is the account index |
Account data accesses that span page boundaries cause access violations. Design your data structures to respect 4KB page alignment.
Program Execution Model: Programs are compiled as position-independent executables (PIE) and execute directly from their account data addresses. For example, a program at account index 5 executes from virtual address
0x030005000000.0x04: Event Data
Reserved for event emission (implementation details in event subsystem).0x05: Stack
Stack memory segment that grows downward from high addresses to low addresses.| Characteristic | Value |
|---|---|
| Growth Direction | Downward (0xFFFFFF → 0x000000) |
| Page Size | 4096 bytes |
| Max Segments | 1 per VM instance |
| Size Limit | 16MB per segment |
| Permission Model | Frame-based access control |
- Virtual address range:
0xFFFFFFdown to current stack size - Stack pointer typically points to
0x000000offset initially - As stack grows, valid addresses extend from
0xFFFFFFdownward
0x07: Heap
Heap memory segment that grows upward from low addresses to high addresses.| Characteristic | Value |
|---|---|
| Growth Direction | Upward (0x000000 → 0xFFFFFF) |
| Page Size | 4096 bytes |
| Max Segments | 1 per VM instance |
| Size Limit | 16MB per segment |
| Permission Model | Frame-based access control |
Address Translation Process
The VM performs the following steps for each memory access:1
Address Validation
Check if the upper 16 bits of the 64-bit address are zero (ensuring 48-bit address space).
2
Segment Extraction
Extract segment type, index, and offset from the address using bit manipulation.
3
Alignment Check
Verify that the offset is properly aligned for the requested access size.
4
Permission Validation
Check write permissions based on segment type and current execution context.
5
Translation
Convert virtual address to host address based on segment type-specific logic. If any validation fails, an access violation occurs.
Access Control and Permissions
Write Access Control
| Segment Type | Write Behavior | Access Violation Conditions |
|---|---|---|
| Read-Only Data | Always fails | Any write attempt causes violation |
| Account Metadata | Always fails | Runtime-managed, no direct writes allowed |
| Account Data | Conditional | Violation if program doesn’t own account |
| Anonymous Data | Conditional | Violation if frame permissions insufficient |
Frame-Based Security
Anonymous memory segments use a frame-based permission system:- Each page is tagged with the frame index (program invocation call depth) that allocated it
- Pages can only be freed by program invocations at the same call depth or higher
- Deeper called programs cannot free pages allocated by shallower called programs
Memory Management
Page Allocation
- Page Size: 4096 bytes (TN_RUNTIME_PAGE_SZ)
- Allocation: Dynamic allocation from a global page pool
- Tracking: Inverted page table for efficient management
- Limits: Enforced at transaction level for resource control
Copy-on-Write (COW)
Account data uses COW semantics:- Initial State: Points to read-only account data
- First Write: Allocates writable page and copies original data
- Subsequent Writes: Operate on the writable copy
- Commit: Modified pages are written back during transaction commit
Access Violations
Memory access can fail in several ways, resulting in access violations:| Violation Type | Cause | Details |
|---|---|---|
| Invalid Address | Malformed address or out-of-bounds | Upper 16 bits non-zero, or address exceeds segment bounds |
| Permission Denied | Unauthorized write access | Attempting to write to read-only segments or unowned accounts |
| Page Boundary Cross | Access spans multiple pages | Single access cannot cross 4KB page boundaries |
| Resource Exhaustion | No free pages available | Anonymous memory allocation when page pool is exhausted |
| Alignment Violation | Misaligned access | Offset not aligned to required boundary for access size |
| Invalid Segment | Unknown segment type/index | Segment type not implemented or segment index out of range |
Access Violation Behavior: When any of these violations occur the VM will
fault and exit, reverting the transaction.
Performance Considerations
- Page Alignment: Design data structures to fit within 4KB pages
- Sequential Access: Prefer sequential memory access patterns
- Segment Locality: Group related data within the same segment type
- COW Overhead: First write to account data incurs page allocation cost