1 /*
   2  * Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 import java.io.File;
  26 import java.io.FileOutputStream;
  27 import jdk.test.lib.process.OutputAnalyzer;
  28 import java.nio.file.Files;
  29 
  30 import java.util.*;
  31 import jdk.internal.org.objectweb.asm.*;
  32 
  33 /**
  34  * The testsets contained in this class are executed by ./VerifierTest_*.java, so that
  35  * individual testsets can be executed in parallel to shorten the total time required.
  36  */
  37 public class VerifierTest implements Opcodes {
  38     // Test verification settings for dumping & runtime
  39     static final String VFY_ALL = "-Xverify:all";
  40     static final String VFY_REMOTE = "-Xverify:remote"; // default
  41     static final String VFY_NONE = "-Xverify:none";
  42 
  43     static final String ERR =
  44         "ERROR: class VerifierTestC was loaded unexpectedly";
  45     static final String MAP_FAIL =
  46         "shared archive file was created with less restrictive verification setting";
  47     static final String VFY_ERR = "java.lang.VerifyError";
  48     static final String PASS_RESULT = "Hi, how are you?";
  49     static final String VFY_INFO_MESSAGE =
  50         "All non-system classes will be verified (-Xverify:remote) during CDS dump time.";
  51 
  52     enum Testset1Part {
  53         A, B
  54     }
  55 
  56     public static void main(String[] args) throws Exception {
  57         String subCaseId = args[0];
  58         String jarName_verifier_test_tmp = "verifier_test_tmp" + "_" + subCaseId;
  59         String jarName_verifier_test = "verifier_test" + "_" + subCaseId;
  60         String jarName_greet = "greet" + "_" + subCaseId;
  61         String jarName_hi = "hi" + "_" + subCaseId;
  62 
  63 
  64         JarBuilder.build(jarName_verifier_test_tmp, "VerifierTest0", "VerifierTestA",
  65                          "VerifierTestB", "VerifierTestC", "VerifierTestD", "VerifierTestE",
  66                          "UnverifiableBase", "UnverifiableIntf", "UnverifiableIntfSub");
  67         JarBuilder.build(jarName_greet, "Greet");
  68         JarBuilder.build(jarName_hi, "Hi", "Hi$MyClass");
  69 
  70         File dir = new File(System.getProperty("test.classes", "."));
  71         File jarSrcFile = new File(dir, jarName_verifier_test_tmp + ".jar");
  72         File jarFile = new File(dir, jarName_verifier_test + ".jar");
  73         String jar = jarFile.getPath();
  74 
  75         if (!jarFile.exists() || jarFile.lastModified() < jarSrcFile.lastModified()) {
  76             createTestJarFile(jarSrcFile, jarFile);
  77         } else {
  78             System.out.println("Already up-to-date: " + jarFile);
  79         }
  80 
  81         String noAppClasses[] = TestCommon.list("");
  82         String appClasses[] = TestCommon.list("UnverifiableBase",
  83                                               "UnverifiableIntf",
  84                                               "UnverifiableIntfSub",
  85                                               "VerifierTestA",
  86                                               "VerifierTestB",
  87                                               "VerifierTestC",
  88                                               "VerifierTestD",
  89                                               "VerifierTestE",
  90                                               "VerifierTest0");
  91 
  92 
  93         switch (subCaseId) {
  94         case "0":         testset_0(jar, noAppClasses, appClasses);                 return;
  95         case "1A":        testset_1(jar, noAppClasses, appClasses, Testset1Part.A); return;
  96         case "1B":        testset_1(jar, noAppClasses, appClasses, Testset1Part.B); return;
  97         case "2":         testset_2(jarName_greet, jarName_hi);                   return;
  98         default:
  99             throw new RuntimeException("Unknown option: " + subCaseId);
 100         }
 101     }
 102 
 103     static void testset_0(String jar, String[] noAppClasses, String[] appClasses) throws Exception {
 104         // Dumping should fail if the IgnoreUnverifiableClassesDuringDump
 105         // option is not enabled.
 106         OutputAnalyzer output = TestCommon.dump(jar, appClasses,
 107                             "-XX:+UnlockDiagnosticVMOptions",
 108                             "-XX:-IgnoreUnverifiableClassesDuringDump");
 109         output.shouldContain("Please remove the unverifiable classes");
 110         output.shouldHaveExitValue(1);
 111 
 112         // By default, bad classes should be ignored during dumping.
 113         TestCommon.testDump(jar, appClasses);
 114     }
 115 
 116     static void checkRuntimeOutput(OutputAnalyzer output, String expected) throws Exception {
 117         output.shouldContain(expected);
 118         if (expected.equals(PASS_RESULT) ||
 119             expected.equals(VFY_ERR)) {
 120             output.shouldHaveExitValue(0);
 121         } else {
 122             output.shouldNotHaveExitValue(0);
 123         }
 124     }
 125 
 126     static void testset_1(String jar, String[] noAppClasses, String[] appClasses, Testset1Part part)
 127         throws Exception
 128     {
 129         String config[][] = {
 130             // {dump_list, dumptime_verification_setting,
 131             //  runtime_verification_setting, expected_output_str},
 132 
 133             // Dump app/ext with -Xverify:remote
 134             {"app",   VFY_REMOTE, VFY_REMOTE, VFY_ERR},
 135             {"app",   VFY_REMOTE, VFY_ALL,    MAP_FAIL},
 136             {"app",   VFY_REMOTE, VFY_NONE,   ERR },
 137             // Dump app/ext with -Xverify:all
 138             {"app",   VFY_ALL,    VFY_REMOTE, VFY_ERR },
 139             {"app",   VFY_ALL,    VFY_ALL,    VFY_ERR },
 140             {"app",   VFY_ALL,    VFY_NONE,   ERR },
 141             // Dump app/ext with -Xverify:none
 142             {"app",   VFY_NONE,   VFY_REMOTE, VFY_ERR},
 143             {"app",   VFY_NONE,   VFY_ALL,    MAP_FAIL},
 144             {"app",   VFY_NONE,   VFY_NONE,   ERR },
 145             // Dump sys only with -Xverify:remote
 146             {"noApp", VFY_REMOTE, VFY_REMOTE, VFY_ERR},
 147             {"noApp", VFY_REMOTE, VFY_ALL,    VFY_ERR},
 148             {"noApp", VFY_REMOTE, VFY_NONE,   ERR},
 149             // Dump sys only with -Xverify:all
 150             {"noApp", VFY_ALL, VFY_REMOTE,    VFY_ERR},
 151             {"noApp", VFY_ALL, VFY_ALL,       VFY_ERR},
 152             {"noApp", VFY_ALL, VFY_NONE,      ERR},
 153             // Dump sys only with -Xverify:none
 154             {"noApp", VFY_NONE, VFY_REMOTE,   VFY_ERR},
 155             {"noApp", VFY_NONE, VFY_ALL,      VFY_ERR},
 156             {"noApp", VFY_NONE, VFY_NONE,     ERR},
 157         };
 158 
 159         int loop_start, loop_stop;
 160 
 161         // Further break down testset_1 into two parts (to be invoked from VerifierTest_1A.java
 162         // and VerifierTest_1B.java) to improve parallel test execution time.
 163         switch (part) {
 164         case A:
 165             loop_start = 0;
 166             loop_stop  = 9;
 167             break;
 168         case B:
 169         default:
 170             assert part == Testset1Part.B;
 171             loop_start = 9;
 172             loop_stop  = config.length;
 173             break;
 174         }
 175 
 176         String prev_dump_setting = "";
 177         for (int i = loop_start; i < loop_stop; i ++) {
 178             String dump_list[] = config[i][0].equals("app") ? appClasses :
 179                 noAppClasses;
 180             String dump_setting = config[i][1];
 181             String runtime_setting = config[i][2];
 182             String expected_output_str = config[i][3];
 183             System.out.println("Test case [" + i + "]: dumping " + config[i][0] +
 184                                " with " + dump_setting +
 185                                ", run with " + runtime_setting);
 186             if (!dump_setting.equals(prev_dump_setting)) {
 187                 OutputAnalyzer dumpOutput = TestCommon.dump(
 188                                                             jar, dump_list, dump_setting,
 189                                                             // FIXME: the following options are for working around a GC
 190                                                             // issue - assert failure when dumping archive with the -Xverify:all
 191                                                             "-Xms256m",
 192                                                             "-Xmx256m");
 193                 if (dump_setting.equals(VFY_NONE) &&
 194                     runtime_setting.equals(VFY_REMOTE)) {
 195                     dumpOutput.shouldContain(VFY_INFO_MESSAGE);
 196                 }
 197             }
 198             TestCommon.run("-cp", jar,
 199                            runtime_setting,
 200                            "VerifierTest0")
 201                 .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str));
 202             prev_dump_setting = dump_setting;
 203         }
 204     }
 205 
 206     static void testset_2(String jarName_greet, String jarName_hi) throws Exception {
 207         String appClasses[];
 208         String jar;
 209 
 210         // The following section is for testing the scenarios where
 211         // the classes are verifiable during dump time.
 212         appClasses = TestCommon.list("Hi",
 213                                      "Greet",
 214                                      "Hi$MyClass");
 215         jar = TestCommon.getTestJar(jarName_hi + ".jar") + File.pathSeparator +
 216             TestCommon.getTestJar(jarName_greet + ".jar");
 217         String config2[][] = {
 218             // {dump_list, dumptime_verification_setting,
 219             //  runtime_verification_setting, expected_output_str},
 220 
 221             // Dump app/ext with -Xverify:remote
 222             {"app",   VFY_REMOTE, VFY_REMOTE, PASS_RESULT},
 223             {"app",   VFY_REMOTE, VFY_ALL,    MAP_FAIL},
 224             {"app",   VFY_REMOTE, VFY_NONE,   PASS_RESULT },
 225             // Dump app/ext with -Xverify:all
 226             {"app",   VFY_ALL,    VFY_REMOTE, PASS_RESULT },
 227             {"app",   VFY_ALL,    VFY_ALL,    PASS_RESULT },
 228             {"app",   VFY_ALL,    VFY_NONE,   PASS_RESULT },
 229             // Dump app/ext with -Xverify:none
 230             {"app",   VFY_NONE,   VFY_REMOTE, PASS_RESULT},
 231             {"app",   VFY_NONE,   VFY_ALL,    MAP_FAIL},
 232             {"app",   VFY_NONE,   VFY_NONE,   PASS_RESULT },
 233         };
 234         String prev_dump_setting = "";
 235         for (int i = 0; i < config2.length; i ++) {
 236             // config2[i][0] is always set to "app" in this test
 237             String dump_setting = config2[i][1];
 238             String runtime_setting = config2[i][2];
 239             String expected_output_str = config2[i][3];
 240             System.out.println("Test case [" + i + "]: dumping " + config2[i][0] +
 241                                " with " + dump_setting +
 242                                ", run with " + runtime_setting);
 243             if (!dump_setting.equals(prev_dump_setting)) {
 244                 OutputAnalyzer dumpOutput = TestCommon.dump(
 245                                                             jar, appClasses, dump_setting,
 246                                                             // FIXME: the following options are for working around a GC
 247                                                             // issue - assert failure when dumping archive with the -Xverify:all
 248                                                             "-Xms256m",
 249                                                             "-Xmx256m");
 250                 if (dump_setting.equals(VFY_NONE) &&
 251                     runtime_setting.equals(VFY_REMOTE)) {
 252                     dumpOutput.shouldContain(VFY_INFO_MESSAGE);
 253                 }
 254             }
 255             TestCommon.run("-cp", jar,
 256                            runtime_setting,
 257                            "Hi")
 258                 .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str));
 259            prev_dump_setting = dump_setting;
 260         }
 261     }
 262 
 263     static void createTestJarFile(File jarSrcFile, File jarFile) throws Exception {
 264         jarFile.delete();
 265         Files.copy(jarSrcFile.toPath(), jarFile.toPath());
 266 
 267         File dir = new File(System.getProperty("test.classes", "."));
 268         File outdir = new File(dir, "verifier_test_classes");
 269         outdir.mkdir();
 270 
 271         writeClassFile(new File(outdir, "UnverifiableBase.class"), makeUnverifiableBase());
 272         writeClassFile(new File(outdir, "UnverifiableIntf.class"), makeUnverifiableIntf());
 273 
 274         JarBuilder.update(jarFile.getPath(), outdir.getPath());
 275     }
 276 
 277     static void writeClassFile(File file, byte bytecodes[]) throws Exception {
 278         try (FileOutputStream fos = new FileOutputStream(file)) {
 279             fos.write(bytecodes);
 280         }
 281     }
 282 
 283     // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableBase.class
 284     static byte[] makeUnverifiableBase() throws Exception {
 285         ClassWriter cw = new ClassWriter(0);
 286         FieldVisitor fv;
 287         MethodVisitor mv;
 288         AnnotationVisitor av0;
 289 
 290         cw.visit(V1_8, ACC_SUPER, "UnverifiableBase", null, "java/lang/Object", null);
 291         {
 292             fv = cw.visitField(ACC_FINAL + ACC_STATIC, "x", "LVerifierTest;", null, null);
 293             fv.visitEnd();
 294         }
 295         {
 296             mv = cw.visitMethod(0, "<init>", "()V", null, null);
 297             mv.visitCode();
 298             mv.visitVarInsn(ALOAD, 0);
 299             mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
 300             mv.visitInsn(RETURN);
 301             mv.visitMaxs(1, 1);
 302             mv.visitEnd();
 303         }
 304         {
 305             mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
 306             mv.visitCode();
 307             mv.visitTypeInsn(NEW, "VerifierTest0");
 308             mv.visitInsn(DUP);
 309             mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false);
 310             mv.visitFieldInsn(PUTSTATIC, "UnverifiableBase", "x", "LVerifierTest;");
 311             mv.visitInsn(RETURN);
 312             mv.visitMaxs(2, 0);
 313             mv.visitEnd();
 314         }
 315         addBadMethod(cw);
 316         cw.visitEnd();
 317 
 318         return cw.toByteArray();
 319     }
 320 
 321     // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableIntf.class
 322     static byte[] makeUnverifiableIntf() throws Exception {
 323         ClassWriter cw = new ClassWriter(0);
 324         FieldVisitor fv;
 325         MethodVisitor mv;
 326         AnnotationVisitor av0;
 327 
 328         cw.visit(V1_8, ACC_ABSTRACT + ACC_INTERFACE, "UnverifiableIntf", null, "java/lang/Object", null);
 329 
 330         {
 331             fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "x", "LVerifierTest0;", null, null);
 332             fv.visitEnd();
 333         }
 334         {
 335             mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null);
 336             mv.visitCode();
 337             mv.visitTypeInsn(NEW, "VerifierTest0");
 338             mv.visitInsn(DUP);
 339             mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false);
 340             mv.visitFieldInsn(PUTSTATIC, "UnverifiableIntf", "x", "LVerifierTest0;");
 341             mv.visitInsn(RETURN);
 342             mv.visitMaxs(2, 0);
 343             mv.visitEnd();
 344         }
 345         addBadMethod(cw);
 346         cw.visitEnd();
 347 
 348         return cw.toByteArray();
 349     }
 350 
 351     // Add a bad method to make the class fail verification.
 352     static void addBadMethod(ClassWriter cw) throws Exception {
 353         MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "bad", "()V", null, null);
 354         mv.visitCode();
 355         mv.visitInsn(ARETURN); //  java.lang.VerifyError: Operand stack underflow
 356         mv.visitMaxs(2, 2);
 357         mv.visitEnd();
 358     }
 359 }