Skip to content

Commit

Permalink
add local var opt
Browse files Browse the repository at this point in the history
  • Loading branch information
nimakarimipour committed Jul 26, 2024
1 parent b0d15fa commit 6910c1d
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
11 changes: 6 additions & 5 deletions src/main/java/edu/ucr/cs/riple/taint/ucrtainting/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ public class Config {
* checker framework.
*/
public static final String SERIALIZATION_ACTIVATION_FLAG = "enableSerialization";

public static final String DISABLE_LOCAL_VAR_OPT = "disableLocalVariableOptimization";
/**
* Path to serialization config. If {@link #serializationActivation} is false, it will be {@code
* null}.
Expand All @@ -61,13 +59,12 @@ public class Config {
*/
@Nullable public final Path outputDir;

public final boolean disableLocalVariableOptimization;
public final boolean enableLocalVariableOptimization;
/** Flag to control serialization internally. */
public final boolean serializationActivation;

public Config(UCRTaintingChecker checker) {
this.serializationActivation = checker.hasOption(SERIALIZATION_ACTIVATION_FLAG);
this.disableLocalVariableOptimization = checker.hasOption(DISABLE_LOCAL_VAR_OPT);
if (serializationActivation && !checker.hasOption(SERIALIZATION_CONFIG_PATH)) {
throw new RuntimeException(
"Please specify the serialization config using the flag: "
Expand All @@ -81,8 +78,8 @@ public Config(UCRTaintingChecker checker) {
? Paths.get(checker.getOptions().get(SERIALIZATION_CONFIG_PATH))
: null;
String outputDirString = null;
Document document = null;
if (configPath != null) {
Document document;
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Expand All @@ -94,6 +91,10 @@ public Config(UCRTaintingChecker checker) {
throw new RuntimeException("Error in reading/parsing config at path: " + configPath, e);
}
}
this.enableLocalVariableOptimization =
Boolean.TRUE.equals(
XMLUtil.getValueFromTag(document, "/serialization/localVarOpt", Boolean.class)
.orElse(true));
this.outputDir = outputDirString == null ? null : Paths.get(outputDirString);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public SerializationService(UCRTaintingChecker checker) {
this.types = Types.instance(context);
this.untaintedTypeMatchVisitor = TypeMatchVisitor.createUntaintedMatcher(typeFactory);
this.polyTypeMatchVisitor = TypeMatchVisitor.createPolyTaintedMatcher(typeFactory);
this.fixComputer = new FixComputer(typeFactory, types, context);
this.fixComputer = new FixComputer(serializer.config, typeFactory, types, context);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class Serializer {
/** File name to write errors. */
public static final String ERROR_OUTPUT = "errors.json";
/** Config object used to configure the serializer. */
private final Config config;
public final Config config;
/** Path to write errors. */
private final @Nullable Path errorOutputPath;

Expand Down Expand Up @@ -82,9 +82,7 @@ private void initializeOutputFiles() {
Files.createDirectories(config.outputDir);
try {
Files.deleteIfExists(errorOutputPath);
Files.write(
config.outputDir.resolve("serialization_version.txt"),
("0\n" + config.disableLocalVariableOptimization).getBytes());
Files.write(config.outputDir.resolve("serialization_version.txt"), "0".getBytes());
} catch (IOException e) {
throw new RuntimeException("Could not clear file at: " + errorOutputPath, e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,15 @@ public class DefaultTypeChangeVisitor extends SpecializedFixComputer {
/** Visitor that fixes all assignments on local variables. */
protected final LocalVariableFixVisitor localVariableFixVisitor;

protected final Config config;

public DefaultTypeChangeVisitor(
UCRTaintingAnnotatedTypeFactory factory, FixComputer fixComputer, Context context) {
Config config,
UCRTaintingAnnotatedTypeFactory factory,
FixComputer fixComputer,
Context context) {
super(factory, fixComputer, context);
this.config = config;
this.returnVisitor = new MethodReturnVisitor(typeFactory, fixComputer, context);
this.types = Types.instance(context);
this.localVariableFixVisitor = new LocalVariableFixVisitor(typeFactory, fixComputer, context);
Expand Down Expand Up @@ -119,8 +125,7 @@ public Set<Fix> visitIdentifier(IdentifierTree node, FoundRequired pair) {
return Set.of();
}
}
return fix.location.getKind().isLocalVariable()
&& !typeFactory.getChecker().hasOption(Config.DISABLE_LOCAL_VAR_OPT)
return fix.location.getKind().isLocalVariable() && config.enableLocalVariableOptimization
? localVariableFixVisitor.visitIdentifier(node, pair)
: Set.of(fix);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import com.sun.tools.javac.code.Types;
import com.sun.tools.javac.tree.JCTree;
import com.sun.tools.javac.util.Context;
import edu.ucr.cs.riple.taint.ucrtainting.Config;
import edu.ucr.cs.riple.taint.ucrtainting.FoundRequired;
import edu.ucr.cs.riple.taint.ucrtainting.UCRTaintingAnnotatedTypeFactory;
import edu.ucr.cs.riple.taint.ucrtainting.handlers.CollectionHandler;
Expand Down Expand Up @@ -67,11 +68,12 @@ public class FixComputer extends SimpleTreeVisitor<Set<Fix>, FoundRequired> {
protected final SpecializedFixComputer methodTypeArgumentFixVisitor;
protected final CollectionFixVisitor collectionFixVisitor;

public FixComputer(UCRTaintingAnnotatedTypeFactory factory, Types types, Context context) {
public FixComputer(
Config config, UCRTaintingAnnotatedTypeFactory factory, Types types, Context context) {
this.typeFactory = factory;
this.context = context;
this.types = types;
this.defaultTypeChangeVisitor = new DefaultTypeChangeVisitor(factory, this, context);
this.defaultTypeChangeVisitor = new DefaultTypeChangeVisitor(config, factory, this, context);
this.unannotatedCodeFixComputer = new UnannotatedCodeFixVisitor(typeFactory, this, context);
this.methodTypeArgumentFixVisitor = new GenericMethodFixVisitor(typeFactory, this, context);
this.collectionFixVisitor = new CollectionFixVisitor(typeFactory, this, context);
Expand Down

0 comments on commit 6910c1d

Please sign in to comment.