Skip to content

Commit de495df

Browse files
y1yang0calvinccheung
authored andcommitted
8264413: Data is written to file header even if its CRC32 was calculated
Reviewed-by: ccheung, minqi
1 parent 52d8a22 commit de495df

File tree

4 files changed

+90
-11
lines changed

4 files changed

+90
-11
lines changed

src/hotspot/share/memory/archiveBuilder.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "oops/instanceKlass.hpp"
4242
#include "oops/objArrayKlass.hpp"
4343
#include "oops/oopHandle.inline.hpp"
44+
#include "runtime/globals_extension.hpp"
4445
#include "runtime/sharedRuntime.hpp"
4546
#include "runtime/thread.hpp"
4647
#include "utilities/align.hpp"
@@ -1089,7 +1090,13 @@ void ArchiveBuilder::write_archive(FileMapInfo* mapinfo,
10891090
print_region_stats(mapinfo, closed_heap_regions, open_heap_regions);
10901091

10911092
mapinfo->set_requested_base((char*)MetaspaceShared::requested_base_address());
1093+
if (mapinfo->header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1094+
mapinfo->set_header_base_archive_name_size(strlen(Arguments::GetSharedArchivePath()) + 1);
1095+
mapinfo->set_header_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
1096+
}
10921097
mapinfo->set_header_crc(mapinfo->compute_header_crc());
1098+
// After this point, we should not write any data into mapinfo->header() since this
1099+
// would corrupt its checksum we have calculated before.
10931100
mapinfo->write_header();
10941101
mapinfo->close();
10951102

src/hotspot/share/memory/filemap.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@
5353
#include "oops/oop.inline.hpp"
5454
#include "prims/jvmtiExport.hpp"
5555
#include "runtime/arguments.hpp"
56-
#include "runtime/globals_extension.hpp"
5756
#include "runtime/java.hpp"
5857
#include "runtime/mutexLocker.hpp"
5958
#include "runtime/os.inline.hpp"
@@ -1236,17 +1235,14 @@ void FileMapInfo::open_for_write(const char* path) {
12361235
void FileMapInfo::write_header() {
12371236
_file_offset = 0;
12381237
seek_to_position(_file_offset);
1239-
char* base_archive_name = NULL;
1240-
if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1241-
base_archive_name = (char*)Arguments::GetSharedArchivePath();
1242-
header()->set_base_archive_name_size(strlen(base_archive_name) + 1);
1243-
header()->set_base_archive_is_default(FLAG_IS_DEFAULT(SharedArchiveFile));
1244-
}
1245-
12461238
assert(is_file_position_aligned(), "must be");
12471239
write_bytes(header(), header()->header_size());
1248-
if (base_archive_name != NULL) {
1249-
write_bytes(base_archive_name, header()->base_archive_name_size());
1240+
1241+
if (header()->magic() == CDS_DYNAMIC_ARCHIVE_MAGIC) {
1242+
char* base_archive_name = (char*)Arguments::GetSharedArchivePath();
1243+
if (base_archive_name != NULL) {
1244+
write_bytes(base_archive_name, header()->base_archive_name_size());
1245+
}
12501246
}
12511247
}
12521248

src/hotspot/share/memory/filemap.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ class FileMapHeader: private CDSFileMapHeaderBase {
241241
void set_as_offset(char* p, size_t *offset);
242242
public:
243243
// Accessors -- fields declared in CDSFileMapHeaderBase
244-
unsigned int magic() const {return _magic;}
244+
unsigned int magic() const { return _magic; }
245245
int crc() const { return _crc; }
246246
int version() const { return _version; }
247247

@@ -383,13 +383,17 @@ class FileMapInfo : public CHeapObj<mtInternal> {
383383
void invalidate();
384384
int crc() const { return header()->crc(); }
385385
int version() const { return header()->version(); }
386+
unsigned int magic() const { return header()->magic(); }
386387
address narrow_oop_base() const { return header()->narrow_oop_base(); }
387388
int narrow_oop_shift() const { return header()->narrow_oop_shift(); }
388389
uintx max_heap_size() const { return header()->max_heap_size(); }
389390
address narrow_klass_base() const { return header()->narrow_klass_base(); }
390391
int narrow_klass_shift() const { return header()->narrow_klass_shift(); }
391392
size_t core_region_alignment() const { return header()->core_region_alignment(); }
392393

394+
void set_header_base_archive_name_size(size_t size) { header()->set_base_archive_name_size(size); }
395+
void set_header_base_archive_is_default(bool is_default) { header()->set_base_archive_is_default(is_default); }
396+
393397
CompressedOops::Mode narrow_oop_mode() const { return header()->narrow_oop_mode(); }
394398
jshort app_module_paths_start_index() const { return header()->app_module_paths_start_index(); }
395399
jshort app_class_paths_start_index() const { return header()->app_class_paths_start_index(); }
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright (c) 2021, Alibaba Group Holding Limited. 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+
* @bug 8264413
28+
* @summary test dynamic cds archive when turning on VerifySharedSpaces
29+
* @requires vm.cds
30+
* @library /test/lib /test/hotspot/jtreg/runtime/cds/appcds
31+
* @compile ../test-classes/Hello.java
32+
* @compile ../test-classes/HelloMore.java
33+
* @build sun.hotspot.WhiteBox
34+
* @run driver jdk.test.lib.helpers.ClassFileInstaller sun.hotspot.WhiteBox
35+
* @run main/othervm -XX:+UnlockDiagnosticVMOptions -XX:+WhiteBoxAPI -Xbootclasspath/a:. VerifyWithDynamicArchive
36+
*/
37+
38+
public class VerifyWithDynamicArchive extends DynamicArchiveTestBase {
39+
40+
public static void main(String[] args) throws Exception {
41+
runTest(VerifyWithDynamicArchive::testDefaultBase);
42+
}
43+
44+
static void testDefaultBase() throws Exception {
45+
String topArchiveName = getNewArchiveName("top");
46+
doTest(topArchiveName);
47+
}
48+
49+
private static void doTest(String topArchiveName) throws Exception {
50+
String appJar = JarBuilder.getOrCreateHelloJar();
51+
52+
dump(topArchiveName,
53+
"-Xlog:cds",
54+
"-Xlog:cds+dynamic=debug",
55+
"-cp", appJar, "Hello")
56+
.assertNormalExit(output -> {
57+
output.shouldContain("Written dynamic archive 0x");
58+
});
59+
60+
run(topArchiveName,
61+
"-Xlog:class+load",
62+
"-Xlog:cds+dynamic=debug,cds=debug",
63+
"-XX:+VerifySharedSpaces",
64+
"-cp", appJar,
65+
"Hello")
66+
.assertNormalExit(output -> {
67+
output.shouldContain("Hello source: shared objects file")
68+
.shouldNotContain("Header checksum verification failed")
69+
.shouldHaveExitValue(0);
70+
});
71+
}
72+
}

0 commit comments

Comments
 (0)