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

Reorganized classes #208

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
Expand Up @@ -21,6 +21,9 @@ public class BasicSourceSinkManager extends SourceSinkManager {
private static final Map<String, Set<String>> taintThrough = new HashMap<>();
// Maps class names to sets of class instances
private static final Map<String, Set<Class<?>>> classMap = new HashMap<>();
public static InputStream sourcesFile;
public static InputStream sinksFile;
public static InputStream taintThroughFile;
// Maps class names to a set of all methods listed as sources for the class or one of its supertypes or superinterfaces
private static Map<String, Set<String>> inheritedSources = new HashMap<>();
// Maps class names to a set of all methods listed as sinks for the class or one of its supertypes or superinterfaces
Expand Down Expand Up @@ -109,9 +112,9 @@ public String getBaseSink(String str) {

/* Reads source, sink and taintThrough methods from their files into their respective maps. */
public static synchronized void loadTaintMethods() {
readTaintMethods(Instrumenter.sourcesFile, AutoTaint.SOURCE);
readTaintMethods(Instrumenter.sinksFile, AutoTaint.SINK);
readTaintMethods(Instrumenter.taintThroughFile, AutoTaint.TAINT_THROUGH);
readTaintMethods(sourcesFile, AutoTaint.SOURCE);
readTaintMethods(sinksFile, AutoTaint.SINK);
readTaintMethods(taintThroughFile, AutoTaint.TAINT_THROUGH);
}

/* Provides access to the single instance of BasicSourceSinkManager */
Expand Down Expand Up @@ -263,7 +266,7 @@ private static synchronized Set<String> getAutoTaintMethods(String className, Ma
// Add any methods from this class that are directly listed as auto taint methods
set.addAll(baseMethods.get(className));
}
ClassNode cn = Instrumenter.getClassNode(className);
ClassNode cn = ClassNodeCache.getClassNode(className);
if (cn != null) {
if (cn.interfaces != null) {
// Add all auto taint methods from interfaces implemented by this class
Expand Down Expand Up @@ -295,7 +298,7 @@ private static synchronized String findSuperTypeAutoTaintProvider(String classNa
if (baseMethods.containsKey(curClassName) && baseMethods.get(curClassName).contains(methodName)) {
return curClassName;
}
ClassNode cn = Instrumenter.getClassNode(curClassName);
ClassNode cn = ClassNodeCache.getClassNode(curClassName);
if (cn != null) {
if (cn.interfaces != null) {
// Enqueue interfaces implemented by the current class
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package edu.columbia.cs.psl.phosphor;

import edu.columbia.cs.psl.phosphor.struct.harmony.util.Collections;
import edu.columbia.cs.psl.phosphor.struct.harmony.util.HashMap;
import edu.columbia.cs.psl.phosphor.struct.harmony.util.Map;
import org.objectweb.asm.ClassReader;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.tree.ClassNode;
import org.objectweb.asm.tree.MethodNode;

import java.io.InputStream;

public final class ClassNodeCache {
public static Map<String, ClassNode> classes = Collections.synchronizedMap(new HashMap<>());

static {
classes.putAll(ClassSupertypeReadingTransformer.classNodes);
ClassSupertypeReadingTransformer.classNodes = null;
}

private ClassNodeCache() {
throw new AssertionError("Tried to instantiate static utility class: " + getClass());
}

/* Returns the class node associated with the specified class name or null if none exists and a new one could not
* successfully be created for the class name. */
public static ClassNode getClassNode(String className) {
ClassNode cn = classes.get(className);
if (cn == null) {
// Class was loaded before ClassSupertypeReadingTransformer was added
return tryToAddClassNode(className);
} else {
return cn;
}
}

/* Attempts to create a ClassNode populated with supertype information for this class. */
private static ClassNode tryToAddClassNode(String className) {
try (InputStream is = ClassLoader.getSystemResourceAsStream(className + ".class")) {
if (is == null) {
return null;
}
ClassReader cr = new ClassReader(is);
cr.accept(
new ClassVisitor(Configuration.ASM_VERSION) {
private ClassNode cn;

@Override
public void visit(
int version,
int access,
String name,
String signature,
String superName,
String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
cn = new ClassNode();
cn.name = name;
cn.superName = superName;
cn.interfaces = new java.util.ArrayList<>(java.util.Arrays.asList(interfaces));
cn.methods = new java.util.LinkedList<>();
classes.put(name, cn);
}

@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
cn.methods.add(new MethodNode(access, name, descriptor, signature, exceptions));
return super.visitMethod(access, name, descriptor, signature, exceptions);
}
},
ClassReader.SKIP_CODE);
return classes.get(className);
} catch (Exception e) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void visit(int version, int access, String name, String signature, String
cn.superName = superName;
cn.interfaces = new ArrayList<>(Arrays.asList(interfaces));
if(classNodes == null) {
Instrumenter.classes.put(name, cn);
ClassNodeCache.classes.put(name, cn);
} else {
classNodes.put(name, cn);
}
Expand Down
193 changes: 0 additions & 193 deletions Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/Instrumenter.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public byte[] transform(ClassLoader loader, final String className2, Class<?> cl
ClassReader cr = (Configuration.READ_AND_SAVE_BCI ? new OffsetPreservingClassReader(classfileBuffer)
: new ClassReader(classfileBuffer));
String className = cr.getClassName();
if (Instrumenter.isIgnoredClass(className)) {
if (Phosphor.isIgnoredClass(className)) {
switch (className) {
case "java/lang/Boolean":
case "java/lang/Byte":
Expand Down
Loading