ANU The Australian National University



____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

COMP2100/2500
Lecture 14: Static Code Analysis

Summary

Introduction to static analysis of code, concentrating on formal verification and code reviews.

Aims


Code Analysis

Dynamic Code Analysis:

Software is run on test data and its behaviour analysed.

  • Specification Testing (black box)

  • Structural Testing (white box)

  • Integration Testing

  • Acceptance Testing

Static Code Analysis:

Software is examined without being run.

  • Formal Verification

  • Static Analysis Tools

  • Code Inspection

None of these is the solution. We need a combination.


Why Do Static Code Analysis?

Why Do Dynamic Code Analysis?


Formal Verification

Formal verification is a proof that a program meets its specification. (But not necessarily that the specification describes what the user wanted. Remember the saying about building the software right vs. building the right software.)

Formal verification

But...

Today, formal verification is largely reserved for


Practical Formal Verification Concepts

These include:

Eiffel allowed all of these to be made executable and verified at run-time. Java doesn't have these features built in to the language, but it is possible to add them. See the way pre- and post-conditions are implemented in the Oops system using the Assert class.

Example: Verifying Loops

from
   S        -- Setup Code
invariant
   I        -- Invariant
variant
   v        -- Variant
until
   C        -- Stopping Condition
loop
   B        -- Loop Body
end

Consider the contract for the loop.

Choose S, I, v, C and B such that

This will ensure that your loop code always establishes the postcondition. In other words, it proves the assertion

{P} loop {Q}

Example: Find the smallest element in an array

Precondition:

a is nonempty

Postcondition:

s is the smallest element in a

In Eiffel:

from 
   i := a.lower
   s := a.item (i)
invariant
   -- s is the smallest element in the set
   -- {a.item (a.lower), ..., a.item (i)}
variant
   a.upper - i
until
   i = a.upper
loop
   i := i + 1
   s := s.min (a.item (i))
end

The same thing in Java:

i = 0;
s = a[0];
while (i < a.length - 1) {
  /*
   * Invariant: s is the smallest element in the set
   * {a[0], a[1], ..., a[i]}
   * Variant: a.length - i - 1
   */
  i++;
  s = Math.min(s, a[i]);
}

Notice that the syntax makes it less obvious. Even less so with a for loop, I think. The point is that the Eiffel loop syntax was designed with correctness proofs in mind.


Example: Find the first occurrence of t in a

Precondition:

Nothing

Postcondition:

If i is a valid index into a then it is the index of the first instance of t in a.

In Eiffel:

from
   i := a.lower
invariant
   -- There does not exist j such that
   -- a.lower <= j < i and a.item (j) = t
variant
   a.upper + 1 - i
until
   i = a.upper + 1 or else a.item (i) = t
loop
  i := i + 1
end

The same thing in Java:

i = 0;
while (i < a.length && a[i] != t) {
    /*
     * Invariant: There is no j with 0 <= j < i and a[j] = t
     * Variant: a.length - i
     */
    i++;
}

Static Analysis Tools

The compiler will check your code for syntactic errors, but other voluntary style constraints can be checked statically:

The ‘lint’ program for C is the best known static analysis tool. I use ‘weblint’ to check the HTML code in these notes.


Code Reviews

(See Sections 13.4 - 13.9 and Chapter 14 of Introduction to the Personal Software Process.)

What is a code review?

When you sit down, either alone or in a small group, and read carefully through every line of a piece of code, looking for and recording defects.

This is a new phase to add to the PSP, between the Code and Compile phases.

Advantages of code reviews

Disadvantages of code reviews

Typically code reviews are skipped when projects come under pressure. This is a false economy.


How to do a code review


Why find defects early?


Typical defect injection and removal rates

  pre-PSP Post-PSP
inject 100 / KLOC 50 / KLOC
remove in code review 0 60%
remove in compile 50% 50%
remove in unit test 80% 80%
defects remaining 10 / KLOC 2 / KLOC

Typical times taken: For code review 10 defects per hour; for compile, 10-12 defects per hour; for unit test 2 defects per hour; for integration and system testing, at least 10 hours per defect.


Defect rates and cost examples

1. Student assignment: 500 LOC

  Without PSP With PSP
Defects  
Total injected5025
Found in code review015
Remaining5010
Found in compile255
Found in unit test204
Defects remaining51
Time  
Code review02.5
Compile20.5
Unit test102
Total defect removal time125

2. Individual project: 10,000 LOC

 Without PSPWith PSP
Defects  
Total injected1,000500
Found in code review0300
Remaining1,000200
Found in compile500100
Found in unit test40080
Defects remaining10020
Time  
Code review050
Compile4010
Unit test20040
Total defect-removal time240100

Those remaining defects passed to the user would typically take about 10 hours each to remove. That's 1,000 hours versus 200 hours: a saving of another 800 hours.

3. Team project: 50,000 LOC, five-member team.

Each member develops 10,000 LOC component as above, then they integrate them to form a complete system. The non-PSP team have approximately 500 defects remaining in their system, while the PSP team has 100.

 Without PSPWith PSP
Time  
Integration and system test5,0001,000

How long does it take 5 people to do 5,000 hours work? With the whole team doing nothing else for 40 hours per week, that's 25 weeks, or just under six months. The PSP team have only 1,000 hours to do, or 5 weeks' full-time work. Taking into account the savings on the individual phase (140 hours, three and a half weeks) they will deliver their product at least five months earlier.

____________________________________________________

[ANU] [DCS] [COMP2100/2500] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [COMP2500] [Assessment] [PSP] [Java] [Reading] [Help]

____________________________________________________

Copyright © 2005, Jim Grundy and Ian Barnes, The Australian National University
Version 2005.3, Tuesday, 26 April 2005, 00:00:11 +1000
Feedback & Queries to comp2100@cs.anu.edu.au