Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

43
Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs Georg Kienesberger - Vienna University of Technology FOSDEM’09 Free and Open Source Software Developers’ European Meeting 7-8 February 2009 - Brussels, Belgium These slides are licensed under a Creative Commons Attribution-Share Alike 3.0 Austria License. http://creativecommons.org 1

description

Georg Kienesberger - Vienna University of Technology FOSDEM’09 Free and Open Source Software Developers’ European Meeting 7-8 February 2009 - Brussels, Belgium These slides are licensed under a Creative Commons Attribution-Share Alike 3.0 Austria License. http://creativecommons.org

Transcript of Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Page 1: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Ast2Cfg - A Framework for CFG-Based Analysisand Visualisation of Ada Programs

Georg Kienesberger - Vienna University of Technology

FOSDEM’09Free and Open Source Software Developers’ European Meeting

7-8 February 2009 - Brussels, Belgium

These slides are licensed under a Creative Commons Attribution-Share Alike 3.0 Austria License. http://creativecommons.org 1

Page 2: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

1 Overview

2 CFG and AST

3 The Software

4 Live Demonstration of Ast2Cfg, Cfg2Dot

5 Examples

2

Page 3: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Overview

• Control Flow Graph (CFG) used in manyanalysis/optimisation methods

3

Page 4: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Overview

• Control Flow Graph (CFG) used in manyanalysis/optimisation methods

• ISO/IEC 15291:1999 - Ada Semantic Interface Specification(ASIS) implemented by ASIS-for-GNAT

3

Page 5: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Overview

• Control Flow Graph (CFG) used in manyanalysis/optimisation methods

• ISO/IEC 15291:1999 - Ada Semantic Interface Specification(ASIS) implemented by ASIS-for-GNAT

• traverse Abstract Syntax Tree (AST) and build CFG

3

Page 6: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Overview

• Control Flow Graph (CFG) used in manyanalysis/optimisation methods

• ISO/IEC 15291:1999 - Ada Semantic Interface Specification(ASIS) implemented by ASIS-for-GNAT

• traverse Abstract Syntax Tree (AST) and build CFG

• Framework

3

Page 7: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Control Flow Graph (CFG)

• directed graph

• nodes represent statements

• There is an edge from node u to node v if v can follow u insome execution sequence.

• unique start node called root or initial node

4

Page 8: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The Source Code

procedure Test isA: array (1 .. 3) of Natural;S: Natural;K: Natural;

beginA := (1,2,3);S := 0;K := A’First;

loopif K <= A’Last then

S := S + A(K);K := K + 1;

elseexit;

end if;end loop;

end Test;

5

Page 9: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The Control Flow Graph (CFG)

exit;

end if;

loop

end loop;

A := (1,2,3);

S := 0;

K := A’First;

if K <= A’Last

S := S + A(K);

K := K + 1;

6

Page 10: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The Control Flow Graph (CFG)

exit;

end if;

loop

end loop;

A := (1,2,3);

S := 0;

K := A’First;

if K <= A’Last

S := S + A(K);

K := K + 1;

6

Page 11: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The Control Flow Graph (CFG)

exit;

end if;

loop

end loop;

A := (1,2,3);

S := 0;

K := A’First;

if K <= A’Last

S := S + A(K);

K := K + 1;

6

Page 12: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The ASIS Abstract Syntax Tree (AST) - 1

7

Page 13: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The ASIS Abstract Syntax Tree (AST) - 1

7

Page 14: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The ASIS Abstract Syntax Tree (ASIS) - 2

8

Page 15: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The ASIS Abstract Syntax Tree (ASIS) - 3

9

Page 16: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: The ASIS Abstract Syntax Tree (AST) - 4

10

Page 17: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

The Software

• Ast2Cfg - the framework, designed as a library

• Cfg2Dot - uses Ast2Cfg, outputs the CFG structure in dot

format

• Ast2Dot - uses ASIS directly to output the AST in dot format

• available under GPLv2 (Ast2Dot) or GPLv3 (Ast2Cfg,Cfg2Dot)

11

Page 18: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

The Structure of the Transformation Process

AST

Structure

.adt

File

Tra

ver

sal

Pre Op

CFGPost Op

Child Has Finished

Tra

nsf

orm

ati

on

Post

12

Page 19: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow World: The Main Data Structure

• World Object

13

Page 20: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow World: The Main Data Structure

• World Object

• Flow Object

• Pkg Object

• CFG Object

• Node Object

13

Page 21: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow World: The Main Data Structure

• World Object

• Flow Object

• Pkg Object

• CFG Object

• Node Object

• Pkg/CFG Tree (implicit structure)

13

Page 22: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: Pkg/CFG Tree

procedure Outer CFG is

package Outer Pkg isend Outer Pkg;

package body Outer Pkg is

package Inner Pkg isend Inner Pkg;

procedure Inner CFG isbeginend Inner CFG;

end Outer Pkg;

beginend Outer CFG;

Default_PKG(body)

Outer_CFG(body)

Outer_Pkg(spec)

Outer_Pkg(body)

Inner_Pkg(spec)

Inner_CFG(body)

14

Page 23: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow World: The Main Data Structure (contd.)

• World Object

• Flow Object

• Pkg Object

• CFG Object

• Node Object

• Pkg/CFG Tree (implicit structure)

• Parameter Tree (saved in nodes)

15

Page 24: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Example: Parameter Tree

V: array(a..b)of Integer;

V(FuncA(X)) :=FuncA(FuncB(0,Y));

Assignment Node 1.3.9:

Idents: V,FuncA,FuncA,FuncBV(FuncA(X)) := FuncA(FuncB(0,Y));

Parameter Node 1.3.8:

ASSIGN/LHS

Parameter Node 1.3.13:

ASSIGN/RHS

Node 1.3.19:

CFG END

Parameter Node 1.3.10:Variable: VIDX/COMP

Parameter Call Node 1.3.11:Dest: FuncA

FUNC/PARAM/ROOT

Parameter Node 1.3.12:Variable: X

PARAM/NODE

Parameter Call Node 1.3.14:Dest: FuncA

FUNC/PARAM/ROOT

Parameter Call Node 1.3.16:Dest: FuncB

FUNC/PARAM/ROOT

Parameter Node 1.3.17:

PARAM/NODE

Parameter Node 1.3.18:Variable: Y

PARAM/NODE

16

Page 25: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow Types: An Overview

Flow Object

abstract

concrete

CFG Object

.

.

.

Node Object

.

.

.

Pkg Object

.

.

.

17

Page 26: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow Types: Package

Def Object

Simple Body Object

Prot Object

Simple Spec Object

Gen Object

Single Prot Object

Prot Type Object

Body Object

Pkg Object

Spec Object

abstract

concrete

18

Page 27: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow Types: CFG

Abort Object

Block Object

Entry Object

Except Object

Func Object

Init Object

Proc Object

Task Object

Generic Object

Task Type Object

Single Task Object

Body Object

CFG Object

Spec Object

abstract

concrete

19

Page 28: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Flow Types: Node

abstract

concrete

Return Node Object

Terminate Node Object

Param Node Object

Param Alloc Node Object

Assign Node Object

Finite Loop Node Object

Infinite Loop Node Object

Branch Node Object

Loop Node Object

Call Node Object

Accept Node Object

Entry Call Node Object

Abort Node Object

Param Call Node Object

Goto Jump Node Object

Jump Node Object

Trivial Exit Jump Node Object

Complex Exit Jump Node Object

Exit Jump Node Object

Node Object

20

Page 29: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Usage

with Ada.Text IO; use Ada.Text IO; with Ast2Cfg.Pkgs; use Ast2Cfg.Pkgs;with Ast2Cfg.Control; with Ast2Cfg.Flow World; with Ast2Cfg.Output;

procedure Run isWorld: Ast2Cfg.Flow World.World Object Ptr;Pkgs: Pkg Class Ptr List.Object;Pkg: Pkg Class Ptr := null;

begin-- InitialisationsAst2Cfg.Output.Set Level(Ast2Cfg.Output.Warning);Ast2Cfg.Control.Init("−CN foo.adt bar.adt");

-- Fill the World with flow dataWorld := Ast2Cfg.Control.Generate;

-- Output the name of all top-level packagesPkgs := Ast2Cfg.Flow World.Get Pkgs(World.all);Pkg Class Ptr List.Reset(Pkgs);while Pkg Class Ptr List.Has Next(Pkgs) loop

Pkg Class Ptr List.Get Next(Pkgs, Pkg);Put Line(Get Name(Pkg.all));

end loop;

-- FinalisationAst2Cfg.Control.Final;

end Run;

21

Page 30: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Excursus on the Command Line

22

Page 31: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: If Statement

if (X>10) thennull;

elsif (false) thennull;

end if;

Branch Node 1.2.5:

if/case

Node 1.2.7:

Idents: >if (X>10) then

Node 1.2.6:

end if/case

Node 1.2.14:

elsif (false) then

Parameter Node 1.2.8:

STMT/PAR

Node 1.2.12:

null;

Parameter Call Node 1.2.9:Dest: >

FUNC/PARAM/ROOT

Parameter Node 1.2.10:Variable: X

PARAM/NODE

Parameter Node 1.2.11:

PARAM/NODE

Node 1.2.18:

CFG END

Node 1.2.16:

null;

23

Page 32: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: Case Statement

<<Start>> case X iswhen A =>

null;when B =>

null;when others =>

goto Start;end case;

Branch Node 1.3.9:

Name: StartIdents: X

if/case

Node 1.3.11:

Idents: Awhen A =>

Node 1.3.15:

Idents: Bwhen B =>

Node 1.3.19:

when others =>

Node 1.3.13:

null;

Node 1.3.10:

end if/case

Node 1.3.24:

CFG END

Node 1.3.17:

null;

Goto Jump Node 1.3.23:Target: StartIdents: Startgoto Start;

24

Page 33: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: Loop Statement

Loop1: loopIncrement(X);exit Loop1

when Condition;end loop Loop1;

Infinite Loop Node 1.6.12:

Name: Loop1Loop1: loop

Call Node 1.6.15:Dest: Increment

Idents: IncrementIncrement(X);

Parameter Call Node 1.6.16:Dest: Increment

PROC/PARAM/ROOT

C-Exit Jump Node 1.6.20:Target: Loop1

Idents: Loop1,Conditionexit Loop1 when Condition;

Parameter Node 1.6.17:Variable: X

PARAM/NODE

Parameter Node 1.6.19:

STMT/PAR

Node 1.6.22:

Loop End

Parameter Call Node 1.6.21:Dest: Condition

FUNC/PARAM/ROOT

Node 1.6.23:

CFG END

25

Page 34: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: A few words on the word static

procedure Not A Loop isI: Integer := 0;

begin

loopexit;I := I + 1;

end loop;

end Not A Loop;

not a loop.adb:7:09: warning: unreachable code

Infinite Loop Node 1.1.5:

loop

T-Exit Jump Node 1.1.8:

exit;

Node 1.1.16:

Loop End

Node 1.1.17:

CFG END

26

Page 35: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: A few words on the word static (2)

procedure Not A Loop 2 isI: Integer := 10;

begin

loopexit when I >= 10;I := I + 1;

end loop;

end Not A Loop 2;

Infinite Loop Node 1.1.5:

loop

C-Exit Jump Node 1.1.8:

Idents: >=exit when I >= 10;

Assignment Node 1.1.14:

Idents: I,+I := I + 1;

Node 1.1.19:

Loop End

Node 1.1.20:

CFG END

27

Page 36: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: Tasks

−− Caller ObjectTask Object.Entry1;

−− Called Objectselectaccept Entry1 do

null;end Entry1;

orwhen Condition =>

accept Entry2 donull;

end Entry2;end select;

Entry Call Node1.6.7:Dest: Task_Object.Entry1

Idents: Task_Object.Entry1Task_Object.Entry1;

Node 1.6.9:

CFG END

Node 1.3.1:

select/acc

Node 1.3.4:

select

Node 1.4.4:

Idents: Conditionor

Accept Node1.3.8:Dest: CFG[1.4.0]accept Entry1 do

Node 1.3.3:

end select

Node 1.5.4:

CFG END

Accept Node1.4.8:Dest: CFG[1.5.0]accept Entry2 do

28

Page 37: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: Protected Objects

package body Prot Pkg is

protected bodyProt Type is

entry Entry1(A: in Integer)when True isbeginnull;

end Entry1;

end Prot Type;

Prot Obj: Prot Type;

beginProt Obj.Entry1(0);

end Prot Pkg;

Prot_Pkg(body)

--VARS--Prot_Obj: {ST: Prot_Type}

Prot_Type(body)

Init_CFG(body)

Entry1(body)

--PARAMS--A: {ST: Integer}

Entry Call Node2.2.3:Dest: Prot_Obj.Entry1

Idents: Prot_Obj.Entry1Prot_Obj.Entry1(0);

Parameter Call Node 2.2.4:Dest: Prot_Obj.Entry1PROC/PARAM/ROOT

Node 2.2.6:

CFG END

Parameter Node 2.2.5:

PARAM/NODE

29

Page 38: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Examples: Types

package Types is

type Rec Type 0 istaggedrecord

F: Float := abs(3.14);B: Boolean;

end record;

type Rec Type 1 isnew Rec Type 0with null record;

end Types;

Types(spec)

--TYPES--Rec_Type_0

[COMPONENTS]F: {CT: Float}

B: {CT: Boolean}Rec_Type_1 {ST: Rec_Type_0}

FINIT

Parameter Node 1.0.2:

INIT/NODE

Parameter Call Node 1.0.4:Dest: abs

FUNC/PARAM/ROOT

Parameter Node 1.0.5:

PARAM/NODE

30

Page 39: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Further Information & Downloads

• http://cfg.w3x.org

• For comments, bug reports and feature requests pleasecontact us:

[email protected]

31

Page 40: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Further Information & Downloads

• http://cfg.w3x.org

• For comments, bug reports and feature requests pleasecontact us:

[email protected]

• Raul Fechete and Georg Kienesberger. Generating control flow graphs for Ada

programs. Technical Report 183/1-139, Department of Automation, TU

Vienna, September 2007.

31

Page 41: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Further Information & Downloads

• http://cfg.w3x.org

• For comments, bug reports and feature requests pleasecontact us:

[email protected]

• Raul Fechete and Georg Kienesberger. Generating control flow graphs for Ada

programs. Technical Report 183/1-139, Department of Automation, TU

Vienna, September 2007.

• Raul Fechete, Georg Kienesberger, and Johann Blieberger. A Framework for

CFG-based Static Program Analysis of Ada Programs. In Ada-Europe’2008

International Conference on Reliable Software Technologies, pages 130-143,

Venice, Italy, June 2008.

31

Page 42: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

Further Information & Downloads

• http://cfg.w3x.org

• For comments, bug reports and feature requests pleasecontact us:

[email protected]

• Raul Fechete and Georg Kienesberger. Generating control flow graphs for Ada

programs. Technical Report 183/1-139, Department of Automation, TU

Vienna, September 2007.

• Raul Fechete, Georg Kienesberger, and Johann Blieberger. A Framework for

CFG-based Static Program Analysis of Ada Programs. In Ada-Europe’2008

International Conference on Reliable Software Technologies, pages 130-143,

Venice, Italy, June 2008.

• updated documentation in the next few months

31

Page 43: Ast2Cfg - A Framework for CFG-Based Analysis and Visualisation of Ada Programs

The End

Thank you very much!

Any questions?

http://cfg.w3x.org

These slides are licensed under a Creative Commons Attribution-Share Alike 3.0 Austria License. http://creativecommons.org

32