What Happens Inside a Program When It Crashes

You’ve seen “Segmentation fault” a hundred times. This post is about what it actually means.

Complete Fuzzing Series

# Blog Link
1 What Is Fuzzing and Why Does It Actually Work? What Is Fuzzing and Why Does It Actually Work?
2 How a Fuzzer Works: Mutations, Seeds, and Corpus Explained How a Fuzzer Works: Mutations, Seeds, and Corpus Explained
3 The Taxonomy: Every Type of Fuzzing Explained The Taxonomy: Every Type of Fuzzing Explained
4 What Happens Inside a Program When It Crashes you are here
5 Sanitizers: Your Fuzzing Superpowers Sanitizers: Your Fuzzing Superpowers
6 Coverage: The Feedback That Makes Fuzzing Smart Coverage: The Feedback That Makes Fuzzing Smart

The first time I saw “Segmentation fault (core dumped)” I did what most people do.

I Googled it. Got ten pages of Stack Overflow answers. Closed all of them. Tried random things. It went away. I moved on.

I had absolutely no idea what just happened.

For a long time I thought “Segmentation fault” was just the Linux way of saying “something broke.” Like an error message. Like a 404 on a website.

That’s completely wrong. And what I got wrong, what a crash actually is, matters a lot. Without it you can’t read a GDB backtrace. You can’t tell whether something is exploitable or just noise. And “UNIQUE_CRASH: 47” means nothing to you.

So that’s what this blog is about.


First, Let’s Run Something and Watch It Break

Here’s a simple C function. I’m not going to hide anything. This is intentionally broken, and I’ll tell you exactly why.

void parse_username(char *input) {
    char buffer[64];       // 64 bytes allocated on the stack
    strcpy(buffer, input); // copies input into buffer -- no length check
    printf("Welcome, %s\n", buffer);
}

The function reserves 64 bytes and then copies whatever the user sent into it. No check on how long that input is.

Now let’s feed it 200 A’s and see what happens.

That message, Segmentation fault (core dumped), is the operating system telling you: your program tried to access memory it didn’t have permission to touch, so I killed it.

Exit code 139. If you’ve seen 128 + N = signal number before, then 128 + 11 = 139 means signal 11, which is SIGSEGV. The operating system sent your process signal 11 and terminated it.

But what actually happened inside the program before that? That’s what most people never learn.


What “Memory” Actually Looks Like When a Program Runs

When a program starts, the OS gives it a chunk of virtual memory. It’s divided into regions. You don’t need to memorize all of them, but three are important:

The stack is where local variables live. When you call a function, the stack grows to hold that function’s variables. When the function returns, the stack shrinks. It’s fast. It has a fixed size per thread (usually 8MB on Linux). And this is where buffer[64] from our parse_username function lives.

The heap is where dynamically allocated memory lives. When you call malloc(100), you get 100 bytes on the heap. It can grow much larger than the stack. You manage it yourself: malloc to get memory, free to give it back.

The code (text) segment is where the program’s actual instructions live. When the CPU executes your program, it reads from here.

There’s one more thing to understand: the return address.

When one function calls another, it needs to know where to come back to when the called function finishes. It stores that “come back to” address on the stack. When the function returns, the CPU reads that address and jumps to it.

This is about to become very relevant.


What Happened When We Sent 200 A’s

Let me trace through what actually happened.

parse_username was called. The stack grew to hold its local variable buffer[64], 64 bytes. The return address, where main is waiting, was also stored on the stack, right next to buffer.

Then strcpy ran. It copied our input, 200 bytes, into a 64-byte buffer. It didn’t stop at 64. strcpy has no idea how big the buffer is. It just copies until it hits a null terminator. So it copied all 200 bytes.

After 64 bytes, it overflowed past buffer and started writing into whatever was next on the stack. Which was the return address.

After 64+ bytes of A’s, the return address is now 0x4141414141414141. Because 0x41 is the ASCII code for ‘A’.

When parse_username tried to return, the CPU read that address and tried to jump to 0x4141414141414141. That address doesn’t exist in the program’s memory. The CPU accessed an invalid memory location. The MMU, the hardware that maps virtual to physical memory, raised a fault. The kernel caught it. The kernel sent SIGSEGV to the process. The process died.

That’s the whole chain. Not “something broke.” A specific mechanical sequence from input to overflow to corrupted return address to invalid memory access to kernel signal to death.


Now Let’s Look at GDB and See the Proof

GDB is a debugger. When a program crashes inside GDB, you can examine its state at the exact moment it died.

Look at the backtrace. Those are all the function calls stacked up at the moment of the crash. The first entry makes sense:

#0  parse_username (input=... 'A' <repeats 200 times>) at stack_overflow.c:8

That’s the function we were inside. Good.

But then look at entries 1 through 16:

#1  0x4141414141414141 in ?? ()
#2  0x4141414141414141 in ?? ()
...

GDB is reading the return addresses from the stack and showing us where it thinks the program was supposed to go. Every single one is 0x4141414141414141. Our A’s completely overwrote the stack.

And at the bottom, check the registers line:

rbp   0x4141414141414141

RBP is the base pointer, the register that holds the reference point for the current stack frame. It’s also been overwritten with our A’s.

Here’s the part that should stop you for a second:

This is not just “the program crashed.” This is “we controlled what address the CPU tried to jump to.”

If instead of A’s, we had put a real memory address at the right offset, the address of some shellcode or some existing function in the binary, the CPU would have jumped there and executed it. That’s a stack buffer overflow turned into code execution. That’s a CVE. That’s how real exploits work.

The fuzzer finds the crash. The researcher looks at the crash type. If the return address is controlled by input, the researcher knows this is likely exploitable.


Crashes on the Heap Are Different (But Just as Bad)

The stack overflow we just looked at is the classic example. But a lot of modern software, especially network services and file parsers, does most of its work on the heap.

Heap overflows look different. Here’s a pattern that shows up constantly in real parsers:

char* read_field(unsigned char *data, int data_len) {
    int claimed_size = *(int*)data;      // read size from first 4 bytes
    char *buf = malloc(claimed_size);    // allocate based on what attacker says
    memcpy(buf, data + 4, data_len - 4); // copy actual bytes -- may exceed claimed_size
    return buf;
}

The parser reads a size from the data itself, something the attacker controls, and allocates that much memory. Then it copies the actual data in. If the actual data is bigger than the claimed size, you overflow the heap allocation.

This exact pattern, trusting a length field inside attacker-controlled data, is one of the most common sources of heap overflows in the real world. It showed up in Heartbleed (the OpenSSL bug that exposed server memory to anyone). It shows up in file parsers constantly.

When we run this without any sanitizers:

malloc(): corrupted top size
Aborted (core dumped)

The crash doesn’t happen where the bug is. It happens later, when malloc tries to do something and finds the heap is corrupted. The error message is confusing. The location is misleading.

I actually spent an embarrassing amount of time staring at this output the first time I hit it, trying to find the bug at the malloc call. The actual overflow was two functions up. That’s the whole problem with heap bugs without tooling.

Now watch what happens when we compile with AddressSanitizer (-fsanitize=address):

Completely different experience:

ERROR: AddressSanitizer: heap-buffer-overflow on address 0x502000000014
WRITE of size 200 at 0x502000000014
    #1  in read_field  heap_overflow.c:10    <-- our bug

ASAN caught it at the exact line the bug happened, told us what address, told us the write size, and pointed us directly to the source line. Without ASAN, we’d be chasing a corrupted-heap crash with no idea where the actual overflow was.

That’s why we’ll spend a whole blog on sanitizers. They turn invisible bugs into screaming crashes.


The Scariest One: The Bug That Doesn’t Crash at All

Here’s something that took me a while to fully accept.

Not all bugs crash. Some of the most dangerous bugs run completely silently and the program exits with code 0, success.

This one is called Use-After-Free. It’s exactly what it sounds like: you use memory after freeing it.

Device *cam = malloc(sizeof(Device));
strcpy(cam->name, "IoT-Camera-01");
cam->log = default_log;  // function pointer stored here

free(cam);  // device is freed -- memory returned to heap

// ... some code later ...
cam->log("Motion detected");  // using freed memory

We allocate a device, store a function pointer in it, free the device, then call the function pointer anyway.

Watch what happens when we run it:

[LOG] Motion detected
$ echo $?
0

It ran. It logged the message. It exited successfully. No crash. No error.

But here’s what actually happened under the hood.

After free(cam), that memory block was returned to the allocator’s free list. The function pointer at cam->log still contained default_log’s address, because nobody cleared it. When we called cam->log(...), we were reading a function pointer from freed memory, and in this run, the freed memory still had the original value there. So it worked.

But here’s the exploit scenario:

An attacker who knows this code has this bug can arrange for a different malloc call to happen between the free and the use. Modern allocators often return recently-freed blocks to the next allocation. So the attacker’s data ends up at the exact same address. The function pointer at cam->log now points to whatever the attacker put there. When cam->log("Motion detected") runs, it jumps to the attacker’s address.

That’s Use-After-Free as a code execution primitive. No crash. No warning. The program thought everything was fine.

A fuzzer running without sanitizers will never find this bug. The program exits 0. The fuzzer sees: “no crash, that input is boring.” It moves on.

A fuzzer running with AddressSanitizer will catch this, because ASAN poisons freed memory and screams when you read from it. That’s the whole value of sanitizers in fuzzing.


Connecting This Back to Fuzzing

In the previous blogs we talked about how a fuzzer generates inputs and watches for crashes. But “watches for crashes” is doing a lot of work there. What is the fuzzer actually watching?

It’s watching the exit code. If the program exits with a non-zero code, especially from a signal like SIGSEGV (11) or SIGABRT (6), the fuzzer saves that input as a crash.

But now you can see the problem.

  • Stack overflow: exit code 139 (SIGSEGV). Fuzzer finds it.
  • Heap overflow without ASAN: exit code 134 (SIGABRT from malloc). Fuzzer finds it, but the crash location is misleading.
  • Use-After-Free without ASAN: exit code 0 (success). Fuzzer misses it completely.

This is why the standard setup is: always fuzz with sanitizers enabled. ASAN, UBSan, MSan. They make the program crash loudly the moment something goes wrong, not later (or never).

A fuzzer without sanitizers is running blind. It can only see the bugs loud enough to crash the process by themselves. With sanitizers, every memory mistake, every out-of-bounds read, every use-after-free, every integer overflow, becomes a detectable crash.

We’ll cover sanitizers completely in the next blog. For now, understand why we need them: because bugs are often silent, and silent bugs are the dangerous ones.


What I Found Confusing (And Now Don’t)

“Segmentation fault just means the program crashed, right?” No. It means the program accessed memory it didn’t have permission to touch, the kernel sent SIGSEGV, and the process died. It’s a specific mechanical chain, not a vague “it broke.”

“Why does 0x4141414141414141 appear in the backtrace?” Because 0x41 is ASCII ‘A’. When we sent 200 A’s and they overflowed the buffer, they overwrote the return address on the stack. GDB is showing us those corrupted addresses. The fact that they’re all A’s means we controlled them, which means an attacker could put a real address there.

“If the program doesn’t crash, there’s no bug?” Completely wrong. Use-After-Free, out-of-bounds reads, uninitialized memory, these can all run without crashing. They corrupt data silently, leak information quietly, or wait to be exploited. The program saying “exit 0” only means it finished. It doesn’t mean it was correct.

“Why does AddressSanitizer find bugs that normal execution misses?” ASAN instruments the program at compile time to add memory tracking. Every malloc and free is tracked. Memory around allocations is “poisoned,” marked as inaccessible. When any access touches poisoned memory, even a silent one that wouldn’t crash normally, ASAN immediately stops the program and reports it. It makes invisible corruption visible.


What We Learned

Segmentation fault (SIGSEGV): Signal 11 from the kernel. Sent when a process accesses a virtual memory address that isn’t mapped or that it doesn’t have permission to access. The process is immediately terminated.

Stack buffer overflow: Writing past the end of a stack-allocated buffer. Because the stack also holds return addresses, overflowing a buffer can corrupt the return address. When the function tries to return, the CPU jumps to the corrupted address. If the corrupted address is controlled by attacker input, this can become code execution.

Return address: The address stored on the stack telling the CPU where to continue execution after a function returns. Stack overflows can overwrite this, redirecting execution.

Heap buffer overflow: Writing past the end of a heap-allocated buffer (from malloc). Corrupts adjacent heap memory. Can damage heap metadata, other allocations, or function pointers stored on the heap.

Use-After-Free (UAF): Reading or writing memory after it has been free()'d. The freed memory may be reallocated to something else. If a function pointer or data pointer in that freed region is used, an attacker who controls subsequent allocations can redirect execution. Often does not crash without sanitizers.

GDB backtrace: The list of function calls active at the moment of a crash, in reverse order (most recent first). Each entry shows the memory address the CPU was at or trying to jump to. Corrupted entries (like 0x4141414141414141) indicate the stack has been overwritten.

AddressSanitizer (ASAN): A compile-time instrumentation tool that detects memory errors at runtime: heap/stack buffer overflows, use-after-free, use-after-return, double-free. Makes silent bugs loud by crashing immediately when any of these occur. Essential for fuzzing, without it most memory bugs go undetected.

Exit code 139: 128 + 11 (SIGSEGV). The standard Linux exit code for a process killed by a segmentation fault.

MMU: Memory Management Unit. Hardware component that translates virtual addresses to physical addresses and enforces memory permissions. When a program accesses an unmapped or forbidden address, the MMU raises a hardware fault to kernel to SIGSEGV.


→ Next: Blog 5; Sanitizers: Your Fuzzing Superpowers

← Previous: Blog 3; The Taxonomy: Every Type of Fuzzing Explained