File: VerifierTest.java

package info (click to toggle)
openjdk-11-jre-dcevm 11.0.12%2B7-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 221,052 kB
  • sloc: java: 1,360,115; cpp: 832,709; ansic: 209,376; xml: 62,473; sh: 8,122; asm: 3,279; python: 1,661; javascript: 942; makefile: 382; sed: 172; perl: 114
file content (359 lines) | stat: -rw-r--r-- 16,010 bytes parent folder | download | duplicates (7)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
/*
 * Copyright (c) 2014, 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.
 *
 */

import java.io.File;
import java.io.FileOutputStream;
import jdk.test.lib.process.OutputAnalyzer;
import java.nio.file.Files;

import java.util.*;
import jdk.internal.org.objectweb.asm.*;

/**
 * The testsets contained in this class are executed by ./VerifierTest_*.java, so that
 * individual testsets can be executed in parallel to shorten the total time required.
 */
public class VerifierTest implements Opcodes {
    // Test verification settings for dumping & runtime
    static final String VFY_ALL = "-Xverify:all";
    static final String VFY_REMOTE = "-Xverify:remote"; // default
    static final String VFY_NONE = "-Xverify:none";

    static final String ERR =
        "ERROR: class VerifierTestC was loaded unexpectedly";
    static final String MAP_FAIL =
        "shared archive file was created with less restrictive verification setting";
    static final String VFY_ERR = "java.lang.VerifyError";
    static final String PASS_RESULT = "Hi, how are you?";
    static final String VFY_INFO_MESSAGE =
        "All non-system classes will be verified (-Xverify:remote) during CDS dump time.";

    enum Testset1Part {
        A, B
    }

    public static void main(String[] args) throws Exception {
        String subCaseId = args[0];
        String jarName_verifier_test_tmp = "verifier_test_tmp" + "_" + subCaseId;
        String jarName_verifier_test = "verifier_test" + "_" + subCaseId;
        String jarName_greet = "greet" + "_" + subCaseId;
        String jarName_hi = "hi" + "_" + subCaseId;


        JarBuilder.build(jarName_verifier_test_tmp, "VerifierTest0", "VerifierTestA",
                         "VerifierTestB", "VerifierTestC", "VerifierTestD", "VerifierTestE",
                         "UnverifiableBase", "UnverifiableIntf", "UnverifiableIntfSub");
        JarBuilder.build(jarName_greet, "Greet");
        JarBuilder.build(jarName_hi, "Hi", "Hi$MyClass");

        File dir = new File(System.getProperty("test.classes", "."));
        File jarSrcFile = new File(dir, jarName_verifier_test_tmp + ".jar");
        File jarFile = new File(dir, jarName_verifier_test + ".jar");
        String jar = jarFile.getPath();

        if (!jarFile.exists() || jarFile.lastModified() < jarSrcFile.lastModified()) {
            createTestJarFile(jarSrcFile, jarFile);
        } else {
            System.out.println("Already up-to-date: " + jarFile);
        }

        String noAppClasses[] = TestCommon.list("");
        String appClasses[] = TestCommon.list("UnverifiableBase",
                                              "UnverifiableIntf",
                                              "UnverifiableIntfSub",
                                              "VerifierTestA",
                                              "VerifierTestB",
                                              "VerifierTestC",
                                              "VerifierTestD",
                                              "VerifierTestE",
                                              "VerifierTest0");


        switch (subCaseId) {
        case "0":         testset_0(jar, noAppClasses, appClasses);                 return;
        case "1A":        testset_1(jar, noAppClasses, appClasses, Testset1Part.A); return;
        case "1B":        testset_1(jar, noAppClasses, appClasses, Testset1Part.B); return;
        case "2":         testset_2(jarName_greet, jarName_hi);                   return;
        default:
            throw new RuntimeException("Unknown option: " + subCaseId);
        }
    }

    static void testset_0(String jar, String[] noAppClasses, String[] appClasses) throws Exception {
        // Dumping should fail if the IgnoreUnverifiableClassesDuringDump
        // option is not enabled.
        OutputAnalyzer output = TestCommon.dump(jar, appClasses,
                            "-XX:+UnlockDiagnosticVMOptions",
                            "-XX:-IgnoreUnverifiableClassesDuringDump");
        output.shouldContain("Please remove the unverifiable classes");
        output.shouldHaveExitValue(1);

        // By default, bad classes should be ignored during dumping.
        TestCommon.testDump(jar, appClasses);
    }

    static void checkRuntimeOutput(OutputAnalyzer output, String expected) throws Exception {
        output.shouldContain(expected);
        if (expected.equals(PASS_RESULT) ||
            expected.equals(VFY_ERR)) {
            output.shouldHaveExitValue(0);
        } else {
            output.shouldNotHaveExitValue(0);
        }
    }

    static void testset_1(String jar, String[] noAppClasses, String[] appClasses, Testset1Part part)
        throws Exception
    {
        String config[][] = {
            // {dump_list, dumptime_verification_setting,
            //  runtime_verification_setting, expected_output_str},

            // Dump app/ext with -Xverify:remote
            {"app",   VFY_REMOTE, VFY_REMOTE, VFY_ERR},
            {"app",   VFY_REMOTE, VFY_ALL,    MAP_FAIL},
            {"app",   VFY_REMOTE, VFY_NONE,   ERR },
            // Dump app/ext with -Xverify:all
            {"app",   VFY_ALL,    VFY_REMOTE, VFY_ERR },
            {"app",   VFY_ALL,    VFY_ALL,    VFY_ERR },
            {"app",   VFY_ALL,    VFY_NONE,   ERR },
            // Dump app/ext with -Xverify:none
            {"app",   VFY_NONE,   VFY_REMOTE, VFY_ERR},
            {"app",   VFY_NONE,   VFY_ALL,    MAP_FAIL},
            {"app",   VFY_NONE,   VFY_NONE,   ERR },
            // Dump sys only with -Xverify:remote
            {"noApp", VFY_REMOTE, VFY_REMOTE, VFY_ERR},
            {"noApp", VFY_REMOTE, VFY_ALL,    VFY_ERR},
            {"noApp", VFY_REMOTE, VFY_NONE,   ERR},
            // Dump sys only with -Xverify:all
            {"noApp", VFY_ALL, VFY_REMOTE,    VFY_ERR},
            {"noApp", VFY_ALL, VFY_ALL,       VFY_ERR},
            {"noApp", VFY_ALL, VFY_NONE,      ERR},
            // Dump sys only with -Xverify:none
            {"noApp", VFY_NONE, VFY_REMOTE,   VFY_ERR},
            {"noApp", VFY_NONE, VFY_ALL,      VFY_ERR},
            {"noApp", VFY_NONE, VFY_NONE,     ERR},
        };

        int loop_start, loop_stop;

        // Further break down testset_1 into two parts (to be invoked from VerifierTest_1A.java
        // and VerifierTest_1B.java) to improve parallel test execution time.
        switch (part) {
        case A:
            loop_start = 0;
            loop_stop  = 9;
            break;
        case B:
        default:
            assert part == Testset1Part.B;
            loop_start = 9;
            loop_stop  = config.length;
            break;
        }

        String prev_dump_setting = "";
        for (int i = loop_start; i < loop_stop; i ++) {
            String dump_list[] = config[i][0].equals("app") ? appClasses :
                noAppClasses;
            String dump_setting = config[i][1];
            String runtime_setting = config[i][2];
            String expected_output_str = config[i][3];
            System.out.println("Test case [" + i + "]: dumping " + config[i][0] +
                               " with " + dump_setting +
                               ", run with " + runtime_setting);
            if (!dump_setting.equals(prev_dump_setting)) {
                OutputAnalyzer dumpOutput = TestCommon.dump(
                                                            jar, dump_list, dump_setting,
                                                            // FIXME: the following options are for working around a GC
                                                            // issue - assert failure when dumping archive with the -Xverify:all
                                                            "-Xms256m",
                                                            "-Xmx256m");
                if (dump_setting.equals(VFY_NONE) &&
                    runtime_setting.equals(VFY_REMOTE)) {
                    dumpOutput.shouldContain(VFY_INFO_MESSAGE);
                }
            }
            TestCommon.run("-cp", jar,
                           runtime_setting,
                           "VerifierTest0")
                .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str));
            prev_dump_setting = dump_setting;
        }
    }

    static void testset_2(String jarName_greet, String jarName_hi) throws Exception {
        String appClasses[];
        String jar;

        // The following section is for testing the scenarios where
        // the classes are verifiable during dump time.
        appClasses = TestCommon.list("Hi",
                                     "Greet",
                                     "Hi$MyClass");
        jar = TestCommon.getTestJar(jarName_hi + ".jar") + File.pathSeparator +
            TestCommon.getTestJar(jarName_greet + ".jar");
        String config2[][] = {
            // {dump_list, dumptime_verification_setting,
            //  runtime_verification_setting, expected_output_str},

            // Dump app/ext with -Xverify:remote
            {"app",   VFY_REMOTE, VFY_REMOTE, PASS_RESULT},
            {"app",   VFY_REMOTE, VFY_ALL,    MAP_FAIL},
            {"app",   VFY_REMOTE, VFY_NONE,   PASS_RESULT },
            // Dump app/ext with -Xverify:all
            {"app",   VFY_ALL,    VFY_REMOTE, PASS_RESULT },
            {"app",   VFY_ALL,    VFY_ALL,    PASS_RESULT },
            {"app",   VFY_ALL,    VFY_NONE,   PASS_RESULT },
            // Dump app/ext with -Xverify:none
            {"app",   VFY_NONE,   VFY_REMOTE, PASS_RESULT},
            {"app",   VFY_NONE,   VFY_ALL,    MAP_FAIL},
            {"app",   VFY_NONE,   VFY_NONE,   PASS_RESULT },
        };
        String prev_dump_setting = "";
        for (int i = 0; i < config2.length; i ++) {
            // config2[i][0] is always set to "app" in this test
            String dump_setting = config2[i][1];
            String runtime_setting = config2[i][2];
            String expected_output_str = config2[i][3];
            System.out.println("Test case [" + i + "]: dumping " + config2[i][0] +
                               " with " + dump_setting +
                               ", run with " + runtime_setting);
            if (!dump_setting.equals(prev_dump_setting)) {
                OutputAnalyzer dumpOutput = TestCommon.dump(
                                                            jar, appClasses, dump_setting,
                                                            // FIXME: the following options are for working around a GC
                                                            // issue - assert failure when dumping archive with the -Xverify:all
                                                            "-Xms256m",
                                                            "-Xmx256m");
                if (dump_setting.equals(VFY_NONE) &&
                    runtime_setting.equals(VFY_REMOTE)) {
                    dumpOutput.shouldContain(VFY_INFO_MESSAGE);
                }
            }
            TestCommon.run("-cp", jar,
                           runtime_setting,
                           "Hi")
                .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str));
           prev_dump_setting = dump_setting;
        }
    }

    static void createTestJarFile(File jarSrcFile, File jarFile) throws Exception {
        jarFile.delete();
        Files.copy(jarSrcFile.toPath(), jarFile.toPath());

        File dir = new File(System.getProperty("test.classes", "."));
        File outdir = new File(dir, "verifier_test_classes");
        outdir.mkdir();

        writeClassFile(new File(outdir, "UnverifiableBase.class"), makeUnverifiableBase());
        writeClassFile(new File(outdir, "UnverifiableIntf.class"), makeUnverifiableIntf());

        JarBuilder.update(jarFile.getPath(), outdir.getPath());
    }

    static void writeClassFile(File file, byte bytecodes[]) throws Exception {
        try (FileOutputStream fos = new FileOutputStream(file)) {
            fos.write(bytecodes);
        }
    }

    // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableBase.class
    static byte[] makeUnverifiableBase() throws Exception {
        ClassWriter cw = new ClassWriter(0);
        FieldVisitor fv;
        MethodVisitor mv;
        AnnotationVisitor av0;

        cw.visit(V1_8, ACC_SUPER, "UnverifiableBase", null, "java/lang/Object", null);
        {
            fv = cw.visitField(ACC_FINAL + ACC_STATIC, "x", "LVerifierTest;", null, null);
            fv.visitEnd();
        }
        {
            mv = cw.visitMethod(0, "<init>", "()V", null, null);
            mv.visitCode();
            mv.visitVarInsn(ALOAD, 0);
            mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
            mv.visitInsn(RETURN);
            mv.visitMaxs(1, 1);
            mv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
            mv.visitCode();
            mv.visitTypeInsn(NEW, "VerifierTest0");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false);
            mv.visitFieldInsn(PUTSTATIC, "UnverifiableBase", "x", "LVerifierTest;");
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 0);
            mv.visitEnd();
        }
        addBadMethod(cw);
        cw.visitEnd();

        return cw.toByteArray();
    }

    // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableIntf.class
    static byte[] makeUnverifiableIntf() throws Exception {
        ClassWriter cw = new ClassWriter(0);
        FieldVisitor fv;
        MethodVisitor mv;
        AnnotationVisitor av0;

        cw.visit(V1_8, ACC_ABSTRACT + ACC_INTERFACE, "UnverifiableIntf", null, "java/lang/Object", null);

        {
            fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "x", "LVerifierTest0;", null, null);
            fv.visitEnd();
        }
        {
            mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
            mv.visitCode();
            mv.visitTypeInsn(NEW, "VerifierTest0");
            mv.visitInsn(DUP);
            mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false);
            mv.visitFieldInsn(PUTSTATIC, "UnverifiableIntf", "x", "LVerifierTest0;");
            mv.visitInsn(RETURN);
            mv.visitMaxs(2, 0);
            mv.visitEnd();
        }
        addBadMethod(cw);
        cw.visitEnd();

        return cw.toByteArray();
    }

    // Add a bad method to make the class fail verification.
    static void addBadMethod(ClassWriter cw) throws Exception {
        MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "bad", "()V", null, null);
        mv.visitCode();
        mv.visitInsn(ARETURN); //  java.lang.VerifyError: Operand stack underflow
        mv.visitMaxs(2, 2);
        mv.visitEnd();
    }
}