Skip to content

Commit 75a5be8

Browse files
committed
8263054: [testbug] SharedArchiveConsistency.java reuses jsa files
Reviewed-by: dholmes, minqi
1 parent 2afbd5d commit 75a5be8

File tree

1 file changed

+46
-56
lines changed

1 file changed

+46
-56
lines changed

test/hotspot/jtreg/runtime/cds/appcds/SharedArchiveConsistency.java

Lines changed: 46 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2014, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -68,8 +68,6 @@ public class SharedArchiveConsistency {
6868
public static int size_t_size; // size of size_t
6969
public static int int_size; // size of int
7070

71-
public static File jsa; // will be updated during test
72-
public static File orgJsaFile; // kept the original file not touched.
7371
// The following should be consistent with the enum in the C++ MetaspaceShared class
7472
public static String[] shared_region_name = {
7573
"mc", // MiscCode
@@ -252,8 +250,8 @@ public static void modifyJsaHeader(File jsaFile) throws Exception {
252250
}
253251
}
254252

255-
public static void modifyJvmIdent() throws Exception {
256-
FileChannel fc = getFileChannel(jsa);
253+
public static void modifyJvmIdent(File jsaFile) throws Exception {
254+
FileChannel fc = getFileChannel(jsaFile);
257255
int headerSize = getFileHeaderSize(fc);
258256
System.out.println(" offset_jvm_ident " + offset_jvm_ident);
259257
byte[] buf = new byte[256];
@@ -264,8 +262,8 @@ public static void modifyJvmIdent() throws Exception {
264262
}
265263
}
266264

267-
public static void modifyHeaderIntField(long offset, int value) throws Exception {
268-
FileChannel fc = getFileChannel(jsa);
265+
public static void modifyHeaderIntField(File jsaFile, long offset, int value) throws Exception {
266+
FileChannel fc = getFileChannel(jsaFile);
269267
int headerSize = getFileHeaderSize(fc);
270268
System.out.println(" offset " + offset);
271269
byte[] buf = ByteBuffer.allocate(4).putInt(value).array();
@@ -276,23 +274,37 @@ public static void modifyHeaderIntField(long offset, int value) throws Exception
276274
}
277275
}
278276

279-
public static void copyFile(File from, File to) throws Exception {
280-
if (to.exists()) {
281-
if(!to.delete()) {
282-
throw new IOException("Could not delete file " + to);
277+
static int testCount = 0;
278+
public static String startNewTestArchive(String testName) {
279+
++ testCount;
280+
String newArchiveName = TestCommon.getNewArchiveName(String.format("%02d", testCount) + "-" + testName);
281+
TestCommon.setCurrentArchiveName(newArchiveName);
282+
return newArchiveName;
283+
}
284+
285+
public static File copyFile(File orgJsaFile, String testName) throws Exception {
286+
File newJsaFile = new File(startNewTestArchive(testName));
287+
if (newJsaFile.exists()) {
288+
if (!newJsaFile.delete()) {
289+
throw new IOException("Could not delete file " + newJsaFile);
283290
}
284291
}
285-
to.createNewFile();
286-
setReadWritePermission(to);
287-
Files.copy(from.toPath(), to.toPath(), REPLACE_EXISTING);
292+
Files.copy(orgJsaFile.toPath(), newJsaFile.toPath(), REPLACE_EXISTING);
293+
294+
// orgJsaFile is read only, and Files.copy passes on this attribute to newJsaFile.
295+
// Since we need to modify newJsaFile later, let's set it to r/w
296+
setReadWritePermission(newJsaFile);
297+
298+
return newJsaFile;
288299
}
289300

290301
// Copy file with bytes deleted or inserted
291302
// del -- true, deleted, false, inserted
292-
public static void copyFile(File from, File to, boolean del) throws Exception {
303+
public static File insertOrDeleteBytes(File orgJsaFile, boolean del) throws Exception {
304+
File newJsaFile = new File(startNewTestArchive(del ? "delete-bytes" : "insert-bytes"));
293305
try (
294-
FileChannel inputChannel = new FileInputStream(from).getChannel();
295-
FileChannel outputChannel = new FileOutputStream(to).getChannel()
306+
FileChannel inputChannel = new FileInputStream(orgJsaFile).getChannel();
307+
FileChannel outputChannel = new FileOutputStream(newJsaFile).getChannel()
296308
) {
297309
long size = inputChannel.size();
298310
int init_size = getFileHeaderSize(inputChannel);
@@ -309,10 +321,8 @@ public static void copyFile(File from, File to, boolean del) throws Exception {
309321
outputChannel.transferFrom(inputChannel, init_size + n , size - init_size);
310322
}
311323
}
312-
}
313324

314-
public static void restoreJsaFile() throws Exception {
315-
Files.copy(orgJsaFile.toPath(), jsa.toPath(), REPLACE_EXISTING);
325+
return newJsaFile;
316326
}
317327

318328
public static void setReadWritePermission(File file) throws Exception {
@@ -365,7 +375,7 @@ public static void main(String... args) throws Exception {
365375
// test, should pass
366376
System.out.println("1. Normal, should pass but may fail\n");
367377

368-
String[] execArgs = {"-Xlog:cds", "-cp", jarFile, "Hello"};
378+
String[] execArgs = {"-Xlog:cds=debug", "-cp", jarFile, "Hello"};
369379
// tests that corrupt contents of the archive need to run with
370380
// VerifySharedSpaces enabled to detect inconsistencies
371381
String[] verifyExecArgs = {"-Xlog:cds", "-XX:+VerifySharedSpaces", "-cp", jarFile, "Hello"};
@@ -378,74 +388,58 @@ public static void main(String... args) throws Exception {
378388
TestCommon.checkExecReturn(output, 1, true, matchMessages[0]);
379389
}
380390

381-
// get current archive name
382-
jsa = new File(TestCommon.getCurrentArchiveName());
383-
if (!jsa.exists()) {
384-
throw new IOException(jsa + " does not exist!");
391+
// get the archive that has just been created.
392+
File orgJsaFile = new File(TestCommon.getCurrentArchiveName());
393+
if (!orgJsaFile.exists()) {
394+
throw new IOException(orgJsaFile + " does not exist!");
385395
}
386396

387-
setReadWritePermission(jsa);
388-
389-
// save as original untouched
390-
orgJsaFile = new File(new File(currentDir), "appcds.jsa.bak");
391-
copyFile(jsa, orgJsaFile);
392-
393397
// modify jsa header, test should fail
394398
System.out.println("\n2. Corrupt header, should fail\n");
395-
modifyJsaHeader(jsa);
399+
modifyJsaHeader(copyFile(orgJsaFile, "corrupt-header"));
396400
output = TestCommon.execCommon(execArgs);
397401
output.shouldContain("The shared archive file has a bad magic number");
398402
output.shouldNotContain("Checksum verification failed");
399403

400-
copyFile(orgJsaFile, jsa);
401404
// modify _jvm_ident, test should fail
402405
System.out.println("\n2a. Corrupt _jvm_ident, should fail\n");
403-
modifyJvmIdent();
406+
modifyJvmIdent(copyFile(orgJsaFile, "modify-jvm-ident"));
404407
output = TestCommon.execCommon(execArgs);
405408
output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
406409
output.shouldNotContain("Checksum verification failed");
407410

408-
copyFile(orgJsaFile, jsa);
409-
// modify _jvm_ident and run with -Xshare:auto
411+
// use the same archive as above, but run with -Xshare:auto
410412
System.out.println("\n2b. Corrupt _jvm_ident run with -Xshare:auto\n");
411-
modifyJvmIdent();
412413
output = TestCommon.execAuto(execArgs);
413414
output.shouldContain("The shared archive file was created by a different version or build of HotSpot");
414415
output.shouldContain("Hello World");
415416

416-
copyFile(orgJsaFile, jsa);
417417
// modify _magic, test should fail
418418
System.out.println("\n2c. Corrupt _magic, should fail\n");
419-
modifyHeaderIntField(offset_magic, 0x00000000);
419+
modifyHeaderIntField(copyFile(orgJsaFile, "modify-magic"), offset_magic, 0x00000000);
420420
output = TestCommon.execCommon(execArgs);
421421
output.shouldContain("The shared archive file has a bad magic number");
422422
output.shouldNotContain("Checksum verification failed");
423423

424-
copyFile(orgJsaFile, jsa);
425424
// modify _version, test should fail
426425
System.out.println("\n2d. Corrupt _version, should fail\n");
427-
modifyHeaderIntField(offset_version, 0x00000000);
426+
modifyHeaderIntField(copyFile(orgJsaFile, "modify-version"), offset_version, 0x00000000);
428427
output = TestCommon.execCommon(execArgs);
429428
output.shouldContain("The shared archive file has the wrong version");
430429
output.shouldNotContain("Checksum verification failed");
431430

432-
File newJsaFile = null;
433-
// modify content
431+
// modify content inside regions
434432
System.out.println("\n3. Corrupt Content, should fail\n");
435433
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());
434+
File newJsaFile = copyFile(orgJsaFile, (shared_region_name[i]));
439435
if (modifyJsaContent(i, newJsaFile)) {
440436
testAndCheck(verifyExecArgs);
441437
}
442438
}
443439

444440
// modify both header and content, test should fail
445441
System.out.println("\n4. Corrupt Header and Content, should fail\n");
446-
newJsaFile = new File(TestCommon.getNewArchiveName("header-and-content"));
447-
copyFile(orgJsaFile, newJsaFile);
448-
TestCommon.setCurrentArchiveName(newJsaFile.toString());
442+
File newJsaFile = copyFile(orgJsaFile, "header-and-content");
449443
modifyJsaHeader(newJsaFile);
450444
modifyJsaContent(0, newJsaFile); // this will not be reached since failed on header change first
451445
output = TestCommon.execCommon(execArgs);
@@ -454,20 +448,16 @@ public static void main(String... args) throws Exception {
454448

455449
// delete bytes in data section
456450
System.out.println("\n5. Delete bytes at beginning of data section, should fail\n");
457-
copyFile(orgJsaFile, jsa, true);
458-
TestCommon.setCurrentArchiveName(jsa.toString());
451+
insertOrDeleteBytes(orgJsaFile, true);
459452
testAndCheck(verifyExecArgs);
460453

461454
// insert bytes in data section forward
462455
System.out.println("\n6. Insert bytes at beginning of data section, should fail\n");
463-
copyFile(orgJsaFile, jsa, false);
456+
insertOrDeleteBytes(orgJsaFile, false);
464457
testAndCheck(verifyExecArgs);
465458

466459
System.out.println("\n7. modify Content in random areas, should fail\n");
467-
newJsaFile = new File(TestCommon.getNewArchiveName("random-areas"));
468-
copyFile(orgJsaFile, newJsaFile);
469-
TestCommon.setCurrentArchiveName(newJsaFile.toString());
470-
modifyJsaContentRandomly(newJsaFile);
460+
modifyJsaContentRandomly(copyFile(orgJsaFile, "random-areas"));
471461
testAndCheck(verifyExecArgs);
472462
}
473463
}

0 commit comments

Comments
 (0)