Skip to content

Commit

Permalink
* Fixed issue with get/putObject not being native post Java 1
Browse files Browse the repository at this point in the history
  • Loading branch information
katherine-hough committed Nov 29, 2023
1 parent efd20e6 commit 0333b8b
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0333b8b

Please sign in to comment.