Download - Lecture 9: Interprocedural Analysis

Transcript

Lecture 9: InterproceduralAnalysis

17-355/17-655/17-819: Program AnalysisRohan Padhye and Jonathan Aldrich

March 4, 2021

* Course materials developed with Claire Le Goues

1(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Extend WHILE with functions

2(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Extend WHILE3ADDR with functions

3(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Extend WHILE3ADDR with functions

4(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Extend WHILE3ADDR with functions

5(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

HOW DO WE ANALYZE THESE PROGRAMS?Data-Flow Analysis

6(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Approach #1: Analyze functions independentlyβ€’ Pretend function f() cannot see the source of function g()β€’ Simulates separate compilation and dynamic linking (e.g. C, Java)β€’ Create CFG for each function body and run intraprocedural analysisβ€’ Q: What should be is 𝜎! and𝑓" π‘₯ ≔ 𝑔(𝑦) and 𝑓" return π‘₯ for zero

analysis?

7(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Can we show that division on line 2 is safe?

8(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Approach #2: User-defined Annotations

9(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

@NonZero -> @NonZero

Approach #2: User-defined Annotations

10(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

@NonZero -> @NonZero @NonZero -> @NonZero

Error!

Approach #2: User-defined Annotations

11(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

@NonZero -> @NonZero @Any -> @NonZero

Error!

Approach #3: Interprocedural CFG

12(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

3:return𝑦

1:fun π‘‘π‘œπ‘’π‘π‘™π‘’(π‘₯)

2:𝑦 ≔ 2 βˆ— π‘₯

4:fun π‘šπ‘Žπ‘–π‘›()

7:…

5:𝑧 ≔ 0

local

6:𝑀 ≔ π‘‘π‘œπ‘’π‘π‘™π‘’(𝑧)returnw

call

𝑓! return π‘₯ "#$ 𝜎 = 𝑧 β†’ 𝜎 𝑧 𝑧 ∈ πΊπ‘™π‘œπ‘π‘Žπ‘™π‘ } βˆͺ {π‘Ÿπ‘’π‘‘ β†’ 𝜎(π‘₯)}

𝑓! π‘₯ ≔ 𝑔 𝑦 %&'(% 𝜎 = 𝜎 \ ( π‘₯ βˆͺ πΊπ‘™π‘œπ‘π‘Žπ‘™π‘ )𝑓! π‘₯ ≔ 𝑔 𝑦 '(%% 𝜎 = {π‘“π‘œπ‘Ÿπ‘šπ‘Žπ‘™ 𝑔 β†’ 𝜎 𝑦 }

Approach #3: Interprocedural CFG

13(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Exercise: What would be the result of zero analysis for this program on line 7 and at the end?

Approach #3: Interprocedural CFG

14(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

3:return𝑦

1:fun π‘‘π‘œπ‘’π‘π‘™π‘’(π‘₯)

2:𝑦 ≔ 2 βˆ— π‘₯

4:fun π‘šπ‘Žπ‘–π‘›()

7:z:=10/w

5:𝑧 ≔ 5

local

6:𝑀 ≔ π‘‘π‘œπ‘’π‘π‘™π‘’(𝑧)returnw

call

8:𝑧 ≔ 0

9:𝑀 ≔ π‘‘π‘œπ‘’π‘π‘™π‘’(𝑧)call

10:…localreturnw

Problems with Interprocedural CFGβ€’ Merges ( joins) information across call sites to same functionβ€’ Loses precisionβ€’ Models infeasible paths (call from one site and return to another)β€’ Can we β€œremember” where to return data-flow values?

15(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

CONTEXT-SENSITIVE ANALYSISEnter:

16(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Context-Sensitive Analysis Example

17(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Key idea: Separate analyses for functions called in different ”contexts”.

(β€œcontext” = some statically definable condition)

Context-Sensitive Analysis Example

18(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Context πˆπ’Šπ’ πˆπ’π’–π’•

Line 6 {x->N} {x->N, y->N}

Line 9 {x->Z} {x->Z, y->Z}

Context-Sensitive Analysis Example

19(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Context πˆπ’Šπ’ πˆπ’π’–π’•

<main, T> T {w->Z, Z->Z}

<double, N> {x->N} {x->N, y->N}

<double, Z> {x->Z} {x->Z, y->Z}

20(c) 2021 J. Aldrich, C. Le Goues, R. Padhye

Context πˆπ’Šπ’ πˆπ’π’–π’•

<main, T> T {w->Z, Z->Z}

<double, N> {x->N} {x->N, y->N}

<double, Z> {x->Z} {x->Z, y->Z}

Works for non-recursive contexts!