diff --git a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/EmbeddedPhosphorPatcher.java b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/EmbeddedPhosphorPatcher.java index 0ad58e9c2..8fd8cc995 100644 --- a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/EmbeddedPhosphorPatcher.java +++ b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/EmbeddedPhosphorPatcher.java @@ -19,8 +19,9 @@ public byte[] patch(String name, byte[] content) { if (ConfigurationEmbeddingCV.isApplicable(name)) { return PhosphorPatcher.apply(content, ConfigurationEmbeddingCV::new); } else if (name.equals("edu/columbia/cs/psl/phosphor/runtime/RuntimeJDKInternalUnsafePropagator")) { - return PhosphorPatcher.apply( - content, cv -> new UnsafePatchingCV(cv, "jdk/internal/misc/Unsafe", patchUnsafeNames)); + return PhosphorPatcher.apply(content, cv -> new UnsafePatchingCV(cv, patchUnsafeNames)); + } else if (JdkUnsafeAdapterPatchingCV.isApplicable(name, patchUnsafeNames)) { + return PhosphorPatcher.apply(content, JdkUnsafeAdapterPatchingCV::new); } else { return content; } diff --git a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/JdkUnsafeAdapterPatchingCV.java b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/JdkUnsafeAdapterPatchingCV.java new file mode 100644 index 000000000..ec66f94a9 --- /dev/null +++ b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/JdkUnsafeAdapterPatchingCV.java @@ -0,0 +1,34 @@ +package edu.columbia.cs.psl.phosphor.agent; + +import edu.columbia.cs.psl.phosphor.Configuration; +import edu.columbia.cs.psl.phosphor.runtime.mask.JdkUnsafeAdapter; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Type; + +public class JdkUnsafeAdapterPatchingCV extends ClassVisitor { + private final String UNSAFE_INTERNAL_NAME = "jdk/internal/misc/Unsafe"; + + public JdkUnsafeAdapterPatchingCV(ClassVisitor cv) { + super(Configuration.ASM_VERSION, cv); + } + + public static boolean isApplicable(String className, boolean patchUnsafeNames) { + return Type.getInternalName(JdkUnsafeAdapter.class).equals(className) && !patchUnsafeNames; + } + + @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 (UNSAFE_INTERNAL_NAME.equals(owner)) { + name = name.replace("Object", "Reference"); + } + super.visitMethodInsn(opcode, owner, name, descriptor, isInterface); + } + }; + } +} diff --git a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/UnsafePatchingCV.java b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/UnsafePatchingCV.java index b9b5f4b11..e4ad8e622 100644 --- a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/UnsafePatchingCV.java +++ b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/agent/UnsafePatchingCV.java @@ -2,34 +2,33 @@ import edu.columbia.cs.psl.phosphor.Configuration; import edu.columbia.cs.psl.phosphor.runtime.mask.UnsafeProxy; -import org.objectweb.asm.*; +import org.objectweb.asm.ClassVisitor; +import org.objectweb.asm.Label; +import org.objectweb.asm.MethodVisitor; +import org.objectweb.asm.Type; class UnsafePatchingCV extends ClassVisitor { private static final String UNSAFE_PROXY_INTERNAL_NAME = Type.getInternalName(UnsafeProxy.class); private static final String UNSAFE_PROXY_DESC = Type.getDescriptor(UnsafeProxy.class); - private final String unsafeDesc; private final boolean patchNames; - private final String unsafeInternalName; + private final String UNSAFE_INTERNAL_NAME = "jdk/internal/misc/Unsafe"; - public UnsafePatchingCV(ClassVisitor cv, String unsafeInternalName, boolean patchNames) { + public UnsafePatchingCV(ClassVisitor cv, boolean patchNames) { super(Configuration.ASM_VERSION, cv); - this.unsafeInternalName = unsafeInternalName; - this.unsafeDesc = "L" + unsafeInternalName + ";"; this.patchNames = patchNames; } private String patchInternalName(String name) { - return UNSAFE_PROXY_INTERNAL_NAME.equals(name) ? unsafeInternalName : name; + return UNSAFE_PROXY_INTERNAL_NAME.equals(name) ? UNSAFE_INTERNAL_NAME : name; } private String patchDesc(String desc) { - return desc == null ? null : - desc.replace(UNSAFE_PROXY_DESC, unsafeDesc); + String UNSAFE_DESCRIPTOR = "L" + UNSAFE_INTERNAL_NAME + ";"; + return desc == null ? null : desc.replace(UNSAFE_PROXY_DESC, UNSAFE_DESCRIPTOR); } private String patchMethodName(String owner, String name) { - return patchNames && owner.equals(UNSAFE_PROXY_INTERNAL_NAME) ? - name.replace("Reference", "Object") : name; + return patchNames && owner.equals(UNSAFE_PROXY_INTERNAL_NAME) ? name.replace("Reference", "Object") : name; } @Override @@ -58,8 +57,7 @@ public void visitFieldInsn(int opcode, String owner, String name, String descrip } @Override - public void visitMethodInsn( - int opcode, String owner, String name, String descriptor, boolean isInterface) { + public void visitMethodInsn(int opcode, String owner, String name, String descriptor, boolean isInterface) { super.visitMethodInsn( opcode, patchInternalName(owner), diff --git a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/runtime/mask/JdkUnsafeAdapter.java b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/runtime/mask/JdkUnsafeAdapter.java index b9b37db0e..fb25d87f1 100644 --- a/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/runtime/mask/JdkUnsafeAdapter.java +++ b/Phosphor/src/main/java/edu/columbia/cs/psl/phosphor/runtime/mask/JdkUnsafeAdapter.java @@ -5,10 +5,6 @@ import java.lang.reflect.Field; import java.security.ProtectionDomain; -/** - * Note that the various get/put Object methods are deprecated but present in Java 21. - * If they are removed in future Java versions, we will need to patch these when packing the specific Java installation. - */ @SuppressWarnings("unused") public class JdkUnsafeAdapter implements UnsafeAdapter { private final Unsafe unsafe = Unsafe.getUnsafe();