Definition
Dynamic analysis is the examination of a program by running it and observing or checking what it does. Added code, called instrumentation §14.24, records events such as executed branches and memory accesses, or checks a property at each event and stops the run on a failure. Testing, fuzzing §10.9 (running many automatically generated inputs) and sanitizers §10.21 (compiler-added checks that catch errors, such as invalid memory accesses, as they happen) are forms of dynamic analysis. A violation found in a run really happened; finding none says nothing about runs that did not happen.
The idea
Static analysis §3.1 reasons about a model of the program, so it can warn about executions that never happen and miss effects the model leaves out. Running the real program avoids both problems: what is observed really happened, on the code the compiler really produced and on the real machine. A memory error caught during a run comes with the input that caused it, so a developer can replay it.
The price is coverage. A run follows one path through the program, with one set of data values, one response from each external function and one ordering of threads. A test drive is a fair picture: it shows how the car behaved on this road, today, at these speeds. The picture fails in one respect. A car usually behaves similarly on a slightly different road, but a program can behave completely differently on an input next to a tested one, because a single comparison can send it into code that no test ran.
How it works
Choose the property and what to observe. For memory safety, the property is that every load and store stays inside a live object.
Instrument the program. A compiler can insert checks at build time. AddressSanitizer §10.22, for example, inserts before each memory access a check that consults shadow memory, a separate table recording which bytes belong to valid objects. A binary instrumentation tool, such as Valgrind, rewrites the machine code while the program runs. An interpreter §2.13, a program that executes another program one instruction at a time, can check every instruction it executes.
Write the test harness: the code that sets up the starting state, supplies inputs and decides whether each outcome is acceptable. The part that decides is the test oracle §10.5.
Run. Use chosen tests, inputs generated by a fuzzer, varied thread schedules, and fault injection §10.28, which forces failures such as an allocation that returns no memory.
Read the result. A report gives a failing run to reproduce and shorten. A coverage metric §10.18, such as branch coverage §10.20, the fraction of branch outcomes that the runs executed, shows which code the runs never reached.
The same instrumentation can enforce a property instead of testing for it. A check that runs on every memory access of the deployed program stops every violating access in every run, tested or not, as long as every access passes through the check.
Worked example
A handler receives a message of bytes, , in an array §14.5 m with positions 0 to . It runs
if (m[0] == 1) { if (n >= 3) return m[2]; else return 0; }
else return m[5];
The else line reads position 5 without checking that . The two conditions have four outcomes in total. The handler runs under AddressSanitizer with three test messages:
, : both conditions are true, and the handler returns
m[2]., : the first condition is false, and the handler returns
m[5], inside positions 0 to 5., : the first condition is true, the second false, and the handler returns 0.
The first two messages execute 3 of the 4 outcomes, a branch coverage of . The third adds the missing outcome: . No run reports an error.
Yet the message , , takes the else line and reads position 5, although its last byte is at position 2. That out-of-bounds read §4.5 lies on a branch that the tests did execute, but only with . Branch coverage counted the branch, not the values of that matter.
Random inputs find this bug quickly. With a uniformly random first byte and a length chosen uniformly from 1 to 16, a message takes the else line with probability and has with probability . About of random messages trigger it. A bug that needs one exact 32-bit value is different: a random 32-bit value matches with probability .
Paths multiply faster than branches. A function with 10 independent if statements in a row has 20 branch outcomes, which two runs can cover, all conditions true and then all false. It has paths.
Stated precisely
Let be the set of all executions the program can have, over every input, every response of its environment and every thread schedule. Let be the executions actually run, and let be a property that each single execution either satisfies or violates, such as “no access outside a live object”. A dynamic analysis decides on the members of , and only as far as its checker detects violations.
If the checker reports a violation in a run and the report is accurate, the program violates : is a counterexample.
If no violation is reported, holds on , to the accuracy of the checker. Nothing follows for , the executions in that were not run.
A claim about all of needs a further argument. There are three: , possible only when is finite and small enough to run completely (exhaustive enumeration §10.33); a static proof; or a statistical claim about a stated input distribution . For the last, independent runs drawn from with no failure give 95% confidence that a run drawn from fails with probability below about , the rule of three §18.5.8. For that bound is about . The claim covers inputs drawn from , not inputs an attacker chooses.
Limits and common mistakes
Checkers miss errors even on executed paths. AddressSanitizer knows where each allocated block ends, not how long the program considers the data to be. If the 3-byte message above sat in a 64-byte receive buffer, reading position 5 would stay inside the buffer and go unreported. An access that jumps far past one object into another valid object can also escape it.
Instrumentation changes the program it observes. AddressSanitizer typically slows a program by a factor of about 2, and any instrumentation changes memory layout and timing. Such instrumentation overhead §11.32 can hide a failure that depends on timing, or create one.
A reported failure can come from the harness or the instrumentation instead of the program, for example from a timeout §9.34 set too short or a harness that builds a starting state the real system never has. Reproduce a failure before believing it.
A finite run can show that a safety property §1.11 fails, because the bad event is visible in the run. It cannot show that a liveness property §1.12 fails, because a run stopped by a timeout might still have finished later.
Enforcement is not testing. A check in the deployed program constrains every execution, but only for the accesses that pass through the check, and turning a violation into a stopped program costs availability §1.10.
Questions answered
What unexecuted paths remain unconstrained by the result?
Every execution that was not run remains unconstrained: a dynamic analysis, which checks a program by running it, says something only about the runs it observed. In particular, these remain open:
branch outcomes that were never taken;
branches that were taken, but not with the data values that matter, such as a length that was tested only with values that happened to be safe;
combinations of branch outcomes, because covering every branch does not cover every path;
responses of the environment that never occurred: failed allocations, error returns from called functions, timeouts, interrupts §6.11 arriving at other moments;
thread schedules that were not run;
starting states that were not used, such as data stored by an earlier version of the program;
builds and machines that were not tested: the release build without instrumentation, other compiler options §12.3, other hardware.
To constrain them, run them, exhaustively if the space is small; prove the property for them with static analysis; make a statistical claim for a stated input distribution; or enforce the property with a check that runs in every execution.
Related entries: Static analysis (§3.1), Symbolic execution (§3.27), Unit test (§10.1), Fuzzing (§10.9), Coverage metric (§10.18), Sanitizer (§10.21), AddressSanitizer (§10.22), Why fuzzing cannot certify universal safety (§17.18). Further reading: [25].