Thu, 21 Aug 2014 13:57:51 -0700
8046070: Class Data Sharing clean up and refactoring
Summary: Cleaned up CDS to be more configurable, maintainable and extensible
Reviewed-by: dholmes, coleenp, acorn, mchung
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 | } |
duke@435 | 100 | } |
duke@435 | 101 | va_end(ap); |
duke@435 | 102 | UseSharedSpaces = false; |
iklam@7089 | 103 | assert(current_info() != NULL, "singleton must be registered"); |
iklam@7089 | 104 | current_info()->close(); |
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 | } |
duke@435 | 317 | _file_offset = (long)n; |
iklam@7089 | 318 | |
iklam@7089 | 319 | size_t info_size = _header->_paths_misc_info_size; |
iklam@7089 | 320 | _paths_misc_info = NEW_C_HEAP_ARRAY_RETURN_NULL(char, info_size, mtClass); |
iklam@7089 | 321 | if (_paths_misc_info == NULL) { |
iklam@7089 | 322 | fail_continue("Unable to read the file header."); |
iklam@7089 | 323 | return false; |
iklam@7089 | 324 | } |
iklam@7089 | 325 | n = os::read(fd, _paths_misc_info, (unsigned int)info_size); |
iklam@7089 | 326 | if (n != info_size) { |
iklam@7089 | 327 | fail_continue("Unable to read the shared path info header."); |
iklam@7089 | 328 | FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass); |
iklam@7089 | 329 | _paths_misc_info = NULL; |
iklam@7089 | 330 | return false; |
iklam@7089 | 331 | } |
iklam@7089 | 332 | |
iklam@7089 | 333 | _file_offset += (long)n; |
duke@435 | 334 | return true; |
duke@435 | 335 | } |
duke@435 | 336 | |
duke@435 | 337 | |
duke@435 | 338 | // Read the FileMapInfo information from the file. |
duke@435 | 339 | bool FileMapInfo::open_for_read() { |
duke@435 | 340 | _full_path = Arguments::GetSharedArchivePath(); |
duke@435 | 341 | int fd = open(_full_path, O_RDONLY | O_BINARY, 0); |
duke@435 | 342 | if (fd < 0) { |
duke@435 | 343 | if (errno == ENOENT) { |
duke@435 | 344 | // Not locating the shared archive is ok. |
duke@435 | 345 | fail_continue("Specified shared archive not found."); |
duke@435 | 346 | } else { |
duke@435 | 347 | fail_continue("Failed to open shared archive file (%s).", |
duke@435 | 348 | strerror(errno)); |
duke@435 | 349 | } |
duke@435 | 350 | return false; |
duke@435 | 351 | } |
duke@435 | 352 | |
duke@435 | 353 | _fd = fd; |
duke@435 | 354 | _file_open = true; |
duke@435 | 355 | return true; |
duke@435 | 356 | } |
duke@435 | 357 | |
duke@435 | 358 | |
duke@435 | 359 | // Write the FileMapInfo information to the file. |
duke@435 | 360 | |
duke@435 | 361 | void FileMapInfo::open_for_write() { |
duke@435 | 362 | _full_path = Arguments::GetSharedArchivePath(); |
duke@435 | 363 | if (PrintSharedSpaces) { |
duke@435 | 364 | tty->print_cr("Dumping shared data to file: "); |
duke@435 | 365 | tty->print_cr(" %s", _full_path); |
duke@435 | 366 | } |
duke@435 | 367 | |
hseigel@4521 | 368 | #ifdef _WINDOWS // On Windows, need WRITE permission to remove the file. |
hseigel@4521 | 369 | chmod(_full_path, _S_IREAD | _S_IWRITE); |
hseigel@4521 | 370 | #endif |
hseigel@4521 | 371 | |
hseigel@4521 | 372 | // Use remove() to delete the existing file because, on Unix, this will |
hseigel@4521 | 373 | // allow processes that have it open continued access to the file. |
duke@435 | 374 | remove(_full_path); |
duke@435 | 375 | int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); |
duke@435 | 376 | if (fd < 0) { |
duke@435 | 377 | fail_stop("Unable to create shared archive file %s.", _full_path); |
duke@435 | 378 | } |
duke@435 | 379 | _fd = fd; |
duke@435 | 380 | _file_offset = 0; |
duke@435 | 381 | _file_open = true; |
duke@435 | 382 | } |
duke@435 | 383 | |
duke@435 | 384 | |
duke@435 | 385 | // Write the header to the file, seek to the next allocation boundary. |
duke@435 | 386 | |
duke@435 | 387 | void FileMapInfo::write_header() { |
iklam@7089 | 388 | int info_size = ClassLoader::get_shared_paths_misc_info_size(); |
iklam@7089 | 389 | |
iklam@7089 | 390 | _header->_paths_misc_info_size = info_size; |
iklam@7089 | 391 | |
iklam@7089 | 392 | align_file_position(); |
iklam@7089 | 393 | size_t sz = _header->data_size(); |
iklam@7089 | 394 | char* addr = _header->data(); |
iklam@7089 | 395 | write_bytes(addr, (int)sz); // skip the C++ vtable |
iklam@7089 | 396 | write_bytes(ClassLoader::get_shared_paths_misc_info(), info_size); |
iklam@7089 | 397 | align_file_position(); |
duke@435 | 398 | } |
duke@435 | 399 | |
duke@435 | 400 | |
duke@435 | 401 | // Dump shared spaces to file. |
duke@435 | 402 | |
coleenp@4037 | 403 | void FileMapInfo::write_space(int i, Metaspace* space, bool read_only) { |
duke@435 | 404 | align_file_position(); |
jmasa@5015 | 405 | size_t used = space->used_bytes_slow(Metaspace::NonClassType); |
jmasa@5015 | 406 | size_t capacity = space->capacity_bytes_slow(Metaspace::NonClassType); |
iklam@7089 | 407 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
coleenp@4037 | 408 | write_region(i, (char*)space->bottom(), used, capacity, read_only, false); |
duke@435 | 409 | } |
duke@435 | 410 | |
duke@435 | 411 | |
duke@435 | 412 | // Dump region to file. |
duke@435 | 413 | |
duke@435 | 414 | void FileMapInfo::write_region(int region, char* base, size_t size, |
duke@435 | 415 | size_t capacity, bool read_only, |
duke@435 | 416 | bool allow_exec) { |
iklam@7089 | 417 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[region]; |
duke@435 | 418 | |
duke@435 | 419 | if (_file_open) { |
duke@435 | 420 | guarantee(si->_file_offset == _file_offset, "file offset mismatch."); |
duke@435 | 421 | if (PrintSharedSpaces) { |
coleenp@4037 | 422 | tty->print_cr("Shared file region %d: 0x%6x bytes, addr " INTPTR_FORMAT |
coleenp@4037 | 423 | " file offset 0x%6x", region, size, base, _file_offset); |
duke@435 | 424 | } |
duke@435 | 425 | } else { |
duke@435 | 426 | si->_file_offset = _file_offset; |
duke@435 | 427 | } |
duke@435 | 428 | si->_base = base; |
duke@435 | 429 | si->_used = size; |
duke@435 | 430 | si->_capacity = capacity; |
duke@435 | 431 | si->_read_only = read_only; |
duke@435 | 432 | si->_allow_exec = allow_exec; |
duke@435 | 433 | write_bytes_aligned(base, (int)size); |
duke@435 | 434 | } |
duke@435 | 435 | |
duke@435 | 436 | |
duke@435 | 437 | // Dump bytes to file -- at the current file position. |
duke@435 | 438 | |
duke@435 | 439 | void FileMapInfo::write_bytes(const void* buffer, int nbytes) { |
duke@435 | 440 | if (_file_open) { |
duke@435 | 441 | int n = ::write(_fd, buffer, nbytes); |
duke@435 | 442 | if (n != nbytes) { |
duke@435 | 443 | // It is dangerous to leave the corrupted shared archive file around, |
duke@435 | 444 | // close and remove the file. See bug 6372906. |
duke@435 | 445 | close(); |
duke@435 | 446 | remove(_full_path); |
duke@435 | 447 | fail_stop("Unable to write to shared archive file.", NULL); |
duke@435 | 448 | } |
duke@435 | 449 | } |
duke@435 | 450 | _file_offset += nbytes; |
duke@435 | 451 | } |
duke@435 | 452 | |
duke@435 | 453 | |
duke@435 | 454 | // Align file position to an allocation unit boundary. |
duke@435 | 455 | |
duke@435 | 456 | void FileMapInfo::align_file_position() { |
duke@435 | 457 | long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity()); |
duke@435 | 458 | if (new_file_offset != _file_offset) { |
duke@435 | 459 | _file_offset = new_file_offset; |
duke@435 | 460 | if (_file_open) { |
duke@435 | 461 | // Seek one byte back from the target and write a byte to insure |
duke@435 | 462 | // that the written file is the correct length. |
duke@435 | 463 | _file_offset -= 1; |
duke@435 | 464 | if (lseek(_fd, _file_offset, SEEK_SET) < 0) { |
duke@435 | 465 | fail_stop("Unable to seek.", NULL); |
duke@435 | 466 | } |
duke@435 | 467 | char zero = 0; |
duke@435 | 468 | write_bytes(&zero, 1); |
duke@435 | 469 | } |
duke@435 | 470 | } |
duke@435 | 471 | } |
duke@435 | 472 | |
duke@435 | 473 | |
duke@435 | 474 | // Dump bytes to file -- at the current file position. |
duke@435 | 475 | |
duke@435 | 476 | void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { |
duke@435 | 477 | align_file_position(); |
duke@435 | 478 | write_bytes(buffer, nbytes); |
duke@435 | 479 | align_file_position(); |
duke@435 | 480 | } |
duke@435 | 481 | |
duke@435 | 482 | |
duke@435 | 483 | // Close the shared archive file. This does NOT unmap mapped regions. |
duke@435 | 484 | |
duke@435 | 485 | void FileMapInfo::close() { |
duke@435 | 486 | if (_file_open) { |
duke@435 | 487 | if (::close(_fd) < 0) { |
duke@435 | 488 | fail_stop("Unable to close the shared archive file."); |
duke@435 | 489 | } |
duke@435 | 490 | _file_open = false; |
duke@435 | 491 | _fd = -1; |
duke@435 | 492 | } |
duke@435 | 493 | } |
duke@435 | 494 | |
duke@435 | 495 | |
duke@435 | 496 | // JVM/TI RedefineClasses() support: |
duke@435 | 497 | // Remap the shared readonly space to shared readwrite, private. |
duke@435 | 498 | bool FileMapInfo::remap_shared_readonly_as_readwrite() { |
iklam@7089 | 499 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; |
duke@435 | 500 | if (!si->_read_only) { |
duke@435 | 501 | // the space is already readwrite so we are done |
duke@435 | 502 | return true; |
duke@435 | 503 | } |
duke@435 | 504 | size_t used = si->_used; |
duke@435 | 505 | size_t size = align_size_up(used, os::vm_allocation_granularity()); |
duke@435 | 506 | if (!open_for_read()) { |
duke@435 | 507 | return false; |
duke@435 | 508 | } |
duke@435 | 509 | char *base = os::remap_memory(_fd, _full_path, si->_file_offset, |
duke@435 | 510 | si->_base, size, false /* !read_only */, |
duke@435 | 511 | si->_allow_exec); |
duke@435 | 512 | close(); |
duke@435 | 513 | if (base == NULL) { |
duke@435 | 514 | fail_continue("Unable to remap shared readonly space (errno=%d).", errno); |
duke@435 | 515 | return false; |
duke@435 | 516 | } |
duke@435 | 517 | if (base != si->_base) { |
duke@435 | 518 | fail_continue("Unable to remap shared readonly space at required address."); |
duke@435 | 519 | return false; |
duke@435 | 520 | } |
duke@435 | 521 | si->_read_only = false; |
duke@435 | 522 | return true; |
duke@435 | 523 | } |
duke@435 | 524 | |
coleenp@4037 | 525 | // Map the whole region at once, assumed to be allocated contiguously. |
coleenp@4037 | 526 | ReservedSpace FileMapInfo::reserve_shared_memory() { |
iklam@7089 | 527 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[0]; |
coleenp@4037 | 528 | char* requested_addr = si->_base; |
coleenp@4037 | 529 | |
hseigel@5528 | 530 | size_t size = FileMapInfo::shared_spaces_size(); |
coleenp@4037 | 531 | |
coleenp@4037 | 532 | // Reserve the space first, then map otherwise map will go right over some |
coleenp@4037 | 533 | // other reserved memory (like the code cache). |
hseigel@5528 | 534 | ReservedSpace rs(size, os::vm_allocation_granularity(), false, requested_addr); |
coleenp@4037 | 535 | if (!rs.is_reserved()) { |
coleenp@4804 | 536 | fail_continue(err_msg("Unable to reserve shared space at required address " INTPTR_FORMAT, requested_addr)); |
coleenp@4037 | 537 | return rs; |
coleenp@4037 | 538 | } |
zgu@4193 | 539 | // the reserved virtual memory is for mapping class data sharing archive |
hseigel@4396 | 540 | MemTracker::record_virtual_memory_type((address)rs.base(), mtClassShared); |
hseigel@4396 | 541 | |
coleenp@4037 | 542 | return rs; |
coleenp@4037 | 543 | } |
duke@435 | 544 | |
duke@435 | 545 | // Memory map a region in the address space. |
coleenp@4037 | 546 | static const char* shared_region_name[] = { "ReadOnly", "ReadWrite", "MiscData", "MiscCode"}; |
duke@435 | 547 | |
coleenp@4037 | 548 | char* FileMapInfo::map_region(int i) { |
iklam@7089 | 549 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
duke@435 | 550 | size_t used = si->_used; |
coleenp@4037 | 551 | size_t alignment = os::vm_allocation_granularity(); |
coleenp@4037 | 552 | size_t size = align_size_up(used, alignment); |
coleenp@4037 | 553 | char *requested_addr = si->_base; |
coleenp@4037 | 554 | |
coleenp@4037 | 555 | // map the contents of the CDS archive in this memory |
duke@435 | 556 | char *base = os::map_memory(_fd, _full_path, si->_file_offset, |
duke@435 | 557 | requested_addr, size, si->_read_only, |
duke@435 | 558 | si->_allow_exec); |
coleenp@4037 | 559 | if (base == NULL || base != si->_base) { |
coleenp@4037 | 560 | fail_continue(err_msg("Unable to map %s shared space at required address.", shared_region_name[i])); |
duke@435 | 561 | return NULL; |
duke@435 | 562 | } |
hseigel@4396 | 563 | #ifdef _WINDOWS |
hseigel@4396 | 564 | // This call is Windows-only because the memory_type gets recorded for the other platforms |
hseigel@4396 | 565 | // in method FileMapInfo::reserve_shared_memory(), which is not called on Windows. |
hseigel@4396 | 566 | MemTracker::record_virtual_memory_type((address)base, mtClassShared); |
hseigel@4396 | 567 | #endif |
duke@435 | 568 | return base; |
duke@435 | 569 | } |
duke@435 | 570 | |
duke@435 | 571 | |
duke@435 | 572 | // Unmap a memory region in the address space. |
duke@435 | 573 | |
duke@435 | 574 | void FileMapInfo::unmap_region(int i) { |
iklam@7089 | 575 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
duke@435 | 576 | size_t used = si->_used; |
duke@435 | 577 | size_t size = align_size_up(used, os::vm_allocation_granularity()); |
duke@435 | 578 | if (!os::unmap_memory(si->_base, size)) { |
duke@435 | 579 | fail_stop("Unable to unmap shared space."); |
duke@435 | 580 | } |
duke@435 | 581 | } |
duke@435 | 582 | |
duke@435 | 583 | |
duke@435 | 584 | void FileMapInfo::assert_mark(bool check) { |
duke@435 | 585 | if (!check) { |
duke@435 | 586 | fail_stop("Mark mismatch while restoring from shared file.", NULL); |
duke@435 | 587 | } |
duke@435 | 588 | } |
duke@435 | 589 | |
duke@435 | 590 | |
duke@435 | 591 | FileMapInfo* FileMapInfo::_current_info = NULL; |
iklam@7089 | 592 | SharedClassPathEntry* FileMapInfo::_classpath_entry_table = NULL; |
iklam@7089 | 593 | int FileMapInfo::_classpath_entry_table_size = 0; |
iklam@7089 | 594 | size_t FileMapInfo::_classpath_entry_size = 0x1234baad; |
iklam@7089 | 595 | bool FileMapInfo::_validating_classpath_entry_table = false; |
duke@435 | 596 | |
duke@435 | 597 | // Open the shared archive file, read and validate the header |
duke@435 | 598 | // information (version, boot classpath, etc.). If initialization |
duke@435 | 599 | // fails, shared spaces are disabled and the file is closed. [See |
duke@435 | 600 | // fail_continue.] |
iklam@7089 | 601 | // |
iklam@7089 | 602 | // Validation of the archive is done in two steps: |
iklam@7089 | 603 | // |
iklam@7089 | 604 | // [1] validate_header() - done here. This checks the header, including _paths_misc_info. |
iklam@7089 | 605 | // [2] validate_classpath_entry_table - this is done later, because the table is in the RW |
iklam@7089 | 606 | // region of the archive, which is not mapped yet. |
duke@435 | 607 | bool FileMapInfo::initialize() { |
duke@435 | 608 | assert(UseSharedSpaces, "UseSharedSpaces expected."); |
duke@435 | 609 | |
duke@435 | 610 | if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { |
duke@435 | 611 | fail_continue("Tool agent requires sharing to be disabled."); |
duke@435 | 612 | return false; |
duke@435 | 613 | } |
duke@435 | 614 | |
duke@435 | 615 | if (!open_for_read()) { |
duke@435 | 616 | return false; |
duke@435 | 617 | } |
duke@435 | 618 | |
duke@435 | 619 | init_from_file(_fd); |
iklam@7089 | 620 | if (!validate_header()) { |
duke@435 | 621 | return false; |
duke@435 | 622 | } |
duke@435 | 623 | |
iklam@7089 | 624 | SharedReadOnlySize = _header->_space[0]._capacity; |
iklam@7089 | 625 | SharedReadWriteSize = _header->_space[1]._capacity; |
iklam@7089 | 626 | SharedMiscDataSize = _header->_space[2]._capacity; |
iklam@7089 | 627 | SharedMiscCodeSize = _header->_space[3]._capacity; |
duke@435 | 628 | return true; |
duke@435 | 629 | } |
duke@435 | 630 | |
iklam@7089 | 631 | bool FileMapInfo::FileMapHeader::validate() { |
iklam@7089 | 632 | if (_version != current_version()) { |
iklam@7089 | 633 | FileMapInfo::fail_continue("The shared archive file is the wrong version."); |
duke@435 | 634 | return false; |
duke@435 | 635 | } |
iklam@7089 | 636 | if (_magic != (int)0xf00baba2) { |
iklam@7089 | 637 | FileMapInfo::fail_continue("The shared archive file has a bad magic number."); |
duke@435 | 638 | return false; |
duke@435 | 639 | } |
hseigel@4293 | 640 | char header_version[JVM_IDENT_MAX]; |
hseigel@4293 | 641 | get_header_version(header_version); |
iklam@7089 | 642 | if (strncmp(_jvm_ident, header_version, JVM_IDENT_MAX-1) != 0) { |
iklam@7089 | 643 | if (TraceClassPaths) { |
iklam@7089 | 644 | tty->print_cr("Expected: %s", header_version); |
iklam@7089 | 645 | tty->print_cr("Actual: %s", _jvm_ident); |
iklam@7089 | 646 | } |
iklam@7089 | 647 | FileMapInfo::fail_continue("The shared archive file was created by a different" |
iklam@7089 | 648 | " version or build of HotSpot"); |
duke@435 | 649 | return false; |
duke@435 | 650 | } |
iklam@7089 | 651 | if (_obj_alignment != ObjectAlignmentInBytes) { |
iklam@7089 | 652 | FileMapInfo::fail_continue("The shared archive file's ObjectAlignmentInBytes of %d" |
hseigel@4397 | 653 | " does not equal the current ObjectAlignmentInBytes of %d.", |
iklam@7089 | 654 | _obj_alignment, ObjectAlignmentInBytes); |
duke@435 | 655 | return false; |
duke@435 | 656 | } |
duke@435 | 657 | |
duke@435 | 658 | return true; |
duke@435 | 659 | } |
duke@435 | 660 | |
iklam@7089 | 661 | bool FileMapInfo::validate_header() { |
iklam@7089 | 662 | bool status = _header->validate(); |
iklam@7089 | 663 | |
iklam@7089 | 664 | if (status) { |
iklam@7089 | 665 | if (!ClassLoader::check_shared_paths_misc_info(_paths_misc_info, _header->_paths_misc_info_size)) { |
iklam@7089 | 666 | if (!PrintSharedArchiveAndExit) { |
iklam@7089 | 667 | fail_continue("shared class paths mismatch (hint: enable -XX:+TraceClassPaths to diagnose the failure)"); |
iklam@7089 | 668 | status = false; |
iklam@7089 | 669 | } |
iklam@7089 | 670 | } |
iklam@7089 | 671 | } |
iklam@7089 | 672 | |
iklam@7089 | 673 | if (_paths_misc_info != NULL) { |
iklam@7089 | 674 | FREE_C_HEAP_ARRAY(char, _paths_misc_info, mtClass); |
iklam@7089 | 675 | _paths_misc_info = NULL; |
iklam@7089 | 676 | } |
iklam@7089 | 677 | return status; |
iklam@7089 | 678 | } |
iklam@7089 | 679 | |
duke@435 | 680 | // The following method is provided to see whether a given pointer |
duke@435 | 681 | // falls in the mapped shared space. |
duke@435 | 682 | // Param: |
duke@435 | 683 | // p, The given pointer |
duke@435 | 684 | // Return: |
duke@435 | 685 | // True if the p is within the mapped shared space, otherwise, false. |
duke@435 | 686 | bool FileMapInfo::is_in_shared_space(const void* p) { |
coleenp@4037 | 687 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
iklam@7089 | 688 | if (p >= _header->_space[i]._base && |
iklam@7089 | 689 | p < _header->_space[i]._base + _header->_space[i]._used) { |
duke@435 | 690 | return true; |
duke@435 | 691 | } |
duke@435 | 692 | } |
duke@435 | 693 | |
duke@435 | 694 | return false; |
duke@435 | 695 | } |
iklam@5329 | 696 | |
iklam@5329 | 697 | void FileMapInfo::print_shared_spaces() { |
iklam@5329 | 698 | gclog_or_tty->print_cr("Shared Spaces:"); |
iklam@5329 | 699 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
iklam@7089 | 700 | struct FileMapInfo::FileMapHeader::space_info* si = &_header->_space[i]; |
iklam@5329 | 701 | gclog_or_tty->print(" %s " INTPTR_FORMAT "-" INTPTR_FORMAT, |
iklam@5329 | 702 | shared_region_name[i], |
iklam@5329 | 703 | si->_base, si->_base + si->_used); |
iklam@5329 | 704 | } |
iklam@5329 | 705 | } |
hseigel@5528 | 706 | |
hseigel@5528 | 707 | // Unmap mapped regions of shared space. |
hseigel@5528 | 708 | void FileMapInfo::stop_sharing_and_unmap(const char* msg) { |
hseigel@5528 | 709 | FileMapInfo *map_info = FileMapInfo::current_info(); |
hseigel@5528 | 710 | if (map_info) { |
hseigel@5528 | 711 | map_info->fail_continue(msg); |
hseigel@5528 | 712 | for (int i = 0; i < MetaspaceShared::n_regions; i++) { |
iklam@7089 | 713 | if (map_info->_header->_space[i]._base != NULL) { |
hseigel@5528 | 714 | map_info->unmap_region(i); |
iklam@7089 | 715 | map_info->_header->_space[i]._base = NULL; |
hseigel@5528 | 716 | } |
hseigel@5528 | 717 | } |
hseigel@5528 | 718 | } else if (DumpSharedSpaces) { |
hseigel@5528 | 719 | fail_stop(msg, NULL); |
hseigel@5528 | 720 | } |
hseigel@5528 | 721 | } |