Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2003, 2014, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #include "precompiled.hpp" |
aoqi@0 | 26 | #include "classfile/classLoader.hpp" |
aoqi@0 | 27 | #include "classfile/symbolTable.hpp" |
aoqi@0 | 28 | #include "classfile/altHashing.hpp" |
aoqi@0 | 29 | #include "memory/filemap.hpp" |
aoqi@0 | 30 | #include "runtime/arguments.hpp" |
aoqi@0 | 31 | #include "runtime/java.hpp" |
aoqi@0 | 32 | #include "runtime/os.hpp" |
aoqi@0 | 33 | #include "services/memTracker.hpp" |
aoqi@0 | 34 | #include "utilities/defaultStream.hpp" |
aoqi@0 | 35 | |
aoqi@0 | 36 | # include <sys/stat.h> |
aoqi@0 | 37 | # include <errno.h> |
aoqi@0 | 38 | |
aoqi@0 | 39 | #ifndef O_BINARY // if defined (Win32) use binary files. |
aoqi@0 | 40 | #define O_BINARY 0 // otherwise do nothing. |
aoqi@0 | 41 | #endif |
aoqi@0 | 42 | |
aoqi@0 | 43 | PRAGMA_FORMAT_MUTE_WARNINGS_FOR_GCC |
aoqi@0 | 44 | |
aoqi@0 | 45 | extern address JVM_FunctionAtStart(); |
aoqi@0 | 46 | extern address JVM_FunctionAtEnd(); |
aoqi@0 | 47 | |
aoqi@0 | 48 | // Complain and stop. All error conditions occurring during the writing of |
aoqi@0 | 49 | // an archive file should stop the process. Unrecoverable errors during |
aoqi@0 | 50 | // the reading of the archive file should stop the process. |
aoqi@0 | 51 | |
aoqi@0 | 52 | static void fail(const char *msg, va_list ap) { |
aoqi@0 | 53 | // This occurs very early during initialization: tty is not initialized. |
aoqi@0 | 54 | jio_fprintf(defaultStream::error_stream(), |
aoqi@0 | 55 | "An error has occurred while processing the" |
aoqi@0 | 56 | " shared archive file.\n"); |
aoqi@0 | 57 | jio_vfprintf(defaultStream::error_stream(), msg, ap); |
aoqi@0 | 58 | jio_fprintf(defaultStream::error_stream(), "\n"); |
aoqi@0 | 59 | // Do not change the text of the below message because some tests check for it. |
aoqi@0 | 60 | vm_exit_during_initialization("Unable to use shared archive.", NULL); |
aoqi@0 | 61 | } |
aoqi@0 | 62 | |
aoqi@0 | 63 | |
aoqi@0 | 64 | void FileMapInfo::fail_stop(const char *msg, ...) { |
aoqi@0 | 65 | va_list ap; |
aoqi@0 | 66 | va_start(ap, msg); |
aoqi@0 | 67 | fail(msg, ap); // Never returns. |
aoqi@0 | 68 | va_end(ap); // for completeness. |
aoqi@0 | 69 | } |
aoqi@0 | 70 | |
aoqi@0 | 71 | |
aoqi@0 | 72 | // Complain and continue. Recoverable errors during the reading of the |
aoqi@0 | 73 | // archive file may continue (with sharing disabled). |
aoqi@0 | 74 | // |
aoqi@0 | 75 | // If we continue, then disable shared spaces and close the file. |
aoqi@0 | 76 | |
aoqi@0 | 77 | void FileMapInfo::fail_continue(const char *msg, ...) { |
aoqi@0 | 78 | va_list ap; |
aoqi@0 | 79 | va_start(ap, msg); |
aoqi@0 | 80 | if (RequireSharedSpaces) { |
aoqi@0 | 81 | fail(msg, ap); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | va_end(ap); |
aoqi@0 | 84 | UseSharedSpaces = false; |
aoqi@0 | 85 | close(); |
aoqi@0 | 86 | } |
aoqi@0 | 87 | |
aoqi@0 | 88 | // Fill in the fileMapInfo structure with data about this VM instance. |
aoqi@0 | 89 | |
aoqi@0 | 90 | // This method copies the vm version info into header_version. If the version is too |
aoqi@0 | 91 | // long then a truncated version, which has a hash code appended to it, is copied. |
aoqi@0 | 92 | // |
aoqi@0 | 93 | // Using a template enables this method to verify that header_version is an array of |
aoqi@0 | 94 | // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and |
aoqi@0 | 95 | // the code that reads the CDS file will both use the same size buffer. Hence, will |
aoqi@0 | 96 | // use identical truncation. This is necessary for matching of truncated versions. |
aoqi@0 | 97 | template <int N> static void get_header_version(char (&header_version) [N]) { |
aoqi@0 | 98 | assert(N == JVM_IDENT_MAX, "Bad header_version size"); |
aoqi@0 | 99 | |
aoqi@0 | 100 | const char *vm_version = VM_Version::internal_vm_info_string(); |
aoqi@0 | 101 | const int version_len = (int)strlen(vm_version); |
aoqi@0 | 102 | |
aoqi@0 | 103 | if (version_len < (JVM_IDENT_MAX-1)) { |
aoqi@0 | 104 | strcpy(header_version, vm_version); |
aoqi@0 | 105 | |
aoqi@0 | 106 | } else { |
aoqi@0 | 107 | // Get the hash value. Use a static seed because the hash needs to return the same |
aoqi@0 | 108 | // value over multiple jvm invocations. |
aoqi@0 | 109 | unsigned int hash = AltHashing::murmur3_32(8191, (const jbyte*)vm_version, version_len); |
aoqi@0 | 110 | |
aoqi@0 | 111 | // Truncate the ident, saving room for the 8 hex character hash value. |
aoqi@0 | 112 | strncpy(header_version, vm_version, JVM_IDENT_MAX-9); |
aoqi@0 | 113 | |
aoqi@0 | 114 | // Append the hash code as eight hex digits. |
aoqi@0 | 115 | sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash); |
aoqi@0 | 116 | header_version[JVM_IDENT_MAX-1] = 0; // Null terminate. |
aoqi@0 | 117 | } |
aoqi@0 | 118 | } |
aoqi@0 | 119 | |
aoqi@0 | 120 | void FileMapInfo::populate_header(size_t alignment) { |
aoqi@0 | 121 | _header._magic = 0xf00baba2; |
aoqi@0 | 122 | _header._version = _current_version; |
aoqi@0 | 123 | _header._alignment = alignment; |
aoqi@0 | 124 | _header._obj_alignment = ObjectAlignmentInBytes; |
aoqi@0 | 125 | |
aoqi@0 | 126 | // The following fields are for sanity checks for whether this archive |
aoqi@0 | 127 | // will function correctly with this JVM and the bootclasspath it's |
aoqi@0 | 128 | // invoked with. |
aoqi@0 | 129 | |
aoqi@0 | 130 | // JVM version string ... changes on each build. |
aoqi@0 | 131 | get_header_version(_header._jvm_ident); |
aoqi@0 | 132 | |
aoqi@0 | 133 | // Build checks on classpath and jar files |
aoqi@0 | 134 | _header._num_jars = 0; |
aoqi@0 | 135 | ClassPathEntry *cpe = ClassLoader::classpath_entry(0); |
aoqi@0 | 136 | for ( ; cpe != NULL; cpe = cpe->next()) { |
aoqi@0 | 137 | |
aoqi@0 | 138 | if (cpe->is_jar_file()) { |
aoqi@0 | 139 | if (_header._num_jars >= JVM_SHARED_JARS_MAX) { |
aoqi@0 | 140 | fail_stop("Too many jar files to share.", NULL); |
aoqi@0 | 141 | } |
aoqi@0 | 142 | |
aoqi@0 | 143 | // Jar file - record timestamp and file size. |
aoqi@0 | 144 | struct stat st; |
aoqi@0 | 145 | const char *path = cpe->name(); |
aoqi@0 | 146 | if (os::stat(path, &st) != 0) { |
aoqi@0 | 147 | // If we can't access a jar file in the boot path, then we can't |
aoqi@0 | 148 | // make assumptions about where classes get loaded from. |
aoqi@0 | 149 | fail_stop("Unable to open jar file %s.", path); |
aoqi@0 | 150 | } |
aoqi@0 | 151 | _header._jar[_header._num_jars]._timestamp = st.st_mtime; |
aoqi@0 | 152 | _header._jar[_header._num_jars]._filesize = st.st_size; |
aoqi@0 | 153 | _header._num_jars++; |
aoqi@0 | 154 | } else { |
aoqi@0 | 155 | |
aoqi@0 | 156 | // If directories appear in boot classpath, they must be empty to |
aoqi@0 | 157 | // avoid having to verify each individual class file. |
aoqi@0 | 158 | const char* name = ((ClassPathDirEntry*)cpe)->name(); |
aoqi@0 | 159 | if (!os::dir_is_empty(name)) { |
aoqi@0 | 160 | fail_stop("Boot classpath directory %s is not empty.", name); |
aoqi@0 | 161 | } |
aoqi@0 | 162 | } |
aoqi@0 | 163 | } |
aoqi@0 | 164 | } |
aoqi@0 | 165 | |
aoqi@0 | 166 | |
aoqi@0 | 167 | // Read the FileMapInfo information from the file. |
aoqi@0 | 168 | |
aoqi@0 | 169 | bool FileMapInfo::init_from_file(int fd) { |
aoqi@0 | 170 | |
aoqi@0 | 171 | size_t n = read(fd, &_header, sizeof(struct FileMapHeader)); |
aoqi@0 | 172 | if (n != sizeof(struct FileMapHeader)) { |
aoqi@0 | 173 | fail_continue("Unable to read the file header."); |
aoqi@0 | 174 | return false; |
aoqi@0 | 175 | } |
aoqi@0 | 176 | if (_header._version != current_version()) { |
aoqi@0 | 177 | fail_continue("The shared archive file has the wrong version."); |
aoqi@0 | 178 | return false; |
aoqi@0 | 179 | } |
aoqi@0 | 180 | size_t len = lseek(fd, 0, SEEK_END); |
aoqi@0 | 181 | struct FileMapInfo::FileMapHeader::space_info* si = |
aoqi@0 | 182 | &_header._space[MetaspaceShared::mc]; |
aoqi@0 | 183 | if (si->_file_offset >= len || len - si->_file_offset < si->_used) { |
aoqi@0 | 184 | fail_continue("The shared archive file has been truncated."); |
aoqi@0 | 185 | return false; |
aoqi@0 | 186 | } |
aoqi@0 | 187 | _file_offset = n; |
aoqi@0 | 188 | return true; |
aoqi@0 | 189 | } |
aoqi@0 | 190 | |
aoqi@0 | 191 | |
aoqi@0 | 192 | // Read the FileMapInfo information from the file. |
aoqi@0 | 193 | bool FileMapInfo::open_for_read() { |
aoqi@0 | 194 | _full_path = Arguments::GetSharedArchivePath(); |
aoqi@0 | 195 | int fd = open(_full_path, O_RDONLY | O_BINARY, 0); |
aoqi@0 | 196 | if (fd < 0) { |
aoqi@0 | 197 | if (errno == ENOENT) { |
aoqi@0 | 198 | // Not locating the shared archive is ok. |
aoqi@0 | 199 | fail_continue("Specified shared archive not found."); |
aoqi@0 | 200 | } else { |
aoqi@0 | 201 | fail_continue("Failed to open shared archive file (%s).", |
aoqi@0 | 202 | strerror(errno)); |
aoqi@0 | 203 | } |
aoqi@0 | 204 | return false; |
aoqi@0 | 205 | } |
aoqi@0 | 206 | |
aoqi@0 | 207 | _fd = fd; |
aoqi@0 | 208 | _file_open = true; |
aoqi@0 | 209 | return true; |
aoqi@0 | 210 | } |
aoqi@0 | 211 | |
aoqi@0 | 212 | |
aoqi@0 | 213 | // Write the FileMapInfo information to the file. |
aoqi@0 | 214 | |
aoqi@0 | 215 | void FileMapInfo::open_for_write() { |
aoqi@0 | 216 | _full_path = Arguments::GetSharedArchivePath(); |
aoqi@0 | 217 | if (PrintSharedSpaces) { |
aoqi@0 | 218 | tty->print_cr("Dumping shared data to file: "); |
aoqi@0 | 219 | tty->print_cr(" %s", _full_path); |
aoqi@0 | 220 | } |
aoqi@0 | 221 | |
aoqi@0 | 222 | #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. |
aoqi@0 | 223 | chmod(_full_path, _S_IREAD | _S_IWRITE); |
aoqi@0 | 224 | #endif |
aoqi@0 | 225 | |
aoqi@0 | 226 | // Use remove() to delete the existing file because, on Unix, this will |
aoqi@0 | 227 | // allow processes that have it open continued access to the file. |
aoqi@0 | 228 | remove(_full_path); |
aoqi@0 | 229 | int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); |
aoqi@0 | 230 | if (fd < 0) { |
aoqi@0 | 231 | fail_stop("Unable to create shared archive file %s.", _full_path); |
aoqi@0 | 232 | } |
aoqi@0 | 233 | _fd = fd; |
aoqi@0 | 234 | _file_offset = 0; |
aoqi@0 | 235 | _file_open = true; |
aoqi@0 | 236 | } |
aoqi@0 | 237 | |
aoqi@0 | 238 | |
aoqi@0 | 239 | // Write the header to the file, seek to the next allocation boundary. |
aoqi@0 | 240 | |
aoqi@0 | 241 | void FileMapInfo::write_header() { |
aoqi@0 | 242 | write_bytes_aligned(&_header, sizeof(FileMapHeader)); |
aoqi@0 | 243 | } |
aoqi@0 | 244 | |
aoqi@0 | 245 | |
aoqi@0 | 246 | // Dump shared spaces to file. |
aoqi@0 | 247 | |
aoqi@0 | 248 | void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { |
aoqi@0 | 249 | align_file_position(); |
aoqi@0 | 250 | size_t used = space->used_bytes_slow(Metaspace::NonClassType); |
aoqi@0 | 251 | size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); |
aoqi@0 | 252 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; |
aoqi@0 | 253 | write_region(i, (char*)space->bottom(), used, capacity, read_only, false); |
aoqi@0 | 254 | } |
aoqi@0 | 255 | |
aoqi@0 | 256 | |
aoqi@0 | 257 | // Dump region to file. |
aoqi@0 | 258 | |
aoqi@0 | 259 | void FileMapInfo::write_region(int region, char* base, size_t size, |
aoqi@0 | 260 | size_t capacity, bool read_only, |
aoqi@0 | 261 | bool allow_exec) { |
aoqi@0 | 262 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region]; |
aoqi@0 | 263 | |
aoqi@0 | 264 | if (_file_open) { |
aoqi@0 | 265 | guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
aoqi@0 | 266 | if (PrintSharedSpaces) { |
aoqi@0 | 267 | tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT |
aoqi@0 | 268 | " file offset 0x%6x", region, size, base, _file_offset); |
aoqi@0 | 269 | } |
aoqi@0 | 270 | } else { |
aoqi@0 | 271 | si->_file_offset = _file_offset; |
aoqi@0 | 272 | } |
aoqi@0 | 273 | si->_base = base; |
aoqi@0 | 274 | si->_used = size; |
aoqi@0 | 275 | si->_capacity = capacity; |
aoqi@0 | 276 | si->_read_only = read_only; |
aoqi@0 | 277 | si->_allow_exec = allow_exec; |
aoqi@0 | 278 | si->_crc = ClassLoader::crc32(0, base, (jint)size); |
aoqi@0 | 279 | write_bytes_aligned(base, (int)size); |
aoqi@0 | 280 | } |
aoqi@0 | 281 | |
aoqi@0 | 282 | |
aoqi@0 | 283 | // Dump bytes to file -- at the current file position. |
aoqi@0 | 284 | |
aoqi@0 | 285 | void FileMapInfo::write_bytes(const void* buffer, int nbytes) { |
aoqi@0 | 286 | if (_file_open) { |
aoqi@0 | 287 | int n = ::write(_fd, buffer, nbytes); |
aoqi@0 | 288 | if (n != nbytes) { |
aoqi@0 | 289 | // It is dangerous to leave the corrupted shared archive file around, |
aoqi@0 | 290 | // close and remove the file. See bug 6372906. |
aoqi@0 | 291 | close(); |
aoqi@0 | 292 | remove(_full_path); |
aoqi@0 | 293 | fail_stop("Unable to write to shared archive file.", NULL); |
aoqi@0 | 294 | } |
aoqi@0 | 295 | } |
aoqi@0 | 296 | _file_offset += nbytes; |
aoqi@0 | 297 | } |
aoqi@0 | 298 | |
aoqi@0 | 299 | |
aoqi@0 | 300 | // Align file position to an allocation unit boundary. |
aoqi@0 | 301 | |
aoqi@0 | 302 | void FileMapInfo::align_file_position() { |
aoqi@0 | 303 | size_t new_file_offset = align_size_up(_file_offset, |
aoqi@0 | 304 | os::vm_allocation_granularity()); |
aoqi@0 | 305 | if (new_file_offset != _file_offset) { |
aoqi@0 | 306 | _file_offset = new_file_offset; |
aoqi@0 | 307 | if (_file_open) { |
aoqi@0 | 308 | // Seek one byte back from the target and write a byte to insure |
aoqi@0 | 309 | // that the written file is the correct length. |
aoqi@0 | 310 | _file_offset -= 1; |
aoqi@0 | 311 | if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) { |
aoqi@0 | 312 | fail_stop("Unable to seek.", NULL); |
aoqi@0 | 313 | } |
aoqi@0 | 314 | char zero = 0; |
aoqi@0 | 315 | write_bytes(&zero, 1); |
aoqi@0 | 316 | } |
aoqi@0 | 317 | } |
aoqi@0 | 318 | } |
aoqi@0 | 319 | |
aoqi@0 | 320 | |
aoqi@0 | 321 | // Dump bytes to file -- at the current file position. |
aoqi@0 | 322 | |
aoqi@0 | 323 | void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { |
aoqi@0 | 324 | align_file_position(); |
aoqi@0 | 325 | write_bytes(buffer, nbytes); |
aoqi@0 | 326 | align_file_position(); |
aoqi@0 | 327 | } |
aoqi@0 | 328 | |
aoqi@0 | 329 | |
aoqi@0 | 330 | // Close the shared archive file. This does NOT unmap mapped regions. |
aoqi@0 | 331 | |
aoqi@0 | 332 | void FileMapInfo::close() { |
aoqi@0 | 333 | if (_file_open) { |
aoqi@0 | 334 | if (::close(_fd) < 0) { |
aoqi@0 | 335 | fail_stop("Unable to close the shared archive file."); |
aoqi@0 | 336 | } |
aoqi@0 | 337 | _file_open = false; |
aoqi@0 | 338 | _fd = -1; |
aoqi@0 | 339 | } |
aoqi@0 | 340 | } |
aoqi@0 | 341 | |
aoqi@0 | 342 | |
aoqi@0 | 343 | // JVM/TI RedefineClasses() support: |
aoqi@0 | 344 | // Remap the shared readonly space to shared readwrite, private. |
aoqi@0 | 345 | bool FileMapInfo::remap_shared_readonly_as_readwrite() { |
aoqi@0 | 346 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0]; |
aoqi@0 | 347 | if (!si->_read_only) { |
aoqi@0 | 348 | // the space is already readwrite so we are done |
aoqi@0 | 349 | return true; |
aoqi@0 | 350 | } |
aoqi@0 | 351 | size_t used = si->_used; |
aoqi@0 | 352 | size_t size = align_size_up(used, os::vm_allocation_granularity()); |
aoqi@0 | 353 | if (!open_for_read()) { |
aoqi@0 | 354 | return false; |
aoqi@0 | 355 | } |
aoqi@0 | 356 | char *base = os::remap_memory(_fd, _full_path, si->_file_offset, |
aoqi@0 | 357 | si->_base, size, false /* !read_only */, |
aoqi@0 | 358 | si->_allow_exec); |
aoqi@0 | 359 | close(); |
aoqi@0 | 360 | if (base == NULL) { |
aoqi@0 | 361 | fail_continue("Unable to remap shared readonly space (errno=%d).", errno); |
aoqi@0 | 362 | return false; |
aoqi@0 | 363 | } |
aoqi@0 | 364 | if (base != si->_base) { |
aoqi@0 | 365 | fail_continue("Unable to remap shared readonly space at required address."); |
aoqi@0 | 366 | return false; |
aoqi@0 | 367 | } |
aoqi@0 | 368 | si->_read_only = false; |
aoqi@0 | 369 | return true; |
aoqi@0 | 370 | } |
aoqi@0 | 371 | |
aoqi@0 | 372 | // Map the whole region at once, assumed to be allocated contiguously. |
aoqi@0 | 373 | ReservedSpace FileMapInfo::reserve_shared_memory() { |
aoqi@0 | 374 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0]; |
aoqi@0 | 375 | char* requested_addr = si->_base; |
aoqi@0 | 376 | |
aoqi@0 | 377 | size_t size = FileMapInfo::shared_spaces_size(); |
aoqi@0 | 378 | |
aoqi@0 | 379 | // Reserve the space first, then map otherwise map will go right over some |
aoqi@0 | 380 | // other reserved memory (like the code cache). |
aoqi@0 | 381 | ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); |
aoqi@0 | 382 | if (!rs.is_reserved()) { |
aoqi@0 | 383 | fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr)); |
aoqi@0 | 384 | return rs; |
aoqi@0 | 385 | } |
aoqi@0 | 386 | // the reserved virtual memory is for mapping class data sharing archive |
aoqi@0 | 387 | MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); |
aoqi@0 | 388 | |
aoqi@0 | 389 | return rs; |
aoqi@0 | 390 | } |
aoqi@0 | 391 | |
aoqi@0 | 392 | // Memory map a region in the address space. |
aoqi@0 | 393 | static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"}; |
aoqi@0 | 394 | |
aoqi@0 | 395 | char* FileMapInfo::map_region(int i) { |
aoqi@0 | 396 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; |
aoqi@0 | 397 | size_t used = si->_used; |
aoqi@0 | 398 | size_t alignment = os::vm_allocation_granularity(); |
aoqi@0 | 399 | size_t size = align_size_up(used, alignment); |
aoqi@0 | 400 | char *requested_addr = si->_base; |
aoqi@0 | 401 | |
aoqi@0 | 402 | // map the contents of the CDS archive in this memory |
aoqi@0 | 403 | char *base = os::map_memory(_fd, _full_path, si->_file_offset, |
aoqi@0 | 404 | requested_addr, size, si->_read_only, |
aoqi@0 | 405 | si->_allow_exec); |
aoqi@0 | 406 | if (base == NULL || base != si->_base) { |
aoqi@0 | 407 | fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i])); |
aoqi@0 | 408 | return NULL; |
aoqi@0 | 409 | } |
aoqi@0 | 410 | #ifdef _WINDOWS |
aoqi@0 | 411 | // This call is Windows-only because the memory_type gets recorded for the other platforms |
aoqi@0 | 412 | // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. |
aoqi@0 | 413 | MemTracker::record_virtual_memory_type((address)base, mtClassShared); |
aoqi@0 | 414 | #endif |
aoqi@0 | 415 | return base; |
aoqi@0 | 416 | } |
aoqi@0 | 417 | |
aoqi@0 | 418 | bool FileMapInfo::verify_region_checksum(int i) { |
aoqi@0 | 419 | if (!VerifySharedSpaces) { |
aoqi@0 | 420 | return true; |
aoqi@0 | 421 | } |
aoqi@0 | 422 | const char* buf = _header._space[i]._base; |
aoqi@0 | 423 | size_t sz = _header._space[i]._used; |
aoqi@0 | 424 | int crc = ClassLoader::crc32(0, buf, (jint)sz); |
aoqi@0 | 425 | if (crc != _header._space[i]._crc) { |
aoqi@0 | 426 | fail_continue("Checksum verification failed."); |
aoqi@0 | 427 | return false; |
aoqi@0 | 428 | } |
aoqi@0 | 429 | return true; |
aoqi@0 | 430 | } |
aoqi@0 | 431 | |
aoqi@0 | 432 | // Unmap a memory region in the address space. |
aoqi@0 | 433 | |
aoqi@0 | 434 | void FileMapInfo::unmap_region(int i) { |
aoqi@0 | 435 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; |
aoqi@0 | 436 | size_t used = si->_used; |
aoqi@0 | 437 | size_t size = align_size_up(used, os::vm_allocation_granularity()); |
aoqi@0 | 438 | if (!os::unmap_memory(si->_base, size)) { |
aoqi@0 | 439 | fail_stop("Unable to unmap shared space."); |
aoqi@0 | 440 | } |
aoqi@0 | 441 | } |
aoqi@0 | 442 | |
aoqi@0 | 443 | |
aoqi@0 | 444 | void FileMapInfo::assert_mark(bool check) { |
aoqi@0 | 445 | if (!check) { |
aoqi@0 | 446 | fail_stop("Mark mismatch while restoring from shared file.", NULL); |
aoqi@0 | 447 | } |
aoqi@0 | 448 | } |
aoqi@0 | 449 | |
aoqi@0 | 450 | |
aoqi@0 | 451 | FileMapInfo* FileMapInfo::_current_info = NULL; |
aoqi@0 | 452 | |
aoqi@0 | 453 | |
aoqi@0 | 454 | // Open the shared archive file, read and validate the header |
aoqi@0 | 455 | // information (version, boot classpath, etc.). If initialization |
aoqi@0 | 456 | // fails, shared spaces are disabled and the file is closed. [See |
aoqi@0 | 457 | // fail_continue.] |
aoqi@0 | 458 | bool FileMapInfo::initialize() { |
aoqi@0 | 459 | assert(UseSharedSpaces, "UseSharedSpaces expected."); |
aoqi@0 | 460 | |
aoqi@0 | 461 | if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { |
aoqi@0 | 462 | fail_continue("Tool agent requires sharing to be disabled."); |
aoqi@0 | 463 | return false; |
aoqi@0 | 464 | } |
aoqi@0 | 465 | |
aoqi@0 | 466 | if (!open_for_read()) { |
aoqi@0 | 467 | return false; |
aoqi@0 | 468 | } |
aoqi@0 | 469 | |
aoqi@0 | 470 | init_from_file(_fd); |
aoqi@0 | 471 | if (!validate()) { |
aoqi@0 | 472 | return false; |
aoqi@0 | 473 | } |
aoqi@0 | 474 | |
aoqi@0 | 475 | SharedReadOnlySize = _header._space[0]._capacity; |
aoqi@0 | 476 | SharedReadWriteSize = _header._space[1]._capacity; |
aoqi@0 | 477 | SharedMiscDataSize = _header._space[2]._capacity; |
aoqi@0 | 478 | SharedMiscCodeSize = _header._space[3]._capacity; |
aoqi@0 | 479 | return true; |
aoqi@0 | 480 | } |
aoqi@0 | 481 | |
aoqi@0 | 482 | int FileMapInfo::compute_header_crc() { |
aoqi@0 | 483 | char* header = (char*)&_header; |
aoqi@0 | 484 | // start computing from the field after _crc |
aoqi@0 | 485 | char* buf = (char*)&_header._crc + sizeof(int); |
aoqi@0 | 486 | size_t sz = sizeof(FileMapInfo::FileMapHeader) - (buf - header); |
aoqi@0 | 487 | int crc = ClassLoader::crc32(0, buf, (jint)sz); |
aoqi@0 | 488 | return crc; |
aoqi@0 | 489 | } |
aoqi@0 | 490 | |
aoqi@0 | 491 | bool FileMapInfo::validate() { |
aoqi@0 | 492 | if (VerifySharedSpaces && compute_header_crc() != _header._crc) { |
aoqi@0 | 493 | fail_continue("Header checksum verification failed."); |
aoqi@0 | 494 | return false; |
aoqi@0 | 495 | } |
aoqi@0 | 496 | if (_header._version != current_version()) { |
aoqi@0 | 497 | fail_continue("The shared archive file is the wrong version."); |
aoqi@0 | 498 | return false; |
aoqi@0 | 499 | } |
aoqi@0 | 500 | if (_header._magic != (int)0xf00baba2) { |
aoqi@0 | 501 | fail_continue("The shared archive file has a bad magic number."); |
aoqi@0 | 502 | return false; |
aoqi@0 | 503 | } |
aoqi@0 | 504 | char header_version[JVM_IDENT_MAX]; |
aoqi@0 | 505 | get_header_version(header_version); |
aoqi@0 | 506 | if (strncmp(_header._jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { |
aoqi@0 | 507 | fail_continue("The shared archive file was created by a different" |
aoqi@0 | 508 | " version or build of HotSpot."); |
aoqi@0 | 509 | return false; |
aoqi@0 | 510 | } |
aoqi@0 | 511 | if (_header._obj_alignment != ObjectAlignmentInBytes) { |
aoqi@0 | 512 | fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" |
aoqi@0 | 513 | " does not equal the current ObjectAlignmentInBytes of %d.", |
aoqi@0 | 514 | _header._obj_alignment, ObjectAlignmentInBytes); |
aoqi@0 | 515 | return false; |
aoqi@0 | 516 | } |
aoqi@0 | 517 | |
aoqi@0 | 518 | // Cannot verify interpreter yet, as it can only be created after the GC |
aoqi@0 | 519 | // heap has been initialized. |
aoqi@0 | 520 | |
aoqi@0 | 521 | if (_header._num_jars >= JVM_SHARED_JARS_MAX) { |
aoqi@0 | 522 | fail_continue("Too many jar files to share."); |
aoqi@0 | 523 | return false; |
aoqi@0 | 524 | } |
aoqi@0 | 525 | |
aoqi@0 | 526 | // Build checks on classpath and jar files |
aoqi@0 | 527 | int num_jars_now = 0; |
aoqi@0 | 528 | ClassPathEntry *cpe = ClassLoader::classpath_entry(0); |
aoqi@0 | 529 | for ( ; cpe != NULL; cpe = cpe->next()) { |
aoqi@0 | 530 | |
aoqi@0 | 531 | if (cpe->is_jar_file()) { |
aoqi@0 | 532 | if (num_jars_now < _header._num_jars) { |
aoqi@0 | 533 | |
aoqi@0 | 534 | // Jar file - verify timestamp and file size. |
aoqi@0 | 535 | struct stat st; |
aoqi@0 | 536 | const char *path = cpe->name(); |
aoqi@0 | 537 | if (os::stat(path, &st) != 0) { |
aoqi@0 | 538 | fail_continue("Unable to open jar file %s.", path); |
aoqi@0 | 539 | return false; |
aoqi@0 | 540 | } |
aoqi@0 | 541 | if (_header._jar[num_jars_now]._timestamp != st.st_mtime || |
aoqi@0 | 542 | _header._jar[num_jars_now]._filesize != st.st_size) { |
aoqi@0 | 543 | fail_continue("A jar file is not the one used while building" |
aoqi@0 | 544 | " the shared archive file."); |
aoqi@0 | 545 | return false; |
aoqi@0 | 546 | } |
aoqi@0 | 547 | } |
aoqi@0 | 548 | ++num_jars_now; |
aoqi@0 | 549 | } else { |
aoqi@0 | 550 | |
aoqi@0 | 551 | // If directories appear in boot classpath, they must be empty to |
aoqi@0 | 552 | // avoid having to verify each individual class file. |
aoqi@0 | 553 | const char* name = ((ClassPathDirEntry*)cpe)->name(); |
aoqi@0 | 554 | if (!os::dir_is_empty(name)) { |
aoqi@0 | 555 | fail_continue("Boot classpath directory %s is not empty.", name); |
aoqi@0 | 556 | return false; |
aoqi@0 | 557 | } |
aoqi@0 | 558 | } |
aoqi@0 | 559 | } |
aoqi@0 | 560 | if (num_jars_now < _header._num_jars) { |
aoqi@0 | 561 | fail_continue("The number of jar files in the boot classpath is" |
aoqi@0 | 562 | " less than the number the shared archive was created with."); |
aoqi@0 | 563 | return false; |
aoqi@0 | 564 | } |
aoqi@0 | 565 | |
aoqi@0 | 566 | return true; |
aoqi@0 | 567 | } |
aoqi@0 | 568 | |
aoqi@0 | 569 | // The following method is provided to see whether a given pointer |
aoqi@0 | 570 | // falls in the mapped shared space. |
aoqi@0 | 571 | // Param: |
aoqi@0 | 572 | // p, The given pointer |
aoqi@0 | 573 | // Return: |
aoqi@0 | 574 | // True if the p is within the mapped shared space, otherwise, false. |
aoqi@0 | 575 | bool FileMapInfo::is_in_shared_space(const void* p) { |
aoqi@0 | 576 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
aoqi@0 | 577 | if (p >= _header._space[i]._base && |
aoqi@0 | 578 | p < _header._space[i]._base + _header._space[i]._used) { |
aoqi@0 | 579 | return true; |
aoqi@0 | 580 | } |
aoqi@0 | 581 | } |
aoqi@0 | 582 | |
aoqi@0 | 583 | return false; |
aoqi@0 | 584 | } |
aoqi@0 | 585 | |
aoqi@0 | 586 | void FileMapInfo::print_shared_spaces() { |
aoqi@0 | 587 | gclog_or_tty->print_cr("Shared Spaces:"); |
aoqi@0 | 588 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
aoqi@0 | 589 | struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; |
aoqi@0 | 590 | gclog_or_tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, |
aoqi@0 | 591 | shared_region_name[i], |
aoqi@0 | 592 | si->_base, si->_base + si->_used); |
aoqi@0 | 593 | } |
aoqi@0 | 594 | } |
aoqi@0 | 595 | |
aoqi@0 | 596 | // Unmap mapped regions of shared space. |
aoqi@0 | 597 | void FileMapInfo::stop_sharing_and_unmap(const char* msg) { |
aoqi@0 | 598 | FileMapInfo *map_info = FileMapInfo::current_info(); |
aoqi@0 | 599 | if (map_info) { |
aoqi@0 | 600 | map_info->fail_continue(msg); |
aoqi@0 | 601 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
aoqi@0 | 602 | if (map_info->_header._space[i]._base != NULL) { |
aoqi@0 | 603 | map_info->unmap_region(i); |
aoqi@0 | 604 | map_info->_header._space[i]._base = NULL; |
aoqi@0 | 605 | } |
aoqi@0 | 606 | } |
aoqi@0 | 607 | } else if (DumpSharedSpaces) { |
aoqi@0 | 608 | fail_stop(msg, NULL); |
aoqi@0 | 609 | } |
aoqi@0 | 610 | } |