1 /*
   2  * Copyright (c) 2019, 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 Corrupt the header CRC fields of the top archive. VM should exit with an error.
  28  * @requires vm.cds
  29  * @library /test/lib /test/hotspot/jtreg/runtime/appcds /test/hotspot/jtreg/runtime/appcds/test-classes
  30  * @build Hello sun.hotspot.WhiteBox
  31  * @run driver ClassFileInstaller -jar hello.jar Hello
  32  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  33  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI ArchiveConsistency
  34  */
  35 
  36 import java.io.File;
  37 import java.io.IOException;
  38 import java.nio.ByteBuffer;
  39 import java.nio.ByteOrder;
  40 import java.nio.channels.FileChannel;
  41 import java.nio.file.StandardOpenOption;
  42 import static java.nio.file.StandardOpenOption.READ;
  43 import static java.nio.file.StandardOpenOption.WRITE;
  44 import java.util.ArrayList;
  45 import java.util.HashSet;
  46 import java.util.List;
  47 import sun.hotspot.WhiteBox;
  48 
  49 public class ArchiveConsistency extends DynamicArchiveTestBase {
  50     public static WhiteBox wb;
  51     public static int int_size;        // size of int
  52     public static String[] shared_region_name = {"MiscCode", "ReadWrite", "ReadOnly", "MiscData"};
  53     public static int num_regions = shared_region_name.length;
  54 
  55     public static void main(String[] args) throws Exception {
  56         runTest(ArchiveConsistency::testCustomBase);
  57     }
  58 
  59     // Test with custom base archive + top archive
  60     static void testCustomBase() throws Exception {
  61         String topArchiveName = getNewArchiveName("top2");
  62         String baseArchiveName = getNewArchiveName("base");
  63         dumpBaseArchive(baseArchiveName);
  64         doTest(baseArchiveName, topArchiveName);
  65     }
  66 
  67     public static void setReadWritePermission(File file) throws Exception {
  68         if (!file.canRead()) {
  69             if (!file.setReadable(true)) {
  70                 throw new IOException("Cannot modify file " + file + " as readable");
  71             }
  72         }
  73         if (!file.canWrite()) {
  74             if (!file.setWritable(true)) {
  75                 throw new IOException("Cannot modify file " + file + " as writable");
  76             }
  77         }
  78     }
  79 
  80     public static long readInt(FileChannel fc, long offset, int nbytes) throws Exception {
  81         ByteBuffer bb = ByteBuffer.allocate(nbytes);
  82         bb.order(ByteOrder.nativeOrder());
  83         fc.position(offset);
  84         fc.read(bb);
  85         return  (nbytes > 4 ? bb.getLong(0) : bb.getInt(0));
  86     }
  87 
  88     public static long align_up_page(long l) throws Exception {
  89         // wb is obtained in getFileOffsetInfo() which is called first in main() else we should call
  90         // WhiteBox.getWhiteBox() here first.
  91         int pageSize = wb.getVMPageSize();
  92         return (l + pageSize -1) & (~ (pageSize - 1));
  93     }
  94 
  95     public static void writeData(FileChannel fc, long offset, ByteBuffer bb) throws Exception {
  96         fc.position(offset);
  97         fc.write(bb);
  98         fc.force(true);
  99     }
 100 
 101     public static FileChannel getFileChannel(File jsa) throws Exception {
 102         List<StandardOpenOption> arry = new ArrayList<StandardOpenOption>();
 103         arry.add(READ);
 104         arry.add(WRITE);
 105         return FileChannel.open(jsa.toPath(), new HashSet<StandardOpenOption>(arry));
 106     }
 107 
 108    public static void modifyJsaHeaderCRC(File jsa) throws Exception {
 109         FileChannel fc = getFileChannel(jsa);
 110         int_size = wb.getOffsetForName("int_size");
 111         System.out.println("    int_size " + int_size);
 112         ByteBuffer bbuf = ByteBuffer.allocateDirect(int_size);
 113         for (int i = 0; i < int_size; i++) {
 114             bbuf.put((byte)0);
 115         }
 116 
 117         int baseArchiveCRCOffset = wb.getOffsetForName("DynamicArchiveHeader::_base_archive_crc");
 118         int crc = 0;
 119         System.out.printf("%-12s%-12s\n", "Space name", "CRC");
 120         for (int i = 0; i < 4; i++) {
 121             baseArchiveCRCOffset += int_size * i;
 122             System.out.println("    baseArchiveCRCOffset " + baseArchiveCRCOffset);
 123             crc = (int)readInt(fc, baseArchiveCRCOffset, int_size );
 124             System.out.printf("%-11s%-12d\n", shared_region_name[i], crc);
 125             bbuf.rewind();
 126             writeData(fc, baseArchiveCRCOffset, bbuf);
 127         }
 128         fc.force(true);
 129         if (fc.isOpen()) {
 130             fc.close();
 131         }
 132     }
 133 
 134     private static void doTest(String baseArchiveName, String topArchiveName) throws Exception {
 135         String appJar = ClassFileInstaller.getJarPath("hello.jar");
 136         String mainClass = "Hello";
 137         dump2(baseArchiveName, topArchiveName,
 138              "-Xlog:cds",
 139              "-Xlog:cds+dynamic=debug",
 140              "-cp", appJar, mainClass)
 141             .assertNormalExit(output -> {
 142                     output.shouldContain("Buffer-space to target-space delta")
 143                           .shouldContain("Written dynamic archive 0x");
 144                 });
 145 
 146         File jsa = new File(topArchiveName);
 147         if (!jsa.exists()) {
 148             throw new IOException(jsa + " does not exist!");
 149         }
 150 
 151         // Modify the CRC values in the header of the top archive.
 152         wb = WhiteBox.getWhiteBox();
 153         setReadWritePermission(jsa);
 154         modifyJsaHeaderCRC(jsa);
 155 
 156         run2(baseArchiveName, topArchiveName,
 157             "-Xlog:class+load",
 158             "-Xlog:cds+dynamic=debug,cds=debug",
 159             "-XX:+VerifySharedSpaces",
 160             "-cp", appJar, mainClass)
 161             .assertAbnormalExit(output -> {
 162                     output.shouldContain("Header checksum verification failed")
 163                           .shouldContain("Unable to use shared archive")
 164                           .shouldHaveExitValue(1);
 165                 });
 166     }
 167 }