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