64
Object Usage Models Andrzej Wasylkowski Saarland University

Object Usage Models

  • Upload
    others

  • View
    7

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Object Usage Models

Object Usage Models

Andrzej WasylkowskiSaarland University

Page 2: Object Usage Models

A bug in AspectJ

2

Page 3: Object Usage Models

A bug in AspectJ

3

/** * This method looks through the type hierarchy for some target type - it is attempting to * find an existing parameterization that clashes with the new parent that the user * wants to apply to the type. If it finds an existing parameterization that matches the * new one, it silently completes, if it finds one that clashes (e.g. a type already has * A<String> when the user wants to add A<Number>) then it will produce an error. * * It uses recursion and exits recursion on hitting 'jlObject' * * Related bugzilla entries: pr110788 */ private boolean verifyNoInheritedAlternateParameterization(ResolvedType typeToVerify,ResolvedType newParent,World world) {

if (typeToVerify.equals(ResolvedType.OBJECT)) return true;

ResolvedType newParentGenericType = newParent.getGenericType(); Iterator iter = typeToVerify.getDirectSupertypes(); while (iter.hasNext()) { ResolvedType supertype = (ResolvedType)iter.next(); if ( ((supertype.isRawType() && newParent.isParameterizedType()) || (supertype.isParameterizedType() && newParent.isRawType())) && newParentGenericType.equals(supertype.getGenericType())) { // new parent is a parameterized type, but this is a raw type world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } if (supertype.isParameterizedType()) { ResolvedType generictype = supertype.getGenericType(); // If the generic types are compatible but the parameterizations aren't then we have a problem if (generictype.isAssignableFrom(newParentGenericType) && !supertype.isAssignableFrom(newParent)) { world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } } return verifyNoInheritedAlternateParameterization(supertype,newParent,world); } return true; }

Page 4: Object Usage Models

A bug in AspectJ

4

private boolean verify… (…) { … Iterator iter = …; while (iter.hasNext()) { … = iter.next(); … return verify… (…); } return true; }

How can we find such a bug automatically?

Page 5: Object Usage Models

The approach

Program Smart tool

Modeling objects’ behavior using finite state automata

a b

c d

!!!

a a b

c d e f

!!!

a

5

Page 6: Object Usage Models

How to create models?

• Use method calls as transitions

• How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

Page 7: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

7

Page 8: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

Execution

7

Page 9: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

GrammarinferenceExecution

q = new

q.offer q.poll

7

Page 10: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

Grammarinference

q = new

q.offer q.poll

8

Page 11: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

Grammar inference

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

Grammarinference

q = new

q.offer q.poll

q = new

q.offer

q.poll

q.offer q.poll

8

Page 12: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

Grammar inference

Which model represents actual behavior?

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

Grammarinference

q = new

q.offer q.poll

q = new

q.offer

q.poll

q.offer q.poll

8

Page 13: Object Usage Models

I. Anonymous states via grammar inference

How to define states?

Grammar inference

Which model represents actual behavior?

q = newq.offerq.pollq.offerq.pollq.offer…q.pollq.offer

Grammarinference

q = new

q.offer q.poll

q = new

q.offer

q.poll

q.offer q.poll

✓Can uncover hidden structure

! Difficult to assign meaning

! NP-hard

! Does not handle ambiguity well

9

Page 14: Object Usage Models

How to define states?II. Based on object’s state

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

10

Page 15: Object Usage Models

How to define states?II. Based on object’s state

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

Execution size() == 0

q = new

size() == 1

q.offer q.poll

10

Page 16: Object Usage Models

size() == 0

q = new

size() == 1

q.offer q.poll

II. Based on object’s state

How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

Execution✓Expresses the true meaning of the state

! Needs abstraction to keep models small

11

Page 17: Object Usage Models

III.Based on the way the object is used

How to define states?

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

12

Page 18: Object Usage Models

III.Based on the way the object is used

How to define states?

Static analysis

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

12

q = new Queue ();

q.offer (...);

...

...

e = q.poll (); ...

q.offer (...);

Page 19: Object Usage Models

III.Based on the way the object is used

How to define states?

Static analysis

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

12

q = new

q.offer

!!!

!!!

q.poll !!!

q.offer

Page 20: Object Usage Models

III.Based on the way the object is used

How to define states?

Static analysis

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

12

q = new

q.offer

q.poll q.offer

Page 21: Object Usage Models

III.Based on the way the object is used

q = new

q.offer

q.poll q.offer

How to define states?

Static analysis

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…q.offer (…);

}

✓User-centered view of the object

! May be too specific

13

Page 22: Object Usage Models

I. Anonymous states via grammar inference

II. Based on object’s state

III.Based on the way the object is used

How to define states?

14

Page 23: Object Usage Models

I. Anonymous states via grammar inference

II. Based on object’s state

III.Based on the way the object is used

How to define states?

14

Page 24: Object Usage Models

External method calls

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…refresh (q);

}

15

Page 25: Object Usage Models

q = new

q.offer

q.poll refresh (q)

External method calls

Static analysis

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…refresh (q);

}

15

Page 26: Object Usage Models

Inlining models

q = new Queue ();q.offer (…);while (…) {…e = q.poll ();…refresh (q);

}

void refresh (Queue q) {if (!q.isEmpty ()) {e = q.poll ();…q.offer (e);

}}

16

Page 27: Object Usage Models

Inlining models

void refresh (Queue q) {if (!q.isEmpty ()) {e = q.poll ();…q.offer (e);

}}

q = new

q.offer

q.poll refresh (q)

16

Page 28: Object Usage Models

Inlining models

q.isEmpty

q.poll

! !!!

q.offer

q = new

q.offer

q.poll refresh (q)

16

Page 29: Object Usage Models

Inlining models

Inlining

q = new

q.offer

q.poll

q.isEmpty

! !!!

q.poll

q.offer

q.isEmpty

q.poll

! !!!

q.offer

q = new

q.offer

q.poll refresh (q)

17

Page 30: Object Usage Models

Subject: AspectJ

jar file Size (kB) Classes Methods

aspectjlib.jar 8 3 21aspectjrt.jar 110 68 496aspectjweaver.jar 1800 956 9999aspectjtools.jar 8100 2980 36372

18

Page 31: Object Usage Models

Time & Models

jar file Classes Models Time

aspectjlib.jar 3 116 0:01aspectjrt.jar 68 2865 0:04aspectjweaver.jar 956 74236 1:23aspectjtools.jar 2980 243804 7:33

19

Page 32: Object Usage Models

Stack sin ThreadStackImpl11.getThreadStack()

s = new s.push

s.elements

20

Page 33: Object Usage Models

StringTokenizer stin AdviceSignatureImpl.toAdviceName()

st = new

st.hasMoreTokens

st.nextToken

! !!!

! !!!

! !!!

21

Page 34: Object Usage Models

StringTokenizer stin Factory.makeConstructorSig()

st = new

st.countTokens

st.nextToken

22

Page 35: Object Usage Models

Method min AjTypeImpl.asPointcut()

m.getAnnotation m.getName

! !!!

m.getDeclaringClass

new PointcutImpl (..., ..., m, ..., ...)

! !!!

23

Page 36: Object Usage Models

Class cin SignatureImpl.shortTypeName()

c.isArray

! !!!c.getName c.getComponentType

! !!! ! !!!

24

Page 37: Object Usage Models

Class cin AjTypeSystem.getAjType()

Map.containsKey (c)

new AjTypeImpl (c) Map.get (c)

Map.put (c, ...) ! !!!

new AjTypeImpl (c)

Map.put (c, ...)

25

Page 38: Object Usage Models

Infrequent models (1)

26

3

4

Occurrences of models: SystemColor

Model 1 Model 2

Page 39: Object Usage Models

Infrequent models (2)

27

11

174

1

Occurrences of models: RuntimeException

Model 1 Model 2 Model 3 Model 4

Page 40: Object Usage Models

Infrequent models (3)

The most frequently occurring RuntimeException model

re = new

RuntimeException thrown

28

Page 41: Object Usage Models

re = new

Trace.exit (..., re)

RuntimeException thrown

Infrequent models (3)

Anomalous RuntimeException models

RuntimeException thrown

re = new

List.add (re)

29

Page 42: Object Usage Models

Infrequent models statistics

Total AnomaliesAnomaliesjar file Classes Classes Models Timeaspectjlib.jar 3 0 0 0:02aspectjrt.jar 68 0 0 0:05aspectjweaver.jar 956 8 75 1:16aspectjtools.jar 2980 20 249 9:06

30

Page 43: Object Usage Models

Subsequences

q = new

q.offer

q.poll q.offer

31

Page 44: Object Usage Models

Subsequences

q = new

q.offer

q.poll q.offer

Subsequences extraction

(new, offer)(new, poll)(offer, offer)(offer, poll)(poll, offer)(poll, poll)

31

Page 45: Object Usage Models

Frequent subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

32

Page 46: Object Usage Models

Frequent subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

Subsequence 48forms an itemset with support 4

33

Page 47: Object Usage Models

Frequent subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

34

Page 48: Object Usage Models

Frequent subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

Subsequences 45 and 48form an itemset with support 3

35

Page 49: Object Usage Models

Missing subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

36

Page 50: Object Usage Models

Missing subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

Missing subsequences point to anomalies

36

Page 51: Object Usage Models

Missing subsequencesMethod Ids of subsequences

Dump.println 28, 45, 46, 47, 48, 4386ClassPathManager.<init> 45, 46, 47, 48DeclareParents.verify… 4, 48, 10537BcelObjectType.get… 45, 48, 12028

Missing subsequences point to anomalies

36

(hasNext, hasNext)Subsequence 45:Subsequence 48: (hasNext, next)

Page 52: Object Usage Models

The anomalous method

37

/** * This method looks through the type hierarchy for some target type - it is attempting to * find an existing parameterization that clashes with the new parent that the user * wants to apply to the type. If it finds an existing parameterization that matches the * new one, it silently completes, if it finds one that clashes (e.g. a type already has * A<String> when the user wants to add A<Number>) then it will produce an error. * * It uses recursion and exits recursion on hitting 'jlObject' * * Related bugzilla entries: pr110788 */ private boolean verifyNoInheritedAlternateParameterization(ResolvedType typeToVerify,ResolvedType newParent,World world) {

if (typeToVerify.equals(ResolvedType.OBJECT)) return true;

ResolvedType newParentGenericType = newParent.getGenericType(); Iterator iter = typeToVerify.getDirectSupertypes(); while (iter.hasNext()) { ResolvedType supertype = (ResolvedType)iter.next(); if ( ((supertype.isRawType() && newParent.isParameterizedType()) || (supertype.isParameterizedType() && newParent.isRawType())) && newParentGenericType.equals(supertype.getGenericType())) { // new parent is a parameterized type, but this is a raw type world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } if (supertype.isParameterizedType()) { ResolvedType generictype = supertype.getGenericType(); // If the generic types are compatible but the parameterizations aren't then we have a problem if (generictype.isAssignableFrom(newParentGenericType) && !supertype.isAssignableFrom(newParent)) { world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } } return verifyNoInheritedAlternateParameterization(supertype,newParent,world); } return true; }

Page 53: Object Usage Models

The anomalous method

37

/** * This method looks through the type hierarchy for some target type - it is attempting to * find an existing parameterization that clashes with the new parent that the user * wants to apply to the type. If it finds an existing parameterization that matches the * new one, it silently completes, if it finds one that clashes (e.g. a type already has * A<String> when the user wants to add A<Number>) then it will produce an error. * * It uses recursion and exits recursion on hitting 'jlObject' * * Related bugzilla entries: pr110788 */ private boolean verifyNoInheritedAlternateParameterization(ResolvedType typeToVerify,ResolvedType newParent,World world) {

if (typeToVerify.equals(ResolvedType.OBJECT)) return true;

ResolvedType newParentGenericType = newParent.getGenericType(); Iterator iter = typeToVerify.getDirectSupertypes(); while (iter.hasNext()) { ResolvedType supertype = (ResolvedType)iter.next(); if ( ((supertype.isRawType() && newParent.isParameterizedType()) || (supertype.isParameterizedType() && newParent.isRawType())) && newParentGenericType.equals(supertype.getGenericType())) { // new parent is a parameterized type, but this is a raw type world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } if (supertype.isParameterizedType()) { ResolvedType generictype = supertype.getGenericType(); // If the generic types are compatible but the parameterizations aren't then we have a problem if (generictype.isAssignableFrom(newParentGenericType) && !supertype.isAssignableFrom(newParent)) { world.getMessageHandler().handleMessage(new Message( WeaverMessages.format(WeaverMessages.CANT_DECP_MULTIPLE_PARAMETERIZATIONS,newParent.getName(),typeToVerify.getName(),supertype.getName()), getSourceLocation(), true, new ISourceLocation[]{typeToVerify.getSourceLocation()})); return false; } } return verifyNoInheritedAlternateParameterization(supertype,newParent,world); } return true; } This method is in fact buggy

Page 54: Object Usage Models

Another anomaly

38

…List values = …;for (Iterator it = values.iterator(); it.hasNext();) {

…return …;

}…

The list holds in fact at most one element

Page 55: Object Usage Models

Frequent subsequences statistics

jar file Classes Rules Flaws Time

aspectjlib.jar 3 0 0 0:03aspectjrt.jar 68 2 0 0:04aspectjweaver.jar 956 38 26 1:06aspectjtools.jar 2980 161 71 6:50

39

Page 56: Object Usage Models

Frequent subsequences statistics

jar file Classes Rules Flaws Time

aspectjlib.jar 3 0 0 0:03aspectjrt.jar 68 2 0 0:04aspectjweaver.jar 956 38 26 1:06aspectjtools.jar 2980 161 71 6:50

✓Fast enough to be practically useful

✓Found previously unknown bug in AspectJ

✓Found violations of convention in AspectJ

! Many false positives

40

Page 57: Object Usage Models

Suggesting method calls

In Eclipse

44

Page 58: Object Usage Models

Suggesting method calls

Using models

st = new

st.hasMoreTokens

st.nextToken

! !!!

! !!!

! !!!

48

Page 59: Object Usage Models

Other ideas (1)

• Use conditions in code to enhance models

st = new

st.hasMoreTokens

!st.hasMoreTokens st.nextToken

! !!!

! !!!

st = new

st.hasMoreTokens

st.nextToken

! !!!

! !!!

! !!!

49

Page 60: Object Usage Models

Other ideas (2)

• Suggest changes based on model’s evolution

1.0 1.1 1.2 1.3

Evolution of a model of class A originating from method B

50

Page 61: Object Usage Models

• Suggest changes based on model’s evolution

Other ideas (2)

1.0 1.1

52

Page 62: Object Usage Models

• Suggest changes based on model’s evolution

Other ideas (2)

2.0

1.0 1.1

52

Page 63: Object Usage Models

• Suggest changes based on model’s evolution

Other ideas (2)

2.0

Suggest the change

1.0 1.1

52

Page 64: Object Usage Models

Conclusion

53