Skip to content
AD
← All projects
REPRODUCIBLE LABC · LINUXADDRESSSANITIZER

A pointer can survive longer than the memory it points to.

A small C program opens a device session, closes it, then tries to print the session details. It is an intentionally broken teaching lab, not a claimed customer incident. The useful part is the investigation sequence: reproduce, capture evidence, trace ownership and validate the correction.

01 / Symptom

The program reads a released session.

The failing lifecycle

close_device_session(session);
print_session_summary(session);

The second call accesses fields through a pointer whose storage was already passed to free().

Run it with instrumentation

git clone https://github.com/avainfo/cpp-linux-debugging-labs.git
cd cpp-linux-debugging-labs/labs/03-use-after-free-asan
make
./buggy

The lab Makefile compiles with -g -O0 -fsanitize=address -fno-omit-frame-pointer to preserve useful source locations and call stacks.

02 / Evidence

AddressSanitizer exposes the memory timeline.

The instrumented reproduction reports a heap use-after-free. The report identifies the read site and includes the free and allocation call stacks. Addresses and line numbers depend on the build, so the relevant structure is shown below.

ERROR: AddressSanitizer: heap-use-after-free
READ of size 4
freed by thread T0 here:
previously allocated by thread T0 here:

This is an excerpted diagnostic pattern, not a full raw trace or performance benchmark. The source lab and its README are linked above.

  1. 01

    Reproduce

    Build with debug symbols and AddressSanitizer, then run the intentionally broken session lifecycle.

  2. 02

    Inspect evidence

    Read the invalid access location, free stack and allocation stack. These give a memory-lifetime timeline instead of a guess.

  3. 03

    Identify ownership

    The session is allocated by open_device_session and released by close_device_session. The report function dereferences the stale pointer after release.

  4. 04

    Fix and verify

    Use the session while it is still valid, release it afterwards and run the instrumented fixed executable.

03 / Correction

Fix the ownership boundary, not the report.

The corrected order

print_session_summary(session);
close_device_session(session);
session = NULL;

The summary runs before releasing the allocation. Setting the local pointer to NULL after cleanup also reduces the chance of accidental reuse, but it does not make arbitrary aliases safe.

Validate the corrected version

./fixed

The lab includes a separate fixed implementation compiled with the same sanitizer flags. Compare the valid report output and the absence of the original memory-access diagnostic.

The engineering lesson

A reproducible failure tells a better story than a guess.

AddressSanitizer is effective here because the bug is a concrete lifetime violation. Real production incidents can require core dumps, logs, traces, race analysis or different instrumentation. The same discipline applies: establish what happened, gather evidence, isolate the cause, and verify the change.