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 };
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) {
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);
|
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 file_header_size = -1;// total size of header, variant, need calculation
65 public static int CDSFileMapRegion_size; // size of CDSFileMapRegion
66 public static int sp_offset; // offset of CDSFileMapRegion
67 public static int sp_used_offset; // offset of CDSFileMapRegion::_used
68 public static int size_t_size; // size of size_t
69 public static int int_size; // size of int
70
71 public static File jsa; // will be updated during test
72 public static File orgJsaFile; // kept the original file not touched.
73 // The following should be consistent with the enum in the C++ MetaspaceShared class
74 public static String[] shared_region_name = {
75 "mc", // MiscCode
76 "rw", // ReadWrite
77 "ro", // ReadOnly
78 "md", // MiscData
79 "first_closed_archive",
80 "last_closed_archive",
81 "first_open_archive",
82 "last_open_archive"
83 };
99 try {
100 int nonExistOffset = wb.getOffsetForName("FileMapHeader::_non_exist_offset");
101 System.exit(-1); // should fail
102 } catch (Exception e) {
103 // success
104 }
105
106 sp_offset = wb.getOffsetForName("FileMapHeader::_space[0]") - offset_magic;
107 sp_used_offset = wb.getOffsetForName("CDSFileMapRegion::_used") - sp_offset_crc;
108 size_t_size = wb.getOffsetForName("size_t_size");
109 CDSFileMapRegion_size = wb.getOffsetForName("CDSFileMapRegion_size");
110 }
111
112 public static int getFileHeaderSize(FileChannel fc) throws Exception {
113 if (file_header_size != -1) {
114 return file_header_size;
115 }
116 // this is not real header size, it is struct size
117 int_size = wb.getOffsetForName("int_size");
118 file_header_size = wb.getOffsetForName("file_header_size");
119 System.out.println("file_header_size = " + file_header_size);
120 file_header_size = (int)align_up_page(file_header_size);
121 System.out.println("file_header_size (aligned to page) = " + file_header_size);
122 return file_header_size;
123 }
124
125 public static long align_up_page(long l) throws Exception {
126 // wb is obtained in getFileOffsetInfo() which is called first in main() else we should call
127 // WhiteBox.getWhiteBox() here first.
128 int pageSize = wb.getVMPageSize();
129 return (l + pageSize -1) & (~ (pageSize - 1));
130 }
131
132 private static long getRandomBetween(long start, long end) throws Exception {
133 if (start > end) {
134 throw new IllegalArgumentException("start must be less than end");
135 }
136 Random aRandom = Utils.getRandomInstance();
137 int d = aRandom.nextInt((int)(end - start));
138 if (d < 1) {
381 // get current archive name
382 jsa = new File(TestCommon.getCurrentArchiveName());
383 if (!jsa.exists()) {
384 throw new IOException(jsa + " does not exist!");
385 }
386
387 setReadWritePermission(jsa);
388
389 // save as original untouched
390 orgJsaFile = new File(new File(currentDir), "appcds.jsa.bak");
391 copyFile(jsa, orgJsaFile);
392
393 // modify jsa header, test should fail
394 System.out.println("\n2. Corrupt header, should fail\n");
395 modifyJsaHeader(jsa);
396 output = TestCommon.execCommon(execArgs);
397 output.shouldContain("The shared archive file has a bad magic number");
398 output.shouldNotContain("Checksum verification failed");
399
400 copyFile(orgJsaFile, jsa);
401 // modify _jvm_ident, test should fail
402 System.out.println("\n2a. Corrupt _jvm_ident, should fail\n");
403 modifyJvmIdent();
404 output = TestCommon.execCommon(execArgs);
405 output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
406 output.shouldNotContain("Checksum verification failed");
407
408 copyFile(orgJsaFile, jsa);
409 // modify _jvm_ident and run with -Xshare:auto
410 System.out.println("\n2b. Corrupt _jvm_ident run with -Xshare:auto\n");
411 modifyJvmIdent();
412 output = TestCommon.execAuto(execArgs);
413 output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
414 output.shouldContain("Hello World");
415
416 copyFile(orgJsaFile, jsa);
417 // modify _magic, test should fail
418 System.out.println("\n2c. Corrupt _magic, should fail\n");
419 modifyHeaderIntField(offset_magic, 0x00000000);
420 output = TestCommon.execCommon(execArgs);
421 output.shouldContain("The shared archive file has a bad magic number");
422 output.shouldNotContain("Checksum verification failed");
423
424 copyFile(orgJsaFile, jsa);
425 // modify _version, test should fail
426 System.out.println("\n2d. Corrupt _version, should fail\n");
427 modifyHeaderIntField(offset_version, 0x00000000);
428 output = TestCommon.execCommon(execArgs);
429 output.shouldContain("The shared archive file has the wrong version");
430 output.shouldNotContain("Checksum verification failed");
431
432 File newJsaFile = null;
433 // modify content
434 System.out.println("\n3. Corrupt Content, should fail\n");
435 for (int i=0; i<num_regions; i++) {
436 newJsaFile = new File(TestCommon.getNewArchiveName(shared_region_name[i]));
437 copyFile(orgJsaFile, newJsaFile);
438 TestCommon.setCurrentArchiveName(newJsaFile.toString());
439 if (modifyJsaContent(i, newJsaFile)) {
440 testAndCheck(verifyExecArgs);
441 }
442 }
443
444 // modify both header and content, test should fail
445 System.out.println("\n4. Corrupt Header and Content, should fail\n");
446 newJsaFile = new File(TestCommon.getNewArchiveName("header-and-content"));
447 copyFile(orgJsaFile, newJsaFile);
|