Top Banner
A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco Logozzo, MSR
38

A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Dec 30, 2015

Download

Documents

Aleesha Poole
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: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content AnalysisPatrick Cousot, NYU & ENSRadhia Cousot, CNRS & ENS & MSRFrancesco Logozzo, MSR

Page 2: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

The problem: Array analysispublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11; j++; }

// here: ∀k.0≤k<j⇒a[k]=11}

if j = 0 then a[0] … not knownelse if j > 0 ∧ j ≤ a.Length a[0] = … a[j-1] = 11else impossible

Challenge 2: Handling of disjunctionChallenge 1:

All the elements are initialized

Page 3: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Haven’t we solved it yet?

Precision

Sca

lab

ility

Array smashi

ng

Array partition

sTemplate/annotatio

nbased Theorem

provers

Functor abstract domain

Array expansio

n

Automation

Automation

Page 4: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Functor abstract domain by example

Page 5: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Array Materialization{0} Top {a.Length}

?

public void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

Segment limits

Segment abstraction

Possibly empty segment

Page 6: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

‘?’ Removal

{0} Top {a.Length}

public void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

Remove doubt

Page 7: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Constant Assignmentpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0,j} Top {a.Length} j:[0,0]

Scalar variables abstraction(omit a.Length ∈ [1, +∞))

Record j = 0

Page 8: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Testpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0,j} Top {a.Length} j:[0,0]

Page 9: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Array assignmentpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0,j} [11, 11] {1, j+1} Top {a.Length}? j:[0,0]

Materialize segment Introduce ‘?’

Page 10: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Scalar Assignmentpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0,j-1} [11, 11] {1,j} Top {a.Length}? j:[1,1]

Replace j by j-1

Page 11: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Joinpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0,j-1} [11, 11] {1, j} Top {a.Length}? j:[1,1]

{0,j} Top {a.Length} j:[0,0]

Page 12: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Segment unification

1. Unify the segments

2. Point-wise join

Similar for order, meet and widening

{0,j} Top {a.Length} {0,j-1} [11, 11]

{1,j} Top {a.Length}?

{0} ⊥ {j}? Top {a.Length} {0} [11, 11]

{j} Top {a.Length}?

{0} [11, 11]

{j}? Top {a.Length}?

Page 13: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

After the first iterationpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0} [11, 11] {j}? Top {a.Length}? j ∈ [0,1]

Page 14: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Testpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++

}}

{0} [11, 11] {j}? Top {a.Length} j ∈ [0,1]

Remove ‘?'

Page 15: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Array assignmentpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0}

[11,11] {j}? [11,11]

{j+1}? Top {a.Length}? j ∈ [0,1]

Page 16: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Scalar assignementpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0}

[11,11] {j-1}?

[11,11]

{j}? Top {a.Length}? j ∈ [1,2]

Page 17: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Wideningpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0}

[11,11] {j-1}?

[11,11]

{j}? Top {a.Length}?

j ∈ [1,2]

{0} [11, 11] {j}? Top {a.Length}? j ∈ [0,1]

Page 18: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Fixpointpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

}}

{0} [11, 11] {j}? Top {a.Length}? j ∈ [0,+∞)

Page 19: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Reductionpublic void Init(int[] a){ Contract.Requires(a.Length > 0);

var j = 0;

while (j < a.Length) { a[j] = 11;

j++;

} // here j ≥ a.Length }

{0} [11, 11] {j}? Top {a.Length}? j ∈ [0,+∞)

{0} [11, 11] {j, a.Length}

j ∈ [1,+∞)

Remove the empty segment

Page 20: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Abstract Semantics

Page 21: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

The Functor FunArray

Given an abstract domainB for boundsS for segmentsE for scalar variables environment

Constructs an abstract domain F(B, S, E) to analyze programs with arrays(Main) Advantages

Fine tuning of the precision/cost ratioEasy lifting of existing analyses

Page 22: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Segment bounds

Sets of symbolic expressionsIn our examples: Exp := k | x | x + k

Meaning:{ e0 … en } { e’1 … e’m} ≝ e0 =… = en < e’1 = … =e’m

{ e0 … en } { e’1 … e’m}? ≝ e0 =… = en ≤ e’1 = … =e’m

Possibly empty segments are key for scalability

Page 23: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Disjunction: Partitions & co.

public void CopyNonNull(object[] a, object[] b){ Contract.Requires(a.Length <= b.Length);

var j = 0; for (var i = 0; i < a.Length; i++) { if (a[i] != null) { b[j] = a[i]; j++; } }}}

Four partitions:j = 0 ∨ 0 ≤ j< b.Length-1 ∨j = b.Length-1 ∨j = b.Length

Page 24: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Disjunction: Our approach

public void CopyNonNull(object[] a, object[] b){ Contract.Requires(a.Length <= b.Length);

var j = 0; for (var i = 0; i < a.Length; i++) { if (a[i] != null) { b[j] = a[i]; j++; } }}}

{0} NotNull {j}? Top {b.Length}? j ∈ [0,+∞)

Segmentation discovered by the analysis

Page 25: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Segment Abstraction

Uniform abstraction for pairs (i, a[i])More general than usual McCarthy definition

Wide choice of abstract domainsFine tuning the cost/precision ratio

Ex: Cardinal power of constants by parity [CC79] public void EvenOdd(int n)

{ var a = new int[n]; var i = 0; while (i < n) { a[i++] = 1; a[i++] = -1; }}

{0}even → 1odd → -1

{i, n, a.Length}? i ∈ [0,+∞)

Page 26: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Segmentation Unification

Given two segmentations, find a common segmentationCrucial for order/join/meet/widening:1. Unify the segments2. Apply the operation point-wiseIn the concrete, a lattice of solutionsIn the abstract, a partial order of solutionsOur algorithm tuned up by examples

Details in the paper

Page 27: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Read: x = a[exp]

Search the bounds for exp

The search queries the scalar environment σ

More precisionA form of abstract domains reduction

Set σ’= σ [x ↦ An ⊔ … ⊔ Am-1]

… … Bn An … Am-1 Bm … … σ

Page 28: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Write: a[exp] = x

Search the bounds for exp

Join the segments

Split the segment

Adjust emptinessMay query scalar variables environment

… … Bn An … Am-1 Bm … …

… … Bn An ⊔ .. ⊔ Am-1 Bm … …

… … Bn An ⊔ .. ⊔ Am-1 exp σ(x) exp+1 An ⊔ .. ⊔ Am-1

Bm … …

Page 29: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Scalar assignment

Invertible assignment x = g(x) Replace x by g-1(x) in all the segments

Non-Invertible assignment x = g()Remove x in all the segmentsRemove all the empty segmentsAdd x to all the bounds containing g()

Page 30: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Assumptions (and tests)

Assume x == ySearch for segments containing x/yAdd y/x to them

Assume x < y Adjust emptiness

Assume x ≤ yDoes the state implies x ≥ y ?If yes, Assume x == y

Assumptions involving arrays similar

Page 31: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Implementation

Fully implemented in CCCheckStatic checker for CodeContractsUsers: Professional programmers

Array analysis completely transparent to users

No parameters to tweak, templates, partitions …

Instantiated withExpressions = Simple expressions (this talk)Segments = Intervals + NotNull + Weak boundsEnvironment = CCCheck default

Page 32: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Results

Main .NET v2.0 Framework librariesUn-annotated code

Analyzes itself at each build (0 warnings)

5297 lines of annotated C#

Assembly # funcs

base

With functo

r

Δ # array invariant

s

Mscorlib 21 475

4:06 4:15 0:09

2 430

System 15 489

3:40 3:46 0:06

1 385

System.Data 12 408

4:49 4:55 0:06

1 325

System.Drawings

3 123 0:28 0:29 0:01

289

System.Web 23 647

4:56 5:02 0:06

840

System.Xml 10 510

3:59 4:16 0:17

807

Page 33: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

More?

Inference of quantified preconditionsSee our VMCAI’11 Paper

Handling of multi-dimensional matrixes

With auto-applicationInference of existential ∀∃ facts

When segments interpreted existentiallyArray purity check

The callee does not modify a sub-array…

Page 34: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

To Sum up…

Fully Automatic Once the functor is instantiatedNo hidden hypotheses

Compact representation for disjunction

Enables ScalabilityPrecision/Cost ratio tunable

Refine the functor parametersRefine the scalar abstract environment

Used everyday in an industrial analyzer

1% Overhead on average

Page 35: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Backup slides

Page 36: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Is this as Array Partitions?

No[GRS05] and [HP07]

They require a pre-determined array partition

Main weakness of their approach

Our segmentation is inferred by the analysis

Totally automatic

They explicitly handle disjunctionsWe have possibly empty segments

Page 37: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Calls

Orthogonal issueIn the implementation in CCCheck

Havoc arrays passed as parametersAssignment of unknown if by ref of one elementAssume the postcondition

Array element passed by refEx: f(ref a[x])The same as assignment a[x] = TopAssume the postcondition

Page 38: A Parametric Segmentation Functor for Fully Automatic and Scalable Array Content Analysis Patrick Cousot, NYU & ENS Radhia Cousot, CNRS & ENS & MSR Francesco.

Multiple arrays as parameters

Orthogonal issueDepends on the underlying heap analysisIn CCCheck:

Optimistic hypotheses on non-aliasingFunArray easily fits in other heap models