Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 36 ยท SPARK and Safety

Flow Analysis and Data Dependencies

Learn SPARK flow analysis: the Global, Depends and Initializes aspects, detecting uninitialized variables, ineffective statements and information leaks, with C stand-ins.

Before the provers try to show that your arithmetic is safe, SPARK runs a faster, simpler analysis called flow analysis. It looks at where data comes from and where it goes. It finds variables read before they are written, assignments whose result is never used, parameters that are not really inputs, and unintended dependencies. This is the Bronze level of assurance from What Is SPARK?.

The tools are not available on this site, so the SPARK listings here are read-only and we describe tool messages in words. The C stand-ins are runnable and model the data-flow questions.

Parameter modes are a promise

Ada's modes in, out and in out are already data-flow statements. Flow analysis holds you to them:

  • An out parameter must be fully assigned on every path; reading it before assignment is an error.
  • An in out parameter must really be read, or flow analysis warns that it could be out.
  • An in parameter that is never used gets flagged as unused.
ADA
procedure Swap (A, B : in out Integer)
  with SPARK_Mode
is
   Tmp : constant Integer := A;
begin
   A := B;
   B := Tmp;
end Swap;

This passes. Now consider a procedure with a path that forgets to assign Result:

ADA
procedure Classify (X : Integer; Result : out Integer)
  with SPARK_Mode
is
begin
   if X > 0 then
      Result := 1;
   elsif X < 0 then
      Result := -1;
   end if;                       --  X = 0: Result is never set
end Classify;

Flow analysis reports that Result might not be initialized on the path where X = 0. The compiler of an ordinary language may stay silent, and the bug would show up only for the input zero. Here it is caught for the whole program before it runs.

Flow aspects

Global which globals are used
Depends output from which inputs
Initializes set at elaboration
Refined_* details in the body

All of them are checked against the code.

1 / 2

Global: naming the hidden inputs

A subprogram that reads or writes a package variable has a hidden input or output. SPARK makes you state it so a reader (and the analysis) sees the whole interface:

ADA
Total : Natural := 0;

procedure Add (N : Natural)
  with Global => (In_Out => Total);

procedure Report
  with Global => (Input => Total);

function Zero return Natural
  with Global => null;          --  touches no global state

Global => null guarantees purity. If the body touches a variable not listed, flow analysis rejects it. Without an explicit Global, the tool can often generate it, but writing it for public subprograms documents intent and keeps proofs stable across refactoring.

Depends: who influences whom

Depends goes one step further and lists, for each output, which inputs it is derived from:

ADA
procedure Scale (X : Integer; Factor : Integer; Result : out Integer)
  with Depends => (Result => (X, Factor));

If the body ignored Factor, flow analysis would report that Factor is not used to compute Result. If an output depended on something undeclared, such as a secret key in a function that should only look at public data, it would report that too. This is how SPARK checks information-flow policies: for example that a value read from a secret register never influences data sent over a public interface.

Initializes is the package-level cousin: it lists the package variables that are given a value during elaboration, so a client can rely on them being valid from the start.

Stand-in: tracing a path that forgets to assign (C, runnable)

The program below mirrors Classify. A sentinel marks "not assigned", and the check at the end is what flow analysis does statically. Running all three inputs shows that only zero leaves the result undefined, proving the point that testing needs the right input while flow analysis does not.

C C stand-in: an out parameter assigned on only some paths

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Stand-in: a dependency matrix (C, runnable)

Depends can be pictured as a table of inputs and outputs. The small program below computes which inputs actually change which output by perturbing one input at a time. It is an experiment, not a proof (flow analysis is static and complete over all inputs), but it shows what the aspect describes. It also catches a function that ignores one of its parameters.

C C stand-in: which inputs does the output depend on?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

The output shows Y has no effect. In SPARK, declaring Depends => (Result => (X, Y, Z)) for this function would produce a flow message saying Y is not used, which usually reveals a copy-and-paste or logic error.

Working with flow messages

  • Start with Global and Depends on a small package and fix every message before adding proofs.
  • Treat "unused assignment" and "ineffective statement" as bugs until you know otherwise.
  • Use Refined_Global and Refined_Depends in the body when state is hidden by an abstract State aspect, so clients see the abstract view.
  • Run flow analysis often: it is fast and gives immediate feedback.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What must be true of an out parameter in SPARK?

    Flow analysis checks that every path initializes the out parameter.
  2. 2. What does Global => null say about a subprogram?

    The subprogram is independent of package-level variables.
  3. 3. What does a Depends aspect state?

    It maps each output to the inputs that influence it, and flow analysis checks the body matches.
  4. 4. Why can flow analysis find a bug that tests miss?

    It is a static analysis over every path, not a sample of executions.

Summary

  • Flow analysis checks data flow before any proof: initialization, unused values and dependencies.
  • Parameter modes are promises; out parameters must be assigned on every path.
  • Global lists hidden inputs and outputs; Global => null marks purity.
  • Depends maps outputs to the inputs that influence them, supporting information-flow checks.
  • Initializes states which package variables are set during elaboration.

Next: Proving Absence of Run-Time Errors