-
Notifications
You must be signed in to change notification settings - Fork 34
Fix assignability analysis for prematurely read fields #190
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
roterEmil
wants to merge
42
commits into
develop
Choose a base branch
from
fix/assignability-prematurelyRead
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
d80463c
fixed prematurely read issue
roterEmil 52754a0
Merge branch 'develop' into fix/assignability-prematurelyRead
84281b7
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil 624d671
deleted sandbox files
roterEmil 8244423
Merge branch 'fix/assignability-prematurelyRead' of https://github.co…
roterEmil fef5c6d
removed debug code
roterEmil 0d76243
Update PrematurelyReadOfFinalField.java due to Dominiks review
roterEmil 253c4fc
Update PrematurelyReadOfFinalField.java inserted line break
roterEmil 2f1d9de
refactoring for more readability
roterEmil 62aed1f
Merge branch 'fix/assignability-prematurelyRead' of https://github.co…
roterEmil 9cce192
fixed multiple assignments of final fields in different branches and …
roterEmil 4675ef7
formatting
roterEmil f3ba6d7
fixed grammar
roterEmil 5fa99d8
removed commented out code
roterEmil 7a0e320
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil c261849
Merge branch 'develop' into fix/assignability-prematurelyRead
roterEmil e5df666
Merge branch 'develop' into fix/assignability-prematurelyRead
1fb4123
Fix typos and improve style
maximilianruesch 11da1b4
Revert minimizing tests
maximilianruesch 8cbab1f
Improve clarity on assignability analysis
maximilianruesch ebcb918
Merge branch 'develop' into fix/assignability-prematurelyRead
maximilianruesch 4ed186b
Improve test case style
maximilianruesch adde086
Improve clarity
maximilianruesch 13bb641
Fix formatting
maximilianruesch f62d034
Add missing semicolon
maximilianruesch 13c4f87
Encapsulate assignability analysis
maximilianruesch df793f7
Simplify mental overhead in assignability
maximilianruesch 9ce7d9e
Move state up
maximilianruesch ae0fc47
Large refactor of assignability analysis
maximilianruesch 9126fed
Shuffle around logic
maximilianruesch 79a8ad3
Fix typos
maximilianruesch 518a1ce
Fix logic in meeting field assignability
maximilianruesch bc49ab5
More shuffling
maximilianruesch c5c2f5a
Fix receiver var of field domination
maximilianruesch 891e92a
Handle domination of multiple branches
maximilianruesch d277036
Cleanup
maximilianruesch 3873fe0
Analyze suspicious usages again
maximilianruesch 05e77cf
Refactor field access structure
maximilianruesch 22a1e18
Refine suspicious uses
maximilianruesch a985a4b
Enable proper updateability of field access
maximilianruesch c644d97
Format code
maximilianruesch ca9ddb2
Merge branch 'refs/heads/develop' into fix/assignability-prematurelyRead
maximilianruesch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
...bility/openworld/assignability/advanced_counter_examples/PrematurelyReadOfFinalField.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| /* BSD 2-Clause License - see OPAL/LICENSE for details. */ | ||
| package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples; | ||
|
|
||
| import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField; | ||
|
|
||
| /** | ||
| * The default value of the field x is assigned to another field n during construction and as | ||
| * a result seen with two different values. | ||
| */ | ||
| public class PrematurelyReadOfFinalField { | ||
|
|
||
| @AssignableField("Field n is assigned with different values.") | ||
| static int n = 5; | ||
|
|
||
| public static void main(String[] args) { | ||
| System.out.println("Value A.X before constructor:" + PrematurelyReadOfFinalField.n); | ||
| C c = new C(); | ||
| System.out.println("Value A.X after constructor:" + PrematurelyReadOfFinalField.n); | ||
| System.out.println("Value C.x after constructor:" + c.x ); | ||
| } | ||
|
|
||
| } | ||
| class B { | ||
|
|
||
| B() { | ||
| PrematurelyReadOfFinalField.n = ((C) this).x; | ||
| } | ||
|
|
||
| void b(C c) { | ||
| PrematurelyReadOfFinalField.n = c.x; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| class C extends B{ | ||
|
|
||
| @AssignableField("Is seen with two different values during construction.") | ||
| public final int x; | ||
|
|
||
| C() { | ||
| super(); | ||
| //this.b(this); | ||
errt marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| x = 3; | ||
| } | ||
| } | ||
23 changes: 23 additions & 0 deletions
23
...lity/openworld/assignability/advanced_counter_examples/ThisEscapesDuringConstruction.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| /* BSD 2-Clause License - see OPAL/LICENSE for details. */ | ||
| package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples; | ||
|
|
||
| import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField; | ||
|
|
||
| /** | ||
| * This test case simulates the fact that the this object escapes in the constructor before (final) fields | ||
| * are assigned. | ||
| */ | ||
| public class ThisEscapesDuringConstruction { | ||
|
|
||
| @AssignableField("The this object escapes in the constructor before the field is assigned.") | ||
| final int n; | ||
|
|
||
| public ThisEscapesDuringConstruction(){ | ||
| C2.m(this); | ||
| n = 7; | ||
| } | ||
| } | ||
|
|
||
| class C2{ | ||
| public static void m(ThisEscapesDuringConstruction c){} | ||
| } |
25 changes: 25 additions & 0 deletions
25
...tability/openworld/assignability/advanced_counter_examples/ValueReadBeforeAssignment.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| /* BSD 2-Clause License - see OPAL/LICENSE for details. */ | ||
| package org.opalj.fpcf.fixtures.immutability.openworld.assignability.advanced_counter_examples; | ||
|
|
||
| import org.opalj.fpcf.properties.immutability.field_assignability.AssignableField; | ||
|
|
||
| /** | ||
| * The value of the field x is read with its default value (0) | ||
| * in the constructor before assignment and assigned to a public field. | ||
| * Thus, the value can be accessed from everywhere. | ||
| */ | ||
| public class ValueReadBeforeAssignment { | ||
| @AssignableField("Field value is read before assignment.") | ||
| private int x; | ||
| @AssignableField("Field y is public and not final.") | ||
| public int y; | ||
|
|
||
| public ValueReadBeforeAssignment() { | ||
| y = x; | ||
| x = 42; | ||
| } | ||
|
|
||
| public ValueReadBeforeAssignment foo() { | ||
| return new ValueReadBeforeAssignment(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -92,18 +92,17 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject) | |
| pc: PC, | ||
| receiver: AccessReceiver | ||
| )(implicit state: AnalysisState): Boolean = { | ||
|
|
||
| val field = state.field | ||
| val method = definedMethod.definedMethod | ||
| val stmts = taCode.stmts | ||
| val receiverVar = receiver.map(uVarForDefSites(_, taCode.pcToIndex)) | ||
|
|
||
| val index = taCode.pcToIndex(pc) | ||
| if (method.isInitializer) { | ||
| if (field.isStatic) { | ||
| method.isConstructor | ||
| } else { | ||
| receiverVar.isDefined && receiverVar.get.definedBy != SelfReferenceParameter | ||
| } | ||
| if (method.isInitializer && method.classFile == field.classFile) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this part of the code needs documentation to understand what all of the different conditions do
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Still not documented |
||
| field.isStatic && method.isConstructor || | ||
| receiverVar.isDefined && receiverVar.get.definedBy != SelfReferenceParameter || | ||
| checkWriteDominance(definedMethod, taCode, receiverVar, index) | ||
| } else { | ||
| if (field.isStatic || receiverVar.isDefined && receiverVar.get.definedBy == SelfReferenceParameter) { | ||
| // We consider lazy initialization if there is only single write | ||
|
|
@@ -152,24 +151,45 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject) | |
| if (writesInMethod.distinctBy(_._2).size > 1) | ||
| return true; // Field is written in multiple locations, thus must be assignable | ||
|
|
||
| // If we have no information about the receiver, we soundly return | ||
| if (receiverVar.isEmpty) | ||
| // If we have no information about the receiver, we soundly return true | ||
| // However, a static field has no receiver | ||
| if (receiverVar.isEmpty && !state.field.isStatic) | ||
| return true; | ||
|
|
||
| val assignedValueObject = receiverVar.get | ||
| if (assignedValueObject.definedBy.exists(_ < 0)) | ||
| val assignedValueObject = | ||
| if (index > 0 && stmts(index).isPutStatic) { | ||
| stmts(index).asPutStatic.value.asVar | ||
| } else | ||
| receiverVar.get | ||
|
|
||
| // When there are more than 1 definitionsite, we soundly return true | ||
roterEmil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (assignedValueObject.definedBy.size != 1) | ||
| return true; | ||
|
|
||
| val definitionSite = assignedValueObject.definedBy.head | ||
|
|
||
| if (definitionSite < -1 || | ||
| (definitionSite == -1 && !definedMethod.definedMethod.isConstructor) | ||
| ) | ||
| return true; | ||
|
|
||
| val assignedValueObjectVar = stmts(assignedValueObject.definedBy.head).asAssignment.targetVar.asVar | ||
| val uses = if (definitionSite == -1) | ||
| taCode.params.thisParameter.useSites | ||
| else { | ||
| val assignedValueObjectVar = stmts(definitionSite).asAssignment.targetVar.asVar | ||
| if (assignedValueObjectVar != null) | ||
| assignedValueObjectVar.usedBy | ||
| else IntTrieSet.empty | ||
| } | ||
|
|
||
| val fieldWriteInMethodIndex = taCode.pcToIndex(writesInMethod.head._2) | ||
| if (assignedValueObjectVar != null && !assignedValueObjectVar.usedBy.forall { index => | ||
| if (!uses.forall { index => | ||
| val stmt = stmts(index) | ||
|
|
||
| fieldWriteInMethodIndex == index || // The value is itself written to another object | ||
| // IMPROVE: Can we use field access information to care about reflective accesses here? | ||
| stmt.isPutField && stmt.asPutField.name != state.field.name || | ||
| stmt.isAssignment && stmt.asAssignment.targetVar == assignedValueObjectVar || | ||
| // stmt.isAssignment && stmt.asAssignment.targetVar == assignedValueObjectVar || | ||
roterEmil marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| stmt.isMethodCall && stmt.asMethodCall.name == "<init>" || | ||
| // CHECK do we really need the taCode here? | ||
| dominates(fieldWriteInMethodIndex, index, taCode) | ||
|
|
@@ -256,15 +276,17 @@ class L2FieldAssignabilityAnalysis private[analyses] (val project: SomeProject) | |
| fieldReadAccessInformation.numIndirectAccesses - seenIndirectAccesses | ||
| ).exists { readAccess => | ||
| val method = contextProvider.contextFromId(readAccess._1).method | ||
| (writeAccess._1 eq method) && { | ||
| val taCode = state.tacDependees(method.asDefinedMethod).ub.tac.get | ||
|
|
||
| if (readAccess._3.isDefined && readAccess._3.get._2.forall(isFormalParameter)) { | ||
| false | ||
| } else { | ||
| !dominates(writeAccess._4, taCode.pcToIndex(readAccess._2), taCode) | ||
| method.definedMethod.classFile != state.field.classFile || | ||
| (writeAccess._1 eq method) && { | ||
| val taCode = state.tacDependees(method.asDefinedMethod).ub.tac.get | ||
|
|
||
| if (readAccess._3.isDefined && readAccess._3.get._2.forall(isFormalParameter)) { | ||
| false | ||
| } else { | ||
| !dominates(writeAccess._4, taCode.pcToIndex(readAccess._2), taCode) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.