Systems Programming Companion
Students taking a university systems course built around how programs really run on x86-64 Linux.
Weeks follow the chapter order of Computer Systems: A Programmer's Perspective by Randal E. Bryant and David R. O'Hallaron. Be Bitwise isn't affiliated with the book or its publisher.
Bits, bytes and integers
Every value in memory is a bit pattern, and the type you read it as decides what it means. Get two's complement and unsigned wraparound straight now and half of next month's bugs stop being mysterious.
Read
Floating point
Floats are scientific notation squeezed into 32 or 64 bits, which is why 0.1 + 0.2 misbehaves. Learn where the sign, exponent and fraction live and the rounding puzzles start to look mechanical.
Read
- Binary ArithmeticBinary & Number Systems
- Floating-Point NumbersData Representation
- Numeric Types: Integers & FloatsVariables & Data Types
Coming soon
- Worked examples of rounding modes, denormals and float-to-int conversion
Machine code basics
Your C compiles down to a few dozen instructions shuffling values between registers and memory. Once mov, lea and the arithmetic ops read naturally, disassembly stops looking like noise.
Read
- Why Assembly?Assembly Language
- Assembly SyntaxAssembly Language
- Writing Your First ProgramAssembly Language
- Assembly and Machine CodeAssembly Language
- x86 Registersx86 Architecture Deep Dive
- x86 Instruction Setx86 Architecture Deep Dive
- From Source to BinaryWhat Your C Code Actually Does
Practise
Control flow and procedures
If statements become compares and jumps; function calls become a careful dance with the stack and six argument registers. This is the week the stack frame finally clicks, so draw it out.
Read
Arrays, structs and buffer overflows
Arrays and structs are just base addresses plus offsets, and nothing stops a write from running off the end. Overflow a stack buffer yourself and the defences in the second half of the week make sense.
Read
- Pointer ArithmeticAddresses & Pointers
- Defining StructsStructs & Data Structures
- Anatomy of a Stack FrameStack-Based Buffer Overflows
- Writing Past the BufferStack-Based Buffer Overflows
- Controlling ExecutionStack-Based Buffer Overflows
- Real-World Stack OverflowsStack-Based Buffer Overflows
- Stack CanariesExploit Mitigations
- ASLR & Position-Independent CodeExploit Mitigations
Practise
Processor architecture
A CPU fetches, decodes and executes, and a pipelined one does all three at once on different instructions. Hazards are what happens when those instructions trip over each other.
Read
- The Instruction CycleCPU Basics
- The Instruction PipelineCPU Pipeline & Microarchitecture
- Branch PredictionCPU Pipeline & Microarchitecture
Practise
Coming soon
- Step-by-step pipeline hazard walkthrough for a simple teaching ISA
Performance and the memory hierarchy
Memory is a stack of ever-bigger, ever-slower caches, and the fastest code is the code that keeps hitting the small ones. Loop order alone can change runtime by an order of magnitude.
Read
- Cache HierarchyMemory Fundamentals
- CPU Cache ArchitectureCache & Performance
- Cache-Friendly ProgrammingCache & Performance
- Memory Performance AnalysisCache & Performance
Coming soon
- Worked cache simulation examples: sets, tags, hits and misses by hand
Linking
The linker stitches object files together by resolving symbols and patching addresses. Learn to read an ELF file and the GOT and PLT, and odd 'undefined reference' errors become easy to fix.
Read
Exceptional control flow
Interrupts, system calls, fork, exec and signals all yank control away from the normal flow of your program. Build a small shell and you'll have used every one of them.
Read
- User-Kernel Transition MechanicsSyscall Interfaces & the Kernel Boundary
- What Is a ProcessProcess Management & Scheduling
- Process Lifecycle & StatesProcess Management & Scheduling
- Signal HandlingSystems Programming in C
- Process ManagementSystems Programming in C
Practise
Coming soon
- Step-by-step support for a shell project: reaping children, races with SIGCHLD
Virtual memory
Every process thinks it owns the whole address space; page tables and the TLB keep that illusion cheap. Walk a translation by hand once and page faults, copy-on-write and mmap all fall out of it.
Read
Dynamic memory allocation
malloc is a bookkeeper carving one big region into chunks and keeping lists of the free ones. Write your own allocator and you'll never look at a use-after-free the same way again.
Read
- The HeapThe Stack & The Heap
- Memory Allocator InternalsAdvanced Memory Management
- Custom AllocatorsAdvanced Memory in C
- Heap Allocation BasicsHeap Vulnerabilities & Use-After-Free
- Use-After-Free & Tcache PoisoningHeap Vulnerabilities & Use-After-Free
Practise
- Build a working memory allocatorCapstone: Build a Memory Allocator
- Watch chunks split, free and coalesceHeap Visualizer
Coming soon
- Explicit free list and segregated fits walkthrough with diagrams
I/O, networking and concurrency
Files and sockets are both just file descriptors, which is why a web server is mostly reads and writes. Add threads and you get speed, plus a whole new family of bugs when two of them share data.