Chapter 4

C, memory, representation, and machine code

This chapter follows data from bytes in memory, through the rules of the C language, down to the instructions that a processor executes, because most security failures in low-level code happen at one of these steps. It begins with the lifetime and ownership of objects and with the classic memory errors: buffer overflows §4.4, out-of-bounds reads §4.5, use after free §4.6, double free §4.7, leaks, heap fragmentation §4.9 and stack overflow §4.10. It then explains how C defines, and in places declines to define, what a program does: undefined, implementation-defined and unspecified behavior §4.14; signed and unsigned arithmetic and the checks that keep it in range; integer promotion §4.18, conversion, truncation and extension §4.19; shifts, endianness §4.21 and alignment §4.22; and the strict aliasing rule §4.23, pointer provenance §4.24, pointer arithmetic §4.25 and null pointers §4.26.

The middle of the chapter treats data formats: structure padding §4.27, serialization §4.28 and deserialization §4.29, length-prefixed frames, type-length-value §4.31 records, checksums §4.32 and cyclic redundancy checks, and the constant-time comparison §4.33 of secret values. Then come the places where software meets hardware: the volatile qualifier §4.34, memory-mapped device registers, direct memory access §4.36 and memory barriers §4.37. The last part descends to machine code on Arm Cortex-M processors: the instruction set architecture §4.38 and the microarchitecture §4.39 beneath it, the Thumb instruction encodings §2.7, condition flags §4.42 and conditional execution, encoded constants, the link register §4.45, and the questions that an analysis of machine code must answer about live registers, forwarded memory values, read-only memory and the boundary of what it has proved. Two reference entries close the chapter: binary floating-point numbers §4.49 and bit operations §4.50.

In this chapter

  1. 4.1 Object lifetime
  2. 4.2 Ownership
  3. 4.3 Borrowed pointer
  4. 4.4 Buffer overflow
  5. 4.5 Out-of-bounds read
  6. 4.6 Use-after-free
  7. 4.7 Double free
  8. 4.8 Memory leak
  9. 4.9 Heap fragmentation
  10. 4.10 Stack overflow
  11. 4.11 Static, stack, and heap storage
  12. 4.12 Undefined behavior (UB)
  13. 4.13 Implementation-defined behavior
  14. 4.14 Unspecified behavior
  15. 4.15 Signed integer overflow
  16. 4.16 Unsigned modular arithmetic
  17. 4.17 Overflow-safe range check
  18. 4.18 Integer promotion and conversion
  19. 4.19 Truncation and extension
  20. 4.20 Shift semantics
  21. 4.21 Endianness
  22. 4.22 Alignment
  23. 4.23 Strict aliasing and effective type
  24. 4.24 Pointer provenance
  25. 4.25 Pointer arithmetic
  26. 4.26 Null pointer
  27. 4.27 Struct padding and packing
  28. 4.28 Serialization
  29. 4.29 Deserialization
  30. 4.30 Length-prefix framing
  31. 4.31 Type-length-value (TLV)
  32. 4.32 Checksum versus CRC
  33. 4.33 Constant-time comparison
  34. 4.34 Volatile qualifier
  35. 4.35 Memory-mapped I/O (MMIO)
  36. 4.36 Direct memory access (DMA)
  37. 4.37 Memory barrier
  38. 4.38 Instruction set architecture (ISA)
  39. 4.39 Microarchitecture
  40. 4.40 Arm Thumb and Thumb-2
  41. 4.41 Conditional execution and IT block
  42. 4.42 Condition flags (APSR)
  43. 4.43 Modified immediate encoding
  44. 4.44 Live-in register
  45. 4.45 Link register (LR)
  46. 4.46 Load-store forwarding analysis
  47. 4.47 Read-only memory range
  48. 4.48 Machine-code proof boundary
  49. 4.49 Binary floating-point representation, special values, and rounding
  50. 4.50 Bit operations: truth tables, masks, shifts, and rotations