Definition
Edge coverage is a coverage metric §10.18 whose elements are the edges of a program’s control-flow graph §3.13. That graph has a node for each basic block §3.14, a run of instructions entered only at its first instruction and left only after its last, and an edge for each possible transfer of control between blocks. Edge coverage records which edges the tests executed at least once, as a set or as a fraction of all edges. It is finer than counting blocks and is the usual feedback in coverage-guided fuzzing §10.12.
The idea
Take the code if (len > 64) len = 64; followed by a copy of len bytes. It has three basic blocks: A tests the condition, B sets len to 64, and C does the copy. The edges are A to B, B to C, and A to C, the edge taken when the condition is false. A single test with len equal to 100 runs A, B and C, so every block has run. The edge from A to C has not: no test has yet copied with a length that was not capped. Block coverage reports 100%; edge coverage reports and names the untested route.
A road map is a fair picture. Blocks are towns and edges are roads. Block coverage lists the towns visited; edge coverage lists the roads driven. The picture stops at the cargo. A list of roads driven says nothing about what was carried, how often each road was driven or in which order, and programs fail because of exactly those things.
How it works
Compilers record edges in one of two ways.
Count each edge directly. An edge whose source block has only one successor runs exactly when that block runs, and an edge whose target block has only one predecessor runs exactly when that block runs, so a counter in that block suffices. The hard case is a critical edge: an edge from a block with several successors to a block with several predecessors, such as A to C above. The compiler places a new, empty block on each critical edge, so that counting the blocks of the new graph counts the edges of the old one. SanitizerCoverage, the instrumentation §14.24 built into Clang §12.17 that libFuzzer §10.26 uses, works this way.
Hash pairs of blocks. The original design of the fuzzer American Fuzzy Lop (AFL) gives each block a random number as its identifier when the program is compiled. At run time it remembers the identifier of the previous block, shifted right by one bit. At the start of each block it computes where and are the identifiers of the current and the previous block, is bitwise exclusive or, and is a shift right by one bit. It then adds 1 to the byte at that index in a table called the coverage map, which has 65,536 bytes in the default configuration. Without the shift, the edge from A to B and the edge from B to A would get the same index, and every block that jumps back to itself would land on index 0.
The hashed map has two side effects. Two different edges can get the same index, a collision, and the fuzzer then cannot tell them apart; AFL++ §10.27, the successor of AFL, also offers a mode that assigns identifiers without collisions when the program is linked. And AFL records how often each edge ran, in coarse buckets: 1, 2, 3, 4 to 7, 8 to 15, 16 to 31, 32 to 127, and 128 or more. A fuzzer of this kind treats an input as new when it reaches a map entry no earlier input reached, or moves an entry into a new bucket.
Worked example
The index. Let block P have the identifier 0x5a3c and block Q the identifier 0x0f0f, both 16-bit numbers in hexadecimal. For the edge from P to Q, 0x5a3c shifted right by one bit is 0x2d1e, and 0x0f0f exclusive-or 0x2d1e is 0x2211. For the edge from Q to P, 0x0f0f shifted right by one bit is 0x0787, and 0x5a3c exclusive-or 0x0787 is 0x5dbb. The two directions land at different indices. Without the shift, both would land at 0x5a3c exclusive-or 0x0f0f, which is 0x5533.
Same edges, different data. A function sums the first bytes of a 64-byte buffer with for (i = 0; i < n; i++) sum += buf[i];. Its blocks are Init (i = 0), Head (the test i < n), Body (the addition and i++) and Exit. For any , a run executes the four edges Init to Head, Head to Body, Body to Head, and Head to Exit, so and give the same edge set. The edges Head to Body and Body to Head run 64 times in the first run and 65 times in the second; both counts fall into the bucket 32 to 127, so even the bucketed map is identical. Yet reads buf[0] to buf[63], all inside the buffer, while also reads buf[64], one byte past the end. Only a check on the address, such as the one AddressSanitizer §10.22 inserts, separates the two runs.
All edges, not all paths. A pointer p starts out pointing to a buffer. Block A tests a; block B, run when a is true, sets p to a null pointer §4.26, a pointer to nothing. Block C tests b; block D, run when b is true, points p back to the buffer. Block E reads through p. The six edges are A to B, A to C, B to C, C to D, C to E, and D to E. The test with a and b both true runs A, B, C, D, E; the test with both false runs A, C, E. Together they execute all 6 edges, which is 100% edge coverage, and neither test crashes. The crash needs a true and b false, the path A, B, C, E, which is one of the paths and was never run.
Stated precisely
Let the control-flow graph be , where is the set of basic blocks and is the set of edges. Each edge is an ordered pair of blocks such that control can pass from directly to . A run that executes the blocks in that order has the covered edge set For a set of runs, where is the number of elements of a set , and is the union over all runs in .
A set records neither order nor repetition. Two runs therefore have the same covered edge set whenever they use the same transitions, whatever the order, the number of repetitions and the data values. Hit-count buckets add back a coarse count of repetitions. A hashed map replaces each edge by its index, so the number of map entries in use can be smaller than the number of distinct edges executed. is the graph of the build that ran: a different compiler or optimization level gives a different .
Limits and common mistakes
Equal edge sets do not mean equal behavior. Values, repetition counts within one bucket, order, timing, memory contents and pointer lifetimes all vary freely between runs with the same edges. A stale pointer §4.1, one that still refers to memory that has been freed, follows the same edges as a valid pointer.
Edge coverage is not path coverage. A function with two-way decisions in a row has edges leaving those decisions but paths; for that is 20 edges against 1,024 paths.
Missing code has no edge. A bounds check or an authorization §1.7 check that was never written adds no edge, so 100% edge coverage cannot reveal its absence.
The compiler decides what an edge is. Compiler optimization §3.39 can turn a short if into a branch-free conditional-move instruction, which leaves no edge to record, and can merge or copy blocks. Figures from builds with different optimization settings cannot be compared.
Edge coverage of compiled C code sees each condition of a && b separately, because b is evaluated only when a is true. It still does not show that each condition changes the outcome on its own; the branch coverage §10.20 entry treats that stronger requirement.
Questions answered
Can two different data states produce the same covered edge set?
Yes. A covered edge set records only which transitions between basic blocks, the straight runs of code between jumps, happened at least once. It ignores data values, the number of repetitions, the order of the edges and the contents of memory. For example, a loop that reads the first bytes of a 64-byte buffer executes the same four edges for and for , and even the repetition counts, 64 and 65, fall into the same coarse bucket that some fuzzers record. Yet the second run reads one byte past the end of the buffer.
In the same way, a pointer to freed memory follows the same edges as a pointer to live memory, and an unauthorized request follows the same edges as an authorized one when the program lacks the check that would separate them. Equal edge sets show equal routes through the code, not equal or equally safe behavior. Telling such runs apart needs checks on the data itself: sanitizers §10.21 that check memory accesses, test oracles §10.5 that check results, and inputs chosen at boundary values.
Related entries: Coverage-guided fuzzing (§10.12), Coverage metric (§10.18), Test oracle (§10.5), Branch coverage (§10.20), Control-flow graph (CFG) (§3.13), Basic block (§3.14).