Skip to content

Commit

Permalink
added json
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Nov 1, 2023
1 parent 9812b94 commit 2ce9a92
Show file tree
Hide file tree
Showing 15 changed files with 314 additions and 270 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.collect.ImmutableList;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.Serializer;
import java.net.URI;
import java.nio.file.Path;
Expand All @@ -19,24 +18,21 @@ public abstract class AbstractSymbolLocation implements SymbolLocation {
public final Path path;
/** Enclosing class of the symbol. */
public final Symbol.ClassSymbol enclosingClass;
/** Declaration tree of the symbol. */
public final JCTree declarationTree;
/** Target symbol. */
public final Symbol target;
/** List of indexes to locate the type variable. */
public List<List<Integer>> typeVariablePositions;

public static final ImmutableList<List<Integer>> ON_TYPE = ImmutableList.of(List.of(0));

public AbstractSymbolLocation(LocationKind kind, Symbol target, JCTree tree) {
public AbstractSymbolLocation(LocationKind kind, Symbol target) {
this.kind = kind;
this.enclosingClass = target.enclClass();
URI pathInURI =
enclosingClass.sourcefile != null
? enclosingClass.sourcefile.toUri()
: (enclosingClass.classfile != null ? enclosingClass.classfile.toUri() : null);
this.path = Serializer.pathToSourceFileFromURI(pathInURI);
this.declarationTree = tree;
this.target = target;
this.typeVariablePositions = ON_TYPE;
}
Expand Down Expand Up @@ -78,15 +74,13 @@ public boolean equals(Object o) {
return getKind() == that.getKind()
&& Objects.equals(path, that.path)
&& Objects.equals(enclosingClass, that.enclosingClass)
&& Objects.equals(declarationTree, that.declarationTree)
&& Objects.equals(target, that.target)
&& Objects.equals(typeVariablePositions, that.typeVariablePositions);
}

@Override
public int hashCode() {
return Objects.hash(
getKind(), path, enclosingClass, declarationTree, target, typeVariablePositions);
return Objects.hash(getKind(), path, enclosingClass, target, typeVariablePositions);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package edu.ucr.cs.riple.taint.ucrtainting.serialization.location;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.code.Type;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.visitors.LocationVisitor;

public class ClassDeclarationLocation extends AbstractSymbolLocation{
public class ClassDeclarationLocation extends AbstractSymbolLocation {

public final Type.ClassType toChange;

public ClassDeclarationLocation(LocationKind kind, Symbol target, JCTree tree) {
super(kind, target, tree);
}
public ClassDeclarationLocation(Symbol target, Type.ClassType toChange) {
super(LocationKind.CLASS_DECLARATION, target);
this.toChange = toChange;
}

@Override
public <R, P> R accept(LocationVisitor<R, P> v, P p) {
return null;
}
@Override
public <R, P> R accept(LocationVisitor<R, P> v, P p) {
return v.visitClassDeclaration(this, p);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.ucr.cs.riple.taint.ucrtainting.serialization.location;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.visitors.LocationVisitor;
import java.util.Objects;

Expand All @@ -11,8 +10,8 @@ public class FieldLocation extends AbstractSymbolLocation {
/** Symbol of targeted class field */
public final Symbol.VarSymbol variableSymbol;

public FieldLocation(Symbol target, JCTree declarationTree) {
super(LocationKind.FIELD, target, declarationTree);
public FieldLocation(Symbol target) {
super(LocationKind.FIELD, target);
variableSymbol = (Symbol.VarSymbol) target;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class LocalVariableLocation extends AbstractSymbolLocation {
public final Symbol.MethodSymbol enclosingMethod;

public LocalVariableLocation(Symbol target, JCTree declarationTree) {
super(LocationKind.LOCAL_VARIABLE, target, declarationTree);
super(LocationKind.LOCAL_VARIABLE, target);
// TODO: Rewrite the enclosing method. This is a hack to make the serialization work for now.
this.enclosingMethod =
(Symbol.MethodSymbol)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package edu.ucr.cs.riple.taint.ucrtainting.serialization.location;

import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.visitors.LocationVisitor;
import java.util.Objects;

Expand All @@ -11,8 +10,8 @@ public class MethodLocation extends AbstractSymbolLocation {
/** Symbol of the targeted method. */
public final Symbol.MethodSymbol enclosingMethod;

public MethodLocation(Symbol target, JCTree declarationTree) {
super(LocationKind.METHOD, target, declarationTree);
public MethodLocation(Symbol target) {
super(LocationKind.METHOD, target);
enclosingMethod = (Symbol.MethodSymbol) target;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.google.common.base.Preconditions;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.tree.JCTree;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.visitors.LocationVisitor;
import java.util.Objects;
import javax.lang.model.element.ElementKind;
Expand All @@ -17,8 +16,8 @@ public class MethodParameterLocation extends AbstractSymbolLocation {
/** Index of the method parameter in the containing method's argument list. */
public final int index;

public MethodParameterLocation(Symbol target, JCTree declarationTree) {
super(LocationKind.PARAMETER, target, declarationTree);
public MethodParameterLocation(Symbol target) {
super(LocationKind.PARAMETER, target);
this.paramSymbol = (Symbol.VarSymbol) target;
Symbol cursor = target;
// Look for the enclosing method.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class PolyMethodLocation extends AbstractSymbolLocation {
public final Set<MethodParameterLocation> arguments;

public PolyMethodLocation(MethodLocation location, Set<MethodParameterLocation> arguments) {
super(LocationKind.POLY_METHOD, location.target, location.declarationTree);
super(LocationKind.POLY_METHOD, location.target);
this.arguments = arguments;
if (arguments.isEmpty()) {
throw new RuntimeException("PolyMethodLocation must have at least one argument");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ public interface SymbolLocation {
*/
@Nullable
static SymbolLocation createLocationFromSymbol(Symbol target, Context context) {
JCTree declarationTree = Utility.locateDeclaration(target, context);
switch (target.getKind()) {
case PARAMETER:
return new MethodParameterLocation(target, declarationTree);
return new MethodParameterLocation(target);
case METHOD:
return new MethodLocation(target, declarationTree);
return new MethodLocation(target);
case FIELD:
return new FieldLocation(target, declarationTree);
return new FieldLocation(target);
case LOCAL_VARIABLE:
JCTree declarationTree = Utility.locateDeclaration(target, context);
return new LocalVariableLocation(target, declarationTree);
case EXCEPTION_PARAMETER:
// currently not supported / desired.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import edu.ucr.cs.riple.taint.ucrtainting.serialization.Serializer;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.AbstractSymbolLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.ClassDeclarationLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.FieldLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.LocalVariableLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.MethodLocation;
Expand Down Expand Up @@ -72,4 +73,11 @@ public JSONObject visitPolyMethod(PolyMethodLocation polyMethodLocation, Void un
.collect(Collectors.toSet())));
return ans;
}

@Override
public JSONObject visitClassDeclaration(ClassDeclarationLocation location, Void unused) {
JSONObject ans = defaultAction(location);
ans.put("target", location.toChange.tsym.toString());
return ans;
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package edu.ucr.cs.riple.taint.ucrtainting.serialization.visitors;

import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.ClassDeclarationLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.FieldLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.LocalVariableLocation;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.MethodLocation;
Expand Down Expand Up @@ -58,4 +59,13 @@ public interface LocationVisitor<R, P> {
* @return a visitor-specified result
*/
R visitPolyMethod(PolyMethodLocation polyMethodLocation, P p);

/**
* Visits a location for a class declaration.
*
* @param classDeclarationLocation the location for a class declaration.
* @param p a visitor-specified parameter
* @return a visitor-specified result
*/
R visitClassDeclaration(ClassDeclarationLocation classDeclarationLocation, P p);
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import edu.ucr.cs.riple.taint.ucrtainting.UCRTaintingAnnotatedTypeFactory;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.Fix;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.Utility;
import edu.ucr.cs.riple.taint.ucrtainting.serialization.location.ClassDeclarationLocation;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import javax.lang.model.element.Element;
import javax.lang.model.type.TypeMirror;
import org.checkerframework.framework.type.AnnotatedTypeFactory;
Expand Down Expand Up @@ -221,21 +221,31 @@ public Set<Fix> computeFixesOnClassDeclarationForRawType(
(AnnotatedTypeMirror.AnnotatedDeclaredType) pair.required;
Type.ClassType requiredType =
(Type.ClassType) ((Type.ClassType) required.getUnderlyingType()).tsym.type;
// We intentionally limit the search to only the first level of inheritance. The type must
// either extend or implement the required type explicitly at the declaration.
Type.ClassType inheritedType = locateInheritedTypeOnExtendOrImplement(classType, requiredType);
if(inheritedType == null){
if (inheritedType == null) {
return Set.of();
}

// We intentionally limit the search to only the first level of inheritance. The type must
// either extend or implement the required type explicitly at the declaration.
throw new RuntimeException("Not implemented");
// return Set.of();
int index =
inheritedType.tsym.type.getTypeArguments().stream()
.map(Type::toString)
.collect(Collectors.toList())
.indexOf(typeVar.toString());
if (index < 0) {
return Set.of();
}
ClassDeclarationLocation classDeclarationLocation =
new ClassDeclarationLocation(classType, inheritedType);
classDeclarationLocation.setTypeVariablePositions(List.of(List.of(index + 1, 0)));
return Set.of(new Fix(classDeclarationLocation));
}

private Type.ClassType locateInheritedTypeOnExtendOrImplement(Symbol.ClassSymbol classType, Type.ClassType requiredType) {
private Type.ClassType locateInheritedTypeOnExtendOrImplement(
Symbol.ClassSymbol classType, Type.ClassType requiredType) {
// Look for interfaces
for (Type type : ((Type.ClassType) classType.type).interfaces_field) {
if(type.tsym.equals(requiredType.tsym)){
if (type.tsym.equals(requiredType.tsym)) {
return (Type.ClassType) type;
}
}
Expand Down
Loading

0 comments on commit 2ce9a92

Please sign in to comment.