ANU The Australian National University
____________________________________________________
[ANU] [DCS] [COMP2100] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [Assessment] [PSP] [Eiffel] [Reading] [Help]
____________________________________________________
____________________________________________________
Lectures: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29]
____________________________________________________

COMP2100
Lecture 18: 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.


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 that the specification describes what the user wanted.)

Formal verification

But...

Today, formal verification is largely reserved for


Practical Formal Verification Concepts

These include:

Eiffel allows all of these to be made executable and verified at run-time.

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

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

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.

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

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.

____________________________________________________
Lectures: [1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] [18] [19] [20] [21] [22] [23] [24] [25] [26] [27] [28] [29]
____________________________________________________
____________________________________________________
[ANU] [DCS] [COMP2100] [Description] [Schedule] [Lectures] [Labs] [Homework] [Assignments] [Assessment] [PSP] [Eiffel] [Reading] [Help]
____________________________________________________

Copyright © 2004, Jim Grundy and Ian Barnes, The Australian National University
Feedback & Queries to comp2100@iwaki.anu.edu.au
Version 2004.1, 31 March 2004, 12:15:28