1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
@@ -68,8 +68,6 @@ public class SharedArchiveConsistency {
68
68
public static int size_t_size ; // size of size_t
69
69
public static int int_size ; // size of int
70
70
71
- public static File jsa ; // will be updated during test
72
- public static File orgJsaFile ; // kept the original file not touched.
73
71
// The following should be consistent with the enum in the C++ MetaspaceShared class
74
72
public static String [] shared_region_name = {
75
73
"mc" , // MiscCode
@@ -252,8 +250,8 @@ public static void modifyJsaHeader(File jsaFile) throws Exception {
252
250
}
253
251
}
254
252
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 );
257
255
int headerSize = getFileHeaderSize (fc );
258
256
System .out .println (" offset_jvm_ident " + offset_jvm_ident );
259
257
byte [] buf = new byte [256 ];
@@ -264,8 +262,8 @@ public static void modifyJvmIdent() throws Exception {
264
262
}
265
263
}
266
264
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 );
269
267
int headerSize = getFileHeaderSize (fc );
270
268
System .out .println (" offset " + offset );
271
269
byte [] buf = ByteBuffer .allocate (4 ).putInt (value ).array ();
@@ -276,23 +274,37 @@ public static void modifyHeaderIntField(long offset, int value) throws Exception
276
274
}
277
275
}
278
276
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 );
283
290
}
284
291
}
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 ;
288
299
}
289
300
290
301
// Copy file with bytes deleted or inserted
291
302
// 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" ));
293
305
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 ()
296
308
) {
297
309
long size = inputChannel .size ();
298
310
int init_size = getFileHeaderSize (inputChannel );
@@ -309,10 +321,8 @@ public static void copyFile(File from, File to, boolean del) throws Exception {
309
321
outputChannel .transferFrom (inputChannel , init_size + n , size - init_size );
310
322
}
311
323
}
312
- }
313
324
314
- public static void restoreJsaFile () throws Exception {
315
- Files .copy (orgJsaFile .toPath (), jsa .toPath (), REPLACE_EXISTING );
325
+ return newJsaFile ;
316
326
}
317
327
318
328
public static void setReadWritePermission (File file ) throws Exception {
@@ -365,7 +375,7 @@ public static void main(String... args) throws Exception {
365
375
// test, should pass
366
376
System .out .println ("1. Normal, should pass but may fail\n " );
367
377
368
- String [] execArgs = {"-Xlog:cds" , "-cp" , jarFile , "Hello" };
378
+ String [] execArgs = {"-Xlog:cds=debug " , "-cp" , jarFile , "Hello" };
369
379
// tests that corrupt contents of the archive need to run with
370
380
// VerifySharedSpaces enabled to detect inconsistencies
371
381
String [] verifyExecArgs = {"-Xlog:cds" , "-XX:+VerifySharedSpaces" , "-cp" , jarFile , "Hello" };
@@ -378,74 +388,58 @@ public static void main(String... args) throws Exception {
378
388
TestCommon .checkExecReturn (output , 1 , true , matchMessages [0 ]);
379
389
}
380
390
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!" );
385
395
}
386
396
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
397
// modify jsa header, test should fail
394
398
System .out .println ("\n 2. Corrupt header, should fail\n " );
395
- modifyJsaHeader (jsa );
399
+ modifyJsaHeader (copyFile ( orgJsaFile , "corrupt-header" ) );
396
400
output = TestCommon .execCommon (execArgs );
397
401
output .shouldContain ("The shared archive file has a bad magic number" );
398
402
output .shouldNotContain ("Checksum verification failed" );
399
403
400
- copyFile (orgJsaFile , jsa );
401
404
// modify _jvm_ident, test should fail
402
405
System .out .println ("\n 2a. Corrupt _jvm_ident, should fail\n " );
403
- modifyJvmIdent ();
406
+ modifyJvmIdent (copyFile ( orgJsaFile , "modify-jvm-ident" ) );
404
407
output = TestCommon .execCommon (execArgs );
405
408
output .shouldContain ("The shared archive file was created by a different version or build of HotSpot" );
406
409
output .shouldNotContain ("Checksum verification failed" );
407
410
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
410
412
System .out .println ("\n 2b. Corrupt _jvm_ident run with -Xshare:auto\n " );
411
- modifyJvmIdent ();
412
413
output = TestCommon .execAuto (execArgs );
413
414
output .shouldContain ("The shared archive file was created by a different version or build of HotSpot" );
414
415
output .shouldContain ("Hello World" );
415
416
416
- copyFile (orgJsaFile , jsa );
417
417
// modify _magic, test should fail
418
418
System .out .println ("\n 2c. Corrupt _magic, should fail\n " );
419
- modifyHeaderIntField (offset_magic , 0x00000000 );
419
+ modifyHeaderIntField (copyFile ( orgJsaFile , "modify-magic" ), offset_magic , 0x00000000 );
420
420
output = TestCommon .execCommon (execArgs );
421
421
output .shouldContain ("The shared archive file has a bad magic number" );
422
422
output .shouldNotContain ("Checksum verification failed" );
423
423
424
- copyFile (orgJsaFile , jsa );
425
424
// modify _version, test should fail
426
425
System .out .println ("\n 2d. Corrupt _version, should fail\n " );
427
- modifyHeaderIntField (offset_version , 0x00000000 );
426
+ modifyHeaderIntField (copyFile ( orgJsaFile , "modify-version" ), offset_version , 0x00000000 );
428
427
output = TestCommon .execCommon (execArgs );
429
428
output .shouldContain ("The shared archive file has the wrong version" );
430
429
output .shouldNotContain ("Checksum verification failed" );
431
430
432
- File newJsaFile = null ;
433
- // modify content
431
+ // modify content inside regions
434
432
System .out .println ("\n 3. Corrupt Content, should fail\n " );
435
433
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 ]));
439
435
if (modifyJsaContent (i , newJsaFile )) {
440
436
testAndCheck (verifyExecArgs );
441
437
}
442
438
}
443
439
444
440
// modify both header and content, test should fail
445
441
System .out .println ("\n 4. 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" );
449
443
modifyJsaHeader (newJsaFile );
450
444
modifyJsaContent (0 , newJsaFile ); // this will not be reached since failed on header change first
451
445
output = TestCommon .execCommon (execArgs );
@@ -454,20 +448,16 @@ public static void main(String... args) throws Exception {
454
448
455
449
// delete bytes in data section
456
450
System .out .println ("\n 5. Delete bytes at beginning of data section, should fail\n " );
457
- copyFile (orgJsaFile , jsa , true );
458
- TestCommon .setCurrentArchiveName (jsa .toString ());
451
+ insertOrDeleteBytes (orgJsaFile , true );
459
452
testAndCheck (verifyExecArgs );
460
453
461
454
// insert bytes in data section forward
462
455
System .out .println ("\n 6. Insert bytes at beginning of data section, should fail\n " );
463
- copyFile (orgJsaFile , jsa , false );
456
+ insertOrDeleteBytes (orgJsaFile , false );
464
457
testAndCheck (verifyExecArgs );
465
458
466
459
System .out .println ("\n 7. 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" ));
471
461
testAndCheck (verifyExecArgs );
472
462
}
473
463
}
0 commit comments