annotate src/hotspot/share/memory/filemap.cpp @ 55534:2216e1963765

Merge
author mikael
date Mon, 01 Jul 2019 16:43:59 -0700
parents 1512d88b24c6
children 7b7df2be6219 b279ae9843b8
rev   line source
duke@1 1 /*
iklam@53913 2 * Copyright (c) 2003, 2019, 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"
iklam@54371 27 #include "classfile/classFileStream.hpp"
hseigel@49345 28 #include "classfile/classLoader.inline.hpp"
iklam@54371 29 #include "classfile/classLoaderData.inline.hpp"
iklam@50049 30 #include "classfile/classLoaderExt.hpp"
stefank@7397 31 #include "classfile/symbolTable.hpp"
iklam@26135 32 #include "classfile/systemDictionaryShared.hpp"
hseigel@14577 33 #include "classfile/altHashing.hpp"
rprotacio@46522 34 #include "logging/log.hpp"
stuefe@46701 35 #include "logging/logStream.hpp"
rprotacio@46522 36 #include "logging/logMessage.hpp"
ccheung@54960 37 #include "memory/dynamicArchive.hpp"
stefank@7397 38 #include "memory/filemap.hpp"
iklam@51508 39 #include "memory/heapShared.inline.hpp"
iklam@51508 40 #include "memory/iterator.inline.hpp"
iklam@26135 41 #include "memory/metadataFactory.hpp"
iklam@46746 42 #include "memory/metaspaceClosure.hpp"
iklam@46746 43 #include "memory/metaspaceShared.hpp"
iklam@26135 44 #include "memory/oopFactory.hpp"
stefank@54858 45 #include "memory/universe.hpp"
ccheung@54960 46 #include "oops/compressedOops.hpp"
stefank@49601 47 #include "oops/compressedOops.inline.hpp"
iklam@26135 48 #include "oops/objArrayOop.hpp"
iklam@51508 49 #include "oops/oop.inline.hpp"
cjplummer@31332 50 #include "prims/jvmtiExport.hpp"
stefank@7397 51 #include "runtime/arguments.hpp"
stefank@7397 52 #include "runtime/java.hpp"
iklam@53913 53 #include "runtime/mutexLocker.hpp"
iklam@51497 54 #include "runtime/os.inline.hpp"
goetz@25715 55 #include "runtime/vm_version.hpp"
zgu@14120 56 #include "services/memTracker.hpp"
stefank@46625 57 #include "utilities/align.hpp"
stefank@7397 58 #include "utilities/defaultStream.hpp"
stefank@49992 59 #if INCLUDE_G1GC
stefank@49601 60 #include "gc/g1/g1CollectedHeap.hpp"
iklam@51508 61 #include "gc/g1/heapRegion.hpp"
stefank@49601 62 #endif
stefank@7397 63
duke@1 64 # include <sys/stat.h>
duke@1 65 # include <errno.h>
duke@1 66
duke@1 67 #ifndef O_BINARY // if defined (Win32) use binary files.
duke@1 68 #define O_BINARY 0 // otherwise do nothing.
duke@1 69 #endif
duke@1 70
duke@1 71 extern address JVM_FunctionAtStart();
duke@1 72 extern address JVM_FunctionAtEnd();
duke@1 73
twisti@2131 74 // Complain and stop. All error conditions occurring during the writing of
duke@1 75 // an archive file should stop the process. Unrecoverable errors during
duke@1 76 // the reading of the archive file should stop the process.
duke@1 77
duke@1 78 static void fail(const char *msg, va_list ap) {
duke@1 79 // This occurs very early during initialization: tty is not initialized.
duke@1 80 jio_fprintf(defaultStream::error_stream(),
twisti@2131 81 "An error has occurred while processing the"
duke@1 82 " shared archive file.\n");
duke@1 83 jio_vfprintf(defaultStream::error_stream(), msg, ap);
duke@1 84 jio_fprintf(defaultStream::error_stream(), "\n");
hseigel@19707 85 // Do not change the text of the below message because some tests check for it.
duke@1 86 vm_exit_during_initialization("Unable to use shared archive.", NULL);
duke@1 87 }
duke@1 88
duke@1 89
duke@1 90 void FileMapInfo::fail_stop(const char *msg, ...) {
duke@1 91 va_list ap;
duke@1 92 va_start(ap, msg);
duke@1 93 fail(msg, ap); // Never returns.
duke@1 94 va_end(ap); // for completeness.
duke@1 95 }
duke@1 96
duke@1 97
duke@1 98 // Complain and continue. Recoverable errors during the reading of the
duke@1 99 // archive file may continue (with sharing disabled).
duke@1 100 //
duke@1 101 // If we continue, then disable shared spaces and close the file.
duke@1 102
duke@1 103 void FileMapInfo::fail_continue(const char *msg, ...) {
duke@1 104 va_list ap;
duke@1 105 va_start(ap, msg);
ccheung@54960 106 if (_dynamic_archive_info == NULL) {
ccheung@54960 107 MetaspaceShared::set_archive_loading_failed();
ccheung@54960 108 } else {
ccheung@54960 109 // _dynamic_archive_info has been setup after mapping the base archive
ccheung@54960 110 DynamicArchive::disable();
ccheung@54960 111 }
ccheung@49748 112 if (PrintSharedArchiveAndExit && _validating_shared_path_table) {
iklam@26135 113 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
iklam@26135 114 // do not validate, we can still continue "limping" to validate the remaining
iklam@26135 115 // entries. No need to quit.
iklam@26135 116 tty->print("[");
iklam@26135 117 tty->vprint(msg, ap);
iklam@26135 118 tty->print_cr("]");
mseledtsov@23186 119 } else {
iklam@26135 120 if (RequireSharedSpaces) {
iklam@26135 121 fail(msg, ap);
iklam@26135 122 } else {
rprotacio@46522 123 if (log_is_enabled(Info, cds)) {
rprotacio@46522 124 ResourceMark rm;
stuefe@46701 125 LogStream ls(Log(cds)::info());
stuefe@46701 126 ls.print("UseSharedSpaces: ");
stuefe@46701 127 ls.vprint_cr(msg, ap);
iklam@26135 128 }
mseledtsov@23186 129 }
ccheung@54960 130 if (_dynamic_archive_info == NULL) {
ccheung@54960 131 UseSharedSpaces = false;
ccheung@54960 132 assert(current_info() != NULL, "singleton must be registered");
ccheung@54960 133 current_info()->close();
ccheung@54960 134 } else {
ccheung@54960 135 // We are failing when loading the top archive, but the base archive should
ccheung@54960 136 // continue to work.
ccheung@54960 137 log_warning(cds, dynamic)("Unable to use shared archive. The top archive failed to load: %s", _dynamic_archive_info->_full_path);
ccheung@54960 138 }
duke@1 139 }
duke@1 140 va_end(ap);
duke@1 141 }
duke@1 142
hseigel@14577 143 // Fill in the fileMapInfo structure with data about this VM instance.
duke@1 144
hseigel@14577 145 // This method copies the vm version info into header_version. If the version is too
hseigel@14577 146 // long then a truncated version, which has a hash code appended to it, is copied.
hseigel@14577 147 //
hseigel@14577 148 // Using a template enables this method to verify that header_version is an array of
hseigel@14577 149 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and
hseigel@14577 150 // the code that reads the CDS file will both use the same size buffer. Hence, will
hseigel@14577 151 // use identical truncation. This is necessary for matching of truncated versions.
hseigel@14577 152 template <int N> static void get_header_version(char (&header_version) [N]) {
hseigel@14577 153 assert(N == JVM_IDENT_MAX, "Bad header_version size");
hseigel@14577 154
hseigel@14577 155 const char *vm_version = VM_Version::internal_vm_info_string();
hseigel@14577 156 const int version_len = (int)strlen(vm_version);
hseigel@14577 157
hseigel@14577 158 if (version_len < (JVM_IDENT_MAX-1)) {
hseigel@14577 159 strcpy(header_version, vm_version);
hseigel@14577 160
hseigel@14577 161 } else {
hseigel@14577 162 // Get the hash value. Use a static seed because the hash needs to return the same
hseigel@14577 163 // value over multiple jvm invocations.
hseigel@14577 164 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
hseigel@14577 165
hseigel@14577 166 // Truncate the ident, saving room for the 8 hex character hash value.
hseigel@14577 167 strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
hseigel@14577 168
hseigel@14577 169 // Append the hash code as eight hex digits.
hseigel@14577 170 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
hseigel@14577 171 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
hseigel@14577 172 }
hseigel@14577 173 }
duke@1 174
ccheung@54960 175 FileMapInfo::FileMapInfo(bool is_static) {
jwilhelm@46630 176 memset((void*)this, 0, sizeof(FileMapInfo));
ccheung@54960 177 _is_static = is_static;
ccheung@54960 178 size_t header_size;
ccheung@54960 179 if (is_static) {
ccheung@54960 180 assert(_current_info == NULL, "must be singleton"); // not thread safe
ccheung@54960 181 _current_info = this;
ccheung@54960 182 header_size = sizeof(FileMapHeader);
ccheung@54960 183 } else {
ccheung@54960 184 assert(_dynamic_archive_info == NULL, "must be singleton"); // not thread safe
ccheung@54960 185 _dynamic_archive_info = this;
ccheung@54960 186 header_size = sizeof(DynamicArchiveHeader);
ccheung@54960 187 }
ccheung@54960 188 _header = (FileMapHeader*)os::malloc(header_size, mtInternal);
ccheung@54960 189 memset((void*)_header, 0, header_size);
ccheung@54960 190 _header->_header_size = header_size;
ccheung@54960 191 _header->_version = INVALID_CDS_ARCHIVE_VERSION;
ccheung@54960 192 _header->_has_platform_or_app_classes = true;
iklam@26135 193 _file_offset = 0;
iklam@26135 194 _file_open = false;
iklam@26135 195 }
iklam@26135 196
iklam@26135 197 FileMapInfo::~FileMapInfo() {
ccheung@54960 198 if (_is_static) {
ccheung@54960 199 assert(_current_info == this, "must be singleton"); // not thread safe
ccheung@54960 200 _current_info = NULL;
ccheung@54960 201 } else {
ccheung@54960 202 assert(_dynamic_archive_info == this, "must be singleton"); // not thread safe
ccheung@54960 203 _dynamic_archive_info = NULL;
ccheung@54960 204 }
iklam@26135 205 }
iklam@26135 206
duke@1 207 void FileMapInfo::populate_header(size_t alignment) {
iklam@26135 208 _header->populate(this, alignment);
iklam@26135 209 }
iklam@26135 210
iklam@51494 211 void FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
ccheung@54960 212 if (DynamicDumpSharedSpaces) {
ccheung@54960 213 _magic = CDS_DYNAMIC_ARCHIVE_MAGIC;
ccheung@54960 214 } else {
ccheung@54960 215 _magic = CDS_ARCHIVE_MAGIC;
ccheung@54960 216 }
iklam@51494 217 _version = CURRENT_CDS_ARCHIVE_VERSION;
iklam@26135 218 _alignment = alignment;
iklam@26135 219 _obj_alignment = ObjectAlignmentInBytes;
thartmann@33628 220 _compact_strings = CompactStrings;
stefank@54813 221 _narrow_oop_mode = CompressedOops::mode();
stefank@54813 222 _narrow_oop_base = CompressedOops::base();
stefank@54813 223 _narrow_oop_shift = CompressedOops::shift();
jiangli@31345 224 _max_heap_size = MaxHeapSize;
stefank@54813 225 _narrow_klass_base = CompressedKlassPointers::base();
stefank@54813 226 _narrow_klass_shift = CompressedKlassPointers::shift();
ccheung@49748 227 _shared_path_table = mapinfo->_shared_path_table;
jiangli@52082 228 if (HeapShared::is_heap_object_archiving_allowed()) {
iklam@51508 229 _heap_reserved = Universe::heap()->reserved_region();
iklam@51508 230 }
duke@1 231
duke@1 232 // The following fields are for sanity checks for whether this archive
duke@1 233 // will function correctly with this JVM and the bootclasspath it's
duke@1 234 // invoked with.
duke@1 235
duke@1 236 // JVM version string ... changes on each build.
iklam@26135 237 get_header_version(_jvm_ident);
iklam@50049 238
iklam@50049 239 ClassLoaderExt::finalize_shared_paths_misc_info();
iklam@50049 240 _app_class_paths_start_index = ClassLoaderExt::app_class_paths_start_index();
iklam@50049 241 _app_module_paths_start_index = ClassLoaderExt::app_module_paths_start_index();
ccheung@54960 242 _num_module_paths = ClassLoader::num_module_path_entries();
ccheung@51456 243 _max_used_path_index = ClassLoaderExt::max_used_path_index();
iklam@50049 244
iklam@50049 245 _verify_local = BytecodeVerificationLocal;
iklam@50049 246 _verify_remote = BytecodeVerificationRemote;
iklam@50049 247 _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
iklam@52536 248 _shared_base_address = SharedBaseAddress;
ccheung@52619 249 _allow_archiving_with_java_agent = AllowArchivingWithJavaAgent;
ccheung@54960 250 // the following 2 fields will be set in write_header for dynamic archive header
ccheung@54960 251 _base_archive_name_size = 0;
ccheung@54960 252 _base_archive_is_default = false;
iklam@26135 253 }
duke@1 254
jiangli@50209 255 void SharedClassPathEntry::init(const char* name, bool is_modules_image, TRAPS) {
ccheung@54960 256 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
iklam@46746 257 _timestamp = 0;
iklam@46746 258 _filesize = 0;
duke@1 259
iklam@46746 260 struct stat st;
iklam@46746 261 if (os::stat(name, &st) == 0) {
iklam@46746 262 if ((st.st_mode & S_IFMT) == S_IFDIR) {
jiangli@50209 263 _type = dir_entry;
iklam@46746 264 } else {
jiangli@50209 265 // The timestamp of the modules_image is not checked at runtime.
jiangli@50209 266 if (is_modules_image) {
jiangli@50209 267 _type = modules_image_entry;
jiangli@50209 268 } else {
jiangli@50209 269 _type = jar_entry;
jiangli@50209 270 _timestamp = st.st_mtime;
jiangli@50209 271 }
iklam@46746 272 _filesize = st.st_size;
iklam@46746 273 }
iklam@46746 274 } else {
iklam@46746 275 // The file/dir must exist, or it would not have been added
iklam@46746 276 // into ClassLoader::classpath_entry().
iklam@46746 277 //
iklam@46746 278 // If we can't access a jar file in the boot path, then we can't
iklam@46746 279 // make assumptions about where classes get loaded from.
iklam@46746 280 FileMapInfo::fail_stop("Unable to open file %s.", name);
iklam@46746 281 }
iklam@26135 282
iklam@46746 283 size_t len = strlen(name) + 1;
iklam@46746 284 _name = MetadataFactory::new_array<char>(ClassLoaderData::the_null_class_loader_data(), (int)len, THREAD);
iklam@46746 285 strcpy(_name->data(), name);
iklam@46746 286 }
iklam@26135 287
ccheung@49748 288 bool SharedClassPathEntry::validate(bool is_class_path) {
jiangli@49941 289 assert(UseSharedSpaces, "runtime only");
jiangli@49941 290
iklam@46746 291 struct stat st;
jiangli@50209 292 const char* name;
jiangli@50209 293
jiangli@50209 294 // In order to validate the runtime modules image file size against the archived
jiangli@50209 295 // size information, we need to obtain the runtime modules image path. The recorded
jiangli@50209 296 // dump time modules image path in the archive may be different from the runtime path
jiangli@50209 297 // if the JDK image has beed moved after generating the archive.
jiangli@50209 298 if (is_modules_image()) {
jiangli@50209 299 name = ClassLoader::get_jrt_entry()->name();
jiangli@50209 300 } else {
jiangli@50209 301 name = this->name();
jiangli@50209 302 }
jiangli@50209 303
iklam@46746 304 bool ok = true;
iklam@46746 305 log_info(class, path)("checking shared classpath entry: %s", name);
ccheung@49748 306 if (os::stat(name, &st) != 0 && is_class_path) {
ccheung@49748 307 // If the archived module path entry does not exist at runtime, it is not fatal
ccheung@49748 308 // (no need to invalid the shared archive) because the shared runtime visibility check
ccheung@49748 309 // filters out any archived module classes that do not have a matching runtime
ccheung@49748 310 // module path location.
iklam@46746 311 FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
iklam@46746 312 ok = false;
iklam@46746 313 } else if (is_dir()) {
iklam@46746 314 if (!os::dir_is_empty(name)) {
iklam@46746 315 FileMapInfo::fail_continue("directory is not empty: %s", name);
iklam@46746 316 ok = false;
lfoltan@40013 317 }
jiangli@50209 318 } else if ((has_timestamp() && _timestamp != st.st_mtime) ||
jiangli@50209 319 _filesize != st.st_size) {
jiangli@50209 320 ok = false;
jiangli@50209 321 if (PrintSharedArchiveAndExit) {
jiangli@50209 322 FileMapInfo::fail_continue(_timestamp != st.st_mtime ?
jiangli@50209 323 "Timestamp mismatch" :
jiangli@50209 324 "File size mismatch");
jiangli@50209 325 } else {
jiangli@50209 326 FileMapInfo::fail_continue("A jar file is not the one used while building"
jiangli@50209 327 " the shared archive file: %s", name);
iklam@26135 328 }
iklam@46746 329 }
jiangli@52585 330
jiangli@52585 331 if (PrintSharedArchiveAndExit && !ok) {
jiangli@52585 332 // If PrintSharedArchiveAndExit is enabled, don't report failure to the
jiangli@52585 333 // caller. Please see above comments for more details.
jiangli@52585 334 ok = true;
ccheung@54960 335 MetaspaceShared::set_archive_loading_failed();
jiangli@52585 336 }
iklam@46746 337 return ok;
iklam@46746 338 }
duke@1 339
iklam@46746 340 void SharedClassPathEntry::metaspace_pointers_do(MetaspaceClosure* it) {
iklam@46746 341 it->push(&_name);
iklam@46746 342 it->push(&_manifest);
iklam@46746 343 }
iklam@46746 344
ccheung@54960 345 void SharedPathTable::metaspace_pointers_do(MetaspaceClosure* it) {
ccheung@54960 346 it->push(&_table);
ccheung@54960 347 for (int i=0; i<_size; i++) {
ccheung@54960 348 path_at(i)->metaspace_pointers_do(it);
ccheung@54960 349 }
ccheung@54960 350 }
ccheung@54960 351
ccheung@54960 352 void SharedPathTable::dumptime_init(ClassLoaderData* loader_data, Thread* THREAD) {
ccheung@54960 353 size_t entry_size = sizeof(SharedClassPathEntry);
ccheung@54960 354 int num_boot_classpath_entries = ClassLoader::num_boot_classpath_entries();
ccheung@54960 355 int num_app_classpath_entries = ClassLoader::num_app_classpath_entries();
ccheung@54960 356 int num_module_path_entries = ClassLoader::num_module_path_entries();
ccheung@54960 357 int num_entries = num_boot_classpath_entries + num_app_classpath_entries + num_module_path_entries;
ccheung@54960 358 size_t bytes = entry_size * num_entries;
ccheung@54960 359
ccheung@54960 360 _table = MetadataFactory::new_array<u8>(loader_data, (int)(bytes + 7 / 8), THREAD);
ccheung@54960 361 _size = num_entries;
ccheung@54960 362 }
ccheung@54960 363
ccheung@49748 364 void FileMapInfo::allocate_shared_path_table() {
ccheung@54960 365 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Sanity");
jiangli@47991 366
iklam@46746 367 Thread* THREAD = Thread::current();
iklam@46746 368 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
jiangli@47991 369 ClassPathEntry* jrt = ClassLoader::get_jrt_entry();
jiangli@47991 370
jiangli@47991 371 assert(jrt != NULL,
jiangli@47991 372 "No modular java runtime image present when allocating the CDS classpath entry table");
jiangli@47991 373
ccheung@54960 374 _shared_path_table.dumptime_init(loader_data, THREAD);
iklam@46746 375
jiangli@47991 376 // 1. boot class path
jiangli@47991 377 int i = 0;
jiangli@47991 378 ClassPathEntry* cpe = jrt;
jiangli@47991 379 while (cpe != NULL) {
jiangli@50209 380 bool is_jrt = (cpe == jrt);
jiangli@50209 381 const char* type = (is_jrt ? "jrt" : (cpe->is_jar_file() ? "jar" : "dir"));
iklam@46746 382 log_info(class, path)("add main shared path (%s) %s", type, cpe->name());
ccheung@49748 383 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 384 ent->init(cpe->name(), is_jrt, THREAD);
jiangli@50209 385 if (!is_jrt) { // No need to do the modules image.
iklam@26135 386 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
iklam@50049 387 update_shared_classpath(cpe, ent, THREAD);
duke@1 388 }
jiangli@47991 389 cpe = ClassLoader::get_next_boot_classpath_entry(cpe);
jiangli@47991 390 i++;
duke@1 391 }
ccheung@54960 392 assert(i == ClassLoader::num_boot_classpath_entries(),
jiangli@47991 393 "number of boot class path entry mismatch");
jiangli@47991 394
jiangli@47991 395 // 2. app class path
jiangli@47991 396 ClassPathEntry *acpe = ClassLoader::app_classpath_entries();
jiangli@47991 397 while (acpe != NULL) {
jiangli@47991 398 log_info(class, path)("add app shared path %s", acpe->name());
ccheung@49748 399 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 400 ent->init(acpe->name(), false, THREAD);
jiangli@47991 401 EXCEPTION_MARK;
iklam@50049 402 update_shared_classpath(acpe, ent, THREAD);
jiangli@47991 403 acpe = acpe->next();
ccheung@49748 404 i++;
jiangli@47991 405 }
ccheung@49748 406
ccheung@49748 407 // 3. module path
ccheung@49748 408 ClassPathEntry *mpe = ClassLoader::module_path_entries();
ccheung@49748 409 while (mpe != NULL) {
ccheung@49748 410 log_info(class, path)("add module path %s",mpe->name());
ccheung@49748 411 SharedClassPathEntry* ent = shared_path(i);
jiangli@50209 412 ent->init(mpe->name(), false, THREAD);
ccheung@49748 413 EXCEPTION_MARK;
iklam@50049 414 update_shared_classpath(mpe, ent, THREAD);
ccheung@49748 415 mpe = mpe->next();
ccheung@49748 416 i++;
ccheung@49748 417 }
ccheung@54960 418 assert(i == _shared_path_table.size(), "number of shared path entry mismatch");
duke@1 419 }
duke@1 420
jiangli@49941 421 void FileMapInfo::check_nonempty_dir_in_shared_path_table() {
ccheung@54960 422 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "dump time only");
jiangli@49941 423
jiangli@49941 424 bool has_nonempty_dir = false;
jiangli@49941 425
ccheung@54960 426 int last = _shared_path_table.size() - 1;
ccheung@51456 427 if (last > ClassLoaderExt::max_used_path_index()) {
ccheung@51456 428 // no need to check any path beyond max_used_path_index
ccheung@51456 429 last = ClassLoaderExt::max_used_path_index();
jiangli@49941 430 }
jiangli@49941 431
ccheung@51456 432 for (int i = 0; i <= last; i++) {
jiangli@49941 433 SharedClassPathEntry *e = shared_path(i);
jiangli@49941 434 if (e->is_dir()) {
jiangli@49941 435 const char* path = e->name();
jiangli@49941 436 if (!os::dir_is_empty(path)) {
jiangli@49941 437 tty->print_cr("Error: non-empty directory '%s'", path);
jiangli@49941 438 has_nonempty_dir = true;
jiangli@49941 439 }
jiangli@49941 440 }
jiangli@49941 441 }
jiangli@49941 442
jiangli@49941 443 if (has_nonempty_dir) {
jiangli@49941 444 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
jiangli@49941 445 }
jiangli@49941 446 }
jiangli@49941 447
iklam@50049 448 class ManifestStream: public ResourceObj {
iklam@50049 449 private:
iklam@50049 450 u1* _buffer_start; // Buffer bottom
iklam@50049 451 u1* _buffer_end; // Buffer top (one past last element)
iklam@50049 452 u1* _current; // Current buffer position
iklam@50049 453
iklam@50049 454 public:
iklam@50049 455 // Constructor
iklam@50049 456 ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
iklam@50049 457 _current(buffer) {
iklam@50049 458 _buffer_end = buffer + length;
iklam@50049 459 }
iklam@50049 460
iklam@50049 461 static bool is_attr(u1* attr, const char* name) {
iklam@50049 462 return strncmp((const char*)attr, name, strlen(name)) == 0;
iklam@50049 463 }
iklam@50049 464
iklam@50049 465 static char* copy_attr(u1* value, size_t len) {
iklam@50049 466 char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
iklam@50049 467 strncpy(buf, (char*)value, len);
iklam@50049 468 buf[len] = 0;
iklam@50049 469 return buf;
iklam@50049 470 }
iklam@50049 471
iklam@50049 472 // The return value indicates if the JAR is signed or not
iklam@50049 473 bool check_is_signed() {
iklam@50049 474 u1* attr = _current;
iklam@50049 475 bool isSigned = false;
iklam@50049 476 while (_current < _buffer_end) {
iklam@50049 477 if (*_current == '\n') {
iklam@50049 478 *_current = '\0';
iklam@50049 479 u1* value = (u1*)strchr((char*)attr, ':');
iklam@50049 480 if (value != NULL) {
iklam@50049 481 assert(*(value+1) == ' ', "Unrecognized format" );
iklam@50049 482 if (strstr((char*)attr, "-Digest") != NULL) {
iklam@50049 483 isSigned = true;
iklam@50049 484 break;
iklam@50049 485 }
iklam@50049 486 }
iklam@50049 487 *_current = '\n'; // restore
iklam@50049 488 attr = _current + 1;
iklam@50049 489 }
iklam@50049 490 _current ++;
iklam@50049 491 }
iklam@50049 492 return isSigned;
iklam@50049 493 }
iklam@50049 494 };
iklam@50049 495
iklam@50049 496 void FileMapInfo::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* ent, TRAPS) {
iklam@50049 497 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
iklam@50049 498 ResourceMark rm(THREAD);
iklam@50049 499 jint manifest_size;
iklam@50049 500
iklam@50049 501 if (cpe->is_jar_file()) {
jiangli@50209 502 assert(ent->is_jar(), "the shared class path entry is not a JAR file");
iklam@50049 503 char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
iklam@50049 504 if (manifest != NULL) {
iklam@50049 505 ManifestStream* stream = new ManifestStream((u1*)manifest,
iklam@50049 506 manifest_size);
jiangli@50209 507 if (stream->check_is_signed()) {
jiangli@50209 508 ent->set_is_signed();
iklam@50049 509 } else {
iklam@50049 510 // Copy the manifest into the shared archive
iklam@50049 511 manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
iklam@50049 512 Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
iklam@50049 513 manifest_size,
iklam@50049 514 THREAD);
iklam@50049 515 char* p = (char*)(buf->data());
iklam@50049 516 memcpy(p, manifest, manifest_size);
iklam@50049 517 ent->set_manifest(buf);
iklam@50049 518 }
iklam@50049 519 }
iklam@50049 520 }
iklam@50049 521 }
iklam@50049 522
iklam@50049 523
ccheung@49748 524 bool FileMapInfo::validate_shared_path_table() {
jiangli@49941 525 assert(UseSharedSpaces, "runtime only");
jiangli@49941 526
ccheung@49748 527 _validating_shared_path_table = true;
ccheung@54960 528
ccheung@54960 529 // Load the shared path table info from the archive header
ccheung@49748 530 _shared_path_table = _header->_shared_path_table;
ccheung@54960 531 if (DynamicDumpSharedSpaces) {
ccheung@54960 532 // Only support dynamic dumping with the usage of the default CDS archive
ccheung@54960 533 // or a simple base archive.
ccheung@54960 534 // If the base layer archive contains additional path component besides
ccheung@54960 535 // the runtime image and the -cp, dynamic dumping is disabled.
ccheung@54960 536 //
ccheung@54960 537 // When dynamic archiving is enabled, the _shared_path_table is overwritten
ccheung@54960 538 // to include the application path and stored in the top layer archive.
ccheung@54960 539 assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
ccheung@54960 540 if (_header->_app_class_paths_start_index > 1) {
ccheung@54960 541 DynamicDumpSharedSpaces = false;
ccheung@54960 542 warning(
ccheung@54960 543 "Dynamic archiving is disabled because base layer archive has appended boot classpath");
ccheung@54960 544 }
ccheung@54960 545 if (_header->_num_module_paths > 0) {
ccheung@54960 546 DynamicDumpSharedSpaces = false;
ccheung@54960 547 warning(
ccheung@54960 548 "Dynamic archiving is disabled because base layer archive has module path");
ccheung@54960 549 }
ccheung@54960 550 }
iklam@26135 551
iklam@50049 552 int module_paths_start_index = _header->_app_module_paths_start_index;
ccheung@49748 553
ccheung@51456 554 // validate the path entries up to the _max_used_path_index
ccheung@51456 555 for (int i=0; i < _header->_max_used_path_index + 1; i++) {
ccheung@49748 556 if (i < module_paths_start_index) {
ccheung@49748 557 if (shared_path(i)->validate()) {
ccheung@49748 558 log_info(class, path)("ok");
jiangli@52585 559 } else {
ccheung@54960 560 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
ccheung@54960 561 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
ccheung@54960 562 }
jiangli@52585 563 return false;
ccheung@49748 564 }
ccheung@49748 565 } else if (i >= module_paths_start_index) {
ccheung@49748 566 if (shared_path(i)->validate(false /* not a class path entry */)) {
ccheung@49748 567 log_info(class, path)("ok");
jiangli@52585 568 } else {
ccheung@54960 569 if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
ccheung@54960 570 assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
ccheung@54960 571 }
jiangli@52585 572 return false;
ccheung@49748 573 }
iklam@26135 574 }
iklam@26135 575 }
iklam@26135 576
ccheung@49748 577 _validating_shared_path_table = false;
iklam@53913 578
iklam@53913 579 #if INCLUDE_JVMTI
iklam@53913 580 if (_classpath_entries_for_jvmti != NULL) {
iklam@53913 581 os::free(_classpath_entries_for_jvmti);
iklam@53913 582 }
ccheung@54960 583 size_t sz = sizeof(ClassPathEntry*) * get_number_of_shared_paths();
iklam@53913 584 _classpath_entries_for_jvmti = (ClassPathEntry**)os::malloc(sz, mtClass);
ccheung@54960 585 memset((void*)_classpath_entries_for_jvmti, 0, sz);
iklam@53913 586 #endif
iklam@53913 587
iklam@26135 588 return true;
iklam@26135 589 }
iklam@26135 590
ccheung@54960 591 bool FileMapInfo::same_files(const char* file1, const char* file2) {
ccheung@54960 592 if (strcmp(file1, file2) == 0) {
ccheung@54960 593 return true;
ccheung@54960 594 }
ccheung@54960 595
ccheung@54960 596 bool is_same = false;
ccheung@54960 597 // if the two paths diff only in case
ccheung@54960 598 struct stat st1;
ccheung@54960 599 struct stat st2;
ccheung@54960 600 int ret1;
ccheung@54960 601 int ret2;
ccheung@54960 602 ret1 = os::stat(file1, &st1);
ccheung@54960 603 ret2 = os::stat(file2, &st2);
ccheung@54960 604 if (ret1 < 0 || ret2 < 0) {
ccheung@54960 605 // one of the files is invalid. So they are not the same.
ccheung@54960 606 is_same = false;
ccheung@54960 607 } else if (st1.st_dev != st2.st_dev || st1.st_ino != st2.st_ino) {
ccheung@54960 608 // different files
ccheung@54960 609 is_same = false;
ccheung@54960 610 #ifndef _WINDOWS
ccheung@54960 611 } else if (st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino) {
ccheung@54960 612 // same files
ccheung@54960 613 is_same = true;
ccheung@54960 614 #else
ccheung@54960 615 } else if ((st1.st_size == st2.st_size) && (st1.st_ctime == st2.st_ctime) &&
ccheung@54960 616 (st1.st_mtime == st2.st_mtime)) {
ccheung@54960 617 // same files
ccheung@54960 618 is_same = true;
ccheung@54960 619 #endif
ccheung@54960 620 }
ccheung@54960 621 return is_same;
ccheung@54960 622 }
ccheung@54960 623
ccheung@54960 624 bool FileMapInfo::check_archive(const char* archive_name, bool is_static) {
ccheung@54960 625 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
ccheung@54960 626 if (fd < 0) {
ccheung@54960 627 // do not vm_exit_during_initialization here because Arguments::init_shared_archive_paths()
ccheung@54960 628 // requires a shared archive name. The open_for_read() function will log a message regarding
ccheung@54960 629 // failure in opening a shared archive.
ccheung@54960 630 return false;
ccheung@54960 631 }
ccheung@54960 632
ccheung@54960 633 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
ccheung@54960 634 void* header = os::malloc(sz, mtInternal);
ccheung@54960 635 memset(header, 0, sz);
ccheung@54960 636 size_t n = os::read(fd, header, (unsigned int)sz);
ccheung@54960 637 if (n != sz) {
ccheung@54960 638 os::free(header);
ccheung@54960 639 os::close(fd);
ccheung@54960 640 vm_exit_during_initialization("Unable to read header from shared archive", archive_name);
ccheung@54960 641 return false;
ccheung@54960 642 }
ccheung@54960 643 if (is_static) {
ccheung@54960 644 FileMapHeader* static_header = (FileMapHeader*)header;
ccheung@54960 645 if (static_header->_magic != CDS_ARCHIVE_MAGIC) {
ccheung@54960 646 os::free(header);
ccheung@54960 647 os::close(fd);
ccheung@54960 648 vm_exit_during_initialization("Not a base shared archive", archive_name);
ccheung@54960 649 return false;
ccheung@54960 650 }
ccheung@54960 651 } else {
ccheung@54960 652 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)header;
ccheung@54960 653 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
ccheung@54960 654 os::free(header);
ccheung@54960 655 os::close(fd);
ccheung@54960 656 vm_exit_during_initialization("Not a top shared archive", archive_name);
ccheung@54960 657 return false;
ccheung@54960 658 }
ccheung@54960 659 }
ccheung@54960 660 os::free(header);
ccheung@54960 661 os::close(fd);
ccheung@54960 662 return true;
ccheung@54960 663 }
ccheung@54960 664
ccheung@54960 665 bool FileMapInfo::get_base_archive_name_from_header(const char* archive_name,
ccheung@54960 666 int* size, char** base_archive_name) {
ccheung@54960 667 int fd = os::open(archive_name, O_RDONLY | O_BINARY, 0);
ccheung@54960 668 if (fd < 0) {
ccheung@54960 669 *size = 0;
ccheung@54960 670 return false;
ccheung@54960 671 }
ccheung@54960 672
ccheung@54960 673 // read the header as a dynamic archive header
ccheung@54960 674 size_t sz = sizeof(DynamicArchiveHeader);
ccheung@54960 675 DynamicArchiveHeader* dynamic_header = (DynamicArchiveHeader*)os::malloc(sz, mtInternal);
ccheung@54960 676 size_t n = os::read(fd, dynamic_header, (unsigned int)sz);
ccheung@54960 677 if (n != sz) {
ccheung@54960 678 fail_continue("Unable to read the file header.");
ccheung@54960 679 os::free(dynamic_header);
ccheung@54960 680 os::close(fd);
ccheung@54960 681 return false;
ccheung@54960 682 }
ccheung@54960 683 if (dynamic_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
ccheung@54960 684 // Not a dynamic header, no need to proceed further.
ccheung@54960 685 *size = 0;
ccheung@54960 686 os::free(dynamic_header);
ccheung@54960 687 os::close(fd);
ccheung@54960 688 return false;
ccheung@54960 689 }
ccheung@54960 690 if (dynamic_header->_base_archive_is_default) {
ccheung@54960 691 *base_archive_name = Arguments::get_default_shared_archive_path();
ccheung@54960 692 } else {
ccheung@54960 693 // skip over the _paths_misc_info
ccheung@54960 694 sz = dynamic_header->_paths_misc_info_size;
ccheung@54960 695 lseek(fd, (long)sz, SEEK_CUR);
ccheung@54960 696 // read the base archive name
ccheung@54960 697 size_t name_size = dynamic_header->_base_archive_name_size;
ccheung@54960 698 if (name_size == 0) {
ccheung@54960 699 os::free(dynamic_header);
ccheung@54960 700 os::close(fd);
ccheung@54960 701 return false;
ccheung@54960 702 }
ccheung@54960 703 *base_archive_name = NEW_C_HEAP_ARRAY(char, name_size, mtInternal);
ccheung@54960 704 n = os::read(fd, *base_archive_name, (unsigned int)name_size);
ccheung@54960 705 if (n != name_size) {
ccheung@54960 706 fail_continue("Unable to read the base archive name from the header.");
ccheung@54960 707 FREE_C_HEAP_ARRAY(char, *base_archive_name);
ccheung@54960 708 *base_archive_name = NULL;
ccheung@54960 709 os::free(dynamic_header);
ccheung@54960 710 os::close(fd);
ccheung@54960 711 return false;
ccheung@54960 712 }
ccheung@54960 713 }
ccheung@54960 714
ccheung@54960 715 os::free(dynamic_header);
ccheung@54960 716 os::close(fd);
ccheung@54960 717 return true;
ccheung@54960 718 }
ccheung@54960 719
ccheung@54960 720 void FileMapInfo::restore_shared_path_table() {
ccheung@54960 721 _shared_path_table = _current_info->_header->_shared_path_table;
ccheung@54960 722 }
ccheung@54960 723
duke@1 724 // Read the FileMapInfo information from the file.
duke@1 725
ccheung@54960 726 bool FileMapInfo::init_from_file(int fd, bool is_static) {
ccheung@54960 727 size_t sz = is_static ? sizeof(FileMapHeader) : sizeof(DynamicArchiveHeader);
iklam@51494 728 size_t n = os::read(fd, _header, (unsigned int)sz);
iklam@26135 729 if (n != sz) {
duke@1 730 fail_continue("Unable to read the file header.");
duke@1 731 return false;
duke@1 732 }
iklam@51494 733 if (_header->_version != CURRENT_CDS_ARCHIVE_VERSION) {
duke@1 734 fail_continue("The shared archive file has the wrong version.");
duke@1 735 return false;
duke@1 736 }
ccheung@54960 737 _file_offset = n;
iklam@26135 738
iklam@26135 739 size_t info_size = _header->_paths_misc_info_size;
ccheung@54960 740 _paths_misc_info = NEW_C_HEAP_ARRAY(char, info_size, mtClass);
iklam@26135 741 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
iklam@26135 742 if (n != info_size) {
iklam@26135 743 fail_continue("Unable to read the shared path info header.");
coleenp@27880 744 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
iklam@26135 745 _paths_misc_info = NULL;
iklam@26135 746 return false;
iklam@26135 747 }
ccheung@54960 748 _file_offset += n + _header->_base_archive_name_size; // accounts for the size of _base_archive_name
iklam@26135 749
ccheung@54960 750 if (is_static) {
ccheung@54960 751 if (_header->_magic != CDS_ARCHIVE_MAGIC) {
ccheung@54960 752 fail_continue("Incorrect static archive magic number");
ccheung@54960 753 return false;
ccheung@54960 754 }
ccheung@54960 755 // just checking the last region is sufficient since the archive is written
ccheung@54960 756 // in sequential order
ccheung@54960 757 size_t len = lseek(fd, 0, SEEK_END);
ccheung@54960 758 CDSFileMapRegion* si = space_at(MetaspaceShared::last_valid_region);
ccheung@54960 759 // The last space might be empty
ccheung@54960 760 if (si->_file_offset > len || len - si->_file_offset < si->_used) {
ccheung@54960 761 fail_continue("The shared archive file has been truncated.");
ccheung@54960 762 return false;
ccheung@54960 763 }
ccheung@54960 764
ccheung@54960 765 SharedBaseAddress = _header->_shared_base_address;
jiangli@27025 766 }
jiangli@27025 767
duke@1 768 return true;
duke@1 769 }
duke@1 770
duke@1 771
duke@1 772 // Read the FileMapInfo information from the file.
ccheung@54960 773 bool FileMapInfo::open_for_read(const char* path) {
ccheung@54960 774 if (_file_open) {
ccheung@54960 775 return true;
ccheung@54960 776 }
ccheung@54960 777 if (path == NULL) {
ccheung@54960 778 _full_path = Arguments::GetSharedArchivePath();
ccheung@54960 779 } else {
ccheung@54960 780 _full_path = path;
ccheung@54960 781 }
mbaesken@53940 782 int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0);
duke@1 783 if (fd < 0) {
duke@1 784 if (errno == ENOENT) {
duke@1 785 // Not locating the shared archive is ok.
ccheung@54960 786 fail_continue("Specified shared archive not found (%s).", _full_path);
duke@1 787 } else {
duke@1 788 fail_continue("Failed to open shared archive file (%s).",
stuefe@37113 789 os::strerror(errno));
duke@1 790 }
duke@1 791 return false;
duke@1 792 }
duke@1 793
duke@1 794 _fd = fd;
duke@1 795 _file_open = true;
duke@1 796 return true;
duke@1 797 }
duke@1 798
duke@1 799 // Write the FileMapInfo information to the file.
duke@1 800
ccheung@54960 801 void FileMapInfo::open_for_write(const char* path) {
ccheung@54960 802 if (path == NULL) {
ccheung@54960 803 _full_path = Arguments::GetSharedArchivePath();
ccheung@54960 804 } else {
ccheung@54960 805 _full_path = path;
ccheung@54960 806 }
mlarsson@48963 807 LogMessage(cds) msg;
mlarsson@48963 808 if (msg.is_info()) {
mlarsson@48963 809 msg.info("Dumping shared data to file: ");
mlarsson@48963 810 msg.info(" %s", _full_path);
duke@1 811 }
duke@1 812
hseigel@15461 813 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file.
ccheung@54960 814 chmod(_full_path, _S_IREAD | _S_IWRITE);
hseigel@15461 815 #endif
hseigel@15461 816
hseigel@15461 817 // Use remove() to delete the existing file because, on Unix, this will
hseigel@15461 818 // allow processes that have it open continued access to the file.
duke@1 819 remove(_full_path);
mbaesken@53940 820 int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
duke@1 821 if (fd < 0) {
hseigel@28950 822 fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
stuefe@37113 823 os::strerror(errno));
duke@1 824 }
duke@1 825 _fd = fd;
duke@1 826 _file_offset = 0;
duke@1 827 _file_open = true;
duke@1 828 }
duke@1 829
duke@1 830
duke@1 831 // Write the header to the file, seek to the next allocation boundary.
duke@1 832
duke@1 833 void FileMapInfo::write_header() {
iklam@26135 834 int info_size = ClassLoader::get_shared_paths_misc_info_size();
iklam@26135 835
iklam@26135 836 _header->_paths_misc_info_size = info_size;
iklam@26135 837
ccheung@54960 838 char* base_archive_name = NULL;
ccheung@54960 839 if (_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC) {
ccheung@54960 840 base_archive_name = (char*)Arguments::GetSharedArchivePath();
ccheung@54960 841 _header->_base_archive_name_size = (int)strlen(base_archive_name) + 1;
ccheung@54960 842 _header->_base_archive_is_default = FLAG_IS_DEFAULT(SharedArchiveFile);
ccheung@54960 843 }
ccheung@54960 844
ccheung@54960 845 assert(is_file_position_aligned(), "must be");
ccheung@54960 846 write_bytes(_header, _header->_header_size);
iklam@51494 847 write_bytes(ClassLoader::get_shared_paths_misc_info(), (size_t)info_size);
ccheung@54960 848 if (base_archive_name != NULL) {
ccheung@54960 849 write_bytes(base_archive_name, (size_t)_header->_base_archive_name_size);
ccheung@54960 850 }
iklam@26135 851 align_file_position();
duke@1 852 }
duke@1 853
duke@1 854 // Dump region to file.
ccheung@54960 855 // This is called twice for each region during archiving, once before
ccheung@54960 856 // the archive file is open (_file_open is false) and once after.
duke@1 857 void FileMapInfo::write_region(int region, char* base, size_t size,
iklam@46746 858 bool read_only, bool allow_exec) {
ccheung@54960 859 assert(DumpSharedSpaces || DynamicDumpSharedSpaces, "Dump time only");
ccheung@54960 860
iklam@51494 861 CDSFileMapRegion* si = space_at(region);
ccheung@54960 862 char* target_base = base;
ccheung@54960 863 if (DynamicDumpSharedSpaces) {
ccheung@54960 864 target_base = DynamicArchive::buffer_to_target(base);
ccheung@54960 865 }
duke@1 866
duke@1 867 if (_file_open) {
duke@1 868 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
iklam@46746 869 log_info(cds)("Shared file region %d: " SIZE_FORMAT_HEX_W(08)
iklam@46746 870 " bytes, addr " INTPTR_FORMAT " file offset " SIZE_FORMAT_HEX_W(08),
ccheung@54960 871 region, size, p2i(target_base), _file_offset);
duke@1 872 } else {
duke@1 873 si->_file_offset = _file_offset;
duke@1 874 }
ccheung@54960 875
jiangli@52424 876 if (HeapShared::is_heap_region(region)) {
ccheung@54960 877 assert((target_base - (char*)CompressedKlassPointers::base()) % HeapWordSize == 0, "Sanity");
ccheung@54960 878 if (target_base != NULL) {
ccheung@54960 879 si->_addr._offset = (intx)CompressedOops::encode_not_null((oop)target_base);
jiangli@31345 880 } else {
jiangli@31345 881 si->_addr._offset = 0;
jiangli@31345 882 }
jiangli@31345 883 } else {
ccheung@54960 884 si->_addr._base = target_base;
jiangli@31345 885 }
duke@1 886 si->_used = size;
duke@1 887 si->_read_only = read_only;
duke@1 888 si->_allow_exec = allow_exec;
ccheung@54960 889
ccheung@54960 890 // Use the current 'base' when computing the CRC value and writing out data
jiangli@27025 891 si->_crc = ClassLoader::crc32(0, base, (jint)size);
goetz@51085 892 if (base != NULL) {
iklam@51494 893 write_bytes_aligned(base, size);
goetz@51085 894 }
duke@1 895 }
duke@1 896
jiangli@46810 897 // Write out the given archive heap memory regions. GC code combines multiple
jiangli@46810 898 // consecutive archive GC regions into one MemRegion whenever possible and
jiangli@46810 899 // produces the 'heap_mem' array.
jiangli@31345 900 //
jiangli@46810 901 // If the archive heap memory size is smaller than a single dump time GC region
jiangli@46810 902 // size, there is only one MemRegion in the array.
iklam@46746 903 //
jiangli@46810 904 // If the archive heap memory size is bigger than one dump time GC region size,
jiangli@46810 905 // the 'heap_mem' array may contain more than one consolidated MemRegions. When
jiangli@46810 906 // the first/bottom archive GC region is a partial GC region (with the empty
jiangli@46810 907 // portion at the higher address within the region), one MemRegion is used for
jiangli@46810 908 // the bottom partial archive GC region. The rest of the consecutive archive
jiangli@46810 909 // GC regions are combined into another MemRegion.
iklam@46746 910 //
jiangli@46810 911 // Here's the mapping from (archive heap GC regions) -> (GrowableArray<MemRegion> *regions).
jiangli@46810 912 // + We have 1 or more archive heap regions: ah0, ah1, ah2 ..... ahn
jiangli@46810 913 // + We have 1 or 2 consolidated heap memory regions: r0 and r1
jiangli@46810 914 //
jiangli@46810 915 // If there's a single archive GC region (ah0), then r0 == ah0, and r1 is empty.
iklam@46746 916 // Otherwise:
iklam@46746 917 //
iklam@46746 918 // "X" represented space that's occupied by heap objects.
iklam@46746 919 // "_" represented unused spaced in the heap region.
iklam@46746 920 //
iklam@46746 921 //
iklam@51508 922 // |ah0 | ah1 | ah2| ...... | ahn|
iklam@46746 923 // |XXXXXX|__ |XXXXX|XXXX|XXXXXXXX|XXXX|
jiangli@46810 924 // |<-r0->| |<- r1 ----------------->|
iklam@46746 925 // ^^^
iklam@46746 926 // |
jiangli@46810 927 // +-- gap
jiangli@46810 928 size_t FileMapInfo::write_archive_heap_regions(GrowableArray<MemRegion> *heap_mem,
iklam@51508 929 GrowableArray<ArchiveHeapOopmapInfo> *oopmaps,
iklam@52536 930 int first_region_id, int max_num_regions,
iklam@52536 931 bool print_log) {
jiangli@46810 932 assert(max_num_regions <= 2, "Only support maximum 2 memory regions");
iklam@46746 933
jiangli@46810 934 int arr_len = heap_mem == NULL ? 0 : heap_mem->length();
jiangli@46810 935 if(arr_len > max_num_regions) {
jiangli@46810 936 fail_stop("Unable to write archive heap memory regions: "
iklam@52834 937 "number of memory regions exceeds maximum due to fragmentation. "
jiangli@52725 938 "Please increase java heap size "
jiangli@52725 939 "(current MaxHeapSize is " SIZE_FORMAT ", InitialHeapSize is " SIZE_FORMAT ").",
jiangli@52725 940 MaxHeapSize, InitialHeapSize);
jiangli@46810 941 }
jiangli@46810 942
jiangli@46810 943 size_t total_size = 0;
jiangli@46810 944 for (int i = first_region_id, arr_idx = 0;
jiangli@46810 945 i < first_region_id + max_num_regions;
jiangli@46810 946 i++, arr_idx++) {
jiangli@31345 947 char* start = NULL;
jiangli@31345 948 size_t size = 0;
jiangli@46810 949 if (arr_idx < arr_len) {
jiangli@46810 950 start = (char*)heap_mem->at(arr_idx).start();
jiangli@46810 951 size = heap_mem->at(arr_idx).byte_size();
jiangli@46810 952 total_size += size;
jiangli@31345 953 }
jiangli@46810 954
iklam@52536 955 if (print_log) {
iklam@52536 956 log_info(cds)("Archive heap region %d " INTPTR_FORMAT " - " INTPTR_FORMAT " = " SIZE_FORMAT_W(8) " bytes",
iklam@52536 957 i, p2i(start), p2i(start + size), size);
iklam@52536 958 }
iklam@46746 959 write_region(i, start, size, false, false);
iklam@51508 960 if (size > 0) {
iklam@51508 961 space_at(i)->_oopmap = oopmaps->at(arr_idx)._oopmap;
iklam@51508 962 space_at(i)->_oopmap_size_in_bits = oopmaps->at(arr_idx)._oopmap_size_in_bits;
iklam@51508 963 }
jiangli@31345 964 }
jiangli@46810 965 return total_size;
jiangli@31345 966 }
jiangli@31345 967
duke@1 968 // Dump bytes to file -- at the current file position.
duke@1 969
iklam@51494 970 void FileMapInfo::write_bytes(const void* buffer, size_t nbytes) {
duke@1 971 if (_file_open) {
iklam@51494 972 size_t n = os::write(_fd, buffer, (unsigned int)nbytes);
duke@1 973 if (n != nbytes) {
ccheung@54960 974 // If the shared archive is corrupted, close it and remove it.
duke@1 975 close();
duke@1 976 remove(_full_path);
sla@26296 977 fail_stop("Unable to write to shared archive file.");
duke@1 978 }
duke@1 979 }
duke@1 980 _file_offset += nbytes;
duke@1 981 }
duke@1 982
ccheung@54960 983 bool FileMapInfo::is_file_position_aligned() const {
ccheung@54960 984 return _file_offset == align_up(_file_offset,
ccheung@54960 985 os::vm_allocation_granularity());
ccheung@54960 986 }
duke@1 987
duke@1 988 // Align file position to an allocation unit boundary.
duke@1 989
duke@1 990 void FileMapInfo::align_file_position() {
stefank@46619 991 size_t new_file_offset = align_up(_file_offset,
jiangli@27025 992 os::vm_allocation_granularity());
duke@1 993 if (new_file_offset != _file_offset) {
duke@1 994 _file_offset = new_file_offset;
duke@1 995 if (_file_open) {
duke@1 996 // Seek one byte back from the target and write a byte to insure
duke@1 997 // that the written file is the correct length.
duke@1 998 _file_offset -= 1;
jiangli@27025 999 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
sla@26296 1000 fail_stop("Unable to seek.");
duke@1 1001 }
duke@1 1002 char zero = 0;
duke@1 1003 write_bytes(&zero, 1);
duke@1 1004 }
duke@1 1005 }
duke@1 1006 }
duke@1 1007
duke@1 1008
duke@1 1009 // Dump bytes to file -- at the current file position.
duke@1 1010
iklam@51494 1011 void FileMapInfo::write_bytes_aligned(const void* buffer, size_t nbytes) {
duke@1 1012 align_file_position();
duke@1 1013 write_bytes(buffer, nbytes);
duke@1 1014 align_file_position();
duke@1 1015 }
duke@1 1016
duke@1 1017
duke@1 1018 // Close the shared archive file. This does NOT unmap mapped regions.
duke@1 1019
duke@1 1020 void FileMapInfo::close() {
duke@1 1021 if (_file_open) {
duke@1 1022 if (::close(_fd) < 0) {
duke@1 1023 fail_stop("Unable to close the shared archive file.");
duke@1 1024 }
duke@1 1025 _file_open = false;
duke@1 1026 _fd = -1;
duke@1 1027 }
duke@1 1028 }
duke@1 1029
duke@1 1030
duke@1 1031 // JVM/TI RedefineClasses() support:
duke@1 1032 // Remap the shared readonly space to shared readwrite, private.
duke@1 1033 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
iklam@46746 1034 int idx = MetaspaceShared::ro;
iklam@51494 1035 CDSFileMapRegion* si = space_at(idx);
duke@1 1036 if (!si->_read_only) {
duke@1 1037 // the space is already readwrite so we are done
duke@1 1038 return true;
duke@1 1039 }
duke@1 1040 size_t used = si->_used;
stefank@46619 1041 size_t size = align_up(used, os::vm_allocation_granularity());
duke@1 1042 if (!open_for_read()) {
duke@1 1043 return false;
duke@1 1044 }
iklam@51508 1045 char *addr = region_addr(idx);
duke@1 1046 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
jiangli@31345 1047 addr, size, false /* !read_only */,
duke@1 1048 si->_allow_exec);
duke@1 1049 close();
coleenp@54605 1050 // These have to be errors because the shared region is now unmapped.
duke@1 1051 if (base == NULL) {
coleenp@54605 1052 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
coleenp@54605 1053 vm_exit(1);
duke@1 1054 }
jiangli@31345 1055 if (base != addr) {
coleenp@54605 1056 log_error(cds)("Unable to remap shared readonly space (errno=%d).", errno);
coleenp@54605 1057 vm_exit(1);
duke@1 1058 }
duke@1 1059 si->_read_only = false;
duke@1 1060 return true;
duke@1 1061 }
duke@1 1062
coleenp@13728 1063 // Map the whole region at once, assumed to be allocated contiguously.
coleenp@13728 1064 ReservedSpace FileMapInfo::reserve_shared_memory() {
iklam@51508 1065 char* requested_addr = region_addr(0);
iklam@46746 1066 size_t size = FileMapInfo::core_spaces_size();
coleenp@13728 1067
coleenp@13728 1068 // Reserve the space first, then map otherwise map will go right over some
coleenp@13728 1069 // other reserved memory (like the code cache).
hseigel@19319 1070 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
coleenp@13728 1071 if (!rs.is_reserved()) {
david@33148 1072 fail_continue("Unable to reserve shared space at required address "
david@33148 1073 INTPTR_FORMAT, p2i(requested_addr));
coleenp@13728 1074 return rs;
coleenp@13728 1075 }
zgu@14120 1076 // the reserved virtual memory is for mapping class data sharing archive
hseigel@15100 1077 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
hseigel@15100 1078
coleenp@13728 1079 return rs;
coleenp@13728 1080 }
duke@1 1081
duke@1 1082 // Memory map a region in the address space.
ccheung@55330 1083 static const char* shared_region_name[] = { "MiscData", "ReadWrite", "ReadOnly", "MiscCode",
jiangli@46810 1084 "String1", "String2", "OpenArchive1", "OpenArchive2" };
duke@1 1085
ccheung@54960 1086 char* FileMapInfo::map_regions(int regions[], char* saved_base[], size_t len) {
ccheung@54960 1087 char* prev_top = NULL;
ccheung@54960 1088 char* curr_base;
ccheung@54960 1089 char* curr_top;
ccheung@54960 1090 int i = 0;
ccheung@54960 1091 for (i = 0; i < (int)len; i++) {
ccheung@54960 1092 curr_base = map_region(regions[i], &curr_top);
ccheung@54960 1093 if (curr_base == NULL) {
ccheung@54960 1094 return NULL;
ccheung@54960 1095 }
ccheung@54960 1096 if (i > 0) {
ccheung@55330 1097 // We require that mc->rw->ro->md to be laid out consecutively, with no
ccheung@54960 1098 // gaps between them. That way, we can ensure that the OS won't be able to
ccheung@54960 1099 // allocate any new memory spaces inside _shared_metaspace_{base,top}, which
ccheung@54960 1100 // would mess up the simple comparision in MetaspaceShared::is_in_shared_metaspace().
ccheung@54960 1101 assert(curr_base == prev_top, "must be");
ccheung@54960 1102 }
ccheung@54960 1103 log_info(cds)("Mapped region #%d at base %p top %p", regions[i], curr_base, curr_top);
ccheung@54960 1104 saved_base[i] = curr_base;
ccheung@54960 1105 prev_top = curr_top;
ccheung@54960 1106 }
ccheung@54960 1107 return curr_top;
ccheung@54960 1108 }
ccheung@54960 1109
iklam@48795 1110 char* FileMapInfo::map_region(int i, char** top_ret) {
jiangli@52424 1111 assert(!HeapShared::is_heap_region(i), "sanity");
iklam@51494 1112 CDSFileMapRegion* si = space_at(i);
duke@1 1113 size_t used = si->_used;
coleenp@13728 1114 size_t alignment = os::vm_allocation_granularity();
stefank@46619 1115 size_t size = align_up(used, alignment);
iklam@51508 1116 char *requested_addr = region_addr(i);
cjplummer@31332 1117
coleenp@54605 1118 #ifdef _WINDOWS
coleenp@54605 1119 // Windows cannot remap read-only shared memory to read-write when required for
coleenp@54605 1120 // RedefineClasses, which is also used by JFR. Always map windows regions as RW.
coleenp@54605 1121 si->_read_only = false;
coleenp@54605 1122 #else
coleenp@54605 1123 // If a tool agent is in use (debugging enabled), or JFR, we must map the address space RW
coleenp@54605 1124 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space() ||
coleenp@54605 1125 Arguments::has_jfr_option()) {
cjplummer@31375 1126 si->_read_only = false;
cjplummer@31332 1127 }
coleenp@54605 1128 #endif // _WINDOWS
cjplummer@31332 1129
coleenp@13728 1130 // map the contents of the CDS archive in this memory
duke@1 1131 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
cjplummer@31375 1132 requested_addr, size, si->_read_only,
duke@1 1133 si->_allow_exec);
jiangli@31345 1134 if (base == NULL || base != requested_addr) {
sla@26296 1135 fail_continue("Unable to map %s shared space at required address.", shared_region_name[i]);
ccheung@54960 1136 _memory_mapping_failed = true;
duke@1 1137 return NULL;
duke@1 1138 }
hseigel@15100 1139 #ifdef _WINDOWS
hseigel@15100 1140 // This call is Windows-only because the memory_type gets recorded for the other platforms
hseigel@15100 1141 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
hseigel@15100 1142 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
hseigel@15100 1143 #endif
jiangli@31345 1144
redestad@54871 1145 if (VerifySharedSpaces && !verify_region_checksum(i)) {
iklam@48795 1146 return NULL;
iklam@48795 1147 }
iklam@48795 1148
iklam@48795 1149 *top_ret = base + size;
duke@1 1150 return base;
duke@1 1151 }
duke@1 1152
ccheung@54960 1153 size_t FileMapInfo::read_bytes(void* buffer, size_t count) {
ccheung@54960 1154 assert(_file_open, "Archive file is not open");
ccheung@54960 1155 size_t n = os::read(_fd, buffer, (unsigned int)count);
ccheung@54960 1156 if (n != count) {
ccheung@54960 1157 // Close the file if there's a problem reading it.
ccheung@54960 1158 close();
ccheung@54960 1159 return 0;
ccheung@54960 1160 }
ccheung@54960 1161 _file_offset += count;
ccheung@54960 1162 return count;
ccheung@54960 1163 }
ccheung@54960 1164
iklam@51508 1165 address FileMapInfo::decode_start_address(CDSFileMapRegion* spc, bool with_current_oop_encoding_mode) {
iklam@51508 1166 if (with_current_oop_encoding_mode) {
iklam@51508 1167 return (address)CompressedOops::decode_not_null(offset_of_space(spc));
iklam@51508 1168 } else {
iklam@51738 1169 return (address)HeapShared::decode_from_archive(offset_of_space(spc));
iklam@51508 1170 }
iklam@51508 1171 }
iklam@51508 1172
jiangli@52424 1173 static MemRegion *closed_archive_heap_ranges = NULL;
jiangli@46810 1174 static MemRegion *open_archive_heap_ranges = NULL;
jiangli@52424 1175 static int num_closed_archive_heap_ranges = 0;
jiangli@46810 1176 static int num_open_archive_heap_ranges = 0;
jiangli@46810 1177
jiangli@46810 1178 #if INCLUDE_CDS_JAVA_HEAP
iklam@51508 1179 bool FileMapInfo::has_heap_regions() {
jiangli@52424 1180 return (_header->_space[MetaspaceShared::first_closed_archive_heap_region]._used > 0);
iklam@51508 1181 }
iklam@51508 1182
iklam@51508 1183 // Returns the address range of the archived heap regions computed using the
iklam@51508 1184 // current oop encoding mode. This range may be different than the one seen at
iklam@51508 1185 // dump time due to encoding mode differences. The result is used in determining
iklam@51508 1186 // if/how these regions should be relocated at run time.
iklam@51508 1187 MemRegion FileMapInfo::get_heap_regions_range_with_current_oop_encoding_mode() {
iklam@51508 1188 address start = (address) max_uintx;
iklam@51508 1189 address end = NULL;
iklam@51508 1190
jiangli@52424 1191 for (int i = MetaspaceShared::first_closed_archive_heap_region;
jiangli@52424 1192 i <= MetaspaceShared::last_valid_region;
jiangli@52424 1193 i++) {
iklam@51508 1194 CDSFileMapRegion* si = space_at(i);
iklam@51508 1195 size_t size = si->_used;
iklam@51508 1196 if (size > 0) {
iklam@51738 1197 address s = start_address_as_decoded_with_current_oop_encoding_mode(si);
iklam@51508 1198 address e = s + size;
iklam@51508 1199 if (start > s) {
iklam@51508 1200 start = s;
iklam@51508 1201 }
iklam@51508 1202 if (end < e) {
iklam@51508 1203 end = e;
iklam@51508 1204 }
iklam@51508 1205 }
iklam@51508 1206 }
iklam@51508 1207 assert(end != NULL, "must have at least one used heap region");
iklam@51508 1208 return MemRegion((HeapWord*)start, (HeapWord*)end);
iklam@51508 1209 }
iklam@51508 1210
jiangli@46810 1211 //
jiangli@52424 1212 // Map the closed and open archive heap objects to the runtime java heap.
jiangli@46810 1213 //
jiangli@52424 1214 // The shared objects are mapped at (or close to ) the java heap top in
jiangli@52424 1215 // closed archive regions. The mapped objects contain no out-going
jiangli@52424 1216 // references to any other java heap regions. GC does not write into the
jiangli@52424 1217 // mapped closed archive heap region.
jiangli@46810 1218 //
jiangli@52424 1219 // The open archive heap objects are mapped below the shared objects in
jiangli@52424 1220 // the runtime java heap. The mapped open archive heap data only contains
jiangli@52424 1221 // references to the shared objects and open archive objects initially.
jiangli@46810 1222 // During runtime execution, out-going references to any other java heap
jiangli@46810 1223 // regions may be added. GC may mark and update references in the mapped
jiangli@46810 1224 // open archive objects.
iklam@51508 1225 void FileMapInfo::map_heap_regions_impl() {
jiangli@52082 1226 if (!HeapShared::is_heap_object_archiving_allowed()) {
iklam@51508 1227 log_info(cds)("CDS heap data is being ignored. UseG1GC, "
iklam@51508 1228 "UseCompressedOops and UseCompressedClassPointers are required.");
iklam@51508 1229 return;
iklam@51508 1230 }
iklam@51508 1231
iklam@52340 1232 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
iklam@52340 1233 ShouldNotReachHere(); // CDS should have been disabled.
iklam@52340 1234 // The archived objects are mapped at JVM start-up, but we don't know if
iklam@52340 1235 // j.l.String or j.l.Class might be replaced by the ClassFileLoadHook,
iklam@52340 1236 // which would make the archived String or mirror objects invalid. Let's be safe and not
iklam@52340 1237 // use the archived objects. These 2 classes are loaded during the JVMTI "early" stage.
iklam@52340 1238 //
iklam@52340 1239 // If JvmtiExport::has_early_class_hook_env() is false, the classes of some objects
iklam@52340 1240 // in the archived subgraphs may be replaced by the ClassFileLoadHook. But that's OK
iklam@52340 1241 // because we won't install an archived object subgraph if the klass of any of the
iklam@52340 1242 // referenced objects are replaced. See HeapShared::initialize_from_archived_subgraph().
iklam@52340 1243 }
iklam@52340 1244
iklam@51508 1245 MemRegion heap_reserved = Universe::heap()->reserved_region();
iklam@51508 1246
iklam@51508 1247 log_info(cds)("CDS archive was created with max heap size = " SIZE_FORMAT "M, and the following configuration:",
iklam@51508 1248 max_heap_size()/M);
iklam@51508 1249 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
iklam@51508 1250 p2i(narrow_klass_base()), narrow_klass_shift());
iklam@51508 1251 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
iklam@51508 1252 narrow_oop_mode(), p2i(narrow_oop_base()), narrow_oop_shift());
iklam@51508 1253
iklam@51508 1254 log_info(cds)("The current max heap size = " SIZE_FORMAT "M, HeapRegion::GrainBytes = " SIZE_FORMAT,
iklam@51508 1255 heap_reserved.byte_size()/M, HeapRegion::GrainBytes);
iklam@51508 1256 log_info(cds)(" narrow_klass_base = " PTR_FORMAT ", narrow_klass_shift = %d",
stefank@54813 1257 p2i(CompressedKlassPointers::base()), CompressedKlassPointers::shift());
iklam@51508 1258 log_info(cds)(" narrow_oop_mode = %d, narrow_oop_base = " PTR_FORMAT ", narrow_oop_shift = %d",
stefank@54813 1259 CompressedOops::mode(), p2i(CompressedOops::base()), CompressedOops::shift());
iklam@51508 1260
stefank@54813 1261 if (narrow_klass_base() != CompressedKlassPointers::base() ||
stefank@54813 1262 narrow_klass_shift() != CompressedKlassPointers::shift()) {
iklam@51508 1263 log_info(cds)("CDS heap data cannot be used because the archive was created with an incompatible narrow klass encoding mode.");
iklam@51508 1264 return;
iklam@51508 1265 }
iklam@51508 1266
stefank@54813 1267 if (narrow_oop_mode() != CompressedOops::mode() ||
stefank@54813 1268 narrow_oop_base() != CompressedOops::base() ||
stefank@54813 1269 narrow_oop_shift() != CompressedOops::shift()) {
iklam@51508 1270 log_info(cds)("CDS heap data need to be relocated because the archive was created with an incompatible oop encoding mode.");
iklam@51508 1271 _heap_pointers_need_patching = true;
iklam@51508 1272 } else {
iklam@51508 1273 MemRegion range = get_heap_regions_range_with_current_oop_encoding_mode();
iklam@51508 1274 if (!heap_reserved.contains(range)) {
iklam@51508 1275 log_info(cds)("CDS heap data need to be relocated because");
iklam@51508 1276 log_info(cds)("the desired range " PTR_FORMAT " - " PTR_FORMAT, p2i(range.start()), p2i(range.end()));
iklam@51508 1277 log_info(cds)("is outside of the heap " PTR_FORMAT " - " PTR_FORMAT, p2i(heap_reserved.start()), p2i(heap_reserved.end()));
iklam@51508 1278 _heap_pointers_need_patching = true;
iklam@51508 1279 }
iklam@51508 1280 }
iklam@51508 1281
iklam@51508 1282 ptrdiff_t delta = 0;
iklam@51508 1283 if (_heap_pointers_need_patching) {
iklam@51508 1284 // dumptime heap end ------------v
iklam@51508 1285 // [ |archived heap regions| ] runtime heap end ------v
iklam@51508 1286 // [ |archived heap regions| ]
iklam@51508 1287 // |<-----delta-------------------->|
iklam@51508 1288 //
iklam@51508 1289 // At dump time, the archived heap regions were near the top of the heap.
iklam@51508 1290 // At run time, they may not be inside the heap, so we move them so
iklam@51508 1291 // that they are now near the top of the runtime time. This can be done by
iklam@51508 1292 // the simple math of adding the delta as shown above.
iklam@51508 1293 address dumptime_heap_end = (address)_header->_heap_reserved.end();
iklam@51508 1294 address runtime_heap_end = (address)heap_reserved.end();
iklam@51508 1295 delta = runtime_heap_end - dumptime_heap_end;
iklam@51508 1296 }
iklam@51508 1297
iklam@51508 1298 log_info(cds)("CDS heap data relocation delta = " INTX_FORMAT " bytes", delta);
iklam@51508 1299 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
iklam@51508 1300
jiangli@52424 1301 CDSFileMapRegion* si = space_at(MetaspaceShared::first_closed_archive_heap_region);
jiangli@52424 1302 address relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
jiangli@52424 1303 if (!is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes)) {
jiangli@52424 1304 // Align the bottom of the closed archive heap regions at G1 region boundary.
jiangli@52424 1305 // This will avoid the situation where the highest open region and the lowest
jiangli@52424 1306 // closed region sharing the same G1 region. Otherwise we will fail to map the
jiangli@52424 1307 // open regions.
jiangli@52424 1308 size_t align = size_t(relocated_closed_heap_region_bottom) % HeapRegion::GrainBytes;
iklam@51508 1309 delta -= align;
iklam@51508 1310 log_info(cds)("CDS heap data need to be relocated lower by a further " SIZE_FORMAT
jiangli@52424 1311 " bytes to " INTX_FORMAT " to be aligned with HeapRegion::GrainBytes",
jiangli@52424 1312 align, delta);
iklam@51508 1313 HeapShared::init_narrow_oop_decoding(narrow_oop_base() + delta, narrow_oop_shift());
iklam@51508 1314 _heap_pointers_need_patching = true;
jiangli@52424 1315 relocated_closed_heap_region_bottom = start_address_as_decoded_from_archive(si);
iklam@51508 1316 }
jiangli@52424 1317 assert(is_aligned(relocated_closed_heap_region_bottom, HeapRegion::GrainBytes),
jiangli@52424 1318 "must be");
iklam@51508 1319
jiangli@52424 1320 // Map the closed_archive_heap regions, GC does not write into the regions.
jiangli@52424 1321 if (map_heap_data(&closed_archive_heap_ranges,
jiangli@52424 1322 MetaspaceShared::first_closed_archive_heap_region,
jiangli@52424 1323 MetaspaceShared::max_closed_archive_heap_region,
jiangli@52424 1324 &num_closed_archive_heap_ranges)) {
jiangli@52424 1325 HeapShared::set_closed_archive_heap_region_mapped();
iklam@51508 1326
iklam@51508 1327 // Now, map open_archive heap regions, GC can write into the regions.
iklam@51508 1328 if (map_heap_data(&open_archive_heap_ranges,
iklam@51508 1329 MetaspaceShared::first_open_archive_heap_region,
iklam@51508 1330 MetaspaceShared::max_open_archive_heap_region,
iklam@51508 1331 &num_open_archive_heap_ranges,
iklam@51508 1332 true /* open */)) {
jiangli@52082 1333 HeapShared::set_open_archive_heap_region_mapped();
iklam@51508 1334 }
iklam@51508 1335 }
iklam@51508 1336 }
iklam@51508 1337
jiangli@46810 1338 void FileMapInfo::map_heap_regions() {
iklam@51508 1339 if (has_heap_regions()) {
iklam@51508 1340 map_heap_regions_impl();
jiangli@31345 1341 }
jiangli@31345 1342
jiangli@52424 1343 if (!HeapShared::closed_archive_heap_region_mapped()) {
jiangli@52424 1344 assert(closed_archive_heap_ranges == NULL &&
jiangli@52424 1345 num_closed_archive_heap_ranges == 0, "sanity");
jiangli@46810 1346 }
jiangli@46810 1347
jiangli@52082 1348 if (!HeapShared::open_archive_heap_region_mapped()) {
jiangli@46810 1349 assert(open_archive_heap_ranges == NULL && num_open_archive_heap_ranges == 0, "sanity");
jiangli@46810 1350 }
jiangli@46810 1351 }
jiangli@46810 1352
jiangli@46810 1353 bool FileMapInfo::map_heap_data(MemRegion **heap_mem, int first,
jiangli@46810 1354 int max, int* num, bool is_open_archive) {
jiangli@46810 1355 MemRegion * regions = new MemRegion[max];
iklam@51494 1356 CDSFileMapRegion* si;
jiangli@46810 1357 int region_num = 0;
jiangli@46810 1358
jiangli@46810 1359 for (int i = first;
jiangli@46810 1360 i < first + max; i++) {
iklam@51494 1361 si = space_at(i);
iklam@51508 1362 size_t size = si->_used;
iklam@51508 1363 if (size > 0) {
iklam@51738 1364 HeapWord* start = (HeapWord*)start_address_as_decoded_from_archive(si);
iklam@51508 1365 regions[region_num] = MemRegion(start, size / HeapWordSize);
jiangli@46810 1366 region_num ++;
iklam@51508 1367 log_info(cds)("Trying to map heap data: region[%d] at " INTPTR_FORMAT ", size = " SIZE_FORMAT_W(8) " bytes",
iklam@51508 1368 i, p2i(start), size);
jiangli@46810 1369 }
jiangli@46810 1370 }
jiangli@46810 1371
jiangli@46810 1372 if (region_num == 0) {
jiangli@46810 1373 return false; // no archived java heap data
jiangli@46810 1374 }
jiangli@46810 1375
jiangli@46810 1376 // Check that ranges are within the java heap
jiangli@46810 1377 if (!G1CollectedHeap::heap()->check_archive_addresses(regions, region_num)) {
iklam@51508 1378 log_info(cds)("UseSharedSpaces: Unable to allocate region, range is not within java heap.");
jiangli@46810 1379 return false;
jiangli@46810 1380 }
jiangli@46810 1381
jiangli@46810 1382 // allocate from java heap
jiangli@46810 1383 if (!G1CollectedHeap::heap()->alloc_archive_regions(
jiangli@46810 1384 regions, region_num, is_open_archive)) {
iklam@51508 1385 log_info(cds)("UseSharedSpaces: Unable to allocate region, java heap range is already in use.");
jiangli@46810 1386 return false;
jiangli@46810 1387 }
jiangli@46810 1388
jiangli@46810 1389 // Map the archived heap data. No need to call MemTracker::record_virtual_memory_type()
jiangli@46810 1390 // for mapped regions as they are part of the reserved java heap, which is
jiangli@46810 1391 // already recorded.
jiangli@46810 1392 for (int i = 0; i < region_num; i++) {
iklam@51494 1393 si = space_at(first + i);
jiangli@46810 1394 char* addr = (char*)regions[i].start();
jiangli@46810 1395 char* base = os::map_memory(_fd, _full_path, si->_file_offset,
jiangli@46810 1396 addr, regions[i].byte_size(), si->_read_only,
jiangli@46810 1397 si->_allow_exec);
jiangli@46810 1398 if (base == NULL || base != addr) {
jiangli@46810 1399 // dealloc the regions from java heap
sjohanss@52697 1400 dealloc_archive_heap_regions(regions, region_num, is_open_archive);
iklam@51508 1401 log_info(cds)("UseSharedSpaces: Unable to map at required address in java heap. "
iklam@51508 1402 INTPTR_FORMAT ", size = " SIZE_FORMAT " bytes",
iklam@51508 1403 p2i(addr), regions[i].byte_size());
jiangli@46810 1404 return false;
jiangli@46810 1405 }
jiangli@46810 1406
ccheung@54960 1407 if (VerifySharedSpaces && !region_crc_check(addr, regions[i].byte_size(), si->_crc)) {
ccheung@54960 1408 // dealloc the regions from java heap
ccheung@54960 1409 dealloc_archive_heap_regions(regions, region_num, is_open_archive);
ccheung@54960 1410 log_info(cds)("UseSharedSpaces: mapped heap regions are corrupt");
ccheung@54960 1411 return false;
ccheung@54960 1412 }
jiangli@46810 1413 }
jiangli@46810 1414
jiangli@46810 1415 // the shared heap data is mapped successfully
jiangli@46810 1416 *heap_mem = regions;
jiangli@46810 1417 *num = region_num;
jiangli@31345 1418 return true;
jiangli@31345 1419 }
jiangli@31345 1420
iklam@51508 1421 void FileMapInfo::patch_archived_heap_embedded_pointers() {
iklam@51508 1422 if (!_heap_pointers_need_patching) {
iklam@51508 1423 return;
iklam@51508 1424 }
iklam@51508 1425
jiangli@52424 1426 patch_archived_heap_embedded_pointers(closed_archive_heap_ranges,
jiangli@52424 1427 num_closed_archive_heap_ranges,
jiangli@52424 1428 MetaspaceShared::first_closed_archive_heap_region);
iklam@51508 1429
iklam@51508 1430 patch_archived_heap_embedded_pointers(open_archive_heap_ranges,
iklam@51508 1431 num_open_archive_heap_ranges,
iklam@51508 1432 MetaspaceShared::first_open_archive_heap_region);
iklam@51508 1433 }
iklam@51508 1434
iklam@51508 1435 void FileMapInfo::patch_archived_heap_embedded_pointers(MemRegion* ranges, int num_ranges,
iklam@51508 1436 int first_region_idx) {
iklam@51508 1437 for (int i=0; i<num_ranges; i++) {
iklam@51508 1438 CDSFileMapRegion* si = space_at(i + first_region_idx);
iklam@51508 1439 HeapShared::patch_archived_heap_embedded_pointers(ranges[i], (address)si->_oopmap,
iklam@51508 1440 si->_oopmap_size_in_bits);
iklam@51508 1441 }
iklam@51508 1442 }
iklam@51508 1443
iklam@51508 1444 // This internally allocates objects using SystemDictionary::Object_klass(), so it
iklam@51508 1445 // must be called after the well-known classes are resolved.
jiangli@46810 1446 void FileMapInfo::fixup_mapped_heap_regions() {
jiangli@52424 1447 // If any closed regions were found, call the fill routine to make them parseable.
jiangli@52424 1448 // Note that closed_archive_heap_ranges may be non-NULL even if no ranges were found.
jiangli@52424 1449 if (num_closed_archive_heap_ranges != 0) {
jiangli@52424 1450 assert(closed_archive_heap_ranges != NULL,
jiangli@52424 1451 "Null closed_archive_heap_ranges array with non-zero count");
jiangli@52424 1452 G1CollectedHeap::heap()->fill_archive_regions(closed_archive_heap_ranges,
jiangli@52424 1453 num_closed_archive_heap_ranges);
jiangli@31345 1454 }
jiangli@46810 1455
jiangli@46810 1456 // do the same for mapped open archive heap regions
jiangli@46810 1457 if (num_open_archive_heap_ranges != 0) {
jiangli@46810 1458 assert(open_archive_heap_ranges != NULL, "NULL open_archive_heap_ranges array with non-zero count");
jiangli@46810 1459 G1CollectedHeap::heap()->fill_archive_regions(open_archive_heap_ranges,
jiangli@46810 1460 num_open_archive_heap_ranges);
jiangli@46810 1461 }
jiangli@31345 1462 }
jiangli@31345 1463
jiangli@46810 1464 // dealloc the archive regions from java heap
sjohanss@52697 1465 void FileMapInfo::dealloc_archive_heap_regions(MemRegion* regions, int num, bool is_open) {
jiangli@46810 1466 if (num > 0) {
jiangli@46810 1467 assert(regions != NULL, "Null archive ranges array with non-zero count");
sjohanss@52697 1468 G1CollectedHeap::heap()->dealloc_archive_regions(regions, num, is_open);
jiangli@46810 1469 }
jiangli@46810 1470 }
jiangli@46810 1471 #endif // INCLUDE_CDS_JAVA_HEAP
jiangli@46810 1472
ccheung@54960 1473 bool FileMapInfo::region_crc_check(char* buf, size_t size, int expected_crc) {
ccheung@54960 1474 int crc = ClassLoader::crc32(0, buf, (jint)size);
ccheung@54960 1475 if (crc != expected_crc) {
ccheung@54960 1476 fail_continue("Checksum verification failed.");
ccheung@54960 1477 return false;
ccheung@54960 1478 }
ccheung@54960 1479 return true;
ccheung@54960 1480 }
ccheung@54960 1481
jiangli@27025 1482 bool FileMapInfo::verify_region_checksum(int i) {
redestad@54871 1483 assert(VerifySharedSpaces, "sanity");
jiangli@31345 1484
iklam@51494 1485 size_t sz = space_at(i)->_used;
jiangli@31345 1486
jiangli@31345 1487 if (sz == 0) {
jiangli@31345 1488 return true; // no data
jiangli@31345 1489 }
ccheung@54960 1490
ccheung@54960 1491 return region_crc_check(region_addr(i), sz, space_at(i)->_crc);
ccheung@54960 1492 }
ccheung@54960 1493
ccheung@54960 1494 void FileMapInfo::unmap_regions(int regions[], char* saved_base[], size_t len) {
ccheung@54960 1495 for (int i = 0; i < (int)len; i++) {
ccheung@54960 1496 if (saved_base[i] != NULL) {
ccheung@54960 1497 unmap_region(regions[i]);
ccheung@54960 1498 }
jiangli@31345 1499 }
jiangli@27025 1500 }
duke@1 1501
duke@1 1502 // Unmap a memory region in the address space.
duke@1 1503
duke@1 1504 void FileMapInfo::unmap_region(int i) {
jiangli@52424 1505 assert(!HeapShared::is_heap_region(i), "sanity");
iklam@51494 1506 CDSFileMapRegion* si = space_at(i);
duke@1 1507 size_t used = si->_used;
stefank@46619 1508 size_t size = align_up(used, os::vm_allocation_granularity());
jiangli@31345 1509
jiangli@31345 1510 if (used == 0) {
jiangli@31345 1511 return;
jiangli@31345 1512 }
jiangli@31345 1513
iklam@51508 1514 char* addr = region_addr(i);
jiangli@31345 1515 if (!os::unmap_memory(addr, size)) {
duke@1 1516 fail_stop("Unable to unmap shared space.");
duke@1 1517 }
duke@1 1518 }
duke@1 1519
duke@1 1520 void FileMapInfo::assert_mark(bool check) {
duke@1 1521 if (!check) {
sla@26296 1522 fail_stop("Mark mismatch while restoring from shared file.");
duke@1 1523 }
duke@1 1524 }
duke@1 1525
iklam@46746 1526 void FileMapInfo::metaspace_pointers_do(MetaspaceClosure* it) {
ccheung@54960 1527 _shared_path_table.metaspace_pointers_do(it);
iklam@46746 1528 }
iklam@46746 1529
duke@1 1530 FileMapInfo* FileMapInfo::_current_info = NULL;
ccheung@54960 1531 FileMapInfo* FileMapInfo::_dynamic_archive_info = NULL;
iklam@51508 1532 bool FileMapInfo::_heap_pointers_need_patching = false;
ccheung@54960 1533 SharedPathTable FileMapInfo::_shared_path_table;
ccheung@49748 1534 bool FileMapInfo::_validating_shared_path_table = false;
ccheung@54960 1535 bool FileMapInfo::_memory_mapping_failed = false;
duke@1 1536
duke@1 1537 // Open the shared archive file, read and validate the header
duke@1 1538 // information (version, boot classpath, etc.). If initialization
duke@1 1539 // fails, shared spaces are disabled and the file is closed. [See
duke@1 1540 // fail_continue.]
iklam@26135 1541 //
iklam@26135 1542 // Validation of the archive is done in two steps:
iklam@26135 1543 //
iklam@26135 1544 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
ccheung@49748 1545 // [2] validate_shared_path_table - this is done later, because the table is in the RW
iklam@26135 1546 // region of the archive, which is not mapped yet.
ccheung@54960 1547 bool FileMapInfo::initialize(bool is_static) {
duke@1 1548 assert(UseSharedSpaces, "UseSharedSpaces expected.");
duke@1 1549
iklam@52340 1550 if (JvmtiExport::should_post_class_file_load_hook() && JvmtiExport::has_early_class_hook_env()) {
iklam@52340 1551 // CDS assumes that no classes resolved in SystemDictionary::resolve_well_known_classes
iklam@52340 1552 // are replaced at runtime by JVMTI ClassFileLoadHook. All of those classes are resolved
iklam@52340 1553 // during the JVMTI "early" stage, so we can still use CDS if
iklam@52340 1554 // JvmtiExport::has_early_class_hook_env() is false.
iklam@52340 1555 FileMapInfo::fail_continue("CDS is disabled because early JVMTI ClassFileLoadHook is in use.");
iklam@52340 1556 return false;
iklam@52340 1557 }
iklam@52340 1558
duke@1 1559 if (!open_for_read()) {
duke@1 1560 return false;
duke@1 1561 }
duke@1 1562
ccheung@54960 1563 init_from_file(_fd, is_static);
ccheung@54960 1564 if (!validate_header(is_static)) {
duke@1 1565 return false;
duke@1 1566 }
duke@1 1567 return true;
duke@1 1568 }
duke@1 1569
iklam@51508 1570 char* FileMapInfo::region_addr(int idx) {
iklam@51508 1571 CDSFileMapRegion* si = space_at(idx);
jiangli@52424 1572 if (HeapShared::is_heap_region(idx)) {
iklam@51508 1573 assert(DumpSharedSpaces, "The following doesn't work at runtime");
iklam@51508 1574 return si->_used > 0 ?
iklam@51738 1575 (char*)start_address_as_decoded_with_current_oop_encoding_mode(si) : NULL;
jiangli@31345 1576 } else {
iklam@51508 1577 return si->_addr._base;
jiangli@31345 1578 }
jiangli@31345 1579 }
jiangli@31345 1580
iklam@51494 1581 int FileMapHeader::compute_crc() {
iklam@51494 1582 char* start = (char*)this;
jiangli@27025 1583 // start computing from the field after _crc
iklam@51494 1584 char* buf = (char*)&_crc + sizeof(_crc);
ccheung@54960 1585 size_t sz = _header_size - (buf - start);
jiangli@27025 1586 int crc = ClassLoader::crc32(0, buf, (jint)sz);
jiangli@27025 1587 return crc;
jiangli@27025 1588 }
jiangli@27025 1589
ccheung@49748 1590 // This function should only be called during run time with UseSharedSpaces enabled.
iklam@51494 1591 bool FileMapHeader::validate() {
jiangli@27025 1592 if (VerifySharedSpaces && compute_crc() != _crc) {
iklam@51494 1593 FileMapInfo::fail_continue("Header checksum verification failed.");
jiangli@27025 1594 return false;
jiangli@27025 1595 }
jiangli@27025 1596
lfoltan@40013 1597 if (!Arguments::has_jimage()) {
lfoltan@40013 1598 FileMapInfo::fail_continue("The shared archive file cannot be used with an exploded module build.");
lfoltan@40013 1599 return false;
lfoltan@40013 1600 }
lfoltan@40013 1601
iklam@51494 1602 if (_version != CURRENT_CDS_ARCHIVE_VERSION) {
iklam@26135 1603 FileMapInfo::fail_continue("The shared archive file is the wrong version.");
duke@1 1604 return false;
duke@1 1605 }
ccheung@54960 1606 if (_magic != CDS_ARCHIVE_MAGIC && _magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
iklam@26135 1607 FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
duke@1 1608 return false;
duke@1 1609 }
hseigel@14577 1610 char header_version[JVM_IDENT_MAX];
hseigel@14577 1611 get_header_version(header_version);
iklam@26135 1612 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
mockner@38151 1613 log_info(class, path)("expected: %s", header_version);
mockner@38151 1614 log_info(class, path)("actual: %s", _jvm_ident);
iklam@26135 1615 FileMapInfo::fail_continue("The shared archive file was created by a different"
iklam@26135 1616 " version or build of HotSpot");
duke@1 1617 return false;
duke@1 1618 }
iklam@26135 1619 if (_obj_alignment != ObjectAlignmentInBytes) {
iklam@26135 1620 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
david@33148 1621 " does not equal the current ObjectAlignmentInBytes of " INTX_FORMAT ".",
iklam@26135 1622 _obj_alignment, ObjectAlignmentInBytes);
duke@1 1623 return false;
duke@1 1624 }
thartmann@33628 1625 if (_compact_strings != CompactStrings) {
thartmann@33628 1626 FileMapInfo::fail_continue("The shared archive file's CompactStrings setting (%s)"
thartmann@33628 1627 " does not equal the current CompactStrings setting (%s).",
thartmann@33628 1628 _compact_strings ? "enabled" : "disabled",
thartmann@33628 1629 CompactStrings ? "enabled" : "disabled");
thartmann@33628 1630 return false;
thartmann@33628 1631 }
duke@1 1632
iklam@50049 1633 // This must be done after header validation because it might change the
iklam@50049 1634 // header data
iklam@50049 1635 const char* prop = Arguments::get_property("java.system.class.loader");
iklam@50049 1636 if (prop != NULL) {
iklam@50049 1637 warning("Archived non-system classes are disabled because the "
iklam@50049 1638 "java.system.class.loader property is specified (value = \"%s\"). "
jiangli@52424 1639 "To use archived non-system classes, this property must not be set", prop);
iklam@50049 1640 _has_platform_or_app_classes = false;
iklam@50049 1641 }
iklam@50049 1642
iklam@50049 1643 // For backwards compatibility, we don't check the verification setting
iklam@50049 1644 // if the archive only contains system classes.
iklam@50049 1645 if (_has_platform_or_app_classes &&
iklam@50049 1646 ((!_verify_local && BytecodeVerificationLocal) ||
iklam@50049 1647 (!_verify_remote && BytecodeVerificationRemote))) {
iklam@50049 1648 FileMapInfo::fail_continue("The shared archive file was created with less restrictive "
iklam@50049 1649 "verification setting than the current setting.");
iklam@50049 1650 return false;
iklam@50049 1651 }
iklam@50049 1652
ccheung@52619 1653 // Java agents are allowed during run time. Therefore, the following condition is not
ccheung@52619 1654 // checked: (!_allow_archiving_with_java_agent && AllowArchivingWithJavaAgent)
ccheung@52619 1655 // Note: _allow_archiving_with_java_agent is set in the shared archive during dump time
ccheung@52619 1656 // while AllowArchivingWithJavaAgent is set during the current run.
ccheung@52619 1657 if (_allow_archiving_with_java_agent && !AllowArchivingWithJavaAgent) {
ccheung@52619 1658 FileMapInfo::fail_continue("The setting of the AllowArchivingWithJavaAgent is different "
ccheung@52619 1659 "from the setting in the shared archive.");
ccheung@52619 1660 return false;
ccheung@52619 1661 }
ccheung@52619 1662
ccheung@52619 1663 if (_allow_archiving_with_java_agent) {
ccheung@52619 1664 warning("This archive was created with AllowArchivingWithJavaAgent. It should be used "
ccheung@52619 1665 "for testing purposes only and should not be used in a production environment");
ccheung@52619 1666 }
ccheung@52619 1667
duke@1 1668 return true;
duke@1 1669 }
duke@1 1670
ccheung@54960 1671 bool FileMapInfo::validate_header(bool is_static) {
iklam@26135 1672 bool status = _header->validate();
iklam@26135 1673
iklam@26135 1674 if (status) {
ccheung@54960 1675 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size, is_static)) {
iklam@26135 1676 if (!PrintSharedArchiveAndExit) {
mockner@38151 1677 fail_continue("shared class paths mismatch (hint: enable -Xlog:class+path=info to diagnose the failure)");
iklam@26135 1678 status = false;
iklam@26135 1679 }
iklam@26135 1680 }
iklam@26135 1681 }
iklam@26135 1682
iklam@26135 1683 if (_paths_misc_info != NULL) {
coleenp@27880 1684 FREE_C_HEAP_ARRAY(char, _paths_misc_info);
iklam@26135 1685 _paths_misc_info = NULL;
iklam@26135 1686 }
iklam@26135 1687 return status;
iklam@26135 1688 }
iklam@26135 1689
jiangli@46810 1690 // Check if a given address is within one of the shared regions
ccheung@37439 1691 bool FileMapInfo::is_in_shared_region(const void* p, int idx) {
iklam@46746 1692 assert(idx == MetaspaceShared::ro ||
iklam@46746 1693 idx == MetaspaceShared::rw ||
iklam@46746 1694 idx == MetaspaceShared::mc ||
iklam@46746 1695 idx == MetaspaceShared::md, "invalid region index");
iklam@51508 1696 char* base = region_addr(idx);
iklam@51494 1697 if (p >= base && p < base + space_at(idx)->_used) {
ccheung@37439 1698 return true;
ccheung@37439 1699 }
ccheung@37439 1700 return false;
ccheung@37439 1701 }
ccheung@37439 1702
hseigel@19319 1703 // Unmap mapped regions of shared space.
hseigel@19319 1704 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
ccheung@54960 1705 MetaspaceShared::set_shared_metaspace_range(NULL, NULL);
iklam@52335 1706
hseigel@19319 1707 FileMapInfo *map_info = FileMapInfo::current_info();
hseigel@19319 1708 if (map_info) {
sla@26296 1709 map_info->fail_continue("%s", msg);
jiangli@46810 1710 for (int i = 0; i < MetaspaceShared::num_non_heap_spaces; i++) {
jiangli@52424 1711 if (!HeapShared::is_heap_region(i)) {
iklam@51508 1712 char *addr = map_info->region_addr(i);
iklam@51508 1713 if (addr != NULL) {
iklam@51508 1714 map_info->unmap_region(i);
iklam@51508 1715 map_info->space_at(i)->_addr._base = NULL;
iklam@51508 1716 }
hseigel@19319 1717 }
hseigel@19319 1718 }
jiangli@46810 1719 // Dealloc the archive heap regions only without unmapping. The regions are part
jiangli@32589 1720 // of the java heap. Unmapping of the heap regions are managed by GC.
jiangli@46810 1721 map_info->dealloc_archive_heap_regions(open_archive_heap_ranges,
sjohanss@52697 1722 num_open_archive_heap_ranges,
sjohanss@52697 1723 true);
jiangli@52424 1724 map_info->dealloc_archive_heap_regions(closed_archive_heap_ranges,
sjohanss@52697 1725 num_closed_archive_heap_ranges,
sjohanss@52697 1726 false);
hseigel@19319 1727 } else if (DumpSharedSpaces) {
sla@26296 1728 fail_stop("%s", msg);
hseigel@19319 1729 }
hseigel@19319 1730 }
iklam@53913 1731
iklam@53913 1732 #if INCLUDE_JVMTI
iklam@53913 1733 ClassPathEntry** FileMapInfo::_classpath_entries_for_jvmti = NULL;
iklam@53913 1734
iklam@53913 1735 ClassPathEntry* FileMapInfo::get_classpath_entry_for_jvmti(int i, TRAPS) {
iklam@53913 1736 ClassPathEntry* ent = _classpath_entries_for_jvmti[i];
iklam@53913 1737 if (ent == NULL) {
iklam@53913 1738 if (i == 0) {
iklam@53913 1739 ent = ClassLoader:: get_jrt_entry();
iklam@53913 1740 assert(ent != NULL, "must be");
iklam@53913 1741 } else {
iklam@53913 1742 SharedClassPathEntry* scpe = shared_path(i);
iklam@53913 1743 assert(scpe->is_jar(), "must be"); // other types of scpe will not produce archived classes
iklam@53913 1744
iklam@53913 1745 const char* path = scpe->name();
iklam@53913 1746 struct stat st;
iklam@53913 1747 if (os::stat(path, &st) != 0) {
iklam@53913 1748 char *msg = NEW_RESOURCE_ARRAY_IN_THREAD(THREAD, char, strlen(path) + 128); ;
iklam@53913 1749 jio_snprintf(msg, strlen(path) + 127, "error in opening JAR file %s", path);
iklam@53913 1750 THROW_MSG_(vmSymbols::java_io_IOException(), msg, NULL);
iklam@53913 1751 } else {
iklam@53913 1752 ent = ClassLoader::create_class_path_entry(path, &st, /*throw_exception=*/true, false, CHECK_NULL);
iklam@53913 1753 }
iklam@53913 1754 }
iklam@53913 1755
iklam@53913 1756 MutexLocker mu(CDSClassFileStream_lock, THREAD);
iklam@53913 1757 if (_classpath_entries_for_jvmti[i] == NULL) {
iklam@53913 1758 _classpath_entries_for_jvmti[i] = ent;
iklam@53913 1759 } else {
iklam@53913 1760 // Another thread has beat me to creating this entry
iklam@53913 1761 delete ent;
iklam@53913 1762 ent = _classpath_entries_for_jvmti[i];
iklam@53913 1763 }
iklam@53913 1764 }
iklam@53913 1765
iklam@53913 1766 return ent;
iklam@53913 1767 }
iklam@53913 1768
iklam@54371 1769 ClassFileStream* FileMapInfo::open_stream_for_jvmti(InstanceKlass* ik, Handle class_loader, TRAPS) {
iklam@53913 1770 int path_index = ik->shared_classpath_index();
iklam@53913 1771 assert(path_index >= 0, "should be called for shared built-in classes only");
ccheung@54960 1772 assert(path_index < (int)get_number_of_shared_paths(), "sanity");
iklam@53913 1773
iklam@53913 1774 ClassPathEntry* cpe = get_classpath_entry_for_jvmti(path_index, CHECK_NULL);
iklam@53913 1775 assert(cpe != NULL, "must be");
iklam@53913 1776
iklam@53913 1777 Symbol* name = ik->name();
iklam@53913 1778 const char* const class_name = name->as_C_string();
iklam@53913 1779 const char* const file_name = ClassLoader::file_name_for_class_name(class_name,
iklam@53913 1780 name->utf8_length());
iklam@54371 1781 ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(class_loader());
iklam@54371 1782 ClassFileStream* cfs = cpe->open_stream_for_loader(file_name, loader_data, THREAD);
iklam@54371 1783 assert(cfs != NULL, "must be able to read the classfile data of shared classes for built-in loaders.");
iklam@54371 1784 log_debug(cds, jvmti)("classfile data for %s [%d: %s] = %d bytes", class_name, path_index,
iklam@54371 1785 cfs->source(), cfs->length());
iklam@54371 1786 return cfs;
iklam@53913 1787 }
iklam@53913 1788
iklam@53913 1789 #endif