Memory Layout and Errors Clinic
Retrieval Prompts
- Name the main segments of a process's virtual address space and what each holds.
- State what a stack frame contains at minimum.
- Explain the difference between
malloc,calloc, andrealloc. - State the canonical idioms after
free(p)and afterrealloc(p, n). - Name three tools that catch memory errors, and one thing each tool excels at.
Compare and Distinguish
Separate these pairs clearly:
- stack versus heap (lifetime, addressing, who frees)
.dataversus.bssversus.rodata- buffer overflow versus use-after-free versus memory leak
- AddressSanitizer versus Valgrind Memcheck versus Clang static analyzer
Common Mistake Check
For each program, predict the sanitizer / tool report before you run it:
char b[8]; strcpy(b, "123456789");int *p = malloc(4 * sizeof *p); free(p); p[0] = 1;int *p = malloc(4 * sizeof *p); free(p); free(p);int *p = malloc(4 * sizeof *p); /* never freed */ return 0;int x; printf("%d\n", x); /* uninitialized read */
Mini Application
-
Read
/proc/self/mapsfrom a small C program and print each line. Identify the segments for.text,.data,.bss, heap, and stack, then run with./a.outseveral times and note which base addresses change (ASLR). -
Write a function that recursively calls itself, printing the address of a local on each call. Observe whether addresses go up or down, and whether the program eventually stack-overflows (it will, eventually).
-
Replace
strcpywithsnprintfin one of the earlier buggy examples. Confirm that ASan is silent afterwards, even for long inputs. -
Insert a deliberate off-by-one in a loop writing into a 32-byte buffer. Build with and without
-fstack-protector-strong. Observe the difference in crash messages. -
Add a
malloc-heavy program that allocates and never frees, then runvalgrind --leak-check=full ./a.out. Read the LEAK SUMMARY lines and match them to your code.
Lab Tasks
- Build a stack-frame tracer: write a
trace()macro that prints the current function and the address of a local variable. Call it from several layers of functions; use the printed addresses to infer frame sizes. - Write a toy leak detector using
__malloc_hook(old-style) orLD_PRELOADwith your ownmallocwrapper that increments a counter. Intentionally leak, then verify the counter. - Add
-fsanitize=address,undefinedto the CMake / Makefile build flags for a small project. Run the test suite. Fix anything the sanitizer complains about.
Evidence Check
This page is complete only if you can:
- classify any variable in a short C program by segment (stack / heap /
.data/.bss/.rodata) without running it - predict which of ASan, UBSan, Valgrind Memcheck, and a static analyzer will report a given bug
- fix a reported memory bug and re-run the tool to confirm the fix