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 static final String CDS_LOGGING = "-Xlog:cds,cds+hashtables"; 52 53 enum Testset1Part { 54 A, B 55 } 56 57 public static void main(String[] args) throws Exception { 58 String subCaseId = args[0]; 59 String jarName_verifier_test_tmp = "verifier_test_tmp" + "_" + subCaseId; 60 String jarName_verifier_test = "verifier_test" + "_" + subCaseId; 61 String jarName_greet = "greet" + "_" + subCaseId; 62 String jarName_hi = "hi" + "_" + subCaseId; 63 64 65 JarBuilder.build(jarName_verifier_test_tmp, "VerifierTest0", "VerifierTestA", 66 "VerifierTestB", "VerifierTestC", "VerifierTestD", "VerifierTestE", 67 "UnverifiableBase", "UnverifiableIntf", "UnverifiableIntfSub"); 68 JarBuilder.build(jarName_greet, "Greet"); 69 JarBuilder.build(jarName_hi, "Hi", "Hi$MyClass"); 70 71 File dir = new File(System.getProperty("test.classes", ".")); 72 File jarSrcFile = new File(dir, jarName_verifier_test_tmp + ".jar"); 73 File jarFile = new File(dir, jarName_verifier_test + ".jar"); 74 String jar = jarFile.getPath(); 75 76 if (!jarFile.exists() || jarFile.lastModified() < jarSrcFile.lastModified()) { 77 createTestJarFile(jarSrcFile, jarFile); 78 } else { 79 System.out.println("Already up-to-date: " + jarFile); 80 } 81 82 String noAppClasses[] = TestCommon.list(""); 83 String appClasses[] = TestCommon.list("UnverifiableBase", 84 "UnverifiableIntf", 85 "UnverifiableIntfSub", 86 "VerifierTestA", 87 "VerifierTestB", 88 "VerifierTestC", 89 "VerifierTestD", 90 "VerifierTestE", 91 "VerifierTest0"); 92 93 94 switch (subCaseId) { 95 case "0": testset_0(jar, noAppClasses, appClasses); return; 96 case "1A": testset_1(jar, noAppClasses, appClasses, Testset1Part.A); return; 97 case "1B": testset_1(jar, noAppClasses, appClasses, Testset1Part.B); return; 98 case "2": testset_2(jarName_greet, jarName_hi); return; 99 default: 100 throw new RuntimeException("Unknown option: " + subCaseId); 101 } 102 } 103 104 static void testset_0(String jar, String[] noAppClasses, String[] appClasses) throws Exception { 105 // IgnoreUnverifiableClassesDuringDump has been obsoleted. 106 // Unverifiable classes won't be included in the CDS archive. 107 OutputAnalyzer output = TestCommon.dump(jar, appClasses, 108 CDS_LOGGING, 109 "-XX:+UnlockDiagnosticVMOptions", 110 "-XX:-IgnoreUnverifiableClassesDuringDump"); 111 output.shouldHaveExitValue(0); 112 113 // By default, bad classes should be ignored during dumping. 114 TestCommon.testDump(jar, appClasses); 115 } 116 117 static void checkRuntimeOutput(OutputAnalyzer output, String expected) throws Exception { 118 output.shouldContain(expected); 119 if (expected.equals(PASS_RESULT) || 120 expected.equals(VFY_ERR)) { 121 output.shouldHaveExitValue(0); 122 } else { 123 output.shouldNotHaveExitValue(0); 124 } 125 } 126 127 static void testset_1(String jar, String[] noAppClasses, String[] appClasses, Testset1Part part) 128 throws Exception 129 { 130 String config[][] = { 131 // {dump_list, dumptime_verification_setting, 132 // runtime_verification_setting, expected_output_str}, 133 134 // Dump app/ext with -Xverify:remote 135 {"app", VFY_REMOTE, VFY_REMOTE, VFY_ERR}, 136 {"app", VFY_REMOTE, VFY_ALL, MAP_FAIL}, 137 {"app", VFY_REMOTE, VFY_NONE, ERR }, 138 // Dump app/ext with -Xverify:all 139 {"app", VFY_ALL, VFY_REMOTE, VFY_ERR }, 140 {"app", VFY_ALL, VFY_ALL, VFY_ERR }, 141 {"app", VFY_ALL, VFY_NONE, ERR }, 142 // Dump app/ext with -Xverify:none 143 {"app", VFY_NONE, VFY_REMOTE, VFY_ERR}, 144 {"app", VFY_NONE, VFY_ALL, MAP_FAIL}, 145 {"app", VFY_NONE, VFY_NONE, ERR }, 146 // Dump sys only with -Xverify:remote 147 {"noApp", VFY_REMOTE, VFY_REMOTE, VFY_ERR}, 148 {"noApp", VFY_REMOTE, VFY_ALL, VFY_ERR}, 149 {"noApp", VFY_REMOTE, VFY_NONE, ERR}, 150 // Dump sys only with -Xverify:all 151 {"noApp", VFY_ALL, VFY_REMOTE, VFY_ERR}, 152 {"noApp", VFY_ALL, VFY_ALL, VFY_ERR}, 153 {"noApp", VFY_ALL, VFY_NONE, ERR}, 154 // Dump sys only with -Xverify:none 155 {"noApp", VFY_NONE, VFY_REMOTE, VFY_ERR}, 156 {"noApp", VFY_NONE, VFY_ALL, VFY_ERR}, 157 {"noApp", VFY_NONE, VFY_NONE, ERR}, 158 }; 159 160 int loop_start, loop_stop; 161 162 // Further break down testset_1 into two parts (to be invoked from VerifierTest_1A.java 163 // and VerifierTest_1B.java) to improve parallel test execution time. 164 switch (part) { 165 case A: 166 loop_start = 0; 167 loop_stop = 9; 168 break; 169 case B: 170 default: 171 assert part == Testset1Part.B; 172 loop_start = 9; 173 loop_stop = config.length; 174 break; 175 } 176 177 String prev_dump_setting = ""; 178 for (int i = loop_start; i < loop_stop; i ++) { 179 String dump_list[] = config[i][0].equals("app") ? appClasses : 180 noAppClasses; 181 String dump_setting = config[i][1]; 182 String runtime_setting = config[i][2]; 183 String expected_output_str = config[i][3]; 184 System.out.println("Test case [" + i + "]: dumping " + config[i][0] + 185 " with " + dump_setting + 186 ", run with " + runtime_setting); 187 if (!dump_setting.equals(prev_dump_setting)) { 188 OutputAnalyzer dumpOutput = TestCommon.dump( 189 jar, dump_list, dump_setting, 190 CDS_LOGGING, 191 // FIXME: the following options are for working around a GC 192 // issue - assert failure when dumping archive with the -Xverify:all 193 "-Xms256m", 194 "-Xmx256m"); 195 if (dump_setting.equals(VFY_NONE) && 196 runtime_setting.equals(VFY_REMOTE)) { 197 dumpOutput.shouldContain(VFY_INFO_MESSAGE); 198 } 199 } 200 TestCommon.run("-cp", jar, 201 runtime_setting, 202 "VerifierTest0") 203 .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str)); 204 prev_dump_setting = dump_setting; 205 } 206 } 207 208 static void testset_2(String jarName_greet, String jarName_hi) throws Exception { 209 String appClasses[]; 210 String jar; 211 212 // The following section is for testing the scenarios where 213 // the classes are verifiable during dump time. 214 appClasses = TestCommon.list("Hi", 215 "Greet", 216 "Hi$MyClass"); 217 jar = TestCommon.getTestJar(jarName_hi + ".jar") + File.pathSeparator + 218 TestCommon.getTestJar(jarName_greet + ".jar"); 219 String config2[][] = { 220 // {dump_list, dumptime_verification_setting, 221 // runtime_verification_setting, expected_output_str}, 222 223 // Dump app/ext with -Xverify:remote 224 {"app", VFY_REMOTE, VFY_REMOTE, PASS_RESULT}, 225 {"app", VFY_REMOTE, VFY_ALL, MAP_FAIL}, 226 {"app", VFY_REMOTE, VFY_NONE, PASS_RESULT }, 227 // Dump app/ext with -Xverify:all 228 {"app", VFY_ALL, VFY_REMOTE, PASS_RESULT }, 229 {"app", VFY_ALL, VFY_ALL, PASS_RESULT }, 230 {"app", VFY_ALL, VFY_NONE, PASS_RESULT }, 231 // Dump app/ext with -Xverify:none 232 {"app", VFY_NONE, VFY_REMOTE, PASS_RESULT}, 233 {"app", VFY_NONE, VFY_ALL, MAP_FAIL}, 234 {"app", VFY_NONE, VFY_NONE, PASS_RESULT }, 235 }; 236 String prev_dump_setting = ""; 237 for (int i = 0; i < config2.length; i ++) { 238 // config2[i][0] is always set to "app" in this test 239 String dump_setting = config2[i][1]; 240 String runtime_setting = config2[i][2]; 241 String expected_output_str = config2[i][3]; 242 System.out.println("Test case [" + i + "]: dumping " + config2[i][0] + 243 " with " + dump_setting + 244 ", run with " + runtime_setting); 245 if (!dump_setting.equals(prev_dump_setting)) { 246 OutputAnalyzer dumpOutput = TestCommon.dump( 247 jar, appClasses, dump_setting, 248 CDS_LOGGING, 249 // FIXME: the following options are for working around a GC 250 // issue - assert failure when dumping archive with the -Xverify:all 251 "-Xms256m", 252 "-Xmx256m"); 253 if (dump_setting.equals(VFY_NONE) && 254 runtime_setting.equals(VFY_REMOTE)) { 255 dumpOutput.shouldContain(VFY_INFO_MESSAGE); 256 } 257 } 258 TestCommon.run("-cp", jar, 259 runtime_setting, 260 "Hi") 261 .ifNoMappingFailure(output -> checkRuntimeOutput(output, expected_output_str)); 262 prev_dump_setting = dump_setting; 263 } 264 } 265 266 static void createTestJarFile(File jarSrcFile, File jarFile) throws Exception { 267 jarFile.delete(); 268 Files.copy(jarSrcFile.toPath(), jarFile.toPath()); 269 270 File dir = new File(System.getProperty("test.classes", ".")); 271 File outdir = new File(dir, "verifier_test_classes"); 272 outdir.mkdir(); 273 274 writeClassFile(new File(outdir, "UnverifiableBase.class"), makeUnverifiableBase()); 275 writeClassFile(new File(outdir, "UnverifiableIntf.class"), makeUnverifiableIntf()); 276 277 JarBuilder.update(jarFile.getPath(), outdir.getPath()); 278 } 279 280 static void writeClassFile(File file, byte bytecodes[]) throws Exception { 281 try (FileOutputStream fos = new FileOutputStream(file)) { 282 fos.write(bytecodes); 283 } 284 } 285 286 // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableBase.class 287 static byte[] makeUnverifiableBase() throws Exception { 288 ClassWriter cw = new ClassWriter(0); 289 FieldVisitor fv; 290 MethodVisitor mv; 291 AnnotationVisitor av0; 292 293 cw.visit(V1_8, ACC_SUPER, "UnverifiableBase", null, "java/lang/Object", null); 294 { 295 fv = cw.visitField(ACC_FINAL + ACC_STATIC, "x", "LVerifierTest;", null, null); 296 fv.visitEnd(); 297 } 298 { 299 mv = cw.visitMethod(0, "<init>", "()V", null, null); 300 mv.visitCode(); 301 mv.visitVarInsn(ALOAD, 0); 302 mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false); 303 mv.visitInsn(RETURN); 304 mv.visitMaxs(1, 1); 305 mv.visitEnd(); 306 } 307 { 308 mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); 309 mv.visitCode(); 310 mv.visitTypeInsn(NEW, "VerifierTest0"); 311 mv.visitInsn(DUP); 312 mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false); 313 mv.visitFieldInsn(PUTSTATIC, "UnverifiableBase", "x", "LVerifierTest;"); 314 mv.visitInsn(RETURN); 315 mv.visitMaxs(2, 0); 316 mv.visitEnd(); 317 } 318 addBadMethod(cw); 319 cw.visitEnd(); 320 321 return cw.toByteArray(); 322 } 323 324 // This was obtained using JDK8: java jdk.internal.org.objectweb.asm.util.ASMifier tmpclasses/UnverifiableIntf.class 325 static byte[] makeUnverifiableIntf() throws Exception { 326 ClassWriter cw = new ClassWriter(0); 327 FieldVisitor fv; 328 MethodVisitor mv; 329 AnnotationVisitor av0; 330 331 cw.visit(V1_8, ACC_ABSTRACT + ACC_INTERFACE, "UnverifiableIntf", null, "java/lang/Object", null); 332 333 { 334 fv = cw.visitField(ACC_PUBLIC + ACC_FINAL + ACC_STATIC, "x", "LVerifierTest0;", null, null); 335 fv.visitEnd(); 336 } 337 { 338 mv = cw.visitMethod(ACC_STATIC, "<clinit>", "()V", null, null); 339 mv.visitCode(); 340 mv.visitTypeInsn(NEW, "VerifierTest0"); 341 mv.visitInsn(DUP); 342 mv.visitMethodInsn(INVOKESPECIAL, "VerifierTest0", "<init>", "()V", false); 343 mv.visitFieldInsn(PUTSTATIC, "UnverifiableIntf", "x", "LVerifierTest0;"); 344 mv.visitInsn(RETURN); 345 mv.visitMaxs(2, 0); 346 mv.visitEnd(); 347 } 348 addBadMethod(cw); 349 cw.visitEnd(); 350 351 return cw.toByteArray(); 352 } 353 354 // Add a bad method to make the class fail verification. 355 static void addBadMethod(ClassWriter cw) throws Exception { 356 MethodVisitor mv = cw.visitMethod(ACC_PUBLIC, "bad", "()V", null, null); 357 mv.visitCode(); 358 mv.visitInsn(ARETURN); // java.lang.VerifyError: Operand stack underflow 359 mv.visitMaxs(2, 2); 360 mv.visitEnd(); 361 } 362 }