Skip to content

Commit

Permalink
* Continued working on a cleaner way to mask methods
Browse files Browse the repository at this point in the history
* Removed Java 8 PhosphorOption since that value should now only be necessary at runtime
  • Loading branch information
katherine-hough committed Nov 29, 2023
1 parent 405063e commit 91696df
Show file tree
Hide file tree
Showing 22 changed files with 1,270 additions and 1,854 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class Configuration {
public static ControlFlowManager controlFlowManager = new StandardControlFlowManager();
public static String controlFlowManagerPackage = null;
public static boolean QUIET_MODE = false;
// Option is set for Java 9+ JVM by the embedded configuration
public static boolean IS_JAVA_8 = true;
public static Set<String> ignoredMethods = new HashSet<>();
public static TaintTagFactory taintTagFactory = new DataAndControlFlowTagFactory();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,6 @@ private static String[] parseOptions(String agentArgs) {
}
}
}
if (Configuration.IS_JAVA_8) {
options.addLast("-java8");
}
return options.toArray(new String[0]);
}

Expand Down Expand Up @@ -172,7 +169,6 @@ public static boolean isUninstrumentedField(String owner, String name) {
}

public static boolean isUnsafeClass(String className) {
return (Configuration.IS_JAVA_8 && "sun/misc/Unsafe".equals(className))
|| (!Configuration.IS_JAVA_8 && "jdk/internal/misc/Unsafe".equals(className));
return "sun/misc/Unsafe".equals(className) || "jdk/internal/misc/Unsafe".equals(className);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,6 @@ public void configure(boolean forRuntimeInst, boolean isPresent, CommandLine com
}
}
},
JAVA_8(new PhosphorOptionBuilder(null, true, true).alternativeName("java8")) {
@Override
public void configure(boolean forRuntimeInst, boolean isPresent, CommandLine commandLine) {
Configuration.IS_JAVA_8 = isPresent;
}
},
JVM_MODULES(new PhosphorOptionBuilder("For Java 9+ JVM generation: list of Java modules to include in instrumented JVM",
true, false).argType(String.class).alternativeName("jvmModules")) {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,6 @@ public class InstrumentedJREProxyGenerator {

public static void main(String[] args) throws IOException {
String outputDir = args[0];
String pathToUnsafePropagator = outputDir
+ "/edu/columbia/cs/psl/phosphor/runtime/jdk/unsupported/RuntimeSunMiscUnsafePropagator.class";
byte[] buffer = InstrumentUtil.readAllBytes(new File(pathToUnsafePropagator));
byte[] instrumentedUnsafe =
PhosphorPatcher.apply(buffer, cv -> new UnsafePatchingCV(cv, "sun/misc/Unsafe", false));
Files.write(Paths.get(pathToUnsafePropagator), instrumentedUnsafe);
for (String clazz : CLASSES) {
String classLocation = outputDir + '/' + clazz.replace('.', '/') + ".class";
ClassReader cr = new ClassReader(InstrumentUtil.readAllBytes(new File(classLocation)));
Expand Down Expand Up @@ -75,26 +69,14 @@ public static void generateFieldHelper(GeneratorAdapter ga, String name, String
name = name.substring(6);
}
String fieldName = name.substring(3);
Type actualFieldType = null;
if (name.startsWith("get")) {
ga.visitVarInsn(Opcodes.ALOAD, 0);
ga.visitFieldInsn(
Opcodes.GETFIELD,
fieldOwner,
fieldName,
(actualFieldType != null ? actualFieldType.getDescriptor() : returnType.getDescriptor()));
ga.visitFieldInsn(Opcodes.GETFIELD, fieldOwner, fieldName, returnType.getDescriptor());
} else {
Type argType = Type.getArgumentTypes(descriptor)[1];
ga.visitVarInsn(Opcodes.ALOAD, 0);
ga.visitVarInsn(argType.getOpcode(Opcodes.ILOAD), 1);
if (actualFieldType != null) {
ga.visitTypeInsn(Opcodes.CHECKCAST, actualFieldType.getInternalName());
}
ga.visitFieldInsn(
Opcodes.PUTFIELD,
fieldOwner,
fieldName,
(actualFieldType != null ? actualFieldType.getDescriptor() : argType.getDescriptor()));
ga.visitFieldInsn(Opcodes.PUTFIELD, fieldOwner, fieldName, argType.getDescriptor());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ private static byte[] patch(String name, byte[] classFileBuffer) {
return apply(classFileBuffer, AsmPatchingCV::new);
} else if (MaskRegistryPatchingCV.isApplicable(name)) {
return apply(classFileBuffer, MaskRegistryPatchingCV::new);
} else if (UnsafeAdapterPatchingCV.isApplicable(name)) {
return apply(classFileBuffer, UnsafeAdapterPatchingCV::new);
}
return classFileBuffer;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package edu.columbia.cs.psl.phosphor.agent;

import edu.columbia.cs.psl.phosphor.Configuration;
import edu.columbia.cs.psl.phosphor.runtime.mask.JdkUnsafeAdapter;
import edu.columbia.cs.psl.phosphor.runtime.mask.SunUnsafeAdapter;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type;

import java.util.regex.Pattern;

public class UnsafeAdapterPatchingCV extends ClassVisitor {
private String className;

public UnsafeAdapterPatchingCV(ClassVisitor cv) {
super(Configuration.ASM_VERSION, cv);
}

@Override
public void visit(int version, int access, String name, String signature, String superName, String[] interfaces) {
super.visit(version, access, name, signature, superName, interfaces);
className = name;
}

@Override
public MethodVisitor visitMethod(
int access, String name, String descriptor, String signature, String[] exceptions) {
MethodVisitor mv = super.visitMethod(access, name, descriptor, signature, exceptions);
return new MethodVisitor(api, mv) {
@Override
public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) {
if (owner.equals(className) && opcode == Opcodes.INVOKESTATIC) {
Type[] args = Type.getArgumentTypes(descriptor);
owner = args[0].getInternalName();
descriptor = descriptor.replaceFirst(Pattern.quote(args[0].getDescriptor()), "");
super.visitMethodInsn(Opcodes.INVOKEVIRTUAL, owner, name, descriptor, false);
} else {
super.visitMethodInsn(opcode, owner, name, descriptor, isInterface);
}
}
};
}

public static boolean isApplicable(String className) {
return Type.getInternalName(SunUnsafeAdapter.class).equals(className)
|| Type.getInternalName(JdkUnsafeAdapter.class).equals(className);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package edu.columbia.cs.psl.phosphor.agent;

import edu.columbia.cs.psl.phosphor.Configuration;
import edu.columbia.cs.psl.phosphor.runtime.jdk.unsupported.UnsafeProxy;
import edu.columbia.cs.psl.phosphor.runtime.mask.UnsafeProxy;
import org.objectweb.asm.*;

class UnsafePatchingCV extends ClassVisitor {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import edu.columbia.cs.psl.phosphor.instrumenter.TaintMethodRecord;
import edu.columbia.cs.psl.phosphor.struct.harmony.util.HashMap;
import edu.columbia.cs.psl.phosphor.struct.harmony.util.Map;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

public final class MaskRegistry {
Expand Down Expand Up @@ -58,5 +59,9 @@ public MaskInfo(MethodRecordImpl record) {
public MethodRecordImpl getRecord() {
return record;
}

public void accept(MethodVisitor mv) {
record.delegateVisit(mv);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import edu.columbia.cs.psl.phosphor.Configuration;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;

import static edu.columbia.cs.psl.phosphor.instrumenter.TaintMethodRecord.IS_INSTANCE;

Expand All @@ -23,9 +22,7 @@ public void visitMethodInsn(int opcode, String owner, String name, String desc,
// Even if we are not masking other methods, this must be masked
IS_INSTANCE.delegateVisit(mv);
} else if (owner.equals("sun/misc/Unsafe") && shouldMask(name)) {
owner = ReflectionMVFactory.getRuntimeUnsafePropagatorClassName();
super.visitMethodInsn(
Opcodes.INVOKESTATIC, owner, name, "(Lsun/misc/Unsafe;" + desc.substring(1), isInterface);
MaskRegistry.getMask(owner, name, desc).accept(mv);
} else {
super.visitMethodInsn(opcode, owner, name, desc, isInterface);
}
Expand Down
Loading

0 comments on commit 91696df

Please sign in to comment.