Skip to content
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

Add alias analysis #194

Open
wants to merge 17 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.alias;

import org.opalj.fpcf.properties.alias.line.MayAliasLine;
import org.opalj.fpcf.properties.alias.line.NoAliasLine;
import org.opalj.tac.fpcf.analyses.alias.pointsto.AllocationSitePointsToBasedAliasAnalysis;

public class FieldAlias {

@NoAliasLine(reason = "no alias for fields of different objects",
lineNumber = 20, fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 21, secondFieldName = "field2", secondFieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void differentObjectsSameFields() {
FieldClass fc = new FieldClass();
FieldClass fc2 = new FieldClass();

fc.field = new Object();
fc2.field = new Object();

fc.field.hashCode();
fc2.field.hashCode();
}

@NoAliasLine(reason = "no alias for different fields of the same object",
lineNumber = 32, fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 33, secondFieldName = "field2", secondFieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void sameObjectDifferentField() {
FieldClass fc = new FieldClass();
fc.field = new Object();
fc.field2 = new Object();

fc.field.hashCode();
fc.field2.hashCode();
}

@NoAliasLine(reason = "no alias for different fields of different objects",
lineNumber = 46, fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 47, secondFieldName = "field2", secondFieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void differentObjectsDifferentFields() {
FieldClass fc = new FieldClass();
FieldClass fc2 = new FieldClass();

fc.field = new Object();
fc2.field2 = new Object();

fc.field.hashCode();
fc2.field2.hashCode();
}

@MayAliasLine(reason = "may alias for same fields of the same object",
lineNumber = 57, fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 60, secondFieldName = "field", secondFieldClass = FieldClass.class)
public void sameObjectSameField() {
FieldClass fc = new FieldClass();

fc.field = new Object();
fc.field.hashCode();

fc.field = new Object();
fc.field.hashCode();
}

@MayAliasLine(reason = "may alias for same fields of the same object",
lineNumber = 71, fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 72, secondFieldName = "field", secondFieldClass = FieldClass.class)
public void sameObjectSameField2() {
FieldClass fc = new FieldClass();

fc.field = new Object();

fc.field.hashCode();
fc.field.hashCode();
}

public static void paramMayBeField(
@MayAliasLine(reason = "may alias with parameter and field",
lineNumber = 110, methodName = "main", fieldName = "field", fieldClass = FieldClass.class)
@NoAliasLine(reason = "no alias with parameter and field",
lineNumber = 111, methodName = "main", fieldName = "field2", fieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
Object o) {
}

@MayAliasLine(reason = "may alias of field via parameter",
lineNumber = 110, methodName = "main", fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 111, secondMethodName = "main", secondFieldName = "field", secondFieldClass = FieldClass.class)
@NoAliasLine(reason = "no alias of field via parameter",
lineNumber = 110, methodName = "main", fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 111, secondMethodName = "main", secondFieldName = "field2", secondFieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public static void fieldAliasViaParameter(FieldClass fc) {
fc.field.hashCode();
fc.field2.hashCode();
}

@NoAliasLine(reason = "no alias of field via parameter",
lineNumber = 110,methodName = "main", fieldName = "field", fieldClass = FieldClass.class,
secondLineNumber = 111, secondMethodName = "main", secondFieldName = "field2", secondFieldClass = FieldClass.class, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public static void noFieldAliasViaParameter(FieldClass fc) {
fc.field.hashCode();
}

public static void main(String[] args) {
FieldClass fc = new FieldClass();

fc.field = new Object();
fc.field2 = new Object();

paramMayBeField(fc.field);
fieldAliasViaParameter(fc);

fc.field.hashCode();
fc.field2.hashCode();

noFieldAliasViaParameter(new FieldClass());
}

}

class FieldClass {

public Object field;

public Object field2;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.alias;

import org.opalj.fpcf.properties.alias.line.MayAliasLine;
import org.opalj.fpcf.properties.alias.line.NoAliasLine;
import org.opalj.tac.fpcf.analyses.alias.pointsto.AllocationSitePointsToBasedAliasAnalysis;
import org.opalj.tac.fpcf.analyses.alias.pointsto.TypePointsToBasedAliasAnalysis;

public class NullAlias {

public static void main(String[] args) {
paramIsAlwaysNull(null);
paramMayBeNull(null);
paramMayBeNull(new Object());
}

public static void paramIsAlwaysNull(
@NoAliasLine(reason = "parameter is always null", lineNumber = 20, analyses = {AllocationSitePointsToBasedAliasAnalysis.class, TypePointsToBasedAliasAnalysis.class})
Object o) {
o.hashCode();
}

public static void paramMayBeNull(
@MayAliasLine(reason = "parameter may be null", lineNumber = 26)
Object o) {
o.hashCode();
}

@NoAliasLine(reason = "uVar is always null", lineNumber = 32, secondLineNumber = 32, analyses = {AllocationSitePointsToBasedAliasAnalysis.class, TypePointsToBasedAliasAnalysis.class})
public static void UVarIsAlwaysNull() {
Object o = null;
o.hashCode();
}

@MayAliasLine(reason = "uVar may be null", lineNumber = 41, secondLineNumber = 41)
public static void UVarMayBeNull() {
Object o = null;
if (Math.random() > 0.5) {
o = new Object();
}
o.hashCode();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.alias;

import org.opalj.fpcf.properties.alias.MayAlias;
import org.opalj.fpcf.properties.alias.NoAlias;
import org.opalj.fpcf.properties.alias.line.MayAliasLine;
import org.opalj.fpcf.properties.alias.line.NoAliasLine;
import org.opalj.tac.fpcf.analyses.alias.pointsto.AllocationSitePointsToBasedAliasAnalysis;

public class ParameterAlias {

public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();

noAliasWithLocal(o1);

noAliasWithParam(o1, o2);

mayAliasWithLocal(o1);

mayAliasWithParam1(o1, o2);
mayAliasWithParam1(o1, o1);

mayAliasWithParam2(o1, o1);


ParameterAlias pa = new ParameterAlias();
ParameterAlias pa2 = new ParameterAlias();

pa.mayAliasThisParam();
pa2.noAliasThisParam();

pa.mayAliasThisParamTwoMethods1();
pa.mayAliasThisParamTwoMethods2();

pa.noAliasThisParamTwoMethods1();
pa2.noAliasThisParamTwoMethods2();
}

public static void noAliasWithLocal(@NoAliasLine(reason = "noAlias with uVar", lineNumber = 44, analyses = {AllocationSitePointsToBasedAliasAnalysis.class}) Object o1) {

Object o2 = new Object();
o2.hashCode();
}

public static void noAliasWithParam(@NoAlias(reason = "noAlias with other parameter", id = 0, analyses = {AllocationSitePointsToBasedAliasAnalysis.class}) Object o1,
@NoAlias(reason = "noAlias with other parameter", id = 0, analyses = {AllocationSitePointsToBasedAliasAnalysis.class}) Object o2) {

}

public static void mayAliasWithLocal(@MayAliasLine(reason = "mayAlias with uVar", lineNumber = 60) Object o1) {

Object o2 = new Object();

if (Math.random() > 0.5) {
o2 = o1;
}

o2.hashCode();
}

public static void mayAliasWithParam1(@MayAlias(reason = "mayAlias with other parameter 1", id = 1) Object o1,
@MayAlias(reason = "mayAlias with other parameter 1", id = 1) Object o2) {

}

public static void mayAliasWithParam2(@MayAlias(reason = "mayAlias with other parameter 2", id = 2) Object o1,
@MayAlias(reason = "mayAlias with other parameter 2", id = 2) Object o2) {

}

@MayAliasLine(reason = "may alias with this parameter and invoked uVar", thisParameter = true,
lineNumber = 31, methodName = "main")
public void mayAliasThisParam() {}

@NoAliasLine(reason = "no alias with this parameter and invoked uVar", thisParameter = true,
lineNumber = 31, methodName = "main", analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void noAliasThisParam() {}

@MayAlias(reason = "may alias with this parameter of two methods", thisParameter = true,
id = 3)
public void mayAliasThisParamTwoMethods1() {}

@MayAlias(reason = "may alias with this parameter of two methods", thisParameter = true,
id = 3)
public void mayAliasThisParamTwoMethods2() {}

@NoAlias(reason = "no alias with this parameter of two methods", thisParameter = true,
id = 4, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void noAliasThisParamTwoMethods1() {}

@NoAlias(reason = "no alias with this parameter of two methods", thisParameter = true,
id = 4, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public void noAliasThisParamTwoMethods2() {}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
/* BSD 2-Clause License - see OPAL/LICENSE for details. */
package org.opalj.fpcf.fixtures.alias;

import org.opalj.fpcf.properties.alias.MayAlias;
import org.opalj.fpcf.properties.alias.NoAlias;
import org.opalj.fpcf.properties.alias.line.MayAliasLine;
import org.opalj.fpcf.properties.alias.line.NoAliasLine;
import org.opalj.tac.fpcf.analyses.alias.pointsto.AllocationSitePointsToBasedAliasAnalysis;
import org.opalj.tac.fpcf.analyses.alias.pointsto.TypePointsToBasedAliasAnalysis;

public class ReturnValueAlias {

@NoAliasLine(reason = "no Alias with local variable", lineNumber = 17, analyses = {AllocationSitePointsToBasedAliasAnalysis.class})
public static Object noAliasWithLocal() {
Object o1 = new Object();

o1.hashCode();

return new Object();
}

@MayAliasLine(reason = "mayAlias with local variable", lineNumber = 31)
public static Object mayAliasWithLocal1() {
Object o1 = new Object();
Object o2 = new Object();

if (Math.random() > 0.5) {
o2 = o1;
}

o2.hashCode();

return o2;
}

@MayAliasLine(reason = "mayAlias with local variable", lineNumber = 40)
public static Object mayAliasWithLocal2() {
Object o1 = new Object();

o1.hashCode();

return o1;
}

@NoAlias(reason = "noAlias with parameter", id = 0, analyses = {AllocationSitePointsToBasedAliasAnalysis.class, TypePointsToBasedAliasAnalysis.class})
public static Object noAliasWithParam(
@NoAlias(reason = "noAlias with parameter", id = 0, analyses = {AllocationSitePointsToBasedAliasAnalysis.class, TypePointsToBasedAliasAnalysis.class})
Object a) {
Object o1 = new Object();
return o1;
}

@MayAlias(reason = "mayAlias with parameter", id = 1)
public static Object mayAliasWithParam1(
@MayAlias(reason = "mayAlias with parameter", id = 1)
Object a) {
return a;
}

@MayAlias(reason = "mayAlias with parameter", id = 2)
public static Object mayAliasWithParam2(
@MayAlias(reason = "mayAlias with parameter", id = 2)
Object a) {
return a;
}

@MayAlias(reason = "mayAlias with parameter", id = 3)
public static Object mayAliasWithParam3(
@MayAlias(reason = "mayAlias with parameter", id = 3)
Object a) {

Object o1 = new Object();

if (Math.random() > 0.5) {
o1 = a;
}

return o1;
}

@MayAlias(reason = "mayAlias with parameter", id = 4)
public static Object mayAliasWithParam4(
@MayAlias(reason = "mayAlias with parameter", id = 4)
Object a) {

Object o1 = new Object();

if (Math.random() > 0.5) {
return o1;
}

return a;
}

public static void main(String[] args) {
Object o1 = new Object();
Object o2 = new Object();

mayAliasWithParam1(o1);

mayAliasWithParam2(o1);
mayAliasWithParam2(o2);

mayAliasWithParam3(o1);

mayAliasWithParam4(o1);
}

}
Loading