Section 10.30

Positive control

Definition

A positive control is a case in a test or experiment whose correct result is known in advance to be positive: the effect under study is made to occur on purpose, and the apparatus must report it. If the apparatus stays silent here, its silence in the real cases means nothing. In software testing, a positive control is, for example, a bug planted on purpose that a bug detector must report, or a valid request that a protected program must accept.

The idea

A fuzzer, a tool that runs a program on very many automatically generated inputs, runs a parser for 24 hours. The parser was compiled with AddressSanitizer §10.22, a tool that adds checks to a program and reports invalid memory accesses. No failure is reported. Either the parser has no bug of the kind AddressSanitizer finds in the code the inputs reached, or the chain from bug to report is broken: the checks were not compiled in, the fuzz harness §10.13 discarded the error, or the reports went to a directory that nobody reads. Silence cannot tell these situations apart. A positive control can: plant one bug that the detector must catch, trigger it, and check that the report arrives.

Laboratory science gave the idea its name. A test for a virus is also run on a sample known to contain the virus. If that sample tests negative, every negative result of the same run is worthless. A positive control therefore guards against false negatives §1.22, effects that are present but not reported. Its partner, the negative control §10.29, guards against reports of effects that are not there.

Testing a protection uses the idea differently. A program that refuses all requests, valid or not, passes every test that checks the refusal of invalid requests. A positive control, a valid request that must succeed, rules out that useless program and shows that the test can observe a success.

How it works

  1. Name the evidence path. Write down the chain that the real results travel along: the event (a bug triggered, a request processed), the detector, the report, the place the report is collected, and the script or person that reads it.

  2. Choose an event inside the detector’s scope. AddressSanitizer reports, among other errors, accesses just outside a block of heap memory, the memory a program requests while it runs, so a one-byte read past the end of a heap buffer is a fair control for it. A data race §9.3 is not, because AddressSanitizer does not look for data races.

  3. Inject the event through the same path as the real runs. Use the same build options, harness, collector and evaluation script. A control fed through a side channel §1.29, such as a report copied by hand into the results directory, tests only the side channel.

  4. State the expected observation in advance. For example: one report of kind heap-buffer-overflow at the planted line, within 10 seconds of the start.

  5. Run and compare. If the expected report is missing, the negative results of the real runs are void until the path is repaired and the control passes.

  6. Keep the control apart from the results. Run it in a separate build or run, label it as planted, and never count it as a finding.

Two established methods are positive controls on a large scale. Mutation testing §10.45 plants many small faults in a program, each one a positive control for the tests, and counts how many the tests detect. Fault injection §10.28 introduces faults, such as a failed memory allocation or a corrupted message, that the program’s error handling must detect.

Worked example

A parser is compiled with AddressSanitizer and run under a fuzzer. For the positive control, the fuzz harness gets one extra line: when the first input byte is 0xff, it reads the byte at index 16 of a 16-byte heap buffer, whose valid indexes are 0 to 15. That read is out of bounds by exactly one byte. The expected observation is one report of kind heap-buffer-overflow at the planted line.

Run 1. An input that starts with 0xff is placed in the seed corpus §10.14, the set of inputs the fuzzer starts from, so it runs within the first second. The expected report arrives. The detection path works for this kind of event.

Run 2, in a hypothetical broken setup. A change to the build script dropped the compiler option §12.3 that enables AddressSanitizer. Without the added checks, a read of one byte past a 16-byte heap buffer reads a neighboring heap byte and normally does not crash, so no report arrives. The 24-hour campaign, built by the same script, had no detector either, and its silence is void. The positive control exposed the fault in one second.

Reliability. One detection shows that detection is possible, not that it always happens. Suppose the control is triggered in 20 independent runs and reported in all 20. If each run detects it with probability pp, the chance of 20 detections in 20 runs is p20p^{20}. For p=0.86p = 0.86 that chance is 0.8620≈0.0490.86^{20} \approx 0.049, just below 0.05. Solving p20=0.05p^{20} = 0.05 gives p=0.051/20≈0.861p = 0.05^{1/20} \approx 0.861. So 20 detections in 20 runs support, with 95% confidence, a detection probability of at least about 0.86 for this event; they do not show that it is 1.

A protection. A device holds a setting with value 5. The positive control is a request to set the value to 9 with a correct authentication §1.6 tag, the code that proves the sender holds the secret key. The test asserts that the reply reports success and that the value is now 9. If the value stays 5, either the device refuses valid requests or the test cannot see a change; either way, the refusals recorded by the negative controls are not evidence that the protection works.

Stated precisely

Let AA be the apparatus: the program as built, the detector, the collector and the oracle that reads the result. Let ee be an event of a stated kind KK, injected under stated conditions CC through the same path as the real runs. The positive control passes when AA reports ee as stated in advance. A pass establishes exactly this: AA can report events of kind KK under conditions CC. It does not establish that AA reports every event of kind KK, or any event of another kind. A real run that ends with no report supports the claim that no event of kind KK occurred only if the positive control for KK passed under the same conditions.

Suppose the control is run nn times and reported every time, and the runs are independent with the same detection probability pp. Let α\alpha be the accepted error probability, such as 0.05. Then the lower end of the one-sided confidence interval §11.18 for pp at confidence 1−α1 - \alpha is α1/n\alpha^{1/n}. The reason: pnp^n is the probability of nn reports in nn runs, and every pp below α1/n\alpha^{1/n} makes that outcome less likely than α\alpha.

Limits and common mistakes

A positive control tests detection, not discovery. A fuzzer that reports a planted bug when it is handed the triggering input has not shown that it would find that input by itself. Measuring how well a fuzzer finds bugs needs planted bugs that the fuzzer must reach on its own, as in benchmarks that insert many known bugs into real programs.

A control covers one kind of event under one set of conditions. A one-byte overflow triggered in the first second says nothing about an access that lands far past a buffer inside another valid block, which AddressSanitizer can miss, or about reports produced late in a long run, for example after the disk has filled.

A planted event must never be counted as a finding, and it must not stay active in the build used for the real results.

A passing positive control does not make the real results true. It removes one explanation for silence: that the apparatus could not have reported anything.

Questions answered

Can the apparatus detect a deliberately introduced known event?

The apparatus can be trusted to detect such an event only after a run has shown it: introduce one known event on purpose, through the same path as the real measurements, and check that the apparatus reports it as stated in advance. Such a run is a positive control. If the report arrives, the apparatus can detect that kind of event under those conditions. If it does not, the silence of the apparatus in the real runs means nothing until the path is repaired.

Choose an event that the detector is designed to find, for example a one-byte read past a heap buffer for a detector of invalid memory accesses. Use the same build, harness, collector and evaluation script as the real runs, and keep the planted event out of the reported results. One detection shows only that detection is possible; 20 detections in 20 independent runs support a detection probability of at least about 0.86 at 95% confidence. When the apparatus tests a protection, the positive control is a valid request that must be accepted, which shows that the test can observe success and that the program does not refuse everything.

Related entries: Negative control (§10.29), Test oracle (§10.5), Sanitizer (§10.21), AddressSanitizer (§10.22), Mutation testing (§10.45), Fault injection (§10.28).