File: UnsafeDefMeths.java

package info (click to toggle)
openjdk-11 11.0.29%2B7-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 781,596 kB
  • sloc: java: 5,209,028; xml: 1,192,267; cpp: 1,143,585; ansic: 462,354; javascript: 162,416; sh: 16,740; objc: 13,730; python: 4,757; asm: 3,570; makefile: 2,969; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (185 lines) | stat: -rw-r--r-- 6,670 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/*
 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
 */

/*
 * @test
 * @bug 8200261
 * @summary Tests an anonymous class that implements interfaces with default methods.
 * @library /testlibrary
 * @modules java.base/jdk.internal.org.objectweb.asm
 *          java.management
 * @compile -XDignore.symbol.file=true UnsafeDefMeths.java
 * @run main UnsafeDefMeths
 */

import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.internal.org.objectweb.asm.Type;
import sun.misc.Unsafe;

import java.lang.invoke.MethodType;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PRIVATE;
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_PUBLIC;
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_SUPER;
import static jdk.internal.org.objectweb.asm.Opcodes.ALOAD;
import static jdk.internal.org.objectweb.asm.Opcodes.ARETURN;
import static jdk.internal.org.objectweb.asm.Opcodes.DUP;
import static jdk.internal.org.objectweb.asm.Opcodes.GETFIELD;
import static jdk.internal.org.objectweb.asm.Opcodes.INVOKESPECIAL;
import static jdk.internal.org.objectweb.asm.Opcodes.PUTFIELD;
import static jdk.internal.org.objectweb.asm.Opcodes.RETURN;
import static jdk.internal.org.objectweb.asm.Opcodes.V1_8;

public class UnsafeDefMeths {

    static final Unsafe UNSAFE;

    static {
        try {
            Field unsafeField = Unsafe.class.getDeclaredField("theUnsafe");
            unsafeField.setAccessible(true);
            UNSAFE = (Unsafe) unsafeField.get(null);
        }
        catch (Exception e) {
            throw new InternalError(e);
        }
    }

    interface Resource {
        Pointer ptr();
    }

    interface Struct extends Resource {
       StructPointer ptr();
    }

    interface Pointer { }

    interface StructPointer extends Pointer { }

    interface I extends Struct {
        void m();
    }

    static String IMPL_PREFIX = "$$impl";
    static String PTR_FIELD_NAME = "ptr";

    public static void main(String[] args) throws Throwable {
        byte[] bytes = new UnsafeDefMeths().generate(I.class);
        Class<?> cl = UNSAFE.defineAnonymousClass(I.class, bytes, new Object[0]);
        I i = (I)cl.getConstructors()[0].newInstance(new Object[] { null }); //exception here!
    }

    // Generate a class similar to:
    //
    // public class UnsafeDefMeths$I$$impl implements UnsafeDefMeths$I, UnsafeDefMeths$Struct {
    //
    //     public UnsafeDefMeths$StructPointer ptr;
    //
    //     public UnsafeDefMeths$I$$impl(UnsafeDefMeths$StructPointer p) {
    //         ptr = p;
    //     }
    //
    //     public UnsafeDefMeths$StructPointer ptr() {
    //         return ptr;
    //     }
    // }
    //
    byte[] generate(Class<?> iface) {
        ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS);

        String ifaceTypeName = Type.getInternalName(iface);
        String proxyClassName = ifaceTypeName + IMPL_PREFIX;
        // class definition
        cw.visit(V1_8, ACC_PUBLIC + ACC_SUPER, proxyClassName,
                desc(Object.class) + desc(ifaceTypeName) + desc(Struct.class),
                name(Object.class),
                new String[] { ifaceTypeName, name(Struct.class) });

        cw.visitField(ACC_PUBLIC, PTR_FIELD_NAME, desc(StructPointer.class), desc(StructPointer.class), null);
        cw.visitEnd();

        // constructor
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "<init>",
                meth(desc(void.class), desc(StructPointer.class)),
                meth(desc(void.class), desc(StructPointer.class)), null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitInsn(DUP);
        mv.visitMethodInsn(INVOKESPECIAL, name(Object.class), "<init>", meth(desc(void.class)), false);
        mv.visitVarInsn(ALOAD, 1);
        // Execution of this PUTFIELD instruction causes the bug's ClassNotFoundException.
        mv.visitFieldInsn(PUTFIELD, proxyClassName, PTR_FIELD_NAME, desc(StructPointer.class));
        mv.visitInsn(RETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        // ptr() impl
        mv = cw.visitMethod(ACC_PUBLIC, PTR_FIELD_NAME, meth(desc(StructPointer.class)),
                meth(desc(StructPointer.class)), null);
        mv.visitCode();
        mv.visitVarInsn(ALOAD, 0);
        mv.visitFieldInsn(GETFIELD, proxyClassName, PTR_FIELD_NAME, desc(StructPointer.class));
        mv.visitInsn(ARETURN);
        mv.visitMaxs(0, 0);
        mv.visitEnd();

        return cw.toByteArray();
    }

    String name(Class<?> clazz) {
        if (clazz.isPrimitive()) {
            throw new IllegalStateException();
        } else if (clazz.isArray()) {
            return desc(clazz);
        } else {
            return clazz.getName().replaceAll("\\.", "/");
        }
    }

    String desc(Class<?> clazz) {
        String mdesc = MethodType.methodType(clazz).toMethodDescriptorString();
        return mdesc.substring(mdesc.indexOf(')') + 1);
    }

    String desc(String clazzName) {
        return "L" + clazzName + ";";
    }

    String gen(String clazz, String... typeargs) {
        return clazz.substring(0, clazz.length() - 1) + Stream.of(typeargs).collect(Collectors.joining("", "<", ">")) + ";";
    }

    String meth(String restype, String... argtypes) {
        return Stream.of(argtypes).collect(Collectors.joining("", "(", ")")) + restype;
    }

    String meth(Method m) {
        return MethodType.methodType(m.getReturnType(), m.getParameterTypes()).toMethodDescriptorString();
    }
}