Presentation is loading. Please wait.

Presentation is loading. Please wait.

Software Verification Graph Model. 2 Graph Coverage Four Structures for Modeling Software Graphs Logic Input Space Syntax Use cases Specs Design Source.

Similar presentations


Presentation on theme: "Software Verification Graph Model. 2 Graph Coverage Four Structures for Modeling Software Graphs Logic Input Space Syntax Use cases Specs Design Source."— Presentation transcript:

1 Software Verification Graph Model

2 2 Graph Coverage Four Structures for Modeling Software Graphs Logic Input Space Syntax Use cases Specs Design Source Applied to DNF Specs FSMs Source Input Models Integ Source

3 3 Definition of a Graph A set N of nodes, N is not empty A set N 0 of initial nodes, N 0 is not empty A set N f of final nodes, N f is not empty A set E of edges, each edge from one node to another –( n i, n j ), i is predecessor, j is successor

4 4 Three Example Graphs 0 21 3 N 0 = { 0 } N f = { 3 } 0 21 3 N 0 = { } N f = { 3 } 9 0 43 7 1 5 8 2 6 N 0 = { 0, 1, 2 } N f = { 7, 8, 9 } Not a validgraph

5 5 Paths in Graphs Path : A sequence of nodes – [n 1, n 2, …, n M ] –Each pair of nodes is an edge Length : The number of edges –A single node is a path of length 0 Subpath : A subsequence of nodes in p is a subpath of p Reach (n) : Subgraph that can be reached from n 9 7 8 0 1 2 43 5 6 A Few Paths [ 0, 3, 7 ] [ 1, 4, 8, 5, 1 ] [ 2, 6, 9 ] Reach (0) = { 0, 3, 4, 7, 8, 5, 1, 9 } Reach ({0, 2}) = G Reach([2,6]) = {2, 6, 9}

6 6 Control Flow Graphs A CFG models all executions of a method by describing control structures Nodes : Statements or sequences of statements (basic blocks) Edges : Transfers of control Basic Block : A sequence of statements such that if the first statement is executed, all statements will be (no branches) CFGs are sometimes annotated with extra information –branch predicates –defs –uses Rules for translating statements into graphs …

7 7 CFG : The if Statement if (x < y) { y = 0; x = x + 1; } else { x = y; } 4 1 23 x >= yx < y x = y y = 0 x = x + 1 if (x < y) { y = 0; x = x + 1; } 3 1 2 x >= y x < y y = 0 x = x + 1

8 8 CFG : The if-return Statement if (x < y) { return; } print (x); return; 3 1 2 x >= y x < y return print (x) return No edge from node 2 to 3. The return nodes must be distinct.

9 9 Loops Loops require “extra” nodes to be added Nodes that do not represent statements or basic blocks

10 10 CFG : while and for Loops x = 0; while (x < y) { y = f (x, y); x = x + 1; } 1 x = 0 43 y =f(x,y) x = x + 1 x >= yx < y for (x = 0; x < y; x++) { y = f (x, y); } 1 x = x + 1 2 35 x >= yx < y y = f (x, y) 4 2 dummy node x = 0 implicitly initializes loop implicitly increments loop

11 11 CFG : do Loop, break and continue x = 0; do { y = f (x, y); x = x + 1; } while (x < y); println (y) 1 x = 0 3 2 x >= y x < y y = f (x, y) x = x+1 1 x = 0 83 x = x + 1 break y < 0 24567 y =f(x,y) y == 0 y = y*2 continue x = 0; while (x < y) { y = f (x, y); if (y == 0) { break; } else if y < 0) { y = y*2; continue; } x = x + 1; } print (y);

12 12 CFG : The Case (switch) Structure read ( c) ; switch ( c ) { case ‘N’: y = 25; break; case ‘Y’: y = 50; break; default: y = 0; break; } print (y); 5 1 read ( c ); c == ‘N’ y = 0; break; 243 c == ‘Y’ default y = 50; break; y = 25; break; print (y);

13 Exponentiation Algorithm 6 5 7 3 4 1 2 1 scanf(“%d %d”,&x, &y); 2 if (y < 0) pow = -y; else pow = y; 3 z = 1.0; 4 while (pow != 0) { z = z * x; pow = pow - 1; 5 } 6 if (y < 0) z = 1.0 / z; 7 printf (“%f”,z);

14 Bubble Sort Algorithm 1234567 1 for (j=1; j<N; j++) { last = N - j + 1; 2 for (k=1; k<last; k++) { 3 if (list[k] > list[k+1]) { temp = list[k]; list[k] = list[k+1]; list[k+1] = temp; 4 } 5 } 6 } 7 print(“Done\n”);

15 © Ammann & Offutt Example Control Flow – Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2]; mean = sum / (double) length; varsum = 0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); }

16 © Ammann & Offutt Control Flow Graph for Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2]; mean = sum / (double) length; varsum = 0; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ I ] - mean) * (numbers [ I ] - mean)); } var = varsum / ( length - 1.0 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); } i = 0 i >= length i < length i++ i >= length i < length i = 0 i++ 1 2 3 5 4 6 8 7

17 17 Test Paths and SESEs Test Path : A path that starts at an initial node and ends at a final node Test paths represent execution of test cases –Some test paths can be executed by many tests –Some test paths cannot be executed by any tests SESE graphs : All test paths start at a single node and end at another node –Single-entry, single-exit –N0 and Nf have exactly one node 0 2 1 63 5 4 Double-diamond graph Four test paths [ 0, 1, 3, 4, 6 ] [ 0, 1, 3, 5, 6 ] [ 0, 2, 3, 4, 6 ] [ 0, 2, 3, 5, 6 ]

18 18 Tests and Test Paths test 1 test 2 test 3 many-to-one test 1 test 2 test 3 many-to-many Test Path 1 Test Path 2 Test Path 3 Non-deterministic software – a test can execute different test paths Test Path Deterministic software – a test always executes the same test path

19 19 Visiting and Touring Visit : A test path p visits node n if n is in p A test path p visits edge e if e is in p Tour : A test path p tours subpath q if q is a subpath of p Path [ 0, 1, 3, 4, 6 ] Visits nodes 0, 1, 3, 4, 6 Visits edges (0, 1), (1, 3), (3, 4), (4, 6) Tours subpaths [0, 1, 3], [1, 3, 4], [3, 4, 6], [0, 1, 3, 4], [1, 3, 4, 6]

20 20 Data Flow Criteria Definition (def) : A location where a value for a variable is stored into memory Use : A location where a variable’s value is accessed def (n) or def (e) : The set of variables that are defined by node n or edge e use (n) or use (e) : The set of variables that are used by node n or edge e Goal: Try to ensure that values are computed and used correctly 0 2 1 63 5 4 X = 42 Z = X-8 Z = X*2 Defs: def (0) = {X} def (4) = {Z} def (5) = {Z} Uses: use (4) = {X} use (5) = {X}

21 Data Flow Coverage for Source def : a location where a value is stored into memory –x appears on the left side of an assignment ( x = 44 ;) –x is an actual parameter in a call and the method changes its value –x is a formal parameter of a method (implicit def when method starts) –x is an input to a program use : a location where variable’s value is accessed –x appears on the right side of an assignment –x appears in a conditional test –x is an actual parameter to a method –x is an output of the program –x is an output of a method in a return statement If a def and a use appear on the same node, then it is only a DU-pair if the def occurs after the use and the node is in a loop

22 22 DU Pairs and DU Paths DU pair : A pair of locations (l i, l j ) such that a variable v is defined at l i and used at l j Def-clear : A path from l i to l j is def-clear with respect to variable v if v is not given another value on any of the nodes or edges in the path Reach : If there is a def-clear path from l i to l j with respect to v, the def of v at l i reaches the use at l j du-path : A simple subpath that is def-clear with respect to v from a def of v to a use of v du (n i, n j, v) – the set of du-paths from n i to n j du (n i, v) – the set of du-paths that start at n i

23 Def-Use Pairs... if (...) { x =... ;... } y =... + x +... ; x =... if (...) {... y =... + x +...... Definition: x gets a value Use: the value of x is extracted Def-Use path

24 Definition-Clear or Killing x =... // A: def x q =... x = y; // B: kill x, def x z =... y = f(x); // C: use x x =...... Definition: x gets a value Use: the value of x is extracted A x = y Definition: x gets a new value, old value is killed... y = f(x) B C Path B..C is definition-clear Path A..C is not definition- clear

25 25 Touring DU-Paths A test path p du-tours subpath d with respect to v if p tours d and the subpath taken is def-clear with respect to v Three criteria –Use every def –Get to every use –Follow all du-paths

26 26 Data Flow Test Criteria All-defs coverage (ADC) : For each set of du-paths S = du (n, v), TR contains at least one path d in S. All-uses coverage (AUC) : For each set of du-paths to uses S = du (n i, n j, v), TR contains at least one path d in S. All-du-paths coverage (ADUPC) : For each set S = du (ni, nj, v), TR contains every path d in S. Then we make sure that every def reaches all possible uses Finally, we cover all the paths between defs and uses First, we make sure every def reaches a use

27 27 Data Flow Testing Example 0 2 1 63 5 4 X = 42 Z = X-8 Z = X*2 All-defs for X [ 0, 1, 3, 4 ] All-uses for X [ 0, 1, 3, 4 ] [ 0, 1, 3, 5 ] All-du-paths for X [ 0, 1, 3, 4 ] [ 0, 2, 3, 4 ] [ 0, 1, 3, 5 ] [ 0, 2, 3, 5 ]

28 Example: Definition and Uses 1. read (x, y); 2. z = x + 2; 3. if (z < y) 4 w = x + 1; else 5. y = y + 1; 6. print (x, y, w, z); What are the definitions and uses for the program below?

29 Example: Definition and Uses 1. read (x, y); 2. z = x + 2; 3. if (z < y) 4 w = x + 1; else 5. y = y + 1; 6. print (x, y, w, z); DefUse x, y zx z, y wx yy x, y, w, z

30 Example Data Flow – Stats public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0.0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2 ]; mean = sum / (double) length; varsum = 0.o; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); } var = varsum / ( length - 1 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); }

31 public static void computeStats (int [ ] numbers) { int length = numbers.length; double med, var, sd, mean, sum, varsum; sum = 0.0; for (int i = 0; i < length; i++) { sum += numbers [ i ]; } med = numbers [ length / 2 ]; mean = sum / (double) length; varsum = 0.o; for (int i = 0; i < length; i++) { varsum = varsum + ((numbers [ i ] - mean) * (numbers [ i ] - mean)); } var = varsum / ( length - 1 ); sd = Math.sqrt ( var ); System.out.println ("length: " + length); System.out.println ("mean: " + mean); System.out.println ("median: " + med); System.out.println ("variance: " + var); System.out.println ("standard deviation: " + sd); } 8 1 2 4 3 5 6 7 Control Flow Graph for Stats ( numbers ) sum = 0 length = numbers.length i = 0 i >= length i < length sum += numbers [ i ] i++ med = numbers [ length / 2 ] mean = sum / (double) length varsum = 0 i = 0 i >= length i < length varsum = … i++ var = varsum / ( length - 1.0 ) sd = Math.sqrt ( var ) print (length, mean, med, var, sd)

32 8 1 2 4 3 5 6 7 CFG for Stats – With Defs & Uses def (1) = { numbers, sum, length } def (2) = { i } def (5) = { med, mean, varsum, i } use (5) = { numbers, length, sum } def (8) = { var, sd } use (8) = { varsum, length, mean, med, var, sd } use (3, 5) = { i, length } use (3, 4) = { i, length } def (4) = { sum, i } use (4) = { sum, numbers, i } use (6, 8) = { i, length } use (6, 7) = { i, length } def (7) = { varsum, i } use (7) = { varsum, numbers, i, mean }

33 Defs and Uses Tables for Stats NodeDefUse 1{ numbers, sum, length } { numbers } 2{ i } 3 4{ sum, i }{ numbers, i, sum } 5{ med, mean, varsum, i } { numbers, length, sum } 6 7{ varsum, i }{ varsum, numbers, i, mean } 8{ var, sd }{ varsum, length, var, mean, med, var, sd } EdgeUse (1, 2) (2, 3) (3, 4){ i, length } (4, 3) (3, 5){ i, length } (5, 6) (6, 7){ i, length } (7, 6) (6, 8){ i, length }

34 DU Pairs for Stats variableDU Pairs numbers(1, 4) (1, 5) (1, 7) length(1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8)) med(5, 8) var(8, 8) sd(8, 8) mean(5, 7) (5, 8) sum(1, 4) (1, 5) (4, 4) (4, 5) varsum(5, 7) (5, 8) (7, 7) (7, 8) i(2, 4) (2, (3,4)) (2, (3,5)) (2, 7) (2, (6,7)) (2, (6,8)) (4, 4) (4, (3,4)) (4, (3,5)) (4, 7) (4, (6,7)) (4, (6,8)) (5, 7) (5, (6,7)) (5, (6,8)) (7, 7) (7, (6,7)) (7, (6,8)) No def-clear path … different scope for i No path through graph from nodes 5 and 7 to 4 or 3 defs come before uses, do not count as DU pairs defs after use in loop, these are valid DU pairs

35 DU Paths for Stats variableDU PairsDU Paths numbers(1, 4) (1, 5) (1, 7) [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] length(1, 5) (1, 8) (1, (3,4)) (1, (3,5)) (1, (6,7)) (1, (6,8)) [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 8 ] [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] med(5, 8)[ 5, 6, 8 ] var(8, 8)No path needed sd(8, 8)No path needed sum(1, 4) (1, 5) (4, 4) (4, 5) [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] variableDU PairsDU Paths mean(5, 7) (5, 8) [ 5, 6, 7 ] [ 5, 6, 8 ] varsum(5, 7) (5, 8) (7, 7) (7, 8) [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] i(2, 4) (2, (3,4)) (2, (3,5)) (4, 4) (4, (3,4)) (4, (3,5)) (5, 7) (5, (6,7)) (5, (6,8)) (7, 7) (7, (6,7)) (7, (6,8)) [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ]

36 DU Paths for Stats – No Duplicates There are 38 DU paths for Stats, but only 12 unique [ 1, 2, 3, 4 ] [ 1, 2, 3, 5 ] [ 1, 2, 3, 5, 6, 7 ] [ 1, 2, 3, 5, 6, 8 ] [ 2, 3, 4 ] [ 2, 3, 5 ] [ 4, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 5, 6, 8 ] [ 7, 6, 7 ] [ 7, 6, 8 ] 4 expect a loop not to be “entered” 2 require at least two iterations of a loop 6 require at least one iteration of a loop

37 Test Cases and Test Paths Test Case : numbers = (44) ; length = 1 Test Path : [ 1, 2, 3, 4, 3, 5, 6, 7, 6, 8 ] Additional DU Paths covered (no sidetrips) [ 1, 2, 3, 4 ] [ 2, 3, 4 ] [ 4, 3, 5 ] [ 5, 6, 7 ] [ 7, 6, 8 ] The five stars that require at least one iteration of a loop Test Case : numbers = (2, 10, 15) ; length = 3 Test Path : [ 1, 2, 3, 4, 3, 4, 3, 4, 3, 5, 6, 7, 6, 7, 6, 7, 6, 8 ] DU Paths covered (no sidetrips) [ 4, 3, 4 ] [ 7, 6, 7 ] The two stars that require at least two iterations of a loop Other DU paths require arrays with length 0 to skip loops But the method fails with index out of bounds exception… med = numbers [length / 2]; A fault was found

38 Example: Def-Use Associations read (z) x = 0 y = 0 if (z  0) { x = sqrt (z) if (0  x && x  5) y = f (x) else y = h (z) } y = g (x, y) print (y) def-use associations for variable z.

39 Example: Def-Use Associations read (z) x = 0 y = 0 if (z  0) { x = sqrt (z) if (0  x && x  5) y = f (x) else y = h (z) } y = g (x, y) print (y) def-use associations for variable x.

40 Example: Def-Use Associations read (z) x = 0 y = 0 if (z  0) { x = sqrt (z) if (0  x && x  5) y = f (x) else y = h (z) } y=g (x, y) print (y) def-use associations for variable y.

41 /* ABS This program function returns the absolute value of the integer passed to the function as a parameter. INPUT: An integer. OUTPUT: The absolute value if the input integer. */ 1 int ABS(int x) 2 { 3 if (x < 0) 4 x = -x; 5 return x; 6 } Using Control-flow Testing to Test Function ABS Consider the following function:

42 The Flowgraph for ABS /* ABS This program function returns the absolute value of the integer passed to the function as a parameter. INPUT: An integer. OUTPUT: The absolute value if the input integer. */ 1 int ABS(int x) 2 { 3 if (x < 0) 4 x = -x; 5 return x; 6 }

43 /* COUNT This program counts the number of characters and lines in a text file. INPUT: Text File OUTPUT: Number of characters and number of lines. */ 1 main(int argc, char *argv[]) 2 { 3 int numChars = 0; 4 int numLines = 0; 5 char chr; 6 FILE *fp = NULL; 7 Example: Using Control-flow Testing to Test Program COUNT Consider the following program:

44 8 if (argc < 2) 9 { 10 printf(“\nUsage: %s ”, argv[0]); 11 return (-1); 12 } 13 fp = fopen(argv[1], “r”); 14 if (fp == NULL) 15 { 16 perror(argv[1]); /* display error message */ 17 return (-2); 18 } Program COUNT (Cont’d)

45 19 while (!feof(fp)) 20 { 21 chr = getc(fp); /* read character */ 22 if (chr == ‘\n’) /* if carriage return */ 23 ++numLines; 24 else 25 ++numChars; 26 } 27 printf(“\nNumber of characters = %d”, numChars); 28 printf(“\nNumber of lines = %d”, numLines); 29 } Program COUNT (Cont’d)

46 The Flowgraph for COUNT The junction at line 12 and line 18 are not needed because if you are at these lines then you must also be at line 14 and 19 respectively.

47 An example: All-du-paths What are all the du-paths in the following program ? read (x,y); for (i = 1; i <= 2; i++) print ( “ hello ” ); if (y < 0) print (x);

48 Example: pow(x,y)

49 Def-Use Pairs (3) /** Euclid's algorithm */ public class GCD { public int gcd(int x, int y) { int tmp; // A: def x, y, tmp while (y != 0) { // B: use y tmp = x % y; // C: def tmp; use x, y x = y; // D: def x; use y y = tmp; // E: def y; use tmp } return x; // F: use x }

50 Find all DU for x and y Public int gcd (int x, int y) { int tmp; While (y!= 0) { tmp= x % y; x = y; y = tmp; } return x; } 50


Download ppt "Software Verification Graph Model. 2 Graph Coverage Four Structures for Modeling Software Graphs Logic Input Space Syntax Use cases Specs Design Source."

Similar presentations


Ads by Google