1 /*
   2  * Copyright (c) 2014, 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 SharedArchiveConsistency
  28  * @requires vm.cds
  29  * @library /test/lib
  30  * @modules jdk.jartool/sun.tools.jar
  31  * @build sun.hotspot.WhiteBox
  32  * @compile test-classes/Hello.java
  33  * @run driver ClassFileInstaller sun.hotspot.WhiteBox
  34  * @run main/othervm -Xbootclasspath/a:. -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI SharedArchiveConsistency
  35  */
  36 import jdk.test.lib.process.OutputAnalyzer;
  37 import jdk.test.lib.Utils;
  38 import java.io.File;
  39 import java.io.FileInputStream;
  40 import java.io.FileOutputStream;
  41 import java.io.IOException;
  42 import java.nio.ByteBuffer;
  43 import java.nio.ByteOrder;
  44 import java.nio.channels.FileChannel;
  45 import java.nio.file.Files;
  46 import java.nio.file.Path;
  47 import java.nio.file.Paths;
  48 import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
  49 import java.nio.file.StandardOpenOption;
  50 import static java.nio.file.StandardOpenOption.READ;
  51 import static java.nio.file.StandardOpenOption.WRITE;
  52 import java.util.ArrayList;
  53 import java.util.HashSet;
  54 import java.util.List;
  55 import java.util.Random;
  56 import sun.hotspot.WhiteBox;
  57 
  58 public class SharedArchiveConsistency {
  59     public static WhiteBox wb;
  60     public static int offset_magic;     // CDSFileMapHeaderBase::_magic
  61     public static int offset_version;   // CDSFileMapHeaderBase::_version
  62     public static int offset_jvm_ident; // FileMapHeader::_jvm_ident
  63     public static int sp_offset_crc;    // CDSFileMapRegion::_crc
  64     public static int offset_paths_misc_info_size;
  65     public static int file_header_size = -1;// total size of header, variant, need calculation
  66     public static int CDSFileMapRegion_size; // size of CDSFileMapRegion
  67     public static int sp_offset;       // offset of CDSFileMapRegion
  68     public static int sp_used_offset;  // offset of CDSFileMapRegion::_used
  69     public static int size_t_size;     // size of size_t
  70     public static int int_size;        // size of int
  71 
  72     public static File jsa;        // will be updated during test
  73     public static File orgJsaFile; // kept the original file not touched.
  74     // The following should be consistent with the enum in the C++ MetaspaceShared class
  75     public static String[] shared_region_name = {
  76         "mc",          // MiscCode
  77         "rw",          // ReadWrite
  78         "ro",          // ReadOnly
  79         "md",          // MiscData
  80         "first_closed_archive",
  81         "last_closed_archive",
  82         "first_open_archive",
  83         "last_open_archive"
  84     };
  85 
  86     public static int num_regions = shared_region_name.length;
  87     public static String[] matchMessages = {
  88         "Unable to use shared archive",
  89         "An error has occurred while processing the shared archive file.",
  90         "Checksum verification failed.",
  91         "The shared archive file has been truncated."
  92     };
  93 
  94     public static void getFileOffsetInfo() throws Exception {
  95         wb = WhiteBox.getWhiteBox();
  96         offset_magic = wb.getOffsetForName("FileMapHeader::_magic");
  97         offset_version = wb.getOffsetForName("FileMapHeader::_version");
  98         offset_jvm_ident = wb.getOffsetForName("FileMapHeader::_jvm_ident");
  99         sp_offset_crc = wb.getOffsetForName("CDSFileMapRegion::_crc");
 100         try {
 101             int nonExistOffset = wb.getOffsetForName("FileMapHeader::_non_exist_offset");
 102             System.exit(-1); // should fail
 103         } catch (Exception e) {
 104             // success
 105         }
 106 
 107         sp_offset = wb.getOffsetForName("FileMapHeader::_space[0]") - offset_magic;
 108         sp_used_offset = wb.getOffsetForName("CDSFileMapRegion::_used") - sp_offset_crc;
 109         size_t_size = wb.getOffsetForName("size_t_size");
 110         CDSFileMapRegion_size  = wb.getOffsetForName("CDSFileMapRegion_size");
 111     }
 112 
 113     public static int getFileHeaderSize(FileChannel fc) throws Exception {
 114         if (file_header_size != -1) {
 115             return file_header_size;
 116         }
 117         // this is not real header size, it is struct size
 118         int_size = wb.getOffsetForName("int_size");
 119         file_header_size = wb.getOffsetForName("file_header_size");
 120         offset_paths_misc_info_size = wb.getOffsetForName("FileMapHeader::_paths_misc_info_size") -
 121             offset_magic;
 122         int path_misc_info_size   = (int)readInt(fc, offset_paths_misc_info_size, int_size);
 123         file_header_size += path_misc_info_size;
 124         System.out.println("offset_paths_misc_info_size = " + offset_paths_misc_info_size);
 125         System.out.println("path_misc_info_size   = " + path_misc_info_size);
 126         System.out.println("file_header_size      = " + file_header_size);
 127         file_header_size = (int)align_up_page(file_header_size);
 128         System.out.println("file_header_size (aligned to page) = " + file_header_size);
 129         return file_header_size;
 130     }
 131 
 132     public static long align_up_page(long l) throws Exception {
 133         // wb is obtained in getFileOffsetInfo() which is called first in main() else we should call
 134         // WhiteBox.getWhiteBox() here first.
 135         int pageSize = wb.getVMPageSize();
 136         return (l + pageSize -1) & (~ (pageSize - 1));
 137     }
 138 
 139     private static long getRandomBetween(long start, long end) throws Exception {
 140         if (start > end) {
 141             throw new IllegalArgumentException("start must be less than end");
 142         }
 143         Random aRandom = Utils.getRandomInstance();
 144         int d = aRandom.nextInt((int)(end - start));
 145         if (d < 1) {
 146             d = 1;
 147         }
 148         return start + d;
 149     }
 150 
 151     public static long readInt(FileChannel fc, long offset, int nbytes) throws Exception {
 152         ByteBuffer bb = ByteBuffer.allocate(nbytes);
 153         bb.order(ByteOrder.nativeOrder());
 154         fc.position(offset);
 155         fc.read(bb);
 156         return  (nbytes > 4 ? bb.getLong(0) : bb.getInt(0));
 157     }
 158 
 159     public static void writeData(FileChannel fc, long offset, ByteBuffer bb) throws Exception {
 160         fc.position(offset);
 161         fc.write(bb);
 162     }
 163 
 164     public static FileChannel getFileChannel(File jsaFile) throws Exception {
 165         List<StandardOpenOption> arry = new ArrayList<StandardOpenOption>();
 166         arry.add(READ);
 167         arry.add(WRITE);
 168         return FileChannel.open(jsaFile.toPath(), new HashSet<StandardOpenOption>(arry));
 169     }
 170 
 171     public static void modifyJsaContentRandomly(File jsaFile) throws Exception {
 172         FileChannel fc = getFileChannel(jsaFile);
 173         // corrupt random area in the data areas
 174         long[] used    = new long[num_regions];       // record used bytes
 175         long start0, start, end, off;
 176         int used_offset, path_info_size;
 177 
 178         int bufSize;
 179         System.out.printf("%-24s%12s%12s%16s\n", "Space Name", "Used bytes", "Reg Start", "Random Offset");
 180         start0 = getFileHeaderSize(fc);
 181         for (int i = 0; i < num_regions; i++) {
 182             used[i] = get_region_used_size_aligned(fc, i);
 183             start = start0;
 184             for (int j = 0; j < i; j++) {
 185                 start += align_up_page(used[j]);
 186             }
 187             end = start + used[i];
 188             if (start == end) {
 189                 continue; // Ignore empty regions
 190             }
 191             off = getRandomBetween(start, end);
 192             System.out.printf("%-24s%12d%12d%16d\n", shared_region_name[i], used[i], start, off);
 193             if (end - off < 1024) {
 194                 bufSize = (int)(end - off + 1);
 195             } else {
 196                 bufSize = 1024;
 197             }
 198             ByteBuffer bbuf = ByteBuffer.wrap(new byte[bufSize]);
 199             writeData(fc, off, bbuf);
 200         }
 201         if (fc.isOpen()) {
 202             fc.close();
 203         }
 204     }
 205 
 206     static long get_region_used_size_aligned(FileChannel fc, int region) throws Exception {
 207         long n = sp_offset + CDSFileMapRegion_size * region + sp_used_offset;
 208         long alignment = WhiteBox.getWhiteBox().metaspaceReserveAlignment();
 209         long used = readInt(fc, n, size_t_size);
 210         used = (used + alignment - 1) & ~(alignment - 1);
 211         return used;
 212     }
 213 
 214     public static boolean modifyJsaContent(int region, File jsaFile) throws Exception {
 215         FileChannel fc = getFileChannel(jsaFile);
 216         byte[] buf = new byte[4096];
 217         ByteBuffer bbuf = ByteBuffer.wrap(buf);
 218 
 219         long total = 0L;
 220         long[] used = new long[num_regions];
 221         System.out.printf("%-24s%12s\n", "Space name", "Used bytes");
 222         for (int i = 0; i < num_regions; i++) {
 223             used[i] = get_region_used_size_aligned(fc, i);
 224             System.out.printf("%-24s%12d\n", shared_region_name[i], used[i]);
 225             total += used[i];
 226         }
 227         System.out.printf("%-24s%12d\n", "Total: ", total);
 228         long header_size = getFileHeaderSize(fc);
 229         long region_start_offset = header_size;
 230         for (int i=0; i<region; i++) {
 231             region_start_offset += used[i];
 232         }
 233         if (used[region] == 0) {
 234             System.out.println("Region " + shared_region_name[region] + " is empty. Nothing to corrupt.");
 235             return false;
 236         }
 237         System.out.println("Corrupt " + shared_region_name[region] + " section, start = " + region_start_offset
 238                            + " (header_size + 0x" + Long.toHexString(region_start_offset-header_size) + ")");
 239         long bytes_written = 0L;
 240         while (bytes_written < used[region]) {
 241             writeData(fc, region_start_offset + bytes_written, bbuf);
 242             bbuf.clear();
 243             bytes_written += 4096;
 244         }
 245         if (fc.isOpen()) {
 246             fc.close();
 247         }
 248         return true;
 249     }
 250 
 251     public static void modifyJsaHeader(File jsaFile) throws Exception {
 252         FileChannel fc = getFileChannel(jsaFile);
 253         // screw up header info
 254         byte[] buf = new byte[getFileHeaderSize(fc)];
 255         ByteBuffer bbuf = ByteBuffer.wrap(buf);
 256         writeData(fc, 0L, bbuf);
 257         if (fc.isOpen()) {
 258             fc.close();
 259         }
 260     }
 261 
 262     public static void modifyJvmIdent() throws Exception {
 263         FileChannel fc = getFileChannel(jsa);
 264         int headerSize = getFileHeaderSize(fc);
 265         System.out.println("    offset_jvm_ident " + offset_jvm_ident);
 266         byte[] buf = new byte[256];
 267         ByteBuffer bbuf = ByteBuffer.wrap(buf);
 268         writeData(fc, (long)offset_jvm_ident, bbuf);
 269         if (fc.isOpen()) {
 270             fc.close();
 271         }
 272     }
 273 
 274     public static void modifyHeaderIntField(long offset, int value) throws Exception {
 275         FileChannel fc = getFileChannel(jsa);
 276         int headerSize = getFileHeaderSize(fc);
 277         System.out.println("    offset " + offset);
 278         byte[] buf = ByteBuffer.allocate(4).putInt(value).array();
 279         ByteBuffer bbuf = ByteBuffer.wrap(buf);
 280         writeData(fc, offset, bbuf);
 281         if (fc.isOpen()) {
 282             fc.close();
 283         }
 284     }
 285 
 286     public static void copyFile(File from, File to) throws Exception {
 287         if (to.exists()) {
 288             if(!to.delete()) {
 289                 throw new IOException("Could not delete file " + to);
 290             }
 291         }
 292         to.createNewFile();
 293         setReadWritePermission(to);
 294         Files.copy(from.toPath(), to.toPath(), REPLACE_EXISTING);
 295     }
 296 
 297     // Copy file with bytes deleted or inserted
 298     // del -- true, deleted, false, inserted
 299     public static void copyFile(File from, File to, boolean del) throws Exception {
 300         try (
 301             FileChannel inputChannel = new FileInputStream(from).getChannel();
 302             FileChannel outputChannel = new FileOutputStream(to).getChannel()
 303         ) {
 304             long size = inputChannel.size();
 305             int init_size = getFileHeaderSize(inputChannel);
 306             outputChannel.transferFrom(inputChannel, 0, init_size);
 307             int n = (int)getRandomBetween(0, 1024);
 308             if (del) {
 309                 System.out.println("Delete " + n + " bytes at data start section");
 310                 inputChannel.position(init_size + n);
 311                 outputChannel.transferFrom(inputChannel, init_size, size - init_size - n);
 312             } else {
 313                 System.out.println("Insert " + n + " bytes at data start section");
 314                 outputChannel.position(init_size);
 315                 outputChannel.write(ByteBuffer.wrap(new byte[n]));
 316                 outputChannel.transferFrom(inputChannel, init_size + n , size - init_size);
 317             }
 318         }
 319     }
 320 
 321     public static void restoreJsaFile() throws Exception {
 322         Files.copy(orgJsaFile.toPath(), jsa.toPath(), REPLACE_EXISTING);
 323     }
 324 
 325     public static void setReadWritePermission(File file) throws Exception {
 326         if (!file.canRead()) {
 327             if (!file.setReadable(true)) {
 328                 throw new IOException("Cannot modify file " + file + " as readable");
 329             }
 330         }
 331         if (!file.canWrite()) {
 332             if (!file.setWritable(true)) {
 333                 throw new IOException("Cannot modify file " + file + " as writable");
 334             }
 335         }
 336     }
 337 
 338     public static void testAndCheck(String[] execArgs) throws Exception {
 339         OutputAnalyzer output = TestCommon.execCommon(execArgs);
 340         String stdtxt = output.getOutput();
 341         System.out.println("Note: this test may fail in very rare occasions due to CRC32 checksum collision");
 342         for (String message : matchMessages) {
 343             if (stdtxt.contains(message)) {
 344                 // match any to return
 345                 return;
 346             }
 347         }
 348         TestCommon.checkExec(output);
 349     }
 350 
 351     // dump with hello.jsa, then
 352     // read the jsa file
 353     //   1) run normal
 354     //   2) modify header
 355     //   3) keep header correct but modify content in each region specified by shared_region_name[]
 356     //   4) update both header and content, test
 357     //   5) delete bytes in data begining
 358     //   6) insert bytes in data begining
 359     //   7) randomly corrupt data in each region specified by shared_region_name[]
 360     public static void main(String... args) throws Exception {
 361         // must call to get offset info first!!!
 362         getFileOffsetInfo();
 363         Path currentRelativePath = Paths.get("");
 364         String currentDir = currentRelativePath.toAbsolutePath().toString();
 365         System.out.println("Current relative path is: " + currentDir);
 366         // get jar file
 367         String jarFile = JarBuilder.getOrCreateHelloJar();
 368 
 369         // dump (appcds.jsa created)
 370         TestCommon.testDump(jarFile, null);
 371 
 372         // test, should pass
 373         System.out.println("1. Normal, should pass but may fail\n");
 374 
 375         String[] execArgs = {"-Xlog:cds", "-cp", jarFile, "Hello"};
 376         // tests that corrupt contents of the archive need to run with
 377         // VerifySharedSpaces enabled to detect inconsistencies
 378         String[] verifyExecArgs = {"-Xlog:cds", "-XX:+VerifySharedSpaces", "-cp", jarFile, "Hello"};
 379 
 380         OutputAnalyzer output = TestCommon.execCommon(execArgs);
 381 
 382         try {
 383             TestCommon.checkExecReturn(output, 0, true, "Hello World");
 384         } catch (Exception e) {
 385             TestCommon.checkExecReturn(output, 1, true, matchMessages[0]);
 386         }
 387 
 388         // get current archive name
 389         jsa = new File(TestCommon.getCurrentArchiveName());
 390         if (!jsa.exists()) {
 391             throw new IOException(jsa + " does not exist!");
 392         }
 393 
 394         setReadWritePermission(jsa);
 395 
 396         // save as original untouched
 397         orgJsaFile = new File(new File(currentDir), "appcds.jsa.bak");
 398         copyFile(jsa, orgJsaFile);
 399 
 400         // modify jsa header, test should fail
 401         System.out.println("\n2. Corrupt header, should fail\n");
 402         modifyJsaHeader(jsa);
 403         output = TestCommon.execCommon(execArgs);
 404         output.shouldContain("The shared archive file has a bad magic number");
 405         output.shouldNotContain("Checksum verification failed");
 406 
 407         copyFile(orgJsaFile, jsa);
 408         // modify _jvm_ident and _paths_misc_info_size, test should fail
 409         System.out.println("\n2a. Corrupt _jvm_ident and _paths_misc_info_size, should fail\n");
 410         modifyJvmIdent();
 411         modifyHeaderIntField(offset_paths_misc_info_size, Integer.MAX_VALUE);
 412         output = TestCommon.execCommon(execArgs);
 413         output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
 414         output.shouldNotContain("Checksum verification failed");
 415 
 416         copyFile(orgJsaFile, jsa);
 417         // modify _jvm_ident and run with -Xshare:auto
 418         System.out.println("\n2b. Corrupt _jvm_ident run with -Xshare:auto\n");
 419         modifyJvmIdent();
 420         output = TestCommon.execAuto(execArgs);
 421         output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
 422         output.shouldContain("Hello World");
 423 
 424         copyFile(orgJsaFile, jsa);
 425         // modify _magic and _paths_misc_info_size, test should fail
 426         System.out.println("\n2c. Corrupt _magic and _paths_misc_info_size, should fail\n");
 427         modifyHeaderIntField(offset_magic, 0x00000000);
 428         modifyHeaderIntField(offset_paths_misc_info_size, Integer.MAX_VALUE);
 429         output = TestCommon.execCommon(execArgs);
 430         output.shouldContain("The shared archive file has a bad magic number");
 431         output.shouldNotContain("Checksum verification failed");
 432 
 433         copyFile(orgJsaFile, jsa);
 434         // modify _version and _paths_misc_info_size, test should fail
 435         System.out.println("\n2d. Corrupt _version and _paths_misc_info_size, should fail\n");
 436         modifyHeaderIntField(offset_version, 0x00000000);
 437         modifyHeaderIntField(offset_paths_misc_info_size, Integer.MAX_VALUE);
 438         output = TestCommon.execCommon(execArgs);
 439         output.shouldContain("The shared archive file has the wrong version");
 440         output.shouldNotContain("Checksum verification failed");
 441 
 442         File newJsaFile = null;
 443         // modify content
 444         System.out.println("\n3. Corrupt Content, should fail\n");
 445         for (int i=0; i<num_regions; i++) {
 446             newJsaFile = new File(TestCommon.getNewArchiveName(shared_region_name[i]));
 447             copyFile(orgJsaFile, newJsaFile);
 448             TestCommon.setCurrentArchiveName(newJsaFile.toString());
 449             if (modifyJsaContent(i, newJsaFile)) {
 450                 testAndCheck(verifyExecArgs);
 451             }
 452         }
 453 
 454         // modify both header and content, test should fail
 455         System.out.println("\n4. Corrupt Header and Content, should fail\n");
 456         newJsaFile = new File(TestCommon.getNewArchiveName("header-and-content"));
 457         copyFile(orgJsaFile, newJsaFile);
 458         TestCommon.setCurrentArchiveName(newJsaFile.toString());
 459         modifyJsaHeader(newJsaFile);
 460         modifyJsaContent(0, newJsaFile);  // this will not be reached since failed on header change first
 461         output = TestCommon.execCommon(execArgs);
 462         output.shouldContain("The shared archive file has a bad magic number");
 463         output.shouldNotContain("Checksum verification failed");
 464 
 465         // delete bytes in data section
 466         System.out.println("\n5. Delete bytes at beginning of data section, should fail\n");
 467         copyFile(orgJsaFile, jsa, true);
 468         TestCommon.setCurrentArchiveName(jsa.toString());
 469         testAndCheck(verifyExecArgs);
 470 
 471         // insert bytes in data section forward
 472         System.out.println("\n6. Insert bytes at beginning of data section, should fail\n");
 473         copyFile(orgJsaFile, jsa, false);
 474         testAndCheck(verifyExecArgs);
 475 
 476         System.out.println("\n7. modify Content in random areas, should fail\n");
 477         newJsaFile = new File(TestCommon.getNewArchiveName("random-areas"));
 478         copyFile(orgJsaFile, newJsaFile);
 479         TestCommon.setCurrentArchiveName(newJsaFile.toString());
 480         modifyJsaContentRandomly(newJsaFile);
 481         testAndCheck(verifyExecArgs);
 482     }
 483 }