Top Banner
Lecture 9: Interprocedural Analysis 17-355/17-655/17-819: Program Analysis Rohan Padhye and Jonathan Aldrich March 4, 2021 * Course materials developed with Claire Le Goues 1 (c) 2021 J. Aldrich, C. Le Goues, R. Padhye
20

Lecture 9: Interprocedural Analysis

Feb 06, 2022

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Lecture 9: Interprocedural Analysis

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

Page 2: Lecture 9: Interprocedural Analysis

Extend WHILE with functions

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

Page 3: Lecture 9: Interprocedural Analysis

Extend WHILE3ADDR with functions

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

Page 4: Lecture 9: Interprocedural Analysis

Extend WHILE3ADDR with functions

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

Page 5: Lecture 9: Interprocedural Analysis

Extend WHILE3ADDR with functions

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

Page 6: Lecture 9: Interprocedural Analysis

HOW DO WE ANALYZE THESE PROGRAMS?Data-Flow Analysis

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

Page 7: Lecture 9: Interprocedural Analysis

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

Page 8: Lecture 9: Interprocedural Analysis

Can we show that division on line 2 is safe?

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

Page 9: Lecture 9: Interprocedural Analysis

Approach #2: User-defined Annotations

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

@NonZero -> @NonZero

Page 10: Lecture 9: Interprocedural Analysis

Approach #2: User-defined Annotations

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

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

Error!

Page 11: Lecture 9: Interprocedural Analysis

Approach #2: User-defined Annotations

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

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

Error!

Page 12: Lecture 9: Interprocedural Analysis

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 ๐‘ฅ "#$ ๐œŽ = ๐‘ง โ†’ ๐œŽ ๐‘ง ๐‘ง โˆˆ ๐บ๐‘™๐‘œ๐‘๐‘Ž๐‘™๐‘ } โˆช {๐‘Ÿ๐‘’๐‘ก โ†’ ๐œŽ(๐‘ฅ)}

๐‘“! ๐‘ฅ โ‰” ๐‘” ๐‘ฆ %&'(% ๐œŽ = ๐œŽ \ ( ๐‘ฅ โˆช ๐บ๐‘™๐‘œ๐‘๐‘Ž๐‘™๐‘ )๐‘“! ๐‘ฅ โ‰” ๐‘” ๐‘ฆ '(%% ๐œŽ = {๐‘“๐‘œ๐‘Ÿ๐‘š๐‘Ž๐‘™ ๐‘” โ†’ ๐œŽ ๐‘ฆ }

Page 13: Lecture 9: Interprocedural Analysis

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?

Page 14: Lecture 9: Interprocedural Analysis

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

Page 15: Lecture 9: Interprocedural Analysis

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

Page 16: Lecture 9: Interprocedural Analysis

CONTEXT-SENSITIVE ANALYSISEnter:

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

Page 17: Lecture 9: Interprocedural Analysis

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)

Page 18: Lecture 9: Interprocedural Analysis

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}

Page 19: Lecture 9: Interprocedural Analysis

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}

Page 20: Lecture 9: Interprocedural Analysis

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!