annotate src/hotspot/share/memory/filemap.cpp @ 52424:72d4e10305b9

8212995: Placing the Integer.IntegerCache and cached Integer objects in the closed archive heap region. Summary: Support shareable archive object subgraphs in closed archive heap regions. Reviewed-by: iklam, ccheung
author jiangli
date Sat, 03 Nov 2018 15:40:19 -0400
parents 625f6c742392
children f4e3900c8d08
rev   line source
duke@1 1 /*
iklam@48795 2 * Copyright (c) 2003, 2018, Oracle and/or its affiliates. All rights reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation.
duke@1 8 *
duke@1 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 13 * accompanied this code).
duke@1 14 *
duke@1 15 * You should have received a copy of the GNU General Public License version
duke@1 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 18 *
trims@5547 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@5547 20 * or visit www.oracle.com if you need additional information or have any
trims@5547 21 * questions.
duke@1 22 *
duke@1 23 */
duke@1 24
stefank@7397 25 #include "precompiled.hpp"
coleenp@47765 26 #include "jvm.h"
hseigel@49345 27 #include "classfile/classLoader.inline.hpp"
iklam@50049 28 #include "classfile/classLoaderExt.hpp"
stefank@7397 29 #include "classfile/symbolTable.hpp"
iklam@26135 30 #include "classfile/systemDictionaryShared.hpp"
hseigel@14577 31 #include "classfile/altHashing.hpp"
rprotacio@46522 32 #include "logging/log.hpp"
stuefe@46701 33 #include "logging/logStream.hpp"
rprotacio@46522 34 #include "logging/logMessage.hpp"
stefank@7397 35 #include "memory/filemap.hpp"
iklam@51508 36 #include "memory/heapShared.inline.hpp"
iklam@51508 37 #include "memory/iterator.inline.hpp"
iklam@26135 38 #include "memory/metadataFactory.hpp"
iklam@46746 39 #include "memory/metaspaceClosure.hpp"
iklam@46746 40 #include "memory/metaspaceShared.hpp"
iklam@26135 41 #include "memory/oopFactory.hpp"
stefank@49601 42 #include "oops/compressedOops.inline.hpp"
iklam@26135 43 #include "oops/objArrayOop.hpp"
iklam@51508 44 #include "oops/oop.inline.hpp"
cjplummer@31332 45 #include "prims/jvmtiExport.hpp"
stefank@7397 46 #include "runtime/arguments.hpp"
stefank@7397 47 #include "runtime/java.hpp"
iklam@51497 48 #include "runtime/os.inline.hpp"
goetz@25715 49 #include "runtime/vm_version.hpp"
zgu@14120 50 #include "services/memTracker.hpp"
stefank@46625 51 #include "utilities/align.hpp"
stefank@7397 52 #include "utilities/defaultStream.hpp"
stefank@49992 53 #if INCLUDE_G1GC
stefank@49601 54 #include "gc/g1/g1CollectedHeap.hpp"
iklam@51508 55 #include "gc/g1/heapRegion.hpp"
stefank@49601 56 #endif
stefank@7397 57
duke@1 58 # include <sys/stat.h>
duke@1 59 # include <errno.h>
duke@1 60
duke@1 61 #ifndef O_BINARY // if defined (Win32) use binary files.
duke@1 62 #define O_BINARY 0 // otherwise do nothing.
duke@1 63 #endif
duke@1 64
duke@1 65 extern address JVM_FunctionAtStart();
duke@1 66 extern address JVM_FunctionAtEnd();
duke@1 67
twisti@2131 68 // Complain and stop. All error conditions occurring during the writing of
duke@1 69 // an archive file should stop the process. Unrecoverable errors during
duke@1 70 // the reading of the archive file should stop the process.
duke@1 71
duke@1 72 static void fail(const char *msg, va_list ap) {
duke@1 73 // This occurs very early during initialization: tty is not initialized.
duke@1 74 jio_fprintf(defaultStream::error_stream(),
twisti@2131 75 "An error has occurred while processing the"
duke@1 76 " shared archive file.\n");
duke@1 77 jio_vfprintf(defaultStream::error_stream(), msg, ap);
duke@1 78 jio_fprintf(defaultStream::error_stream(), "\n");
hseigel@19707 79 // Do not change the text of the below message because some tests check for it.
duke@1 80 vm_exit_during_initialization("Unable to use shared archive.", NULL);
duke@1 81 }
duke@1 82
duke@1 83
duke@1 84 void FileMapInfo::fail_stop(const char *msg, ...) {
duke@1 85 va_list ap;
duke@1 86 va_start(ap, msg);
duke@1 87 fail(msg, ap); // Never returns.
duke@1 88 va_end(ap); // for completeness.
duke@1 89 }
duke@1 90
duke@1 91
duke@1 92 // Complain and continue. Recoverable errors during the reading of the
duke@1 93 // archive file may continue (with sharing disabled).
duke@1 94 //
duke@1 95 // If we continue, then disable shared spaces and close the file.
duke@1 96
duke@1 97 void FileMapInfo::fail_continue(const char *msg, ...) {
duke@1 98 va_list ap;
duke@1 99 va_start(ap, msg);
iklam@26135 100 MetaspaceShared::set_archive_loading_failed();
ccheung@49748 101 if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
iklam@26135 102 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
iklam@26135 103 // do not validate, we can still continue "limping" to validate the remaining
iklam@26135 104 // entries. No need to quit.
iklam@26135 105 tty->print("[");
iklam@26135 106 tty->vprint(msg, ap);
iklam@26135 107 tty->print_cr("]");
mseledtsov@23186 108 } else {
iklam@26135 109 if (RequireSharedSpaces) {
iklam@26135 110 fail(msg, ap);
iklam@26135 111 } else {
rprotacio@46522 112 if (log_is_enabled(Info, cds)) {
rprotacio@46522 113 ResourceMark rm;
stuefe@46701 114 LogStream ls(Log(cds)::info());
stuefe@46701 115 ls.print("UseSharedSpaces: ");
stuefe@46701 116 ls.vprint_cr(msg, ap);
iklam@26135 117 }
mseledtsov@23186 118 }
iklam@28017 119 UseSharedSpaces = false;
iklam@28017 120 assert(current_info() != NULL, "singleton must be registered");
iklam@28017 121 current_info()->close();
duke@1 122 }
duke@1 123 va_end(ap);
duke@1 124 }
duke@1 125
hseigel@14577 126 // Fill in the fileMapInfo structure with data about this VM instance.
duke@1 127
hseigel@14577 128 // This method copies the vm version info into header_version. If the version is too
hseigel@14577 129 // long then a truncated version, which has a hash code appended to it, is copied.
hseigel@14577 130 //
hseigel@14577 131 // Using a template enables this method to verify that header_version is an array of
hseigel@14577 132 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and
hseigel@14577 133 // the code that reads the CDS file will both use the same size buffer. Hence, will
hseigel@14577 134 // use identical truncation. This is necessary for matching of truncated versions.
hseigel@14577 135 template <int N> static void get_header_version(char (&header_version) [N]) {
hseigel@14577 136 assert(N == JVM_IDENT_MAX, "Bad header_version size");
hseigel@14577 137
hseigel@14577 138 const char *vm_version = VM_Version::internal_vm_info_string();
hseigel@14577 139 const int version_len = (int)strlen(vm_version);
hseigel@14577 140
hseigel@14577 141 if (version_len < (JVM_IDENT_MAX-1)) {
hseigel@14577 142 strcpy(header_version, vm_version);
hseigel@14577 143
hseigel@14577 144 } else {
hseigel@14577 145 // Get the hash value. Use a static seed because the hash needs to return the same
hseigel@14577 146 // value over multiple jvm invocations.
hseigel@14577 147 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
hseigel@14577 148
hseigel@14577 149 // Truncate the ident, saving room for the 8 hex character hash value.
hseigel@14577 150 strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
hseigel@14577 151
hseigel@14577 152 // Append the hash code as eight hex digits.
hseigel@14577 153 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
hseigel@14577 154 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
hseigel@14577 155 }
hseigel@14577 156 }
duke@1 157
iklam@26135 158 FileMapInfo::FileMapInfo() {
iklam@26135 159 assert(_current_info == NULL, "must be singleton"); // not thread safe
iklam@26135 160 _current_info = this;
jwilhelm@46630 161 memset((void*)this, 0, sizeof(FileMapInfo));
iklam@26135 162 _file_offset = 0;
iklam@26135 163 _file_open = false;
iklam@51494 164 _header = (FileMapHeader*)os::malloc(sizeof(FileMapHeader), mtInternal);
iklam@51494 165 _header->_version = INVALID_CDS_ARCHIVE_VERSION;
iklam@50049 166 _header->_has_platform_or_app_classes = true;
iklam@26135 167 }
iklam@26135 168
iklam@26135 169 FileMapInfo::~FileMapInfo() {
iklam@26135 170 assert(_current_info == this, "must be singleton"); // not thread safe
iklam@26135 171 _current_info = NULL;
iklam@26135 172 }
iklam@26135 173
duke@1 174 void FileMapInfo::populate_header(size_t alignment) {
iklam@26135 175 _header->populate(this, alignment);
iklam@26135 176 }
iklam@26135 177
iklam@51494 178 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
iklam@51494 179 _magic = CDS_ARCHIVE_MAGIC;
iklam@51494 180 _version = CURRENT_CDS_ARCHIVE_VERSION;
iklam@26135 181 _alignment = alignment;
iklam@26135 182 _obj_alignment = ObjectAlignmentInBytes;
thartmann@33628 183 _compact_strings = CompactStrings;
jiangli@31345 184 _narrow_oop_mode = Universe::narrow_oop_mode();
jiangli@47599 185 _narrow_oop_base = Universe::narrow_oop_base();
jiangli@31345 186 _narrow_oop_shift = Universe::narrow_oop_shift();
jiangli@31345 187 _max_heap_size = MaxHeapSize;
coleenp@31362 188 _narrow_klass_base = Universe::narrow_klass_base();
coleenp@31362 189 _narrow_klass_shift = Universe::narrow_klass_shift();
ccheung@49748 190 _shared_path_table_size = mapinfo->_shared_path_table_size;
ccheung@49748 191 _shared_path_table = mapinfo->_shared_path_table;
ccheung@49748 192 _shared_path_entry_size = mapinfo->_shared_path_entry_size;
jiangli@52082 193 if (HeapShared::is_heap_object_archiving_allowed()) {
iklam@51508 194 _heap_reserved = Universe::heap()->reserved_region();
iklam@51508 195 }
duke@1 196
duke@1 197 // The following fields are for sanity checks for whether this archive
duke@1 198 // will function correctly with this JVM and the bootclasspath it's
duke@1 199 // invoked with.
duke@1 200
duke@1 201 // JVM version string ... changes on each build.
iklam@26135 202 get_header_version(_jvm_ident);
iklam@50049 203
iklam@50049 204 ClassLoaderExt::finalize_shared_paths_misc_info();
iklam@50049 205 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
iklam@50049 206 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
ccheung@51456 207 _max_used_path_index = ClassLoaderExt::max_used_path_index();
iklam@50049 208
iklam@50049 209 _verify_local = BytecodeVerificationLocal;
iklam@50049 210 _verify_remote = BytecodeVerificationRemote;
iklam@50049 211 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
iklam@26135 212 }
duke@1 213
jiangli@50209 214 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
jiangli@50209 215 assert(DumpSharedSpaces, "dump time only");
iklam@46746 216 _timestamp = 0;
iklam@46746 217 _filesize = 0;
duke@1 218
iklam@46746 219 struct stat st;
iklam@46746 220 if (os::stat(name, &st) == 0) {
iklam@46746 221 if ((st.st_mode & S_IFMT) == S_IFDIR) {
jiangli@50209 222 _type = dir_entry;
iklam@46746 223 } else {
jiangli@50209 224 // The timestamp of the modules_image is not checked at runtime.
jiangli@50209 225 if (is_modules_image) {
jiangli@50209 226 _type = modules_image_entry;
jiangli@50209 227 } else {
jiangli@50209 228 _type = jar_entry;
jiangli@50209 229 _timestamp = st.st_mtime;
jiangli@50209 230 }
iklam@46746 231 _filesize = st.st_size;
iklam@46746 232 }
iklam@46746 233 } else {
iklam@46746 234 // The file/dir must exist, or it would not have been added
iklam@46746 235 // into ClassLoader::classpath_entry().
iklam@46746 236 //
iklam@46746 237 // If we can't access a jar file in the boot path, then we can't
iklam@46746 238 // make assumptions about where classes get loaded from.
iklam@46746 239 FileMapInfo::fail_stop("Unable to open file %s.", name);
iklam@46746 240 }
iklam@26135 241
iklam@46746 242 size_t len = strlen(name) + 1;
iklam@46746 243 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
iklam@46746 244 strcpy(_name->data(), name);
iklam@46746 245 }
iklam@26135 246
ccheung@49748 247 bool SharedClassPathEntry::validate(bool is_class_path) {
jiangli@49941 248 assert(UseSharedSpaces, "runtime only");
jiangli@49941 249
iklam@46746 250 struct stat st;
jiangli@50209 251 const char* name;
jiangli@50209 252
jiangli@50209 253 // In order to validate the runtime modules image file size against the archived
jiangli@50209 254 // size information, we need to obtain the runtime modules image path. The recorded
jiangli@50209 255 // dump time modules image path in the archive may be different from the runtime path
jiangli@50209 256 // if the JDK image has beed moved after generating the archive.
jiangli@50209 257 if (is_modules_image()) {
jiangli@50209 258 name = ClassLoader::get_jrt_entry()->name();
jiangli@50209 259 } else {
jiangli@50209 260 name = this->name();
jiangli@50209 261 }
jiangli@50209 262
iklam@46746 263 bool ok = true;
iklam@46746 264 log_info(class, path)("checking shared classpath entry: %s", name);
ccheung@49748 265 if (os::stat(name, &st) != 0 && is_class_path) {
ccheung@49748 266 // If the archived module path entry does not exist at runtime, it is not fatal
ccheung@49748 267 // (no need to invalid the shared archive) because the shared runtime visibility check
ccheung@49748 268 // filters out any archived module classes that do not have a matching runtime
ccheung@49748 269 // module path location.
iklam@46746 270 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
iklam@46746 271 ok = false;
iklam@46746 272 } else if (is_dir()) {
iklam@46746 273 if (!os::dir_is_empty(name)) {
iklam@46746 274 FileMapInfo::fail_continue("directory is not empty: %s", name);
iklam@46746 275 ok = false;
lfoltan@40013 276 }
jiangli@50209 277 } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
jiangli@50209 278 _filesize != st.st_size) {
jiangli@50209 279 ok = false;
jiangli@50209 280 if (PrintSharedArchiveAndExit) {
jiangli@50209 281 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
jiangli@50209 282 "Timestamp mismatch" :
jiangli@50209 283 "File size mismatch");
jiangli@50209 284 } else {
jiangli@50209 285 FileMapInfo::fail_continue("A jar file is not the one used while building"
jiangli@50209 286 " the shared archive file: %s", name);
iklam@26135 287 }
iklam@46746 288 }
iklam@46746 289 return ok;
iklam@46746 290 }
duke@1 291
iklam@46746 292 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
iklam@46746 293 it->push(&_name);
iklam@46746 294 it->push(&_manifest);
iklam@46746 295 }
iklam@46746 296
ccheung@49748 297 void FileMapInfo::allocate_shared_path_table() {
jiangli@47991 298 assert(DumpSharedSpaces, "Sanity");
jiangli@47991 299
iklam@46746 300 Thread* THREAD = Thread::current();
iklam@46746 301 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
jiangli@47991 302 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
jiangli@47991 303
jiangli@47991 304 assert(jrt != NULL,
jiangli@47991 305 "No modular java runtime image present when allocating the CDS classpath entry table");
jiangli@47991 306
iklam@50049 307 size_t entry_size = sizeof(SharedClassPathEntry); // assert ( should be 8 byte aligned??)
jiangli@47991 308 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
jiangli@47991 309 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
ccheung@49748 310 int num_module_path_entries = ClassLoader::num_module_path_entries();
ccheung@49748 311 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
iklam@46746 312 size_t bytes = entry_size * num_entries;
iklam@46746 313
ccheung@49748 314 _shared_path_table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
ccheung@49748 315 _shared_path_table_size = num_entries;
ccheung@49748 316 _shared_path_entry_size = entry_size;
iklam@46746 317
jiangli@47991 318 // 1. boot class path
jiangli@47991 319 int i = 0;
jiangli@47991 320 ClassPathEntry* cpe = jrt;
jiangli@47991 321 while (cpe != NULL) {
jiangli@50209 322 bool is_jrt = (cpe == jrt);
jiangli@50209 323 const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
iklam@46746 324 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
ccheung@49748 325 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 326 ent->init(cpe->name(), is_jrt, THREAD);
jiangli@50209 327 if (!is_jrt) { // No need to do the modules image.
iklam@26135 328 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
iklam@50049 329 update_shared_classpath(cpe, ent, THREAD);
duke@1 330 }
jiangli@47991 331 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
jiangli@47991 332 i++;
duke@1 333 }
jiangli@47991 334 assert(i == num_boot_classpath_entries,
jiangli@47991 335 "number of boot class path entry mismatch");
jiangli@47991 336
jiangli@47991 337 // 2. app class path
jiangli@47991 338 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
jiangli@47991 339 while (acpe != NULL) {
jiangli@47991 340 log_info(class, path)("add app shared path %s", acpe->name());
ccheung@49748 341 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 342 ent->init(acpe->name(), false, THREAD);
jiangli@47991 343 EXCEPTION_MARK;
iklam@50049 344 update_shared_classpath(acpe, ent, THREAD);
jiangli@47991 345 acpe = acpe->next();
ccheung@49748 346 i++;
jiangli@47991 347 }
ccheung@49748 348
ccheung@49748 349 // 3. module path
ccheung@49748 350 ClassPathEntry *mpe = ClassLoader::module_path_entries();
ccheung@49748 351 while (mpe != NULL) {
ccheung@49748 352 log_info(class, path)("add module path %s",mpe->name());
ccheung@49748 353 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 354 ent->init(mpe->name(), false, THREAD);
ccheung@49748 355 EXCEPTION_MARK;
iklam@50049 356 update_shared_classpath(mpe, ent, THREAD);
ccheung@49748 357 mpe = mpe->next();
ccheung@49748 358 i++;
ccheung@49748 359 }
ccheung@49748 360 assert(i == num_entries, "number of shared path entry mismatch");
duke@1 361 }
duke@1 362
jiangli@49941 363 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
jiangli@49941 364 assert(DumpSharedSpaces, "dump time only");
jiangli@49941 365
jiangli@49941 366 bool has_nonempty_dir = false;
jiangli@49941 367
ccheung@51456 368 int last = _shared_path_table_size - 1;
ccheung@51456 369 if (last > ClassLoaderExt::max_used_path_index()) {
ccheung@51456 370 // no need to check any path beyond max_used_path_index
ccheung@51456 371 last = ClassLoaderExt::max_used_path_index();
jiangli@49941 372 }
jiangli@49941 373
ccheung@51456 374 for (int i = 0; i <= last; i++) {
jiangli@49941 375 SharedClassPathEntry *e = shared_path(i);
jiangli@49941 376 if (e->is_dir()) {
jiangli@49941 377 const char* path = e->name();
jiangli@49941 378 if (!os::dir_is_empty(path)) {
jiangli@49941 379 tty->print_cr("Error: non-empty directory '%s'", path);
jiangli@49941 380 has_nonempty_dir = true;
jiangli@49941 381 }
jiangli@49941 382 }
jiangli@49941 383 }
jiangli@49941 384
jiangli@49941 385 if (has_nonempty_dir) {
jiangli@49941 386 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
jiangli@49941 387 }
jiangli@49941 388 }
jiangli@49941 389
iklam@50049 390 class ManifestStream: public ResourceObj {
iklam@50049 391 private:
iklam@50049 392 u1* _buffer_start; // Buffer bottom
iklam@50049 393 u1* _buffer_end; // Buffer top (one past last element)
iklam@50049 394 u1* _current; // Current buffer position
iklam@50049 395
iklam@50049 396 public:
iklam@50049 397 // Constructor
iklam@50049 398 ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
iklam@50049 399 _current(buffer) {
iklam@50049 400 _buffer_end = buffer + length;
iklam@50049 401 }
iklam@50049 402
iklam@50049 403 static bool is_attr(u1* attr, const char* name) {
iklam@50049 404 return strncmp((const char*)attr, name, strlen(name)) == 0;
iklam@50049 405 }
iklam@50049 406
iklam@50049 407 static char* copy_attr(u1* value, size_t len) {
iklam@50049 408 char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
iklam@50049 409 strncpy(buf, (char*)value, len);
iklam@50049 410 buf[len] = 0;
iklam@50049 411 return buf;
iklam@50049 412 }
iklam@50049 413
iklam@50049 414 // The return value indicates if the JAR is signed or not
iklam@50049 415 bool check_is_signed() {
iklam@50049 416 u1* attr = _current;
iklam@50049 417 bool isSigned = false;
iklam@50049 418 while (_current < _buffer_end) {
iklam@50049 419 if (*_current == '\n') {
iklam@50049 420 *_current = '\0';
iklam@50049 421 u1* value = (u1*)strchr((char*)attr, ':');
iklam@50049 422 if (value != NULL) {
iklam@50049 423 assert(*(value+1) == ' ', "Unrecognized format" );
iklam@50049 424 if (strstr((char*)attr, "-Digest") != NULL) {
iklam@50049 425 isSigned = true;
iklam@50049 426 break;
iklam@50049 427 }
iklam@50049 428 }
iklam@50049 429 *_current = '\n'; // restore
iklam@50049 430 attr = _current + 1;
iklam@50049 431 }
iklam@50049 432 _current ++;
iklam@50049 433 }
iklam@50049 434 return isSigned;
iklam@50049 435 }
iklam@50049 436 };
iklam@50049 437
iklam@50049 438 void FileMapInfo::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
iklam@50049 439 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
iklam@50049 440 ResourceMark rm(THREAD);
iklam@50049 441 jint manifest_size;
iklam@50049 442
iklam@50049 443 if (cpe->is_jar_file()) {
jiangli@50209 444 assert(ent->is_jar(), "the shared class path entry is not a JAR file");
iklam@50049 445 char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
iklam@50049 446 if (manifest != NULL) {
iklam@50049 447 ManifestStream* stream = new ManifestStream((u1*)manifest,
iklam@50049 448 manifest_size);
jiangli@50209 449 if (stream->check_is_signed()) {
jiangli@50209 450 ent->set_is_signed();
iklam@50049 451 } else {
iklam@50049 452 // Copy the manifest into the shared archive
iklam@50049 453 manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
iklam@50049 454 Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
iklam@50049 455 manifest_size,
iklam@50049 456 THREAD);
iklam@50049 457 char* p = (char*)(buf->data());
iklam@50049 458 memcpy(p, manifest, manifest_size);
iklam@50049 459 ent->set_manifest(buf);
iklam@50049 460 }
iklam@50049 461 }
iklam@50049 462 }
iklam@50049 463 }
iklam@50049 464
iklam@50049 465
ccheung@49748 466 bool FileMapInfo::validate_shared_path_table() {
jiangli@49941 467 assert(UseSharedSpaces, "runtime only");
jiangli@49941 468
ccheung@49748 469 _validating_shared_path_table = true;
ccheung@49748 470 _shared_path_table = _header->_shared_path_table;
ccheung@49748 471 _shared_path_entry_size = _header->_shared_path_entry_size;
ccheung@49748 472 _shared_path_table_size = _header->_shared_path_table_size;
iklam@26135 473
iklam@50049 474 int module_paths_start_index = _header->_app_module_paths_start_index;
ccheung@49748 475
ccheung@51456 476 // validate the path entries up to the _max_used_path_index
ccheung@51456 477 for (int i=0; i < _header->_max_used_path_index + 1; i++) {
ccheung@49748 478 if (i < module_paths_start_index) {
ccheung@49748 479 if (shared_path(i)->validate()) {
ccheung@49748 480 log_info(class, path)("ok");
ccheung@49748 481 }
ccheung@49748 482 } else if (i >= module_paths_start_index) {
ccheung@49748 483 if (shared_path(i)->validate(false /* not a class path entry */)) {
ccheung@49748 484 log_info(class, path)("ok");
ccheung@49748 485 }
iklam@26135 486 } else if (!PrintSharedArchiveAndExit) {
ccheung@49748 487 _validating_shared_path_table = false;
ccheung@49748 488 _shared_path_table = NULL;
ccheung@49748 489 _shared_path_table_size = 0;
iklam@26135 490 return false;
iklam@26135 491 }
iklam@26135 492 }
iklam@26135 493
ccheung@49748 494 _validating_shared_path_table = false;
iklam@26135 495 return true;
iklam@26135 496 }
iklam@26135 497
duke@1 498 // Read the FileMapInfo information from the file.
duke@1 499
duke@1 500 bool FileMapInfo::init_from_file(int fd) {
iklam@51494 501 size_t sz = sizeof(FileMapHeader);
iklam@51494 502 size_t n = os::read(fd, _header, (unsigned int)sz);
iklam@26135 503 if (n != sz) {
duke@1 504 fail_continue("Unable to read the file header.");
duke@1 505 return false;
duke@1 506 }
iklam@51494 507 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
duke@1 508 fail_continue("The shared archive file has the wrong version.");
duke@1 509 return false;
duke@1 510 }
duke@1 511 _file_offset = (long)n;
iklam@26135 512
iklam@26135 513 size_t info_size = _header->_paths_misc_info_size;
iklam@26135 514 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
iklam@26135 515 if (_paths_misc_info == NULL) {
iklam@26135 516 fail_continue("Unable to read the file header.");
iklam@26135 517 return false;
iklam@26135 518 }
iklam@26135 519 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
iklam@26135 520 if (n != info_size) {
iklam@26135 521 fail_continue("Unable to read the shared path info header.");
coleenp@27880 522 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
iklam@26135 523 _paths_misc_info = NULL;
iklam@26135 524 return false;
iklam@26135 525 }
iklam@26135 526
jiangli@27025 527 size_t len = lseek(fd, 0, SEEK_END);
iklam@51494 528 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
jiangli@46810 529 // The last space might be empty
jiangli@46810 530 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
jiangli@27025 531 fail_continue("The shared archive file has been truncated.");
jiangli@27025 532 return false;
jiangli@27025 533 }
jiangli@27025 534
iklam@26135 535 _file_offset += (long)n;
duke@1 536 return true;
duke@1 537 }
duke@1 538
duke@1 539
duke@1 540 // Read the FileMapInfo information from the file.
duke@1 541 bool FileMapInfo::open_for_read() {
duke@1 542 _full_path = Arguments::GetSharedArchivePath();
duke@1 543 int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
duke@1 544 if (fd < 0) {
duke@1 545 if (errno == ENOENT) {
duke@1 546 // Not locating the shared archive is ok.
duke@1 547 fail_continue("Specified shared archive not found.");
duke@1 548 } else {
duke@1 549 fail_continue("Failed to open shared archive file (%s).",
stuefe@37113 550 os::strerror(errno));
duke@1 551 }
duke@1 552 return false;
duke@1 553 }
duke@1 554
duke@1 555 _fd = fd;
duke@1 556 _file_open = true;
duke@1 557 return true;
duke@1 558 }
duke@1 559
duke@1 560
duke@1 561 // Write the FileMapInfo information to the file.
duke@1 562
duke@1 563 void FileMapInfo::open_for_write() {
mlarsson@48963 564 _full_path = Arguments::GetSharedArchivePath();
mlarsson@48963 565 LogMessage(cds) msg;
mlarsson@48963 566 if (msg.is_info()) {
mlarsson@48963 567 msg.info("Dumping shared data to file: ");
mlarsson@48963 568 msg.info(" %s", _full_path);
duke@1 569 }
duke@1 570
hseigel@15461 571 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file.
hseigel@15461 572 chmod(_full_path, _S_IREAD | _S_IWRITE);
hseigel@15461 573 #endif
hseigel@15461 574
hseigel@15461 575 // Use remove() to delete the existing file because, on Unix, this will
hseigel@15461 576 // allow processes that have it open continued access to the file.
duke@1 577 remove(_full_path);
duke@1 578 int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
duke@1 579 if (fd < 0) {
hseigel@28950 580 fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
stuefe@37113 581 os::strerror(errno));
duke@1 582 }
duke@1 583 _fd = fd;
duke@1 584 _file_offset = 0;
duke@1 585 _file_open = true;
duke@1 586 }
duke@1 587
duke@1 588
duke@1 589 // Write the header to the file, seek to the next allocation boundary.
duke@1 590
duke@1 591 void FileMapInfo::write_header() {
iklam@26135 592 int info_size = ClassLoader::get_shared_paths_misc_info_size();
iklam@26135 593
iklam@26135 594 _header->_paths_misc_info_size = info_size;
iklam@26135 595
iklam@26135 596 align_file_position();
iklam@51494 597 write_bytes(_header, sizeof(FileMapHeader));
iklam@51494 598 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
iklam@26135 599 align_file_position();
duke@1 600 }
duke@1 601
duke@1 602
duke@1 603 // Dump region to file.
duke@1 604
duke@1 605 void FileMapInfo::write_region(int region, char* base, size_t size,
iklam@46746 606 bool read_only, bool allow_exec) {
iklam@51494 607 CDSFileMapRegion* si = space_at(region);
duke@1 608
duke@1 609 if (_file_open) {
duke@1 610 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
iklam@46746 611 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
iklam@46746 612 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
rprotacio@46522 613 region, size, p2i(base), _file_offset);
duke@1 614 } else {
duke@1 615 si->_file_offset = _file_offset;
duke@1 616 }
jiangli@52424 617 if (HeapShared::is_heap_region(region)) {
jiangli@31345 618 assert((base - (char*)Universe::narrow_oop_base()) % HeapWordSize == 0, "Sanity");
jiangli@31345 619 if (base != NULL) {
stefank@49601 620 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)base);
jiangli@31345 621 } else {
jiangli@31345 622 si->_addr._offset = 0;
jiangli@31345 623 }
jiangli@31345 624 } else {
jiangli@31345 625 si->_addr._base = base;
jiangli@31345 626 }
duke@1 627 si->_used = size;
duke@1 628 si->_read_only = read_only;
duke@1 629 si->_allow_exec = allow_exec;
jiangli@27025 630 si->_crc = ClassLoader::crc32(0, base, (jint)size);
goetz@51085 631 if (base != NULL) {
iklam@51494 632 write_bytes_aligned(base, size);
goetz@51085 633 }
duke@1 634 }
duke@1 635
jiangli@46810 636 // Write out the given archive heap memory regions. GC code combines multiple
jiangli@46810 637 // consecutive archive GC regions into one MemRegion whenever possible and
jiangli@46810 638 // produces the 'heap_mem' array.
jiangli@31345 639 //
jiangli@46810 640 // If the archive heap memory size is smaller than a single dump time GC region
jiangli@46810 641 // size, there is only one MemRegion in the array.
iklam@46746 642 //
jiangli@46810 643 // If the archive heap memory size is bigger than one dump time GC region size,
jiangli@46810 644 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
jiangli@46810 645 // the first/bottom archive GC region is a partial GC region (with the empty
jiangli@46810 646 // portion at the higher address within the region), one MemRegion is used for
jiangli@46810 647 // the bottom partial archive GC region. The rest of the consecutive archive
jiangli@46810 648 // GC regions are combined into another MemRegion.
iklam@46746 649 //
jiangli@46810 650 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
jiangli@46810 651 // + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
jiangli@46810 652 // + We have 1 or 2 consolidated heap memory regions: r0 and r1
jiangli@46810 653 //
jiangli@46810 654 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
iklam@46746 655 // Otherwise:
iklam@46746 656 //
iklam@46746 657 // "X" represented space that's occupied by heap objects.
iklam@46746 658 // "_" represented unused spaced in the heap region.
iklam@46746 659 //
iklam@46746 660 //
iklam@51508 661 // |ah0 | ah1 | ah2| ...... | ahn|
iklam@46746 662 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
jiangli@46810 663 // |<-r0->| |<- r1 ----------------->|
iklam@46746 664 // ^^^
iklam@46746 665 // |
jiangli@46810 666 // +-- gap
jiangli@46810 667 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
iklam@51508 668 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
jiangli@46810 669 int first_region_id, int max_num_regions) {
jiangli@46810 670 assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
iklam@46746 671
jiangli@46810 672 int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
jiangli@46810 673 if(arr_len > max_num_regions) {
jiangli@46810 674 fail_stop("Unable to write archive heap memory regions: "
jiangli@46810 675 "number of memory regions exceeds maximum due to fragmentation");
jiangli@46810 676 }
jiangli@46810 677
jiangli@46810 678 size_t total_size = 0;
jiangli@46810 679 for (int i = first_region_id, arr_idx = 0;
jiangli@46810 680 i < first_region_id + max_num_regions;
jiangli@46810 681 i++, arr_idx++) {
jiangli@31345 682 char* start = NULL;
jiangli@31345 683 size_t size = 0;
jiangli@46810 684 if (arr_idx < arr_len) {
jiangli@46810 685 start = (char*)heap_mem->at(arr_idx).start();
jiangli@46810 686 size = heap_mem->at(arr_idx).byte_size();
jiangli@46810 687 total_size += size;
jiangli@31345 688 }
jiangli@46810 689
jiangli@46810 690 log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
iklam@46746 691 i, p2i(start), p2i(start + size), size);
iklam@46746 692 write_region(i, start, size, false, false);
iklam@51508 693 if (size > 0) {
iklam@51508 694 space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap;
iklam@51508 695 space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits;
iklam@51508 696 }
jiangli@31345 697 }
jiangli@46810 698 return total_size;
jiangli@31345 699 }
jiangli@31345 700
duke@1 701 // Dump bytes to file -- at the current file position.
duke@1 702
iklam@51494 703 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
duke@1 704 if (_file_open) {
iklam@51494 705 size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
duke@1 706 if (n != nbytes) {
duke@1 707 // It is dangerous to leave the corrupted shared archive file around,
duke@1 708 // close and remove the file. See bug 6372906.
duke@1 709 close();
duke@1 710 remove(_full_path);
sla@26296 711 fail_stop("Unable to write to shared archive file.");
duke@1 712 }
duke@1 713 }
duke@1 714 _file_offset += nbytes;
duke@1 715 }
duke@1 716
duke@1 717
duke@1 718 // Align file position to an allocation unit boundary.
duke@1 719
duke@1 720 void FileMapInfo::align_file_position() {
stefank@46619 721 size_t new_file_offset = align_up(_file_offset,
jiangli@27025 722 os::vm_allocation_granularity());
duke@1 723 if (new_file_offset != _file_offset) {
duke@1 724 _file_offset = new_file_offset;
duke@1 725 if (_file_open) {
duke@1 726 // Seek one byte back from the target and write a byte to insure
duke@1 727 // that the written file is the correct length.
duke@1 728 _file_offset -= 1;
jiangli@27025 729 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
sla@26296 730 fail_stop("Unable to seek.");
duke@1 731 }
duke@1 732 char zero = 0;
duke@1 733 write_bytes(&zero, 1);
duke@1 734 }
duke@1 735 }
duke@1 736 }
duke@1 737
duke@1 738
duke@1 739 // Dump bytes to file -- at the current file position.
duke@1 740
iklam@51494 741 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
duke@1 742 align_file_position();
duke@1 743 write_bytes(buffer, nbytes);
duke@1 744 align_file_position();
duke@1 745 }
duke@1 746
duke@1 747
duke@1 748 // Close the shared archive file. This does NOT unmap mapped regions.
duke@1 749
duke@1 750 void FileMapInfo::close() {
duke@1 751 if (_file_open) {
duke@1 752 if (::close(_fd) < 0) {
duke@1 753 fail_stop("Unable to close the shared archive file.");
duke@1 754 }
duke@1 755 _file_open = false;
duke@1 756 _fd = -1;
duke@1 757 }
duke@1 758 }
duke@1 759
duke@1 760
duke@1 761 // JVM/TI RedefineClasses() support:
duke@1 762 // Remap the shared readonly space to shared readwrite, private.
duke@1 763 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
iklam@46746 764 int idx = MetaspaceShared::ro;
iklam@51494 765 CDSFileMapRegion* si = space_at(idx);
duke@1 766 if (!si->_read_only) {
duke@1 767 // the space is already readwrite so we are done
duke@1 768 return true;
duke@1 769 }
duke@1 770 size_t used = si->_used;
stefank@46619 771 size_t size = align_up(used, os::vm_allocation_granularity());
duke@1 772 if (!open_for_read()) {
duke@1 773 return false;
duke@1 774 }
iklam@51508 775 char *addr = region_addr(idx);
duke@1 776 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
jiangli@31345 777 addr, size, false /* !read_only */,
duke@1 778 si->_allow_exec);
duke@1 779 close();
duke@1 780 if (base == NULL) {
duke@1 781 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
duke@1 782 return false;
duke@1 783 }
jiangli@31345 784 if (base != addr) {
duke@1 785 fail_continue("Unable to remap shared readonly space at required address.");
duke@1 786 return false;
duke@1 787 }
duke@1 788 si->_read_only = false;
duke@1 789 return true;
duke@1 790 }
duke@1 791
coleenp@13728 792 // Map the whole region at once, assumed to be allocated contiguously.
coleenp@13728 793 ReservedSpace FileMapInfo::reserve_shared_memory() {
iklam@51508 794 char* requested_addr = region_addr(0);
iklam@46746 795 size_t size = FileMapInfo::core_spaces_size();
coleenp@13728 796
coleenp@13728 797 // Reserve the space first, then map otherwise map will go right over some
coleenp@13728 798 // other reserved memory (like the code cache).
hseigel@19319 799 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
coleenp@13728 800 if (!rs.is_reserved()) {
david@33148 801 fail_continue("Unable to reserve shared space at required address "
david@33148 802 INTPTR_FORMAT, p2i(requested_addr));
coleenp@13728 803 return rs;
coleenp@13728 804 }
zgu@14120 805 // the reserved virtual memory is for mapping class data sharing archive
hseigel@15100 806 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
hseigel@15100 807
coleenp@13728 808 return rs;
coleenp@13728 809 }
duke@1 810
duke@1 811 // Memory map a region in the address space.
jiangli@46810 812 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode", "OptionalData",
jiangli@46810 813 "String1", "String2", "OpenArchive1", "OpenArchive2" };
duke@1 814
iklam@48795 815 char* FileMapInfo::map_region(int i, char** top_ret) {
jiangli@52424 816 assert(!HeapShared::is_heap_region(i), "sanity");
iklam@51494 817 CDSFileMapRegion* si = space_at(i);
duke@1 818 size_t used = si->_used;
coleenp@13728 819 size_t alignment = os::vm_allocation_granularity();
stefank@46619 820 size_t size = align_up(used, alignment);
iklam@51508 821 char *requested_addr = region_addr(i);
cjplummer@31332 822
cjplummer@31332 823 // If a tool agent is in use (debugging enabled), we must map the address space RW
cjplummer@31332 824 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
cjplummer@31375 825 si->_read_only = false;
cjplummer@31332 826 }
cjplummer@31332 827
coleenp@13728 828 // map the contents of the CDS archive in this memory
duke@1 829 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
cjplummer@31375 830 requested_addr, size, si->_read_only,
duke@1 831 si->_allow_exec);
jiangli@31345 832 if (base == NULL || base != requested_addr) {
sla@26296 833 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
duke@1 834 return NULL;
duke@1 835 }
hseigel@15100 836 #ifdef _WINDOWS
hseigel@15100 837 // This call is Windows-only because the memory_type gets recorded for the other platforms
hseigel@15100 838 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
hseigel@15100 839 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
hseigel@15100 840 #endif
jiangli@31345 841
iklam@48795 842
iklam@48795 843 if (!verify_region_checksum(i)) {
iklam@48795 844 return NULL;
iklam@48795 845 }
iklam@48795 846
iklam@48795 847 *top_ret = base + size;
duke@1 848 return base;
duke@1 849 }
duke@1 850
iklam@51508 851 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
iklam@51508 852 if (with_current_oop_encoding_mode) {
iklam@51508 853 return (address)CompressedOops::decode_not_null(offset_of_space(spc));
iklam@51508 854 } else {
iklam@51738 855 return (address)HeapShared::decode_from_archive(offset_of_space(spc));
iklam@51508 856 }
iklam@51508 857 }
iklam@51508 858
jiangli@52424 859 static MemRegion *closed_archive_heap_ranges = NULL;
jiangli@46810 860 static MemRegion *open_archive_heap_ranges = NULL;
jiangli@52424 861 static int num_closed_archive_heap_ranges = 0;
jiangli@46810 862 static int num_open_archive_heap_ranges = 0;
jiangli@46810 863
jiangli@46810 864 #if INCLUDE_CDS_JAVA_HEAP
iklam@51508 865 bool FileMapInfo::has_heap_regions() {
jiangli@52424 866 return (_header->_space[MetaspaceShared::first_closed_archive_heap_region]._used > 0);
iklam@51508 867 }
iklam@51508 868
iklam@51508 869 // Returns the address range of the archived heap regions computed using the
iklam@51508 870 // current oop encoding mode. This range may be different than the one seen at
iklam@51508 871 // dump time due to encoding mode differences. The result is used in determining
iklam@51508 872 // if/how these regions should be relocated at run time.
iklam@51508 873 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
iklam@51508 874 address start = (address) max_uintx;
iklam@51508 875 address end = NULL;
iklam@51508 876
jiangli@52424 877 for (int i = MetaspaceShared::first_closed_archive_heap_region;
jiangli@52424 878 i <= MetaspaceShared::last_valid_region;
jiangli@52424 879 i++) {
iklam@51508 880 CDSFileMapRegion* si = space_at(i);
iklam@51508 881 size_t size = si->_used;
iklam@51508 882 if (size > 0) {
iklam@51738 883 address s = start_address_as_decoded_with_current_oop_encoding_mode(si);
iklam@51508 884 address e = s + size;
iklam@51508 885 if (start > s) {
iklam@51508 886 start = s;
iklam@51508 887 }
iklam@51508 888 if (end < e) {
iklam@51508 889 end = e;
iklam@51508 890 }
iklam@51508 891 }
iklam@51508 892 }
iklam@51508 893 assert(end != NULL, "must have at least one used heap region");
iklam@51508 894 return MemRegion((HeapWord*)start, (HeapWord*)end);
iklam@51508 895 }
iklam@51508 896
jiangli@46810 897 //
jiangli@52424 898 // Map the closed and open archive heap objects to the runtime java heap.
jiangli@46810 899 //
jiangli@52424 900 // The shared objects are mapped at (or close to ) the java heap top in
jiangli@52424 901 // closed archive regions. The mapped objects contain no out-going
jiangli@52424 902 // references to any other java heap regions. GC does not write into the
jiangli@52424 903 // mapped closed archive heap region.
jiangli@46810 904 //
jiangli@52424 905 // The open archive heap objects are mapped below the shared objects in
jiangli@52424 906 // the runtime java heap. The mapped open archive heap data only contains
jiangli@52424 907 // references to the shared objects and open archive objects initially.
jiangli@46810 908 // During runtime execution, out-going references to any other java heap
jiangli@46810 909 // regions may be added. GC may mark and update references in the mapped
jiangli@46810 910 // open archive objects.
iklam@51508 911 void FileMapInfo::map_heap_regions_impl() {
jiangli@52082 912 if (!HeapShared::is_heap_object_archiving_allowed()) {
iklam@51508 913 log_info(cds)("CDS heap data is being ignored. UseG1GC, "
iklam@51508 914 "UseCompressedOops and UseCompressedClassPointers are required.");
iklam@51508 915 return;
iklam@51508 916 }
iklam@51508 917
iklam@52340 918 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
iklam@52340 919 ShouldNotReachHere(); // CDS should have been disabled.
iklam@52340 920 // The archived objects are mapped at JVM start-up, but we don't know if
iklam@52340 921 // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
iklam@52340 922 // which would make the archived String or mirror objects invalid. Let's be safe and not
iklam@52340 923 // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
iklam@52340 924 //
iklam@52340 925 // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
iklam@52340 926 // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
iklam@52340 927 // because we won't install an archived object subgraph if the klass of any of the
iklam@52340 928 // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
iklam@52340 929 }
iklam@52340 930
iklam@51508 931 MemRegion heap_reserved = Universe::heap()->reserved_region();
iklam@51508 932
iklam@51508 933 log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
iklam@51508 934 max_heap_size()/M);
iklam@51508 935 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
iklam@51508 936 p2i(narrow_klass_base()), narrow_klass_shift());
iklam@51508 937 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
iklam@51508 938 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
iklam@51508 939
iklam@51508 940 log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
iklam@51508 941 heap_reserved.byte_size()/M, HeapRegion::GrainBytes);
iklam@51508 942 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
iklam@51508 943 p2i(Universe::narrow_klass_base()), Universe::narrow_klass_shift());
iklam@51508 944 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
iklam@51508 945 Universe::narrow_oop_mode(), p2i(Universe::narrow_oop_base()), Universe::narrow_oop_shift());
iklam@51508 946
iklam@51508 947 if (narrow_klass_base() != Universe::narrow_klass_base() ||
iklam@51508 948 narrow_klass_shift() != Universe::narrow_klass_shift()) {
iklam@51508 949 log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode.");
iklam@51508 950 return;
iklam@51508 951 }
iklam@51508 952
iklam@51508 953 if (narrow_oop_mode() != Universe::narrow_oop_mode() ||
iklam@51508 954 narrow_oop_base() != Universe::narrow_oop_base() ||
iklam@51508 955 narrow_oop_shift() != Universe::narrow_oop_shift()) {
iklam@51508 956 log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode.");
iklam@51508 957 _heap_pointers_need_patching = true;
iklam@51508 958 } else {
iklam@51508 959 MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
iklam@51508 960 if (!heap_reserved.contains(range)) {
iklam@51508 961 log_info(cds)("CDS heap data need to be relocated because");
iklam@51508 962 log_info(cds)("the desired range " PTR_FORMAT " - " PTR_FORMAT, p2i(range.start()), p2i(range.end()));
iklam@51508 963 log_info(cds)("is outside of the heap " PTR_FORMAT " - " PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end()));
iklam@51508 964 _heap_pointers_need_patching = true;
iklam@51508 965 }
iklam@51508 966 }
iklam@51508 967
iklam@51508 968 ptrdiff_t delta = 0;
iklam@51508 969 if (_heap_pointers_need_patching) {
iklam@51508 970 // dumptime heap end ------------v
iklam@51508 971 // [ |archived heap regions| ] runtime heap end ------v
iklam@51508 972 // [ |archived heap regions| ]
iklam@51508 973 // |<-----delta-------------------->|
iklam@51508 974 //
iklam@51508 975 // At dump time, the archived heap regions were near the top of the heap.
iklam@51508 976 // At run time, they may not be inside the heap, so we move them so
iklam@51508 977 // that they are now near the top of the runtime time. This can be done by
iklam@51508 978 // the simple math of adding the delta as shown above.
iklam@51508 979 address dumptime_heap_end = (address)_header->_heap_reserved.end();
iklam@51508 980 address runtime_heap_end = (address)heap_reserved.end();
iklam@51508 981 delta = runtime_heap_end - dumptime_heap_end;
iklam@51508 982 }
iklam@51508 983
iklam@51508 984 log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
iklam@51508 985 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
iklam@51508 986
jiangli@52424 987 CDSFileMapRegion* si = space_at(MetaspaceShared::first_closed_archive_heap_region);
jiangli@52424 988 address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
jiangli@52424 989 if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) {
jiangli@52424 990 // Align the bottom of the closed archive heap regions at G1 region boundary.
jiangli@52424 991 // This will avoid the situation where the highest open region and the lowest
jiangli@52424 992 // closed region sharing the same G1 region. Otherwise we will fail to map the
jiangli@52424 993 // open regions.
jiangli@52424 994 size_t align = size_t(relocated_closed_heap_region_bottom) % HeapRegion::GrainBytes;
iklam@51508 995 delta -= align;
iklam@51508 996 log_info(cds)("CDS heap data need to be relocated lower by a further " SIZE_FORMAT
jiangli@52424 997 " bytes to " INTX_FORMAT " to be aligned with HeapRegion::GrainBytes",
jiangli@52424 998 align, delta);
iklam@51508 999 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
iklam@51508 1000 _heap_pointers_need_patching = true;
jiangli@52424 1001 relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
iklam@51508 1002 }
jiangli@52424 1003 assert(is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes),
jiangli@52424 1004 "must be");
iklam@51508 1005
jiangli@52424 1006 // Map the closed_archive_heap regions, GC does not write into the regions.
jiangli@52424 1007 if (map_heap_data(&closed_archive_heap_ranges,
jiangli@52424 1008 MetaspaceShared::first_closed_archive_heap_region,
jiangli@52424 1009 MetaspaceShared::max_closed_archive_heap_region,
jiangli@52424 1010 &num_closed_archive_heap_ranges)) {
jiangli@52424 1011 HeapShared::set_closed_archive_heap_region_mapped();
iklam@51508 1012
iklam@51508 1013 // Now, map open_archive heap regions, GC can write into the regions.
iklam@51508 1014 if (map_heap_data(&open_archive_heap_ranges,
iklam@51508 1015 MetaspaceShared::first_open_archive_heap_region,
iklam@51508 1016 MetaspaceShared::max_open_archive_heap_region,
iklam@51508 1017 &num_open_archive_heap_ranges,
iklam@51508 1018 true /* open */)) {
jiangli@52082 1019 HeapShared::set_open_archive_heap_region_mapped();
iklam@51508 1020 }
iklam@51508 1021 }
iklam@51508 1022 }
iklam@51508 1023
jiangli@46810 1024 void FileMapInfo::map_heap_regions() {
iklam@51508 1025 if (has_heap_regions()) {
iklam@51508 1026 map_heap_regions_impl();
jiangli@31345 1027 }
jiangli@31345 1028
jiangli@52424 1029 if (!HeapShared::closed_archive_heap_region_mapped()) {
jiangli@52424 1030 assert(closed_archive_heap_ranges == NULL &&
jiangli@52424 1031 num_closed_archive_heap_ranges == 0, "sanity");
jiangli@46810 1032 }
jiangli@46810 1033
jiangli@52082 1034 if (!HeapShared::open_archive_heap_region_mapped()) {
jiangli@46810 1035 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
jiangli@46810 1036 }
jiangli@46810 1037 }
jiangli@46810 1038
jiangli@46810 1039 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
jiangli@46810 1040 int max, int* num, bool is_open_archive) {
jiangli@46810 1041 MemRegion * regions = new MemRegion[max];
iklam@51494 1042 CDSFileMapRegion* si;
jiangli@46810 1043 int region_num = 0;
jiangli@46810 1044
jiangli@46810 1045 for (int i = first;
jiangli@46810 1046 i < first + max; i++) {
iklam@51494 1047 si = space_at(i);
iklam@51508 1048 size_t size = si->_used;
iklam@51508 1049 if (size > 0) {
iklam@51738 1050 HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si);
iklam@51508 1051 regions[region_num] = MemRegion(start, size / HeapWordSize);
jiangli@46810 1052 region_num ++;
iklam@51508 1053 log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
iklam@51508 1054 i, p2i(start), size);
jiangli@46810 1055 }
jiangli@46810 1056 }
jiangli@46810 1057
jiangli@46810 1058 if (region_num == 0) {
jiangli@46810 1059 return false; // no archived java heap data
jiangli@46810 1060 }
jiangli@46810 1061
jiangli@46810 1062 // Check that ranges are within the java heap
jiangli@46810 1063 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
iklam@51508 1064 log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap.");
jiangli@46810 1065 return false;
jiangli@46810 1066 }
jiangli@46810 1067
jiangli@46810 1068 // allocate from java heap
jiangli@46810 1069 if (!G1CollectedHeap::heap()->alloc_archive_regions(
jiangli@46810 1070 regions, region_num, is_open_archive)) {
iklam@51508 1071 log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use.");
jiangli@46810 1072 return false;
jiangli@46810 1073 }
jiangli@46810 1074
jiangli@46810 1075 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
jiangli@46810 1076 // for mapped regions as they are part of the reserved java heap, which is
jiangli@46810 1077 // already recorded.
jiangli@46810 1078 for (int i = 0; i < region_num; i++) {
iklam@51494 1079 si = space_at(first + i);
jiangli@46810 1080 char* addr = (char*)regions[i].start();
jiangli@46810 1081 char* base = os::map_memory(_fd, _full_path, si->_file_offset,
jiangli@46810 1082 addr, regions[i].byte_size(), si->_read_only,
jiangli@46810 1083 si->_allow_exec);
jiangli@46810 1084 if (base == NULL || base != addr) {
jiangli@46810 1085 // dealloc the regions from java heap
jiangli@46810 1086 dealloc_archive_heap_regions(regions, region_num);
iklam@51508 1087 log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
iklam@51508 1088 INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
iklam@51508 1089 p2i(addr), regions[i].byte_size());
jiangli@46810 1090 return false;
jiangli@46810 1091 }
jiangli@46810 1092 }
jiangli@46810 1093
jiangli@46810 1094 if (!verify_mapped_heap_regions(first, region_num)) {
jiangli@46810 1095 // dealloc the regions from java heap
jiangli@46810 1096 dealloc_archive_heap_regions(regions, region_num);
jiangli@46810 1097 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
jiangli@46810 1098 return false;
jiangli@46810 1099 }
jiangli@46810 1100
jiangli@46810 1101 // the shared heap data is mapped successfully
jiangli@46810 1102 *heap_mem = regions;
jiangli@46810 1103 *num = region_num;
jiangli@31345 1104 return true;
jiangli@31345 1105 }
jiangli@31345 1106
jiangli@46810 1107 bool FileMapInfo::verify_mapped_heap_regions(int first, int num) {
jiangli@52146 1108 assert(num > 0, "sanity");
jiangli@52146 1109 for (int i = first; i < first + num; i++) {
jiangli@31345 1110 if (!verify_region_checksum(i)) {
jiangli@31345 1111 return false;
jiangli@31345 1112 }
jiangli@31345 1113 }
jiangli@31345 1114 return true;
jiangli@31345 1115 }
jiangli@31345 1116
iklam@51508 1117 void FileMapInfo::patch_archived_heap_embedded_pointers() {
iklam@51508 1118 if (!_heap_pointers_need_patching) {
iklam@51508 1119 return;
iklam@51508 1120 }
iklam@51508 1121
jiangli@52424 1122 patch_archived_heap_embedded_pointers(closed_archive_heap_ranges,
jiangli@52424 1123 num_closed_archive_heap_ranges,
jiangli@52424 1124 MetaspaceShared::first_closed_archive_heap_region);
iklam@51508 1125
iklam@51508 1126 patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
iklam@51508 1127 num_open_archive_heap_ranges,
iklam@51508 1128 MetaspaceShared::first_open_archive_heap_region);
iklam@51508 1129 }
iklam@51508 1130
iklam@51508 1131 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
iklam@51508 1132 int first_region_idx) {
iklam@51508 1133 for (int i=0; i<num_ranges; i++) {
iklam@51508 1134 CDSFileMapRegion* si = space_at(i + first_region_idx);
iklam@51508 1135 HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap,
iklam@51508 1136 si->_oopmap_size_in_bits);
iklam@51508 1137 }
iklam@51508 1138 }
iklam@51508 1139
iklam@51508 1140 // This internally allocates objects using SystemDictionary::Object_klass(), so it
iklam@51508 1141 // must be called after the well-known classes are resolved.
jiangli@46810 1142 void FileMapInfo::fixup_mapped_heap_regions() {
jiangli@52424 1143 // If any closed regions were found, call the fill routine to make them parseable.
jiangli@52424 1144 // Note that closed_archive_heap_ranges may be non-NULL even if no ranges were found.
jiangli@52424 1145 if (num_closed_archive_heap_ranges != 0) {
jiangli@52424 1146 assert(closed_archive_heap_ranges != NULL,
jiangli@52424 1147 "Null closed_archive_heap_ranges array with non-zero count");
jiangli@52424 1148 G1CollectedHeap::heap()->fill_archive_regions(closed_archive_heap_ranges,
jiangli@52424 1149 num_closed_archive_heap_ranges);
jiangli@31345 1150 }
jiangli@46810 1151
jiangli@46810 1152 // do the same for mapped open archive heap regions
jiangli@46810 1153 if (num_open_archive_heap_ranges != 0) {
jiangli@46810 1154 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
jiangli@46810 1155 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
jiangli@46810 1156 num_open_archive_heap_ranges);
jiangli@46810 1157 }
jiangli@31345 1158 }
jiangli@31345 1159
jiangli@46810 1160 // dealloc the archive regions from java heap
jiangli@46810 1161 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num) {
jiangli@46810 1162 if (num > 0) {
jiangli@46810 1163 assert(regions != NULL, "Null archive ranges array with non-zero count");
jiangli@46810 1164 G1CollectedHeap::heap()->dealloc_archive_regions(regions, num);
jiangli@46810 1165 }
jiangli@46810 1166 }
jiangli@46810 1167 #endif // INCLUDE_CDS_JAVA_HEAP
jiangli@46810 1168
jiangli@27025 1169 bool FileMapInfo::verify_region_checksum(int i) {
jiangli@27025 1170 if (!VerifySharedSpaces) {
jiangli@27025 1171 return true;
jiangli@27025 1172 }
jiangli@31345 1173
iklam@51494 1174 size_t sz = space_at(i)->_used;
jiangli@31345 1175
jiangli@31345 1176 if (sz == 0) {
jiangli@31345 1177 return true; // no data
jiangli@31345 1178 }
jiangli@52424 1179 if ((HeapShared::is_closed_archive_heap_region(i) &&
jiangli@52424 1180 !HeapShared::closed_archive_heap_region_mapped()) ||
jiangli@52424 1181 (HeapShared::is_open_archive_heap_region(i) &&
jiangli@52082 1182 !HeapShared::open_archive_heap_region_mapped())) {
jiangli@46810 1183 return true; // archived heap data is not mapped
jiangli@31345 1184 }
iklam@51508 1185 const char* buf = region_addr(i);
jiangli@27025 1186 int crc = ClassLoader::crc32(0, buf, (jint)sz);
iklam@51494 1187 if (crc != space_at(i)->_crc) {
jiangli@27025 1188 fail_continue("Checksum verification failed.");
jiangli@27025 1189 return false;
jiangli@27025 1190 }
jiangli@27025 1191 return true;
jiangli@27025 1192 }
duke@1 1193
duke@1 1194 // Unmap a memory region in the address space.
duke@1 1195
duke@1 1196 void FileMapInfo::unmap_region(int i) {
jiangli@52424 1197 assert(!HeapShared::is_heap_region(i), "sanity");
iklam@51494 1198 CDSFileMapRegion* si = space_at(i);
duke@1 1199 size_t used = si->_used;
stefank@46619 1200 size_t size = align_up(used, os::vm_allocation_granularity());
jiangli@31345 1201
jiangli@31345 1202 if (used == 0) {
jiangli@31345 1203 return;
jiangli@31345 1204 }
jiangli@31345 1205
iklam@51508 1206 char* addr = region_addr(i);
jiangli@31345 1207 if (!os::unmap_memory(addr, size)) {
duke@1 1208 fail_stop("Unable to unmap shared space.");
duke@1 1209 }
duke@1 1210 }
duke@1 1211
duke@1 1212 void FileMapInfo::assert_mark(bool check) {
duke@1 1213 if (!check) {
sla@26296 1214 fail_stop("Mark mismatch while restoring from shared file.");
duke@1 1215 }
duke@1 1216 }
duke@1 1217
iklam@46746 1218 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
ccheung@49748 1219 it->push(&_shared_path_table);
ccheung@49748 1220 for (int i=0; i<_shared_path_table_size; i++) {
ccheung@49748 1221 shared_path(i)->metaspace_pointers_do(it);
iklam@46746 1222 }
iklam@46746 1223 }
iklam@46746 1224
duke@1 1225
duke@1 1226 FileMapInfo* FileMapInfo::_current_info = NULL;
iklam@51508 1227 bool FileMapInfo::_heap_pointers_need_patching = false;
ccheung@49748 1228 Array<u8>* FileMapInfo::_shared_path_table = NULL;
ccheung@49748 1229 int FileMapInfo::_shared_path_table_size = 0;
ccheung@49748 1230 size_t FileMapInfo::_shared_path_entry_size = 0x1234baad;
ccheung@49748 1231 bool FileMapInfo::_validating_shared_path_table = false;
duke@1 1232
duke@1 1233 // Open the shared archive file, read and validate the header
duke@1 1234 // information (version, boot classpath, etc.). If initialization
duke@1 1235 // fails, shared spaces are disabled and the file is closed. [See
duke@1 1236 // fail_continue.]
iklam@26135 1237 //
iklam@26135 1238 // Validation of the archive is done in two steps:
iklam@26135 1239 //
iklam@26135 1240 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
ccheung@49748 1241 // [2] validate_shared_path_table - this is done later, because the table is in the RW
iklam@26135 1242 // region of the archive, which is not mapped yet.
duke@1 1243 bool FileMapInfo::initialize() {
duke@1 1244 assert(UseSharedSpaces, "UseSharedSpaces expected.");
duke@1 1245
iklam@52340 1246 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
iklam@52340 1247 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
iklam@52340 1248 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
iklam@52340 1249 // during the JVMTI "early" stage, so we can still use CDS if
iklam@52340 1250 // JvmtiExport::has_early_class_hook_env() is false.
iklam@52340 1251 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
iklam@52340 1252 return false;
iklam@52340 1253 }
iklam@52340 1254
duke@1 1255 if (!open_for_read()) {
duke@1 1256 return false;
duke@1 1257 }
duke@1 1258
duke@1 1259 init_from_file(_fd);
iklam@26135 1260 if (!validate_header()) {
duke@1 1261 return false;
duke@1 1262 }
duke@1 1263 return true;
duke@1 1264 }
duke@1 1265
iklam@51508 1266 char* FileMapInfo::region_addr(int idx) {
iklam@51508 1267 CDSFileMapRegion* si = space_at(idx);
jiangli@52424 1268 if (HeapShared::is_heap_region(idx)) {
iklam@51508 1269 assert(DumpSharedSpaces, "The following doesn't work at runtime");
iklam@51508 1270 return si->_used > 0 ?
iklam@51738 1271 (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
jiangli@31345 1272 } else {
iklam@51508 1273 return si->_addr._base;
jiangli@31345 1274 }
jiangli@31345 1275 }
jiangli@31345 1276
iklam@51494 1277 int FileMapHeader::compute_crc() {
iklam@51494 1278 char* start = (char*)this;
jiangli@27025 1279 // start computing from the field after _crc
iklam@51494 1280 char* buf = (char*)&_crc + sizeof(_crc);
iklam@51494 1281 size_t sz = sizeof(FileMapHeader) - (buf - start);
jiangli@27025 1282 int crc = ClassLoader::crc32(0, buf, (jint)sz);
jiangli@27025 1283 return crc;
jiangli@27025 1284 }
jiangli@27025 1285
ccheung@49748 1286 // This function should only be called during run time with UseSharedSpaces enabled.
iklam@51494 1287 bool FileMapHeader::validate() {
jiangli@27025 1288 if (VerifySharedSpaces && compute_crc() != _crc) {
iklam@51494 1289 FileMapInfo::fail_continue("Header checksum verification failed.");
jiangli@27025 1290 return false;
jiangli@27025 1291 }
jiangli@27025 1292
lfoltan@40013 1293 if (!Arguments::has_jimage()) {
lfoltan@40013 1294 FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
lfoltan@40013 1295 return false;
lfoltan@40013 1296 }
lfoltan@40013 1297
iklam@51494 1298 if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
iklam@26135 1299 FileMapInfo::fail_continue("The shared archive file is the wrong version.");
duke@1 1300 return false;
duke@1 1301 }
iklam@51494 1302 if (_magic != CDS_ARCHIVE_MAGIC) {
iklam@26135 1303 FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
duke@1 1304 return false;
duke@1 1305 }
hseigel@14577 1306 char header_version[JVM_IDENT_MAX];
hseigel@14577 1307 get_header_version(header_version);
iklam@26135 1308 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
mockner@38151 1309 log_info(class, path)("expected: %s", header_version);
mockner@38151 1310 log_info(class, path)("actual: %s", _jvm_ident);
iklam@26135 1311 FileMapInfo::fail_continue("The shared archive file was created by a different"
iklam@26135 1312 " version or build of HotSpot");
duke@1 1313 return false;
duke@1 1314 }
iklam@26135 1315 if (_obj_alignment != ObjectAlignmentInBytes) {
iklam@26135 1316 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
david@33148 1317 " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
iklam@26135 1318 _obj_alignment, ObjectAlignmentInBytes);
duke@1 1319 return false;
duke@1 1320 }
thartmann@33628 1321 if (_compact_strings != CompactStrings) {
thartmann@33628 1322 FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"
thartmann@33628 1323 " does not equal the current CompactStrings setting (%s).",
thartmann@33628 1324 _compact_strings ? "enabled" : "disabled",
thartmann@33628 1325 CompactStrings ? "enabled" : "disabled");
thartmann@33628 1326 return false;
thartmann@33628 1327 }
duke@1 1328
iklam@50049 1329 // This must be done after header validation because it might change the
iklam@50049 1330 // header data
iklam@50049 1331 const char* prop = Arguments::get_property("java.system.class.loader");
iklam@50049 1332 if (prop != NULL) {
iklam@50049 1333 warning("Archived non-system classes are disabled because the "
iklam@50049 1334 "java.system.class.loader property is specified (value = \"%s\"). "
jiangli@52424 1335 "To use archived non-system classes, this property must not be set", prop);
iklam@50049 1336 _has_platform_or_app_classes = false;
iklam@50049 1337 }
iklam@50049 1338
iklam@50049 1339 // For backwards compatibility, we don't check the verification setting
iklam@50049 1340 // if the archive only contains system classes.
iklam@50049 1341 if (_has_platform_or_app_classes &&
iklam@50049 1342 ((!_verify_local && BytecodeVerificationLocal) ||
iklam@50049 1343 (!_verify_remote && BytecodeVerificationRemote))) {
iklam@50049 1344 FileMapInfo::fail_continue("The shared archive file was created with less restrictive "
iklam@50049 1345 "verification setting than the current setting.");
iklam@50049 1346 return false;
iklam@50049 1347 }
iklam@50049 1348
duke@1 1349 return true;
duke@1 1350 }
duke@1 1351
iklam@26135 1352 bool FileMapInfo::validate_header() {
iklam@26135 1353 bool status = _header->validate();
iklam@26135 1354
iklam@26135 1355 if (status) {
iklam@26135 1356 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) {
iklam@26135 1357 if (!PrintSharedArchiveAndExit) {
mockner@38151 1358 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
iklam@26135 1359 status = false;
iklam@26135 1360 }
iklam@26135 1361 }
iklam@26135 1362 }
iklam@26135 1363
iklam@26135 1364 if (_paths_misc_info != NULL) {
coleenp@27880 1365 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
iklam@26135 1366 _paths_misc_info = NULL;
iklam@26135 1367 }
iklam@26135 1368 return status;
iklam@26135 1369 }
iklam@26135 1370
jiangli@46810 1371 // Check if a given address is within one of the shared regions
ccheung@37439 1372 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
iklam@46746 1373 assert(idx == MetaspaceShared::ro ||
iklam@46746 1374 idx == MetaspaceShared::rw ||
iklam@46746 1375 idx == MetaspaceShared::mc ||
iklam@46746 1376 idx == MetaspaceShared::md, "invalid region index");
iklam@51508 1377 char* base = region_addr(idx);
iklam@51494 1378 if (p >= base && p < base + space_at(idx)->_used) {
ccheung@37439 1379 return true;
ccheung@37439 1380 }
ccheung@37439 1381 return false;
ccheung@37439 1382 }
ccheung@37439 1383
hseigel@19319 1384 // Unmap mapped regions of shared space.
hseigel@19319 1385 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
iklam@52335 1386 MetaspaceObj::set_shared_metaspace_range(NULL, NULL);
iklam@52335 1387
hseigel@19319 1388 FileMapInfo *map_info = FileMapInfo::current_info();
hseigel@19319 1389 if (map_info) {
sla@26296 1390 map_info->fail_continue("%s", msg);
jiangli@46810 1391 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
jiangli@52424 1392 if (!HeapShared::is_heap_region(i)) {
iklam@51508 1393 char *addr = map_info->region_addr(i);
iklam@51508 1394 if (addr != NULL) {
iklam@51508 1395 map_info->unmap_region(i);
iklam@51508 1396 map_info->space_at(i)->_addr._base = NULL;
iklam@51508 1397 }
hseigel@19319 1398 }
hseigel@19319 1399 }
jiangli@46810 1400 // Dealloc the archive heap regions only without unmapping. The regions are part
jiangli@32589 1401 // of the java heap. Unmapping of the heap regions are managed by GC.
jiangli@46810 1402 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
jiangli@46810 1403 num_open_archive_heap_ranges);
jiangli@52424 1404 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
jiangli@52424 1405 num_closed_archive_heap_ranges);
hseigel@19319 1406 } else if (DumpSharedSpaces) {
sla@26296 1407 fail_stop("%s", msg);
hseigel@19319 1408 }
hseigel@19319 1409 }