Table of Contents 3
Preface 9
About the Author 10
Introduction 11
Prerequisites 11
Training Goals 12
Warning 12
Training Principles 13
Schedule 13
Training Idea 14
General Rust Aspects 14
What We Do Not Cover 15
Linux Rust Aspects 16
Windows Rust Aspects 16
Why Rust? 17
My Genealogy of Rust 18
Rust Mastery Process 19
Thought Process 19
Philosophy of Unsafe Pointers 20
A General Pointer Concept 20
Pointer 21
Pointer Dereference 21
One to Many 22
Many to One 22
Many to One Dereference 23
Invalid Pointer 23
Invalid Pointer Dereference 24
Wild (Dangling) Pointer 24
Pointer to Pointer 25
Pointer to Pointer Dereference 25
Naming Pointers and Entities 26
Names as Pointer Content 26
Pointers as Entities 27
Unsafe Rust Code Examples 28
Pointer 29
Unsafe Pointer Dereference 30
One to Many 32
Memory Leak 34
Many to One 35
Unsafe Many to One Dereference 37
Invalid Pointer 38
Unsafe Invalid Pointer Dereference (Alignment) 39
Unsafe Invalid Pointer Dereference (Access Violation) 40
Unsafe Wild (Dangling) Pointer 41
Pointer to Pointer 42
Unsafe Pointer to Pointer Dereference 43
Philosophy of Values 45
Values and Owners 46
Moving Values 47
Copying Values 49
Dropping Values 50
Ownership Tree 52
Ownership Tree and Drops 53
Partial Drops 54
Ownership Tree and Moves 56
Multiple Owners (not in Rust) 57
Multiple Owners and Drops 58
Owners vs. Pointers 58
Rust: A Copernican Revolution 59
Values Revolve Around Pointers 59
Owners Revolve Around Values 60
Rust Philosophy of Values 61
Restricted Ownership 62
Value Lifetime 63
Owner Lifetime 65
Rust Philosophy of Pointers 68
Types of Pointers 69
Mut Pointers vs. Pointers to Mut 69
References as Pointer Types 70
Mut Refs vs. Refs to Mut 72
References as Addresses 73
Borrowing References 75
Reference Lifetime 77
x64 Disassembly Review (WinDbg) 79
x64 CPU Registers 79
Instructions and Registers 80
Memory and Stack Addressing 80
Memory Cell Sizes 81
Memory Load Instructions 81
Memory Store Instructions 82
Flow Instructions 82
Function Parameters 83
Struct Function Parameters 83
x64 Disassembly Review (GDB AT&T Flavor) 84
x64 CPU Registers 84
x64 Instructions and Registers 85
Memory and Stack Addressing 85
x64 Memory Load Instructions 86
x64 Memory Store Instructions 86
x64 Flow Instructions 87
x64 Function Parameters 87
x64 Struct Function Parameters 88
ARM64 Disassembly Review 89
A64 CPU Registers 89
A64 Instructions and Registers 90
Memory and Stack Addressing 90
A64 Memory Load Instructions 91
A64 Memory Store Instructions 91
A64 Flow Instructions 92
A64 Function Parameters 92
A64 Struct Function Parameters 93
Memory Storage 94
Memory Regions 95
Dynamic Virtual Memory 95
Static Memory 96
Rust Static Memory Values 96
Global vs. Local Static 97
Rust Static Memory References 97
Stack Memory 102
Thread Stack Frames 102
Local Value Lifecycle 103
Scope 103
Rust Stack Memory Values 104
Rust Stack Memory References 104
Heap Memory 107
Rust Heap Memory Values 108
Rust Const Values 110
Useful WinDbg Commands 112
Useful GDB Commands 112
Memory and Pointers 113
Mental Exercise 114
Debugger Memory Layout 114
Memory Dereference Layout 115
Names as Addresses 115
Addresses and Entities 116
Addresses and Structures 116
Pointers to Structures 117
Arrays 117
Arrays and Pointers to Arrays 118
Fat Pointers 118
Array Slices 119
String Literals (UTF-8) 122
Byte Strings 124
Vectors 125
Vector Slices 126
Strings 129
String Slices 130
C-Strings 132
C-String Slices 133
Basic Types 135
Bytes, Pointers, and References 136
u32, Pointers, and References 138
Little-Endian System 140
u64 and Pointers 141
Size 143
Alignment 147
Entity Conversion 149
Conversion through Pointers 150
Safe Conversion (Explicit Cast) 151
Safe Conversion (Coercion) 153
Forcing 155
Tuples 157
Structs 159
Tuple-like Structs 160
Newtypes 163
Newtypes (Binary Compatible) 165
Named-field Structs 166
Reference/Pointer to Struct 169
Ref/Ptr to Struct Dereference 170
Dereference with Replacement 171
One Ref/Ptr to Many Structs 173
Memory Leak 173
Many Ref/Ptr to One Struct 174
Many to One Dereference 174
Ref/Ptr to Ref/Ptr to Struct 175
Ref/Ptr to Ref/Ptr Dereference 175
Memory and Structs 179
Addresses and Structs 180
Struct Field Addresses 180
Ref/Ptr to Structs 183
Ref/Ptr to Struct and Fields 184
External Struct Alignment 187
Internal Struct Alignment (WinDbg) 187
Internal Struct Alignment (GDB) 188
Enums 191
Simple Enums 192
Enums with Structs 195
Enum Null Pointer Optimization 199
Source Code and Symbols 201
Conceptual Layer (Modules) 202
Logical Layer (Crates) 202
Physical Layer (Source Files) 203
Name Isolation 203
Functions 206
Pointers to Functions 207
References to Functions 209
Function Pointer Types 211
Struct Function Fields 212
Associated Functions 213
Pointers to Associated Functions 215
Type-associated Functions 217
Trait Functions 219
Trait Objects 219
vtable Memory Layout 221
Trait Object Memory Layout 221
Boxed Trait Object Layout 225
Struct Constructors 228
Struct Destructor 230
Struct Clone 234
Struct Copy 235
Parameters by Value 237
Parameters by Ref/Ptr 240
self 242
Trait Objects as Parameters 243
Struct as Return Value 244
Closures and Captures 246
Closure Struct 247
A64 Closure Struct Example 248
Captures (Borrowing) 248
Captures (Borrowing) x64 Linux 249
Captures (Move) 250
Captures (Move) x64 Linux 251
Smart Pointers 253
Why Smart Pointers? 254
Types of Smart Pointers 254
Interior Immutable Single Owner 255
Interior Mutable Single Ownership 257
Shared Ownership 261
Pinning 266
Use Cases 267
Rust Books 272
