23
24 import java.lang.classfile.ClassFile;
25 import jdk.test.lib.util.ForceGC;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.ValueSource;
29
30 import java.lang.constant.ClassDesc;
31 import java.lang.constant.MethodTypeDesc;
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandleProxies;
34 import java.lang.invoke.MethodHandles;
35 import java.lang.invoke.MethodType;
36 import java.lang.ref.WeakReference;
37 import java.util.Comparator;
38
39 import static java.lang.constant.ConstantDescs.*;
40 import static java.lang.invoke.MethodHandleProxies.*;
41 import static java.lang.invoke.MethodType.methodType;
42 import static java.lang.classfile.ClassFile.*;
43 import static org.junit.jupiter.api.Assertions.*;
44
45 /*
46 * @test
47 * @bug 6983726
48 * @library /test/lib
49 * @enablePreview
50 * @summary Tests on implementation hidden classes spinned by MethodHandleProxies
51 * @build WrapperHiddenClassTest Client jdk.test.lib.util.ForceGC
52 * @run junit WrapperHiddenClassTest
53 */
54 public class WrapperHiddenClassTest {
55
56 /**
57 * Tests an adversary "implementation" class will not be
58 * "recovered" by the wrapperInstance* APIs
59 */
60 @Test
61 public void testWrapperInstance() throws Throwable {
62 Comparator<Integer> hostile = createHostileInstance();
63 var mh = MethodHandles.publicLookup()
64 .findVirtual(Integer.class, "compareTo", methodType(int.class, Integer.class));
65 @SuppressWarnings("unchecked")
66 Comparator<Integer> proxy = (Comparator<Integer>) asInterfaceInstance(Comparator.class, mh);
67
68 assertTrue(isWrapperInstance(proxy));
69 assertFalse(isWrapperInstance(hostile));
70 assertSame(mh, wrapperInstanceTarget(proxy));
71 assertThrows(IllegalArgumentException.class, () -> wrapperInstanceTarget(hostile));
72 assertSame(Comparator.class, wrapperInstanceType(proxy));
73 assertThrows(IllegalArgumentException.class, () -> wrapperInstanceType(hostile));
74 }
75
76 private static final String TYPE = "interfaceType";
77 private static final String TARGET = "target";
78 private static final ClassDesc CD_HostileWrapper = ClassDesc.of("HostileWrapper");
79 private static final ClassDesc CD_Comparator = ClassDesc.of("java.util.Comparator");
80 private static final MethodTypeDesc MTD_int_Object_Object = MethodTypeDesc.of(CD_int, CD_Object, CD_Object);
81 private static final MethodTypeDesc MTD_int_Integer = MethodTypeDesc.of(CD_int, CD_Integer);
82
83 // Update this template when the MHP template is updated
84 @SuppressWarnings("unchecked")
85 private Comparator<Integer> createHostileInstance() throws Throwable {
86 var cf = ClassFile.of();
87 var bytes = cf.build(CD_HostileWrapper, clb -> {
88 clb.withSuperclass(CD_Object);
89 clb.withFlags(ACC_FINAL | ACC_SYNTHETIC);
90 clb.withInterfaceSymbols(CD_Comparator);
91
92 // static and instance fields
93 clb.withField(TYPE, CD_Class, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
94 clb.withField(TARGET, CD_MethodHandle, ACC_PRIVATE | ACC_FINAL);
95
96 // <clinit>
97 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, cob -> {
98 cob.loadConstant(CD_Comparator);
99 cob.putstatic(CD_HostileWrapper, TYPE, CD_Class);
100 cob.return_();
101 });
102
103 // <init>
104 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
105 cob.aload(0);
106 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
107 cob.return_();
108 });
109
|
23
24 import java.lang.classfile.ClassFile;
25 import jdk.test.lib.util.ForceGC;
26 import org.junit.jupiter.api.Test;
27 import org.junit.jupiter.params.ParameterizedTest;
28 import org.junit.jupiter.params.provider.ValueSource;
29
30 import java.lang.constant.ClassDesc;
31 import java.lang.constant.MethodTypeDesc;
32 import java.lang.invoke.MethodHandle;
33 import java.lang.invoke.MethodHandleProxies;
34 import java.lang.invoke.MethodHandles;
35 import java.lang.invoke.MethodType;
36 import java.lang.ref.WeakReference;
37 import java.util.Comparator;
38
39 import static java.lang.constant.ConstantDescs.*;
40 import static java.lang.invoke.MethodHandleProxies.*;
41 import static java.lang.invoke.MethodType.methodType;
42 import static java.lang.classfile.ClassFile.*;
43
44 import jdk.internal.misc.PreviewFeatures;
45
46 import static org.junit.jupiter.api.Assertions.*;
47
48 /*
49 * @test
50 * @bug 6983726
51 * @library /test/lib
52 * @modules java.base/jdk.internal.misc
53 * @enablePreview
54 * @summary Tests on implementation hidden classes spinned by MethodHandleProxies
55 * @build WrapperHiddenClassTest Client jdk.test.lib.util.ForceGC
56 * @run junit WrapperHiddenClassTest
57 */
58 public class WrapperHiddenClassTest {
59
60 /**
61 * Tests an adversary "implementation" class will not be
62 * "recovered" by the wrapperInstance* APIs
63 */
64 @Test
65 public void testWrapperInstance() throws Throwable {
66 Comparator<Integer> hostile = createHostileInstance();
67 var mh = MethodHandles.publicLookup()
68 .findVirtual(Integer.class, "compareTo", methodType(int.class, Integer.class));
69 @SuppressWarnings("unchecked")
70 Comparator<Integer> proxy = (Comparator<Integer>) asInterfaceInstance(Comparator.class, mh);
71
72 assertTrue(isWrapperInstance(proxy));
73 assertFalse(isWrapperInstance(hostile));
74 assertSame(mh, wrapperInstanceTarget(proxy));
75 assertThrows(IllegalArgumentException.class, () -> wrapperInstanceTarget(hostile));
76 assertSame(Comparator.class, wrapperInstanceType(proxy));
77 assertThrows(IllegalArgumentException.class, () -> wrapperInstanceType(hostile));
78 }
79
80 private static final String TYPE = "interfaceType";
81 private static final String TARGET = "target";
82 private static final ClassDesc CD_HostileWrapper = ClassDesc.of("HostileWrapper");
83 private static final ClassDesc CD_Comparator = ClassDesc.of("java.util.Comparator");
84 private static final MethodTypeDesc MTD_int_Object_Object = MethodTypeDesc.of(CD_int, CD_Object, CD_Object);
85 private static final MethodTypeDesc MTD_int_Integer = MethodTypeDesc.of(CD_int, CD_Integer);
86
87 // Update this template when the MHP template is updated
88 @SuppressWarnings("unchecked")
89 private Comparator<Integer> createHostileInstance() throws Throwable {
90 var cf = ClassFile.of();
91 var bytes = cf.build(CD_HostileWrapper, clb -> {
92 clb.withSuperclass(CD_Object);
93 clb.withFlags((PreviewFeatures.isEnabled() ? ACC_IDENTITY : 0) | ACC_FINAL | ACC_SYNTHETIC);
94 clb.withInterfaceSymbols(CD_Comparator);
95
96 // static and instance fields
97 clb.withField(TYPE, CD_Class, ACC_PRIVATE | ACC_STATIC | ACC_FINAL);
98 clb.withField(TARGET, CD_MethodHandle, ACC_PRIVATE | ACC_FINAL);
99
100 // <clinit>
101 clb.withMethodBody(CLASS_INIT_NAME, MTD_void, ACC_STATIC, cob -> {
102 cob.loadConstant(CD_Comparator);
103 cob.putstatic(CD_HostileWrapper, TYPE, CD_Class);
104 cob.return_();
105 });
106
107 // <init>
108 clb.withMethodBody(INIT_NAME, MTD_void, ACC_PUBLIC, cob -> {
109 cob.aload(0);
110 cob.invokespecial(CD_Object, INIT_NAME, MTD_void);
111 cob.return_();
112 });
113
|