Computer systems, a programmer's view

What really happens when a C program runs: how bits become integers and floats, how C becomes x86-64, how the stack holds procedures and how attackers abuse it, why caches make some loops ten times faster, and how linking, processes, virtual memory, malloc, files, sockets and threads work, traced through real machine code and memory.

Lessons

  1. Bits, bytes and words: Memory is an array of bytes, each with an address, and a value wider than a byte is stored across several, in little-endian order on x86-64, so the same bytes mean different things depending on the type that reads them.
  2. Unsigned and two's complement: The same w bits mean an unsigned number or a two's complement one, where the top bit weighs minus two to the w minus one, and C converts between them by keeping the bits and changing the meaning.
  3. Integer arithmetic and overflow: Machine addition and multiplication are exact modulo two to the w, so they overflow silently, and shifts multiply and divide by powers of two, with a bias needed to round negative numbers toward zero.
  4. Floating point: An IEEE 754 float stores a sign, a biased exponent and a fraction, so it spaces its values unevenly, rounds to even, and has special values for zero, infinity and not-a-number.
  5. Registers, moves and addressing: An x86-64 program moves data between sixteen registers and memory, and the addressing mode D(Rb,Ri,S) reaches the address Rb plus S times Ri plus D in one instruction.
  6. Arithmetic and lea: Arithmetic instructions update a destination in place, and lea computes an address without touching memory, which compilers use to do small multiplications in one instruction.
  7. Condition codes and control: Comparisons set condition codes that jumps and conditional moves read, and every if, loop and switch in C compiles to these, a switch often to a jump table.
  8. Procedures and the stack: call pushes a return address and ret pops it, arguments travel in six registers and then on the stack, and each call gets a frame for its saved registers and locals, which is what makes recursion work.
  9. Arrays, structs and alignment: An array element is found by scaling its index, a struct field by a fixed offset, and alignment pads structs so every field starts at a multiple of its size.
  10. Buffer overflows and defences: Writing past the end of a stack buffer can overwrite the return address, and stack canaries, non-executable stacks and randomised addresses each block a different step of the attack.
  11. Storage and locality: Faster storage is smaller and dearer, so memory is a hierarchy of caches, and it works because programs reuse recent data and touch nearby data, temporal and spatial locality.
  12. How a cache works: A cache splits an address into tag, set index and block offset, looks in one set, and hits only if a valid line there has the same tag, otherwise it misses and evicts a line.
  13. Writing cache-friendly code: Stride-one loops over arrays use every byte of each cache block, so reordering loops and blocking a matrix multiply turns misses into hits and multiplies the speed.
  14. Linking: The linker resolves each symbol reference to one definition and relocates code to final addresses, from object files, static archives and shared libraries loaded at run time.
  15. Exceptions and processes: Exceptions hand control to the kernel, which gives each process the illusion of its own processor and memory, and fork, execve and waitpid create, replace and reap processes.
  16. Signals: A signal is a small message from the kernel that interrupts a process to run a handler, and because handlers run at any moment they must be async-signal-safe and signals do not queue.
  17. Address translation: The MMU splits a virtual address into a page number and an offset, looks the page up in a multi-level page table with a TLB in front, and faults to the kernel when the page is not in memory.
  18. Memory mapping and sharing: Each process maps areas of its address space to files or anonymous memory, which lets processes share code and lets fork copy memory only when a page is written.
  19. Dynamic memory allocation: malloc carves blocks out of the heap with headers and footers, finds free blocks from a free list, and splits and coalesces them to fight fragmentation.
  20. Files and system-level input and output: Unix treats every device as a file reached through a descriptor, read and write can move fewer bytes than asked, and descriptors are shared through open file table entries after fork and dup2.
  21. Network programming: A client and a server talk through sockets, the server binding, listening and accepting, the client connecting, and every connection is a pair of addresses and ports.
  22. Concurrent programming: A server can serve many clients at once with processes, by watching many descriptors at once, or with threads, which share one address space and are cheapest but easiest to get wrong.
  23. Synchronisation: Threads that update shared data race unless a mutex or semaphore makes the update atomic, and taking locks in different orders can deadlock.