1 /*
   2  * Copyright (c) 2017, 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 /*
  26  * @test
  27  * @summary Unused metadata created during dump time should be freed from the CDS archive.
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules jdk.jartool/sun.tools.jar
  31  * @compile test-classes/MethodNoReturn.jasm test-classes/Hello.java
  32  * @run main FreeUnusedMetadata
  33  */
  34 
  35 import java.nio.file.Files;
  36 import java.nio.file.Paths;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 
  39 public class FreeUnusedMetadata {
  40     static byte iconst_1 =  4;
  41     static byte pop      = 87;
  42     static byte[] pattern = { // This has the same sequence as in test-classes/MethodNoReturn.jasm
  43         iconst_1,
  44         pop,
  45         iconst_1,
  46         pop,
  47         iconst_1,
  48         pop,
  49         iconst_1,
  50         pop,
  51         iconst_1,
  52         pop,
  53         iconst_1,
  54         pop,
  55         iconst_1,
  56         pop,
  57         iconst_1,
  58         pop,
  59         iconst_1,
  60         pop,
  61         iconst_1,
  62         pop,
  63         iconst_1,
  64         iconst_1,
  65         iconst_1,
  66         iconst_1,
  67         iconst_1,
  68         iconst_1,
  69         iconst_1,
  70         iconst_1,
  71         pop,
  72         pop,
  73         pop,
  74         pop,
  75         pop,
  76         pop,
  77         pop,
  78         pop
  79     };
  80 
  81     public static void main(String[] args) throws Exception {
  82         String[] ARCHIVE_CLASSES = {"Hello", "MethodNoReturn"};
  83         String appJar = JarBuilder.build("FreeUnusedMetadata", ARCHIVE_CLASSES);
  84 
  85         OutputAnalyzer dumpOutput = TestCommon.dump(
  86                 appJar, ARCHIVE_CLASSES);
  87         TestCommon.checkDump(dumpOutput, "Loading classes to share");
  88 
  89         OutputAnalyzer execOutput = TestCommon.exec(appJar, "Hello");
  90         TestCommon.checkExec(execOutput, "Hello World");
  91 
  92 
  93         String archive = TestCommon.getCurrentArchiveName();
  94         System.out.println("Checking for pattern inside " + archive + "...");
  95 
  96         byte[] data = Files.readAllBytes(Paths.get(archive));
  97         int max = data.length - pattern.length;
  98         for (int i=0; i<max; i++) {
  99             if (data[i+0] == iconst_1 && data[i+1] == pop &&
 100                 data[i+2] == iconst_1 && data[i+3] == pop) {
 101                 boolean match = true;
 102                 for (int x=4; x<pattern.length; x++) {
 103                     if (data[i+x] != pattern[x]) {
 104                         match = false;
 105                         break;
 106                     }
 107                 }
 108 
 109                 if (match) {
 110                     throw new RuntimeException("method of unverifiable class should have been " +
 111                         "removed from the archive " + archive +
 112                         " , but was found at offset " + i);
 113                 }
 114             }
 115         }
 116         System.out.println("Not found: method from unverifiable class has been removed");
 117     }
 118 }