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 inference support for UCRTaint checker #194

Open
wants to merge 52 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
ae85f9b
add visitor architecture
nimakarimipour Apr 4, 2023
ed7bda6
update visitibility to public
nimakarimipour Apr 4, 2023
e64c754
use visitor for json creation of locations
nimakarimipour Apr 4, 2023
d695841
add visit method for visitor
nimakarimipour Apr 4, 2023
8eb6614
make singleton
nimakarimipour Apr 4, 2023
4204867
move KEYS to LocationToJsonVisitor
nimakarimipour Apr 5, 2023
3d4cd7a
Merge branch 'master' into nimak/add-visitor-loc
nimakarimipour Apr 8, 2023
548a392
Merge branch 'master' into nimak/visitor-for-json
nimakarimipour Apr 8, 2023
22a1a65
Merge branch 'nimak/add-visitor-loc' into nimak/visitor-for-json
nimakarimipour Apr 8, 2023
29ef390
merged latest master into this
nimakarimipour Apr 20, 2023
add1b53
Merge branch 'master' into nimak/local-var
nimakarimipour Apr 21, 2023
7d0a2ce
use stack instead
nimakarimipour Apr 21, 2023
e122344
add comment
nimakarimipour Apr 21, 2023
6d3f3e4
add a new line for field
nimakarimipour Apr 21, 2023
fb7e8a7
use OnMethod instance for local variable location:
nimakarimipour Apr 21, 2023
1e8b402
add javadoc
nimakarimipour Apr 21, 2023
cc86959
update
nimakarimipour May 2, 2023
a3c4b1d
update
nimakarimipour May 9, 2023
4592d93
Merge branch 'master' into nimak/local-var
nimakarimipour May 9, 2023
0e8483c
update
nimakarimipour May 9, 2023
6503403
intial impl
nimakarimipour May 9, 2023
2f4ac1e
update
nimakarimipour May 9, 2023
7999b56
Merge branch 'master' into nimak/local-var
nimakarimipour May 10, 2023
dcb9e78
merged master into this
nimakarimipour May 10, 2023
97a9ea4
add javadoc
nimakarimipour May 10, 2023
68316e9
add javadoc
nimakarimipour May 10, 2023
62fe352
Merge branch 'nimak/type-use' of github.com:ucr-riple/NullAwayAnnotat…
nimakarimipour May 10, 2023
53a0a69
Merge branch 'master' into nimak/local-var
nimakarimipour May 16, 2023
c972b6f
Merge branch 'nimak/local-var' into nimak/type-use
nimakarimipour May 16, 2023
4170c9d
init
nimakarimipour May 17, 2023
94bfbda
fix test
nimakarimipour May 17, 2023
07a6d3b
remove unused classes
nimakarimipour May 17, 2023
0947c5d
use prepare for build
nimakarimipour May 17, 2023
42543a2
delete write nullaway config as xml in utility
nimakarimipour May 17, 2023
f2e2a1e
fix bug
nimakarimipour May 17, 2023
9c56a43
rename checker name
nimakarimipour May 17, 2023
8c38412
update
nimakarimipour May 17, 2023
c2da72f
fix bug
nimakarimipour May 17, 2023
155aaca
fix up interface names
nimakarimipour May 17, 2023
42d3ee2
Merge branch 'nimak/local-var' into nimak/add-taint
nimakarimipour May 17, 2023
99271b6
Merge branch 'nimak/type-use' into nimak/add-taint
nimakarimipour May 17, 2023
37b09fd
initial
nimakarimipour May 17, 2023
da01475
rename and refactor
nimakarimipour May 18, 2023
9dd87ad
rename
nimakarimipour May 18, 2023
277173d
Merge branch 'nimak/generalize' into nimak/add-taint
nimakarimipour May 18, 2023
10e7098
update
nimakarimipour May 18, 2023
bc8b43f
update
nimakarimipour May 18, 2023
079f304
add UCRTaint to get checker by name
nimakarimipour May 18, 2023
7ffa8e0
update
nimakarimipour May 24, 2023
70402e8
merged master into this
nimakarimipour May 31, 2023
2c85855
remove unwanted changes
nimakarimipour May 31, 2023
8e6a2df
Merge branch 'master' into nimak/add-taint
nimakarimipour May 31, 2023
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
Expand Up @@ -27,6 +27,7 @@
import edu.ucr.cs.riple.core.Config;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.checkers.nullaway.NullAway;
import edu.ucr.cs.riple.core.checkers.ucrtaint.UCRTaint;
import edu.ucr.cs.riple.core.metadata.index.Error;
import edu.ucr.cs.riple.core.module.ModuleInfo;
import edu.ucr.cs.riple.injector.location.OnField;
Expand Down Expand Up @@ -75,6 +76,8 @@ public static Checker<?> getCheckerByName(String name, Context context) {
switch (name) {
case NullAway.NAME:
return new NullAway(context);
case UCRTaint.NAME:
return new UCRTaint(context);
default:
throw new RuntimeException("Unknown checker name: " + name);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
/*
* MIT License
*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package edu.ucr.cs.riple.core.checkers.ucrtaint;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.Context;
import edu.ucr.cs.riple.core.checkers.CheckerBaseClass;
import edu.ucr.cs.riple.core.injectors.AnnotationInjector;
import edu.ucr.cs.riple.core.metadata.index.Fix;
import edu.ucr.cs.riple.core.metadata.region.Region;
import edu.ucr.cs.riple.core.module.ModuleConfiguration;
import edu.ucr.cs.riple.core.module.ModuleInfo;
import edu.ucr.cs.riple.injector.changes.AddTypeUseMarkerAnnotation;
import edu.ucr.cs.riple.injector.location.Location;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
* Represents <a href="https://github.com/kanaksad/UCRTaintingChecker">UCRTaint</a> checker in
* Annotator.
*/
public class UCRTaint extends CheckerBaseClass<UCRTaintError> {

/** The name of the checker. This is used to identify the checker in the configuration file. */
public static final String NAME = "UCRTaint";

public UCRTaint(Context context) {
super(context);
}

@Override
public void preprocess(AnnotationInjector injector) {}

@Override
public Set<UCRTaintError> deserializeErrors(ModuleInfo module) {
ImmutableSet<Path> paths =
module.getModuleConfiguration().stream()
.map(moduleInfo -> moduleInfo.dir.resolve("errors.json"))
.collect(ImmutableSet.toImmutableSet());
Set<UCRTaintError> errors = new HashSet<>();
paths.forEach(
path -> {
try {
String content = Files.readString(path, Charset.defaultCharset());
content = "{ \"errors\": [" + content.substring(0, content.length() - 1) + "]}";
JSONObject jsonObject = (JSONObject) new JSONParser().parse(content);
JSONArray errorsJson = (JSONArray) jsonObject.get("errors");
errorsJson.forEach(o -> errors.add(deserializeErrorFromJSON((JSONObject) o, module)));
} catch (IOException | ParseException e) {
throw new RuntimeException(e);
}
});
return errors;
}

private UCRTaintError deserializeErrorFromJSON(JSONObject errorsJson, ModuleInfo moduleInfo) {
String errorType = (String) errorsJson.get("messageKey");
int offset = ((Long) errorsJson.get("offset")).intValue();
Region region =
new Region(
(String) ((JSONObject) errorsJson.get("region")).get("member"),
(String) ((JSONObject) errorsJson.get("region")).get("class"));
ImmutableSet.Builder<Fix> builder = ImmutableSet.builder();
((JSONArray) errorsJson.get("fixes"))
.forEach(
o -> {
JSONObject fixJson = (JSONObject) o;
Location location =
Location.createLocationFromJSON((JSONObject) fixJson.get("location"));
location.ifField(onField -> extendVariableList(onField, moduleInfo));
builder.add(
new Fix(
new AddTypeUseMarkerAnnotation(
location, "edu.ucr.cs.riple.taint.ucrtainting.qual.RUntainted"),
errorType,
true));
});
return new UCRTaintError(errorType, "", region, offset, builder.build());
}

@Override
public void suppressRemainingErrors(AnnotationInjector injector) {
throw new RuntimeException(
"Suppression for remaining errors is not supported for " + NAME + "yet!");
}

@Override
public void verifyCheckerCompatibility() {}

@Override
public void prepareConfigFilesForBuild(ImmutableSet<ModuleConfiguration> configurations) {
// TODO: implement this once configuration on UCRTaint is finalized.
}

@Override
public UCRTaintError createError(
String errorType,
String errorMessage,
Region region,
int offset,
ImmutableSet<Fix> resolvingFixes,
ModuleInfo moduleInfo) {
return new UCRTaintError(errorType, errorMessage, region, offset, resolvingFixes);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* MIT License
*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package edu.ucr.cs.riple.core.checkers.ucrtaint;

import com.google.common.collect.ImmutableSet;
import edu.ucr.cs.riple.core.metadata.index.Error;
import edu.ucr.cs.riple.core.metadata.index.Fix;
import edu.ucr.cs.riple.core.metadata.region.Region;

/** Represents an error reported by {@link UCRTaint}. */
public class UCRTaintError extends Error {

public UCRTaintError(
String messageType,
String message,
Region region,
int offset,
ImmutableSet<Fix> resolvingFixes) {
super(messageType, message, region, offset, resolvingFixes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public CompoundRegionRegistry(ModuleInfo moduleInfo) {
ImmutableSet.of(
new FieldRegionRegistry(moduleInfo),
methodRegionRegistry,
new LocalVariableRegionRegistry(),
new ParameterRegionRegistry(moduleInfo, methodRegionRegistry));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* MIT License
*
* Copyright (c) 2023 Nima Karimipour
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package edu.ucr.cs.riple.core.metadata.region;

import edu.ucr.cs.riple.injector.location.Location;
import edu.ucr.cs.riple.injector.location.OnLocalVariable;
import java.util.Optional;
import java.util.Set;

/** Region Registry for Local variables. */
public class LocalVariableRegionRegistry implements RegionRegistry {
@Override
public Optional<Set<Region>> getImpactedRegions(Location location) {
if (!location.isOnLocalVariable()) {
return Optional.empty();
}
OnLocalVariable localVariable = location.toLocalVariable();
return Optional.of(Set.of(new Region(localVariable.clazz, localVariable.encMethod.method)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,12 @@
import com.google.common.collect.Sets;
import edu.ucr.cs.riple.injector.Helper;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.Objects;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import org.json.simple.JSONObject;

/** Represents a location of an element in the source code. */
public abstract class Location {
Expand All @@ -57,6 +59,19 @@ public Location(LocationKind type, Path path, String clazz) {
this.path = path;
}

/**
* Creates an instance of {@link Location} for a given type, and retrieves path and class values
* from the given JSON object.
*
* @param type The type of the location.
* @param json The JSON object containing the path and class values.
*/
public Location(LocationKind type, JSONObject json) {
this.type = type;
this.clazz = (String) json.get("class");
this.path = Paths.get((String) json.get("path"));
}

/**
* Creates an instance of {@link Location} based on values written in a row of a TSV file. These
* values should be in order of:
Expand Down Expand Up @@ -98,6 +113,24 @@ public static Location createLocationFromArrayInfo(String[] values) {
throw new RuntimeException("Cannot reach this statement, values: " + Arrays.toString(values));
}

public static Location createLocationFromJSON(JSONObject json) {
String kind = (String) json.get("kind");
switch (kind) {
case "METHOD":
return new OnMethod(json);
case "FIELD":
return new OnField(json);
case "PARAMETER":
return new OnParameter(json);
case "LOCAL_VARIABLE":
return new OnLocalVariable(json);
case "CLASS":
return new OnClass(json);
default:
throw new RuntimeException("Cannot reach this statement, kind: " + kind);
}
}

/**
* If this location is of kind {@link LocationKind#METHOD}, calls the consumer on the location.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import edu.ucr.cs.riple.injector.Helper;
import java.nio.file.Path;
import java.util.regex.Pattern;
import org.json.simple.JSONObject;

/** Represents a location for class element. This location is used to apply changes to a class. */
public class OnClass extends Location {
Expand All @@ -45,6 +46,10 @@ public OnClass(String path, String clazz) {
this(Helper.deserializePath(path), clazz);
}

public OnClass(JSONObject json) {
super(LocationKind.CLASS, json);
}

/**
* Checks if flat name is for an anonymous class.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,11 @@
import edu.ucr.cs.riple.injector.Helper;
import java.nio.file.Path;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;
import org.json.simple.JSONObject;

/**
* Represents a location for field element. This location is used to apply changes to a class field.
Expand Down Expand Up @@ -59,6 +61,11 @@ public OnField(String path, String clazz, Set<String> variables) {
this(Helper.deserializePath(path), clazz, variables);
}

public OnField(JSONObject json) {
super(LocationKind.FIELD, json);
this.variables = new HashSet<>(Collections.singletonList((String) json.get("variables")));
}

@Override
public void ifField(Consumer<OnField> consumer) {
consumer.accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.nio.file.Path;
import java.util.Objects;
import java.util.function.Consumer;
import org.json.simple.JSONObject;

/**
* Represents a location for local variable element. This location is used to apply changes to a
Expand All @@ -51,6 +52,12 @@ public OnLocalVariable(String path, String clazz, String encMethod, String varNa
this(Helper.deserializePath(path), clazz, encMethod, varName);
}

public OnLocalVariable(JSONObject json) {
super(LocationKind.LOCAL_VARIABLE, json);
this.encMethod = new OnMethod(json);
this.varName = (String) json.get("varName");
}

@Override
public void ifLocalVariable(Consumer<OnLocalVariable> consumer) {
consumer.accept(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import java.nio.file.Path;
import java.util.Objects;
import java.util.function.Consumer;
import org.json.simple.JSONObject;

/** Represents a location for method element. This location is used to apply changes to a method. */
public class OnMethod extends Location {
Expand All @@ -52,6 +53,12 @@ public OnMethod(String path, String clazz, String method) {
this(Helper.deserializePath(path), clazz, method);
}

public OnMethod(JSONObject json) {
super(LocationKind.METHOD, json);
this.method = (String) json.get("method");
this.matcher = new SignatureMatcher(method);
}

/**
* Checks if the given method matches the method signature of this location.
*
Expand Down
Loading
Loading