File: BadCanonicalCtrTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (291 lines) | stat: -rw-r--r-- 12,576 bytes parent folder | download | duplicates (6)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
 * Copyright (c) 2019, 2020, 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 8246774
 * @summary InvalidClassException is thrown when the canonical constructor
 *          cannot be found during deserialization.
 * @library /test/lib
 * @modules java.base/jdk.internal.org.objectweb.asm
 * @run testng BadCanonicalCtrTest
 */

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InvalidClassException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamClass;
import jdk.internal.org.objectweb.asm.ClassReader;
import jdk.internal.org.objectweb.asm.ClassVisitor;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.test.lib.compiler.InMemoryJavaCompiler;
import jdk.test.lib.ByteCodeLoader;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import static java.lang.System.out;
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_FRAMES;
import static jdk.internal.org.objectweb.asm.ClassWriter.COMPUTE_MAXS;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.expectThrows;

/**
 * Checks that an InvalidClassException is thrown when the canonical
 * constructor cannot be found during deserialization.
 */
public class BadCanonicalCtrTest {

    // ClassLoader for creating instances of the records to test with.
    ClassLoader goodRecordClassLoader;
    // ClassLoader that can be used during deserialization. Loads record
    // classes where the canonical constructor has been removed.
    ClassLoader missingCtrClassLoader;
    // ClassLoader that can be used during deserialization. Loads record
    // classes where the canonical constructor has been tampered with.
    ClassLoader nonCanonicalCtrClassLoader;

    /**
     * Generates the serializable record classes used by the test. First creates
     * the initial bytecode for the record classes using javac, then removes or
     * modifies the generated canonical constructor.
     */
    @BeforeTest
    public void setup() {
        {
            byte[] byteCode = InMemoryJavaCompiler.compile("R1",
                    "public record R1 () implements java.io.Serializable { }");
            goodRecordClassLoader = new ByteCodeLoader("R1", byteCode, BadCanonicalCtrTest.class.getClassLoader());
            byte[] bc1 = removeConstructor(byteCode);
            missingCtrClassLoader = new ByteCodeLoader("R1", bc1, BadCanonicalCtrTest.class.getClassLoader());
            byte[] bc2 = modifyConstructor(byteCode);
            nonCanonicalCtrClassLoader = new ByteCodeLoader("R1", bc2, BadCanonicalCtrTest.class.getClassLoader());
        }
        {
            byte[] byteCode = InMemoryJavaCompiler.compile("R2",
                    "public record R2 (int x, int y) implements java.io.Serializable { }");
            goodRecordClassLoader = new ByteCodeLoader("R2", byteCode, goodRecordClassLoader);
            byte[] bc1 = removeConstructor(byteCode);
            missingCtrClassLoader = new ByteCodeLoader("R2", bc1, missingCtrClassLoader);
            byte[] bc2 = modifyConstructor(byteCode);
            nonCanonicalCtrClassLoader = new ByteCodeLoader("R2", bc2, nonCanonicalCtrClassLoader);
        }
        {
            byte[] byteCode = InMemoryJavaCompiler.compile("R3",
                    "public record R3 (long l) implements java.io.Externalizable {" +
                    "    public void writeExternal(java.io.ObjectOutput out) { }" +
                    "    public void readExternal(java.io.ObjectInput in)    { } }");
            goodRecordClassLoader = new ByteCodeLoader("R3", byteCode, goodRecordClassLoader);
            byte[] bc1 = removeConstructor(byteCode);
            missingCtrClassLoader = new ByteCodeLoader("R3", bc1, missingCtrClassLoader);
            byte[] bc2 = modifyConstructor(byteCode);
            nonCanonicalCtrClassLoader = new ByteCodeLoader("R3", bc2, nonCanonicalCtrClassLoader);
        }
    }

    /** Constructs a new instance of record R1. */
    Object newR1() throws Exception {
        Class<?> c = Class.forName("R1", true, goodRecordClassLoader);
        assert c.isRecord();
        assert c.getRecordComponents() != null;
        return c.getConstructor().newInstance();
    }

    /** Constructs a new instance of record R2. */
    Object newR2(int x, int y) throws Exception{
        Class<?> c = Class.forName("R2", true, goodRecordClassLoader);
        assert c.isRecord();
        assert c.getRecordComponents().length == 2;
        return c.getConstructor(int.class, int.class).newInstance(x, y);
    }

    /** Constructs a new instance of record R3. */
    Object newR3(long l) throws Exception {
        Class<?> c = Class.forName("R3", true, goodRecordClassLoader);
        assert c.isRecord();
        assert c.getRecordComponents().length == 1;
        return c.getConstructor(long.class).newInstance(l);
    }

    @DataProvider(name = "recordInstances")
    public Object[][] recordInstances() throws Exception {
        return new Object[][] {
                new Object[] { newR1()        },
                new Object[] { newR2(19, 20)  },
                new Object[] { newR3(67L)     },
        };
    }

    static final Class<InvalidClassException> ICE = InvalidClassException.class;

    /**
     * Tests that InvalidClassException is thrown when no constructor is
     * present.
     */
    @Test(dataProvider = "recordInstances")
    public void missingConstructorTest(Object objToSerialize) throws Exception {
        out.println("\n---");
        out.println("serializing : " + objToSerialize);
        byte[] bytes = serialize(objToSerialize);
        out.println("deserializing");
        InvalidClassException ice = expectThrows(ICE, () -> deserialize(bytes, missingCtrClassLoader));
        out.println("caught expected ICE: " + ice);
        assertTrue(ice.getMessage().contains("record canonical constructor not found"));
    }

    /**
     * Tests that InvalidClassException is thrown when the canonical
     * constructor is not present. ( a non-canonical constructor is
     * present ).
     */
    @Test(dataProvider = "recordInstances")
    public void nonCanonicalConstructorTest(Object objToSerialize) throws Exception {
        out.println("\n---");
        out.println("serializing : " + objToSerialize);
        byte[] bytes = serialize(objToSerialize);
        out.println("deserializing");
        InvalidClassException ice = expectThrows(ICE, () -> deserialize(bytes, nonCanonicalCtrClassLoader));
        out.println("caught expected ICE: " + ice);
        assertTrue(ice.getMessage().contains("record canonical constructor not found"));
    }

    <T> byte[] serialize(T obj) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(obj);
        oos.close();
        return baos.toByteArray();
    }

    @SuppressWarnings("unchecked")
    <T> T deserialize(byte[] streamBytes, ClassLoader cl)
        throws IOException, ClassNotFoundException
    {
        ByteArrayInputStream bais = new ByteArrayInputStream(streamBytes);
        ObjectInputStream ois  = new ObjectInputStream(bais) {
            @Override
            protected Class<?> resolveClass(ObjectStreamClass desc)
                    throws ClassNotFoundException {
                return Class.forName(desc.getName(), false, cl);
            }
        };
        return (T) ois.readObject();
    }

    // -- machinery for augmenting record class bytes --

    /**
     * Removes the constructor from the given class bytes.
     * Assumes just a single, canonical, constructor.
     */
    static byte[] removeConstructor(byte[] classBytes) {
        ClassReader reader = new ClassReader(classBytes);
        ClassWriter writer = new ClassWriter(reader, COMPUTE_MAXS | COMPUTE_FRAMES);
        reader.accept(new RemoveCanonicalCtrVisitor(writer), 0);
        return writer.toByteArray();
    }

    /** Removes the <init> method. */
    static class RemoveCanonicalCtrVisitor extends ClassVisitor {
        static final String CTR_NAME = "<init>";
        RemoveCanonicalCtrVisitor(ClassVisitor cv) {
            super(ASM8, cv);
        }
        volatile boolean foundCanonicalCtr;
        @Override
        public MethodVisitor visitMethod(final int access,
                                         final String name,
                                         final String descriptor,
                                         final String signature,
                                         final String[] exceptions) {
            if (name.equals(CTR_NAME)) {  // assume just a single constructor
                assert foundCanonicalCtr == false;
                foundCanonicalCtr = true;
                return null;
            } else {
                return cv.visitMethod(access, name, descriptor, signature, exceptions);
            }
        }
    }

    /**
     * Modifies the descriptor of the constructor from the given class bytes.
     * Assumes just a single, canonical, constructor.
     */
    static byte[] modifyConstructor(byte[] classBytes) {
        ClassReader reader = new ClassReader(classBytes);
        ClassWriter writer = new ClassWriter(reader, COMPUTE_MAXS | COMPUTE_FRAMES);
        reader.accept(new ModifyCanonicalCtrVisitor(writer), 0);
        return writer.toByteArray();
    }

    /** Replaces whatever <init> method it finds with <init>(Ljava/lang/Object;)V. */
    static class ModifyCanonicalCtrVisitor extends ClassVisitor {
        ModifyCanonicalCtrVisitor(ClassVisitor cv) {
            super(ASM8, cv);
        }
        boolean foundCanonicalCtr;
        String className;
        @Override
        public void visit(final int version,
                          final int access,
                          final String name,
                          final String signature,
                          final String superName,
                          final String[] interfaces) {
            this.className = name;
            cv.visit(version, access, name, signature, superName, interfaces);
        }
        @Override
        public MethodVisitor visitMethod(final int access,
                                         final String name,
                                         final String descriptor,
                                         final String signature,
                                         final String[] exceptions) {
            if (name.equals("<init>")) {  // assume just a single constructor
                assert foundCanonicalCtr == false;
                foundCanonicalCtr = true;
                return null;
            } else {
                return cv.visitMethod(access, name, descriptor, signature, exceptions);
            }
        }
        @Override
        public void visitEnd() {
            // must have a signature that is not the same as the test record constructor
            MethodVisitor mv = cv.visitMethod(ACC_PUBLIC, "<init>", "(Ljava/lang/Object;)V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Record", "<init>", "()V", false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();

            cv.visitEnd();
        }
    }
}