Section 3.2

Dynamic analysis

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

  1. Choose the property and what to observe. For memory safety, the property is that every load and store stays inside a live object.

  2. 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.

  3. 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.

  4. 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.

  5. 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 nn bytes, n≥1n \ge 1, in an array §14.5 m with positions 0 to n−1n - 1. 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 n≥6n \ge 6. The two conditions have four outcomes in total. The handler runs under AddressSanitizer with three test messages:

The first two messages execute 3 of the 4 outcomes, a branch coverage of 3/4=75%3 / 4 = 75\%. The third adds the missing outcome: 4/4=100%4 / 4 = 100\%. No run reports an error.

Yet the message (2,0,0)(2, 0, 0), n=3n = 3, 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 n=6n = 6. Branch coverage counted the branch, not the values of nn 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 255/256≈0.996255 / 256 \approx 0.996 and has n<6n < 6 with probability 5/16=0.31255 / 16 = 0.3125. About 0.996×0.3125≈0.310.996 \times 0.3125 \approx 0.31 of random messages trigger it. A bug that needs one exact 32-bit value is different: a random 32-bit value matches with probability 2−32≈2.3×10−102^{-32} \approx 2.3 \times 10^{-10}.

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 210=10242^{10} = 1024 paths.

Stated precisely

Let BB be the set of all executions the program can have, over every input, every response of its environment and every thread schedule. Let T⊆BT \subseteq B be the executions actually run, and let PP be a property that each single execution either satisfies or violates, such as “no access outside a live object”. A dynamic analysis decides PP on the members of TT, and only as far as its checker detects violations.

A claim about all of BB needs a further argument. There are three: T=BT = B, possible only when BB is finite and small enough to run completely (exhaustive enumeration §10.33); a static proof; or a statistical claim about a stated input distribution DD. For the last, NN independent runs drawn from DD with no failure give 95% confidence that a run drawn from DD fails with probability below about 3/N3 / N, the rule of three §18.5.8. For N=1000N = 1000 that bound is about 3/1000=0.3%3 / 1000 = 0.3\%. The claim covers inputs drawn from DD, 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:

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].