CS711 Advanced Programming Languages Shape Analysis With Tracked - - PowerPoint PPT Presentation

cs711 advanced programming languages shape analysis with
SMART_READER_LITE
LIVE PREVIEW

CS711 Advanced Programming Languages Shape Analysis With Tracked - - PowerPoint PPT Presentation

CS711 Advanced Programming Languages Shape Analysis With Tracked Locations Radu Rugina 22 Sep 2005 Shape Analysis with Local Reasoning All previous abstractions: Describe the entire heap at once Makes inter-procedural analysis


slide-1
SLIDE 1

CS711 Advanced Programming Languages Shape Analysis With Tracked Locations

Radu Rugina 22 Sep 2005

slide-2
SLIDE 2

Shape Analysis with Local Reasoning

  • All previous abstractions:

– Describe the entire heap at once – Makes inter-procedural analysis difficult

  • This approach:

– Idea 1: build shape analysis on top of an underlying pointer analysis – Idea 2: Reason locally about one heap cell at a time.

slide-3
SLIDE 3

New Memory Abstraction

  • Decompose memory abstraction

Heap Abstraction

slide-4
SLIDE 4

New Memory Abstraction

  • Decompose memory abstraction

– run pointer analysis, then shape analysis Shape Abstraction Region Abstraction Shape analysis Pointer analysis

slide-5
SLIDE 5

New Memory Abstraction

  • Decompose memory abstraction

– Build shape abstraction using independent pieces Region Abstraction Shape analysis Pointer analysis

slide-6
SLIDE 6

New Memory Abstraction

  • Decompose memory abstraction

– Build shape abstraction using independent pieces Region Abstraction Shape analysis Pointer analysis

slide-7
SLIDE 7

Configurations

Region Abstraction Shape analysis Pointer analysis Configuration: Configuration:

  • Talk about one location:

the “tracked location”

  • No knowledge about
  • ther locations
slide-8
SLIDE 8

Configurations

Region Abstraction Shape analysis Pointer analysis Configuration: Configuration:

  • Reference counts from

each region

  • Hit expressions
  • Miss expressions
slide-9
SLIDE 9

Concrete Memory: Region Abstraction Shape Abstraction

Example Abstraction

x y X L Y

slide-10
SLIDE 10

Concrete Memory: Region Abstraction Shape Abstraction

Example Abstraction

x (X1, {x}, ø) (L1Y1, {x->n,y}, ø) (L1, ø, {x->n}) X L y Y

slide-11
SLIDE 11

Concrete Memory: Region Abstraction Shape Abstraction

Cyclic Structures

x (X1, {x}, ø) (L1Y1, {x->n,y}, ø) (L1, ø, {x->n}) X L y Y (L2, ø, {x->n})

slide-12
SLIDE 12

Analysis Example: List Reversal

List *reverse(List *x) { List *t, *y; y = NULL; while (x != NULL) { t = x->n; x->n = y; y = x; x = t; } return y; }

Given acyclic list x: is returned list y acyclic?

slide-13
SLIDE 13

List Reversal

  • Region abstraction:
  • Acyclic list x, two configurations:

– (X1,{x},ø) describes list head – (L1, ø, ø) describes tail

Y L T X

slide-14
SLIDE 14

Loop Body Analysis

t = x->n; x->n = y; y = x; x = t; X1,{x},ø X1,{x},ø X1,{x},ø X1Y1,{x,y},ø Y1,{y},ø

slide-15
SLIDE 15

Loop Body Analysis

t = x->n; x->n = y; y = x; x = t;

L1

ø,{x->n}

L1T1

{t,x->n},ø

T1

{t},ø

L1

ø,{x->n}

T1

{t},ø

T1X1

{t,x},ø

L1

ø,{x->n}

L1

ø,ø

L1,ø,ø

slide-16
SLIDE 16

Analysis Result

List *reverse(List *x) { List *t, *y; y = NULL; while (x != NULL) { t = x->next; x->next = y; y = x; x = t; } return y; }

X1 L1 X1 L1T1 L1 X1 T1 L1 X1Y1 T1 Y1 T1X1 T1X1 L1 L1 X1 L1 Y1 L1 Y1 Y1 Y1L1

slide-17
SLIDE 17

Analysis Result

List *reverse(List *x) { List *t, *y; y = NULL; while (x != NULL) { t = x->next; x->next = y; y = x; x = t; } return y; }

X1 L1 X1 L1T1 L1 X1 T1 L1 X1Y1 T1 Y1 T1X1 T1X1 L1 L1 X1 L1 Y1 L1 Y1 Y1 Y1L1

slide-18
SLIDE 18

Property Verified

List *reverse(List *x) { List *t, *y; y = NULL; while (x != NULL) { t = x->next; x->next = y; y = x; x = t; } return y; }

Acyclic input Acyclic output X1 L1 X1 L1T1 L1 X1 T1 L1 X1Y1 T1 Y1 T1X1 T1X1 L1 L1 X1 L1 Y1 L1 Y1 Y1 Y1L1

slide-19
SLIDE 19

Cyclic Input

reverse

x y

slide-20
SLIDE 20

Cyclic Input

reverse

x y

slide-21
SLIDE 21

Cyclic Input

X1 L1 Y1 L1 L2 L2 Analysis:

reverse

x y

slide-22
SLIDE 22

Analysis Algorithm

  • Phase 1: Pointer Analysis

– Flow-insensitive, unification-based – Context-sensitive

  • Phase 2: Shape Analysis

– Intra and inter-procedural – Flow-sensitive, context-sensitive – Granularity of configurations

slide-23
SLIDE 23

Inter-Procedural Shape Analysis

  • Context-sensitive analysis
  • Summary input

= a configuration

  • Summary output

= set of configurations that correspond to the input

  • Tag configurations with the input they originated from

– Output = retrieve configurations with the desired tag

foo() input

  • utput
slide-24
SLIDE 24

Inter-Procedural Shape Analysis

  • Efficient: reuse previous analyses of functions

– Match individual configurations!

  • Not entire heap abstractions

– Works even if there is only partial redundancy

Reuse!

Abstraction at a call site Abstraction at a different site

slide-25
SLIDE 25

Detecting Memory Errors

  • For languages with explicit de-allocation

– free(e) de-allocates cell referenced by e

  • Extend configurations with one bit:

has the tracked cell been de-allocated?

– malloc() sets bit to false – free() sets bit to true – Keep tracking cells even after de-allocation

Reference counts Hit expressions Miss expressions Freed flag

slide-26
SLIDE 26

Detecting Memory Errors

  • Dereference *e may be unsafe if:

– Expression e may reference the tracked locations – And tracked location is marked as de-allocated – Catches double frees: free(e) checked as *e

  • A potential memory leak occurs if:

– The tracked location has all reference counts zero – And not marked as de-allocated – Allocated in the current function

slide-27
SLIDE 27

Implementation

  • Implementation for C programs in SUIF
  • Singly linked lists

– Handles standard list manipulations:

insert, append, swap, reverse, quicksort, insertionsort.

  • Doubly linked lists

– Does not identify structural invariants

slide-28
SLIDE 28

Implementation

  • Tested tool on three larger programs:

44 sec 22 sec 45 sec Total Time 6 sec 13 sec 16 sec Points-to 38 sec 9 sec 29 sec Shape 24 4 10 Bugs 58 13 26 Reported 24.4 KLOC 25.6 KLOC 18.6 KLOC Lines binutils SSL SSH

slide-29
SLIDE 29

Comparison

< 30, 222 YES YES Jeannet, Loginov, Reps, Sagiv /2004 1.3K, 12881 YES YES Yahav, Ramalingam /2004 < 30, 295 no YES Lev-Ami, Reps, Sagiv, Wilhelm/2000 < 30, 2 no YES Dor, Rodeh, Sagiv/2000 < 30, 1028 YES YES Rinetzky, Sagiv /2001 no Sagiv, Reps,Wilhelm /1996 no Sagiv, Reps,Wilhelm /1999 25 K, 45 YES YES Hackett/Rugina /2005 3.3 K, n/a YES YES Ghiya, Hendren /1996 no Chase, Wegman,Zadeck / 1990 no Jones, Muchnick / 1979

size(LOC), time(sec) Inter-Procedural? Implemented? Analysis/Year

slide-30
SLIDE 30

Summary

  • Shape analysis:

– Needed for precise analysis of heap structures – Necessarily flow-sensitive – Not scalable until recently