src/share/vm/memory/filemap.cpp

Tue, 16 Feb 2016 21:42:29 +0000

author
poonam
date
Tue, 16 Feb 2016 21:42:29 +0000
changeset 8308
6acf14e730dd
parent 7414
0558eb13dcf3
child 7535
7ae4e26cb1e0
child 10008
fd3484fadbe3
permissions
-rw-r--r--

8072725: Provide more granular levels for GC verification
Summary: Add option VerifySubSet to selectively verify the memory sub-systems
Reviewed-by: kevinw, jmasa

duke@435 1 /*
drchase@6680 2 * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
stefank@2314 25 #include "precompiled.hpp"
stefank@2314 26 #include "classfile/classLoader.hpp"
iklam@7089 27 #include "classfile/sharedClassUtil.hpp"
stefank@2314 28 #include "classfile/symbolTable.hpp"
iklam@7089 29 #include "classfile/systemDictionaryShared.hpp"
hseigel@4293 30 #include "classfile/altHashing.hpp"
stefank@2314 31 #include "memory/filemap.hpp"
iklam@7089 32 #include "memory/metadataFactory.hpp"
iklam@7089 33 #include "memory/oopFactory.hpp"
iklam@7089 34 #include "oops/objArrayOop.hpp"
stefank@2314 35 #include "runtime/arguments.hpp"
stefank@2314 36 #include "runtime/java.hpp"
stefank@2314 37 #include "runtime/os.hpp"
zgu@4193 38 #include "services/memTracker.hpp"
stefank@2314 39 #include "utilities/defaultStream.hpp"
stefank@2314 40
duke@435 41 # include <sys/stat.h>
duke@435 42 # include <errno.h>
duke@435 43
duke@435 44 #ifndef O_BINARY // if defined (Win32) use binary files.
duke@435 45 #define O_BINARY 0 // otherwise do nothing.
duke@435 46 #endif
duke@435 47
drchase@6680 48 PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC
duke@435 49 extern address JVM_FunctionAtStart();
duke@435 50 extern address JVM_FunctionAtEnd();
duke@435 51
twisti@1040 52 // Complain and stop. All error conditions occurring during the writing of
duke@435 53 // an archive file should stop the process. Unrecoverable errors during
duke@435 54 // the reading of the archive file should stop the process.
duke@435 55
duke@435 56 static void fail(const char *msg, va_list ap) {
duke@435 57 // This occurs very early during initialization: tty is not initialized.
duke@435 58 jio_fprintf(defaultStream::error_stream(),
twisti@1040 59 "An error has occurred while processing the"
duke@435 60 " shared archive file.\n");
duke@435 61 jio_vfprintf(defaultStream::error_stream(), msg, ap);
duke@435 62 jio_fprintf(defaultStream::error_stream(), "\n");
hseigel@5625 63 // Do not change the text of the below message because some tests check for it.
duke@435 64 vm_exit_during_initialization("Unable to use shared archive.", NULL);
duke@435 65 }
duke@435 66
duke@435 67
duke@435 68 void FileMapInfo::fail_stop(const char *msg, ...) {
duke@435 69 va_list ap;
duke@435 70 va_start(ap, msg);
duke@435 71 fail(msg, ap); // Never returns.
duke@435 72 va_end(ap); // for completeness.
duke@435 73 }
duke@435 74
duke@435 75
duke@435 76 // Complain and continue. Recoverable errors during the reading of the
duke@435 77 // archive file may continue (with sharing disabled).
duke@435 78 //
duke@435 79 // If we continue, then disable shared spaces and close the file.
duke@435 80
duke@435 81 void FileMapInfo::fail_continue(const char *msg, ...) {
duke@435 82 va_list ap;
duke@435 83 va_start(ap, msg);
iklam@7089 84 MetaspaceShared::set_archive_loading_failed();
iklam@7089 85 if (PrintSharedArchiveAndExit && _validating_classpath_entry_table) {
iklam@7089 86 // If we are doing PrintSharedArchiveAndExit and some of the classpath entries
iklam@7089 87 // do not validate, we can still continue "limping" to validate the remaining
iklam@7089 88 // entries. No need to quit.
iklam@7089 89 tty->print("[");
iklam@7089 90 tty->vprint(msg, ap);
iklam@7089 91 tty->print_cr("]");
iklam@7089 92 } else {
iklam@7089 93 if (RequireSharedSpaces) {
iklam@7089 94 fail(msg, ap);
iklam@7089 95 } else {
iklam@7089 96 if (PrintSharedSpaces) {
iklam@7089 97 tty->print_cr("UseSharedSpaces: %s", msg);
iklam@7089 98 }
iklam@7089 99 }
iklam@7414 100 UseSharedSpaces = false;
iklam@7414 101 assert(current_info() != NULL, "singleton must be registered");
iklam@7414 102 current_info()->close();
duke@435 103 }
duke@435 104 va_end(ap);
duke@435 105 }
duke@435 106
hseigel@4293 107 // Fill in the fileMapInfo structure with data about this VM instance.
duke@435 108
hseigel@4293 109 // This method copies the vm version info into header_version. If the version is too
hseigel@4293 110 // long then a truncated version, which has a hash code appended to it, is copied.
hseigel@4293 111 //
hseigel@4293 112 // Using a template enables this method to verify that header_version is an array of
hseigel@4293 113 // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and
hseigel@4293 114 // the code that reads the CDS file will both use the same size buffer. Hence, will
hseigel@4293 115 // use identical truncation. This is necessary for matching of truncated versions.
hseigel@4293 116 template <int N> static void get_header_version(char (&header_version) [N]) {
hseigel@4293 117 assert(N == JVM_IDENT_MAX, "Bad header_version size");
hseigel@4293 118
hseigel@4293 119 const char *vm_version = VM_Version::internal_vm_info_string();
hseigel@4293 120 const int version_len = (int)strlen(vm_version);
hseigel@4293 121
hseigel@4293 122 if (version_len < (JVM_IDENT_MAX-1)) {
hseigel@4293 123 strcpy(header_version, vm_version);
hseigel@4293 124
hseigel@4293 125 } else {
hseigel@4293 126 // Get the hash value. Use a static seed because the hash needs to return the same
hseigel@4293 127 // value over multiple jvm invocations.
hseigel@4293 128 unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len);
hseigel@4293 129
hseigel@4293 130 // Truncate the ident, saving room for the 8 hex character hash value.
hseigel@4293 131 strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
hseigel@4293 132
hseigel@4293 133 // Append the hash code as eight hex digits.
hseigel@4293 134 sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
hseigel@4293 135 header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
hseigel@4293 136 }
hseigel@4293 137 }
duke@435 138
iklam@7089 139 FileMapInfo::FileMapInfo() {
iklam@7089 140 assert(_current_info == NULL, "must be singleton"); // not thread safe
iklam@7089 141 _current_info = this;
iklam@7089 142 memset(this, 0, sizeof(FileMapInfo));
iklam@7089 143 _file_offset = 0;
iklam@7089 144 _file_open = false;
iklam@7089 145 _header = SharedClassUtil::allocate_file_map_header();
iklam@7089 146 _header->_version = _invalid_version;
iklam@7089 147 }
iklam@7089 148
iklam@7089 149 FileMapInfo::~FileMapInfo() {
iklam@7089 150 assert(_current_info == this, "must be singleton"); // not thread safe
iklam@7089 151 _current_info = NULL;
iklam@7089 152 }
iklam@7089 153
duke@435 154 void FileMapInfo::populate_header(size_t alignment) {
iklam@7089 155 _header->populate(this, alignment);
iklam@7089 156 }
iklam@7089 157
iklam@7089 158 size_t FileMapInfo::FileMapHeader::data_size() {
iklam@7089 159 return SharedClassUtil::file_map_header_size() - sizeof(FileMapInfo::FileMapHeaderBase);
iklam@7089 160 }
iklam@7089 161
iklam@7089 162 void FileMapInfo::FileMapHeader::populate(FileMapInfo* mapinfo, size_t alignment) {
iklam@7089 163 _magic = 0xf00baba2;
iklam@7089 164 _version = _current_version;
iklam@7089 165 _alignment = alignment;
iklam@7089 166 _obj_alignment = ObjectAlignmentInBytes;
iklam@7089 167 _classpath_entry_table_size = mapinfo->_classpath_entry_table_size;
iklam@7089 168 _classpath_entry_table = mapinfo->_classpath_entry_table;
iklam@7089 169 _classpath_entry_size = mapinfo->_classpath_entry_size;
duke@435 170
duke@435 171 // The following fields are for sanity checks for whether this archive
duke@435 172 // will function correctly with this JVM and the bootclasspath it's
duke@435 173 // invoked with.
duke@435 174
duke@435 175 // JVM version string ... changes on each build.
iklam@7089 176 get_header_version(_jvm_ident);
iklam@7089 177 }
duke@435 178
iklam@7089 179 void FileMapInfo::allocate_classpath_entry_table() {
iklam@7089 180 int bytes = 0;
iklam@7089 181 int count = 0;
iklam@7089 182 char* strptr = NULL;
iklam@7089 183 char* strptr_max = NULL;
iklam@7089 184 Thread* THREAD = Thread::current();
duke@435 185
iklam@7089 186 ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
iklam@7089 187 size_t entry_size = SharedClassUtil::shared_class_path_entry_size();
iklam@7089 188
iklam@7089 189 for (int pass=0; pass<2; pass++) {
iklam@7089 190 ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
iklam@7089 191
iklam@7089 192 for (int cur_entry = 0 ; cpe != NULL; cpe = cpe->next(), cur_entry++) {
iklam@7089 193 const char *name = cpe->name();
iklam@7089 194 int name_bytes = (int)(strlen(name) + 1);
iklam@7089 195
iklam@7089 196 if (pass == 0) {
iklam@7089 197 count ++;
iklam@7089 198 bytes += (int)entry_size;
iklam@7089 199 bytes += name_bytes;
iklam@7089 200 if (TraceClassPaths || (TraceClassLoading && Verbose)) {
iklam@7089 201 tty->print_cr("[Add main shared path (%s) %s]", (cpe->is_jar_file() ? "jar" : "dir"), name);
iklam@7089 202 }
iklam@7089 203 } else {
iklam@7089 204 SharedClassPathEntry* ent = shared_classpath(cur_entry);
iklam@7089 205 if (cpe->is_jar_file()) {
iklam@7089 206 struct stat st;
iklam@7089 207 if (os::stat(name, &st) != 0) {
iklam@7089 208 // The file/dir must exist, or it would not have been added
iklam@7089 209 // into ClassLoader::classpath_entry().
iklam@7089 210 //
iklam@7089 211 // If we can't access a jar file in the boot path, then we can't
iklam@7089 212 // make assumptions about where classes get loaded from.
iklam@7089 213 FileMapInfo::fail_stop("Unable to open jar file %s.", name);
iklam@7089 214 }
iklam@7089 215
iklam@7089 216 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
iklam@7089 217 SharedClassUtil::update_shared_classpath(cpe, ent, st.st_mtime, st.st_size, THREAD);
iklam@7089 218 } else {
iklam@7089 219 ent->_filesize = -1;
iklam@7089 220 if (!os::dir_is_empty(name)) {
iklam@7089 221 ClassLoader::exit_with_path_failure("Cannot have non-empty directory in archived classpaths", name);
iklam@7089 222 }
iklam@7089 223 }
iklam@7089 224 ent->_name = strptr;
iklam@7089 225 if (strptr + name_bytes <= strptr_max) {
iklam@7089 226 strncpy(strptr, name, (size_t)name_bytes); // name_bytes includes trailing 0.
iklam@7089 227 strptr += name_bytes;
iklam@7089 228 } else {
iklam@7089 229 assert(0, "miscalculated buffer size");
iklam@7089 230 }
duke@435 231 }
iklam@7089 232 }
duke@435 233
iklam@7089 234 if (pass == 0) {
iklam@7089 235 EXCEPTION_MARK; // The following call should never throw, but would exit VM on error.
iklam@7089 236 Array<u8>* arr = MetadataFactory::new_array<u8>(loader_data, (bytes + 7)/8, THREAD);
iklam@7089 237 strptr = (char*)(arr->data());
iklam@7089 238 strptr_max = strptr + bytes;
iklam@7089 239 SharedClassPathEntry* table = (SharedClassPathEntry*)strptr;
iklam@7089 240 strptr += entry_size * count;
duke@435 241
iklam@7089 242 _classpath_entry_table_size = count;
iklam@7089 243 _classpath_entry_table = table;
iklam@7089 244 _classpath_entry_size = entry_size;
duke@435 245 }
duke@435 246 }
duke@435 247 }
duke@435 248
iklam@7089 249 bool FileMapInfo::validate_classpath_entry_table() {
iklam@7089 250 _validating_classpath_entry_table = true;
iklam@7089 251
iklam@7089 252 int count = _header->_classpath_entry_table_size;
iklam@7089 253
iklam@7089 254 _classpath_entry_table = _header->_classpath_entry_table;
iklam@7089 255 _classpath_entry_size = _header->_classpath_entry_size;
iklam@7089 256
iklam@7089 257 for (int i=0; i<count; i++) {
iklam@7089 258 SharedClassPathEntry* ent = shared_classpath(i);
iklam@7089 259 struct stat st;
iklam@7089 260 const char* name = ent->_name;
iklam@7089 261 bool ok = true;
iklam@7089 262 if (TraceClassPaths || (TraceClassLoading && Verbose)) {
iklam@7089 263 tty->print_cr("[Checking shared classpath entry: %s]", name);
iklam@7089 264 }
iklam@7089 265 if (os::stat(name, &st) != 0) {
iklam@7089 266 fail_continue("Required classpath entry does not exist: %s", name);
iklam@7089 267 ok = false;
iklam@7089 268 } else if (ent->is_dir()) {
iklam@7089 269 if (!os::dir_is_empty(name)) {
iklam@7089 270 fail_continue("directory is not empty: %s", name);
iklam@7089 271 ok = false;
iklam@7089 272 }
iklam@7089 273 } else {
iklam@7089 274 if (ent->_timestamp != st.st_mtime ||
iklam@7089 275 ent->_filesize != st.st_size) {
iklam@7089 276 ok = false;
iklam@7089 277 if (PrintSharedArchiveAndExit) {
iklam@7089 278 fail_continue(ent->_timestamp != st.st_mtime ?
iklam@7089 279 "Timestamp mismatch" :
iklam@7089 280 "File size mismatch");
iklam@7089 281 } else {
iklam@7089 282 fail_continue("A jar file is not the one used while building"
iklam@7089 283 " the shared archive file: %s", name);
iklam@7089 284 }
iklam@7089 285 }
iklam@7089 286 }
iklam@7089 287 if (ok) {
iklam@7089 288 if (TraceClassPaths || (TraceClassLoading && Verbose)) {
iklam@7089 289 tty->print_cr("[ok]");
iklam@7089 290 }
iklam@7089 291 } else if (!PrintSharedArchiveAndExit) {
iklam@7089 292 _validating_classpath_entry_table = false;
iklam@7089 293 return false;
iklam@7089 294 }
iklam@7089 295 }
iklam@7089 296
iklam@7089 297 _classpath_entry_table_size = _header->_classpath_entry_table_size;
iklam@7089 298 _validating_classpath_entry_table = false;
iklam@7089 299 return true;
iklam@7089 300 }
iklam@7089 301
duke@435 302
duke@435 303 // Read the FileMapInfo information from the file.
duke@435 304
duke@435 305 bool FileMapInfo::init_from_file(int fd) {
iklam@7089 306 size_t sz = _header->data_size();
iklam@7089 307 char* addr = _header->data();
iklam@7089 308 size_t n = os::read(fd, addr, (unsigned int)sz);
iklam@7089 309 if (n != sz) {
duke@435 310 fail_continue("Unable to read the file header.");
duke@435 311 return false;
duke@435 312 }
iklam@7089 313 if (_header->_version != current_version()) {
duke@435 314 fail_continue("The shared archive file has the wrong version.");
duke@435 315 return false;
duke@435 316 }
iklam@7089 317
iklam@7089 318 size_t info_size = _header->_paths_misc_info_size;
iklam@7089 319 _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass);
iklam@7089 320 if (_paths_misc_info == NULL) {
iklam@7089 321 fail_continue("Unable to read the file header.");
iklam@7089 322 return false;
iklam@7089 323 }
iklam@7089 324 n = os::read(fd, _paths_misc_info, (unsigned int)info_size);
iklam@7089 325 if (n != info_size) {
iklam@7089 326 fail_continue("Unable to read the shared path info header.");
iklam@7089 327 FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass);
iklam@7089 328 _paths_misc_info = NULL;
iklam@7089 329 return false;
iklam@7089 330 }
iklam@7089 331
jiangli@6868 332 size_t len = lseek(fd, 0, SEEK_END);
jiangli@6868 333 struct FileMapInfo::FileMapHeader::space_info* si =
jiangli@7241 334 &_header->_space[MetaspaceShared::mc];
jiangli@6868 335 if (si->_file_offset >= len || len - si->_file_offset < si->_used) {
jiangli@6868 336 fail_continue("The shared archive file has been truncated.");
jiangli@6868 337 return false;
jiangli@6868 338 }
jiangli@7241 339
iklam@7089 340 _file_offset += (long)n;
duke@435 341 return true;
duke@435 342 }
duke@435 343
duke@435 344
duke@435 345 // Read the FileMapInfo information from the file.
duke@435 346 bool FileMapInfo::open_for_read() {
duke@435 347 _full_path = Arguments::GetSharedArchivePath();
duke@435 348 int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
duke@435 349 if (fd < 0) {
duke@435 350 if (errno == ENOENT) {
duke@435 351 // Not locating the shared archive is ok.
duke@435 352 fail_continue("Specified shared archive not found.");
duke@435 353 } else {
duke@435 354 fail_continue("Failed to open shared archive file (%s).",
duke@435 355 strerror(errno));
duke@435 356 }
duke@435 357 return false;
duke@435 358 }
duke@435 359
duke@435 360 _fd = fd;
duke@435 361 _file_open = true;
duke@435 362 return true;
duke@435 363 }
duke@435 364
duke@435 365
duke@435 366 // Write the FileMapInfo information to the file.
duke@435 367
duke@435 368 void FileMapInfo::open_for_write() {
duke@435 369 _full_path = Arguments::GetSharedArchivePath();
duke@435 370 if (PrintSharedSpaces) {
duke@435 371 tty->print_cr("Dumping shared data to file: ");
duke@435 372 tty->print_cr(" %s", _full_path);
duke@435 373 }
duke@435 374
hseigel@4521 375 #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file.
hseigel@4521 376 chmod(_full_path, _S_IREAD | _S_IWRITE);
hseigel@4521 377 #endif
hseigel@4521 378
hseigel@4521 379 // Use remove() to delete the existing file because, on Unix, this will
hseigel@4521 380 // allow processes that have it open continued access to the file.
duke@435 381 remove(_full_path);
duke@435 382 int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
duke@435 383 if (fd < 0) {
duke@435 384 fail_stop("Unable to create shared archive file %s.", _full_path);
duke@435 385 }
duke@435 386 _fd = fd;
duke@435 387 _file_offset = 0;
duke@435 388 _file_open = true;
duke@435 389 }
duke@435 390
duke@435 391
duke@435 392 // Write the header to the file, seek to the next allocation boundary.
duke@435 393
duke@435 394 void FileMapInfo::write_header() {
iklam@7089 395 int info_size = ClassLoader::get_shared_paths_misc_info_size();
iklam@7089 396
iklam@7089 397 _header->_paths_misc_info_size = info_size;
iklam@7089 398
iklam@7089 399 align_file_position();
iklam@7089 400 size_t sz = _header->data_size();
iklam@7089 401 char* addr = _header->data();
iklam@7089 402 write_bytes(addr, (int)sz); // skip the C++ vtable
iklam@7089 403 write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size);
iklam@7089 404 align_file_position();
duke@435 405 }
duke@435 406
duke@435 407
duke@435 408 // Dump shared spaces to file.
duke@435 409
coleenp@4037 410 void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) {
duke@435 411 align_file_position();
jmasa@5015 412 size_t used = space->used_bytes_slow(Metaspace::NonClassType);
jmasa@5015 413 size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType);
iklam@7089 414 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
coleenp@4037 415 write_region(i, (char*)space->bottom(), used, capacity, read_only, false);
duke@435 416 }
duke@435 417
duke@435 418
duke@435 419 // Dump region to file.
duke@435 420
duke@435 421 void FileMapInfo::write_region(int region, char* base, size_t size,
duke@435 422 size_t capacity, bool read_only,
duke@435 423 bool allow_exec) {
iklam@7089 424 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region];
duke@435 425
duke@435 426 if (_file_open) {
duke@435 427 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
duke@435 428 if (PrintSharedSpaces) {
coleenp@4037 429 tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT
coleenp@4037 430 " file offset 0x%6x", region, size, base, _file_offset);
duke@435 431 }
duke@435 432 } else {
duke@435 433 si->_file_offset = _file_offset;
duke@435 434 }
duke@435 435 si->_base = base;
duke@435 436 si->_used = size;
duke@435 437 si->_capacity = capacity;
duke@435 438 si->_read_only = read_only;
duke@435 439 si->_allow_exec = allow_exec;
jiangli@6868 440 si->_crc = ClassLoader::crc32(0, base, (jint)size);
duke@435 441 write_bytes_aligned(base, (int)size);
duke@435 442 }
duke@435 443
duke@435 444
duke@435 445 // Dump bytes to file -- at the current file position.
duke@435 446
duke@435 447 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
duke@435 448 if (_file_open) {
duke@435 449 int n = ::write(_fd, buffer, nbytes);
duke@435 450 if (n != nbytes) {
duke@435 451 // It is dangerous to leave the corrupted shared archive file around,
duke@435 452 // close and remove the file. See bug 6372906.
duke@435 453 close();
duke@435 454 remove(_full_path);
duke@435 455 fail_stop("Unable to write to shared archive file.", NULL);
duke@435 456 }
duke@435 457 }
duke@435 458 _file_offset += nbytes;
duke@435 459 }
duke@435 460
duke@435 461
duke@435 462 // Align file position to an allocation unit boundary.
duke@435 463
duke@435 464 void FileMapInfo::align_file_position() {
jiangli@6868 465 size_t new_file_offset = align_size_up(_file_offset,
jiangli@6868 466 os::vm_allocation_granularity());
duke@435 467 if (new_file_offset != _file_offset) {
duke@435 468 _file_offset = new_file_offset;
duke@435 469 if (_file_open) {
duke@435 470 // Seek one byte back from the target and write a byte to insure
duke@435 471 // that the written file is the correct length.
duke@435 472 _file_offset -= 1;
jiangli@6868 473 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
duke@435 474 fail_stop("Unable to seek.", NULL);
duke@435 475 }
duke@435 476 char zero = 0;
duke@435 477 write_bytes(&zero, 1);
duke@435 478 }
duke@435 479 }
duke@435 480 }
duke@435 481
duke@435 482
duke@435 483 // Dump bytes to file -- at the current file position.
duke@435 484
duke@435 485 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
duke@435 486 align_file_position();
duke@435 487 write_bytes(buffer, nbytes);
duke@435 488 align_file_position();
duke@435 489 }
duke@435 490
duke@435 491
duke@435 492 // Close the shared archive file. This does NOT unmap mapped regions.
duke@435 493
duke@435 494 void FileMapInfo::close() {
duke@435 495 if (_file_open) {
duke@435 496 if (::close(_fd) < 0) {
duke@435 497 fail_stop("Unable to close the shared archive file.");
duke@435 498 }
duke@435 499 _file_open = false;
duke@435 500 _fd = -1;
duke@435 501 }
duke@435 502 }
duke@435 503
duke@435 504
duke@435 505 // JVM/TI RedefineClasses() support:
duke@435 506 // Remap the shared readonly space to shared readwrite, private.
duke@435 507 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
iklam@7089 508 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
duke@435 509 if (!si->_read_only) {
duke@435 510 // the space is already readwrite so we are done
duke@435 511 return true;
duke@435 512 }
duke@435 513 size_t used = si->_used;
duke@435 514 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 515 if (!open_for_read()) {
duke@435 516 return false;
duke@435 517 }
duke@435 518 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
duke@435 519 si->_base, size, false /* !read_only */,
duke@435 520 si->_allow_exec);
duke@435 521 close();
duke@435 522 if (base == NULL) {
duke@435 523 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
duke@435 524 return false;
duke@435 525 }
duke@435 526 if (base != si->_base) {
duke@435 527 fail_continue("Unable to remap shared readonly space at required address.");
duke@435 528 return false;
duke@435 529 }
duke@435 530 si->_read_only = false;
duke@435 531 return true;
duke@435 532 }
duke@435 533
coleenp@4037 534 // Map the whole region at once, assumed to be allocated contiguously.
coleenp@4037 535 ReservedSpace FileMapInfo::reserve_shared_memory() {
iklam@7089 536 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0];
coleenp@4037 537 char* requested_addr = si->_base;
coleenp@4037 538
hseigel@5528 539 size_t size = FileMapInfo::shared_spaces_size();
coleenp@4037 540
coleenp@4037 541 // Reserve the space first, then map otherwise map will go right over some
coleenp@4037 542 // other reserved memory (like the code cache).
hseigel@5528 543 ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr);
coleenp@4037 544 if (!rs.is_reserved()) {
coleenp@4804 545 fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr));
coleenp@4037 546 return rs;
coleenp@4037 547 }
zgu@4193 548 // the reserved virtual memory is for mapping class data sharing archive
hseigel@4396 549 MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared);
hseigel@4396 550
coleenp@4037 551 return rs;
coleenp@4037 552 }
duke@435 553
duke@435 554 // Memory map a region in the address space.
coleenp@4037 555 static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"};
duke@435 556
coleenp@4037 557 char* FileMapInfo::map_region(int i) {
iklam@7089 558 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
duke@435 559 size_t used = si->_used;
coleenp@4037 560 size_t alignment = os::vm_allocation_granularity();
coleenp@4037 561 size_t size = align_size_up(used, alignment);
coleenp@4037 562 char *requested_addr = si->_base;
coleenp@4037 563
coleenp@4037 564 // map the contents of the CDS archive in this memory
duke@435 565 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
duke@435 566 requested_addr, size, si->_read_only,
duke@435 567 si->_allow_exec);
coleenp@4037 568 if (base == NULL || base != si->_base) {
coleenp@4037 569 fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i]));
duke@435 570 return NULL;
duke@435 571 }
hseigel@4396 572 #ifdef _WINDOWS
hseigel@4396 573 // This call is Windows-only because the memory_type gets recorded for the other platforms
hseigel@4396 574 // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows.
hseigel@4396 575 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
hseigel@4396 576 #endif
duke@435 577 return base;
duke@435 578 }
duke@435 579
jiangli@6868 580 bool FileMapInfo::verify_region_checksum(int i) {
jiangli@6868 581 if (!VerifySharedSpaces) {
jiangli@6868 582 return true;
jiangli@6868 583 }
jiangli@7241 584 const char* buf = _header->_space[i]._base;
jiangli@7241 585 size_t sz = _header->_space[i]._used;
jiangli@6868 586 int crc = ClassLoader::crc32(0, buf, (jint)sz);
jiangli@7241 587 if (crc != _header->_space[i]._crc) {
jiangli@6868 588 fail_continue("Checksum verification failed.");
jiangli@6868 589 return false;
jiangli@6868 590 }
jiangli@6868 591 return true;
jiangli@6868 592 }
duke@435 593
duke@435 594 // Unmap a memory region in the address space.
duke@435 595
duke@435 596 void FileMapInfo::unmap_region(int i) {
iklam@7089 597 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
duke@435 598 size_t used = si->_used;
duke@435 599 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 600 if (!os::unmap_memory(si->_base, size)) {
duke@435 601 fail_stop("Unable to unmap shared space.");
duke@435 602 }
duke@435 603 }
duke@435 604
duke@435 605
duke@435 606 void FileMapInfo::assert_mark(bool check) {
duke@435 607 if (!check) {
duke@435 608 fail_stop("Mark mismatch while restoring from shared file.", NULL);
duke@435 609 }
duke@435 610 }
duke@435 611
duke@435 612
duke@435 613 FileMapInfo* FileMapInfo::_current_info = NULL;
iklam@7089 614 SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL;
iklam@7089 615 int FileMapInfo::_classpath_entry_table_size = 0;
iklam@7089 616 size_t FileMapInfo::_classpath_entry_size = 0x1234baad;
iklam@7089 617 bool FileMapInfo::_validating_classpath_entry_table = false;
duke@435 618
duke@435 619 // Open the shared archive file, read and validate the header
duke@435 620 // information (version, boot classpath, etc.). If initialization
duke@435 621 // fails, shared spaces are disabled and the file is closed. [See
duke@435 622 // fail_continue.]
iklam@7089 623 //
iklam@7089 624 // Validation of the archive is done in two steps:
iklam@7089 625 //
iklam@7089 626 // [1] validate_header() - done here. This checks the header, including _paths_misc_info.
iklam@7089 627 // [2] validate_classpath_entry_table - this is done later, because the table is in the RW
iklam@7089 628 // region of the archive, which is not mapped yet.
duke@435 629 bool FileMapInfo::initialize() {
duke@435 630 assert(UseSharedSpaces, "UseSharedSpaces expected.");
duke@435 631
duke@435 632 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
duke@435 633 fail_continue("Tool agent requires sharing to be disabled.");
duke@435 634 return false;
duke@435 635 }
duke@435 636
duke@435 637 if (!open_for_read()) {
duke@435 638 return false;
duke@435 639 }
duke@435 640
duke@435 641 init_from_file(_fd);
iklam@7089 642 if (!validate_header()) {
duke@435 643 return false;
duke@435 644 }
duke@435 645
iklam@7089 646 SharedReadOnlySize = _header->_space[0]._capacity;
iklam@7089 647 SharedReadWriteSize = _header->_space[1]._capacity;
iklam@7089 648 SharedMiscDataSize = _header->_space[2]._capacity;
iklam@7089 649 SharedMiscCodeSize = _header->_space[3]._capacity;
duke@435 650 return true;
duke@435 651 }
duke@435 652
jiangli@7241 653 int FileMapInfo::FileMapHeader::compute_crc() {
jiangli@7241 654 char* header = data();
jiangli@6868 655 // start computing from the field after _crc
jiangli@7241 656 char* buf = (char*)&_crc + sizeof(int);
jiangli@7241 657 size_t sz = data_size() - (buf - header);
jiangli@6868 658 int crc = ClassLoader::crc32(0, buf, (jint)sz);
jiangli@6868 659 return crc;
jiangli@6868 660 }
duke@435 661
jiangli@7241 662 int FileMapInfo::compute_header_crc() {
jiangli@7241 663 return _header->compute_crc();
jiangli@7241 664 }
jiangli@7241 665
iklam@7089 666 bool FileMapInfo::FileMapHeader::validate() {
jiangli@7241 667 if (_magic != (int)0xf00baba2) {
jiangli@7241 668 FileMapInfo::fail_continue("The shared archive file has a bad magic number.");
jiangli@7241 669 return false;
jiangli@7241 670 }
jiangli@7241 671 if (VerifySharedSpaces && compute_crc() != _crc) {
jiangli@6868 672 fail_continue("Header checksum verification failed.");
jiangli@6868 673 return false;
jiangli@6868 674 }
iklam@7089 675 if (_version != current_version()) {
iklam@7089 676 FileMapInfo::fail_continue("The shared archive file is the wrong version.");
jiangli@7241 677
duke@435 678 return false;
duke@435 679 }
hseigel@4293 680 char header_version[JVM_IDENT_MAX];
hseigel@4293 681 get_header_version(header_version);
iklam@7089 682 if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) {
iklam@7089 683 if (TraceClassPaths) {
iklam@7089 684 tty->print_cr("Expected: %s", header_version);
iklam@7089 685 tty->print_cr("Actual: %s", _jvm_ident);
iklam@7089 686 }
iklam@7089 687 FileMapInfo::fail_continue("The shared archive file was created by a different"
iklam@7089 688 " version or build of HotSpot");
duke@435 689 return false;
duke@435 690 }
iklam@7089 691 if (_obj_alignment != ObjectAlignmentInBytes) {
iklam@7089 692 FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d"
hseigel@4397 693 " does not equal the current ObjectAlignmentInBytes of %d.",
iklam@7089 694 _obj_alignment, ObjectAlignmentInBytes);
duke@435 695 return false;
duke@435 696 }
duke@435 697
duke@435 698 return true;
duke@435 699 }
duke@435 700
iklam@7089 701 bool FileMapInfo::validate_header() {
iklam@7089 702 bool status = _header->validate();
iklam@7089 703
iklam@7089 704 if (status) {
iklam@7089 705 if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) {
iklam@7089 706 if (!PrintSharedArchiveAndExit) {
iklam@7089 707 fail_continue("shared class paths mismatch (hint: enable -XX:+TraceClassPaths to diagnose the failure)");
iklam@7089 708 status = false;
iklam@7089 709 }
iklam@7089 710 }
iklam@7089 711 }
iklam@7089 712
iklam@7089 713 if (_paths_misc_info != NULL) {
iklam@7089 714 FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass);
iklam@7089 715 _paths_misc_info = NULL;
iklam@7089 716 }
iklam@7089 717 return status;
iklam@7089 718 }
iklam@7089 719
duke@435 720 // The following method is provided to see whether a given pointer
duke@435 721 // falls in the mapped shared space.
duke@435 722 // Param:
duke@435 723 // p, The given pointer
duke@435 724 // Return:
duke@435 725 // True if the p is within the mapped shared space, otherwise, false.
duke@435 726 bool FileMapInfo::is_in_shared_space(const void* p) {
coleenp@4037 727 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
iklam@7089 728 if (p >= _header->_space[i]._base &&
iklam@7089 729 p < _header->_space[i]._base + _header->_space[i]._used) {
duke@435 730 return true;
duke@435 731 }
duke@435 732 }
duke@435 733
duke@435 734 return false;
duke@435 735 }
iklam@5329 736
iklam@5329 737 void FileMapInfo::print_shared_spaces() {
iklam@5329 738 gclog_or_tty->print_cr("Shared Spaces:");
iklam@5329 739 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
iklam@7089 740 struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i];
iklam@5329 741 gclog_or_tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT,
iklam@5329 742 shared_region_name[i],
iklam@5329 743 si->_base, si->_base + si->_used);
iklam@5329 744 }
iklam@5329 745 }
hseigel@5528 746
hseigel@5528 747 // Unmap mapped regions of shared space.
hseigel@5528 748 void FileMapInfo::stop_sharing_and_unmap(const char* msg) {
hseigel@5528 749 FileMapInfo *map_info = FileMapInfo::current_info();
hseigel@5528 750 if (map_info) {
hseigel@5528 751 map_info->fail_continue(msg);
hseigel@5528 752 for (int i = 0; i < MetaspaceShared::n_regions; i++) {
iklam@7089 753 if (map_info->_header->_space[i]._base != NULL) {
hseigel@5528 754 map_info->unmap_region(i);
iklam@7089 755 map_info->_header->_space[i]._base = NULL;
hseigel@5528 756 }
hseigel@5528 757 }
hseigel@5528 758 } else if (DumpSharedSpaces) {
hseigel@5528 759 fail_stop(msg, NULL);
hseigel@5528 760 }
hseigel@5528 761 }

mercurial