/* * Copyright (c) 2003, 2022, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. *
*/
#ifndef O_BINARY // if defined (Win32) use binary files. #define O_BINARY 0 // otherwise do nothing. #endif
// Complain and stop. All error conditions occurring during the writing of // an archive file should stop the process. Unrecoverable errors during // the reading of the archive file should stop the process.
staticvoid fail_exit(constchar *msg, va_list ap) { // This occurs very early during initialization: tty is not initialized.
jio_fprintf(defaultStream::error_stream(), "An error has occurred while processing the" " shared archive file.\n");
jio_vfprintf(defaultStream::error_stream(), msg, ap);
jio_fprintf(defaultStream::error_stream(), "\n"); // Do not change the text of the below message because some tests check for it.
vm_exit_during_initialization("Unable to use shared archive.", NULL);
}
void FileMapInfo::fail_stop(constchar *msg, ...) {
va_list ap;
va_start(ap, msg);
fail_exit(msg, ap); // Never returns.
va_end(ap); // for completeness.
}
// Complain and continue. Recoverable errors during the reading of the // archive file may continue (with sharing disabled). // // If we continue, then disable shared spaces and close the file.
void FileMapInfo::fail_continue_impl(LogLevelType level, constchar *msg, va_list ap) { if (PrintSharedArchiveAndExit && _validating_shared_path_table) { // If we are doing PrintSharedArchiveAndExit and some of the classpath entries // do not validate, we can still continue "limping" to validate the remaining // entries. No need to quit.
tty->print("[");
tty->vprint(msg, ap);
tty->print_cr("]");
} else { if (RequireSharedSpaces) {
fail_exit(msg, ap);
} else {
LogMessage(cds) lm;
lm.vwrite(level, msg, ap);
}
}
}
// Fill in the fileMapInfo structure with data about this VM instance.
// This method copies the vm version info into header_version. If the version is too // long then a truncated version, which has a hash code appended to it, is copied. // // Using a template enables this method to verify that header_version is an array of // length JVM_IDENT_MAX. This ensures that the code that writes to the CDS file and // the code that reads the CDS file will both use the same size buffer. Hence, will // use identical truncation. This is necessary for matching of truncated versions. template <int N> staticvoid get_header_version(char (&header_version) [N]) {
assert(N == JVM_IDENT_MAX, "Bad header_version size");
if (version_len < (JVM_IDENT_MAX-1)) {
strcpy(header_version, vm_version);
} else { // Get the hash value. Use a static seed because the hash needs to return the same // value over multiple jvm invocations.
uint32_t hash = AltHashing::halfsiphash_32(8191, (const uint8_t*)vm_version, version_len);
// Truncate the ident, saving room for the 8 hex character hash value.
strncpy(header_version, vm_version, JVM_IDENT_MAX-9);
// Append the hash code as eight hex digits.
sprintf(&header_version[JVM_IDENT_MAX-9], "%08x", hash);
header_version[JVM_IDENT_MAX-1] = 0; // Null terminate.
}
void FileMapHeader::populate(FileMapInfo *info, size_t core_region_alignment,
size_t header_size, size_t base_archive_name_size,
size_t base_archive_name_offset, size_t common_app_classpath_prefix_size) { // 1. We require _generic_header._magic to be at the beginning of the file // 2. FileMapHeader also assumes that _generic_header is at the beginning of the file
assert(offset_of(FileMapHeader, _generic_header) == 0, "must be");
set_header_size((unsignedint)header_size);
set_base_archive_name_offset((unsignedint)base_archive_name_offset);
set_base_archive_name_size((unsignedint)base_archive_name_size);
set_common_app_classpath_prefix_size((unsignedint)common_app_classpath_prefix_size);
set_magic(DynamicDumpSharedSpaces ? CDS_DYNAMIC_ARCHIVE_MAGIC : CDS_ARCHIVE_MAGIC);
set_version(CURRENT_CDS_ARCHIVE_VERSION);
// The following fields are for sanity checks for whether this archive // will function correctly with this JVM and the bootclasspath it's // invoked with.
// JVM version string ... changes on each build.
get_header_version(_jvm_ident);
struct stat st; if (os::stat(cpe->name(), &st) == 0) { if ((st.st_mode & S_IFMT) == S_IFDIR) {
_type = dir_entry;
} else { // The timestamp of the modules_image is not checked at runtime. if (is_modules_image) {
_type = modules_image_entry;
} else {
_type = jar_entry;
_timestamp = st.st_mtime;
_from_class_path_attr = cpe->from_class_path_attr();
}
_filesize = st.st_size;
_is_module_path = is_module_path;
}
} else { // The file/dir must exist, or it would not have been added // into ClassLoader::classpath_entry(). // // If we can't access a jar file in the boot path, then we can't // make assumptions about where classes get loaded from.
FileMapInfo::fail_stop("Unable to open file %s.", cpe->name());
}
// No need to save the name of the module file, as it will be computed at run time // to allow relocation of the JDK directory. constchar* name = is_modules_image ? "" : cpe->name();
set_name(name, CHECK);
}
constchar* SharedClassPathEntry::name() const { if (UseSharedSpaces && is_modules_image()) { // In order to validate the runtime modules image file size against the archived // size information, we need to obtain the runtime modules image path. The recorded // dump time modules image path in the archive may be different from the runtime path // if the JDK image has beed moved after generating the archive. return ClassLoader::get_jrt_entry()->name();
} else { return _name->data();
}
}
bool ok = true;
log_info(class, path)("checking shared classpath entry: %s", name); if (os::stat(name, &st) != 0 && is_class_path) { // If the archived module path entry does not exist at runtime, it is not fatal // (no need to invalid the shared archive) because the shared runtime visibility check // filters out any archived module classes that do not have a matching runtime // module path location.
FileMapInfo::fail_continue("Required classpath entry does not exist: %s", name);
ok = false;
} elseif (is_dir()) { if (!os::dir_is_empty(name)) {
FileMapInfo::fail_continue("directory is not empty: %s", name);
ok = false;
}
} elseif ((has_timestamp() && _timestamp != st.st_mtime) ||
_filesize != st.st_size) {
ok = false; if (PrintSharedArchiveAndExit) {
FileMapInfo::fail_continue(_timestamp != st.st_mtime ? "Timestamp mismatch" : "File size mismatch");
} else { constchar* bad_jar_msg = "A jar file is not the one used while building the shared archive file:";
FileMapInfo::fail_continue("%s %s", bad_jar_msg, name); if (!log_is_enabled(Info, cds)) {
log_warning(cds)("%s %s", bad_jar_msg, name);
} if (_timestamp != st.st_mtime) {
log_warning(cds)("%s timestamp has changed.", name);
} else {
log_warning(cds)("%s size has changed.", name);
}
}
}
if (PrintSharedArchiveAndExit && !ok) { // If PrintSharedArchiveAndExit is enabled, don't report failure to the // caller. Please see above comments for more details.
ok = true;
MetaspaceShared::set_archive_loading_failed();
} return ok;
}
// Make a copy of the _shared_path_table for use during dynamic CDS dump. // It is needed because some Java code continues to execute after dynamic dump has finished. // However, during dynamic dump, we have modified FileMapInfo::_shared_path_table so // FileMapInfo::shared_path(i) returns incorrect information in ClassLoader::record_result(). void FileMapInfo::copy_shared_path_table(ClassLoaderData* loader_data, TRAPS) {
size_t entry_size = sizeof(SharedClassPathEntry);
size_t bytes = entry_size * _shared_path_table.size();
// 1. boot class path int i = 0;
i = add_shared_classpaths(i, "boot", jrt, CHECK);
i = add_shared_classpaths(i, "app", ClassLoader::app_classpath_entries(), CHECK);
i = add_shared_classpaths(i, "module", ClassLoader::module_path_entries(), CHECK);
for (int x = 0; x < num_non_existent_class_paths(); x++, i++) { constchar* path = _non_existent_class_paths->at(x);
shared_path(i)->init_as_non_existent(path, CHECK);
}
int last = _shared_path_table.size() - 1; if (last > ClassLoaderExt::max_used_path_index()) { // no need to check any path beyond max_used_path_index
last = ClassLoaderExt::max_used_path_index();
}
for (int i = 0; i <= last; i++) {
SharedClassPathEntry *e = shared_path(i); if (e->is_dir()) { constchar* path = e->name(); if (!os::dir_is_empty(path)) {
log_error(cds)("Error: non-empty directory '%s'", path);
has_nonempty_dir = true;
}
}
}
if (has_nonempty_dir) {
ClassLoader::exit_with_path_failure("Cannot have non-empty directory in paths", NULL);
}
}
int FileMapInfo::num_non_existent_class_paths() {
Arguments::assert_is_dumping_archive(); if (_non_existent_class_paths != NULL) { return _non_existent_class_paths->length();
} else { return 0;
}
}
int FileMapInfo::get_module_shared_path_index(Symbol* location) { if (location->starts_with("jrt:", 4) && get_number_of_shared_paths() > 0) {
assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); return 0;
}
if (ClassLoaderExt::app_module_paths_start_index() >= get_number_of_shared_paths()) { // The archive(s) were created without --module-path option return -1;
}
if (!location->starts_with("file:", 5)) { return -1;
}
// skip_uri_protocol was also called during dump time -- see ClassLoaderExt::process_module_table()
ResourceMark rm; constchar* file = ClassLoader::skip_uri_protocol(location->as_C_string()); for (int i = ClassLoaderExt::app_module_paths_start_index(); i < get_number_of_shared_paths(); i++) {
SharedClassPathEntry* ent = shared_path(i);
assert(ent->in_named_module(), "must be"); bool cond = strcmp(file, ent->name()) == 0;
log_debug(class, path)("get_module_shared_path_index (%d) %s : %s = %s", i,
location->as_C_string(), ent->name(), cond ? "same" : "different"); if (cond) { return i;
}
}
return -1;
}
class ManifestStream: public ResourceObj { private:
u1* _buffer_start; // Buffer bottom
u1* _buffer_end; // Buffer top (one past last element)
u1* _current; // Current buffer position
unsignedint FileMapInfo::longest_common_app_classpath_prefix_len(int num_paths,
GrowableArray<constchar*>* rp_array) { if (num_paths == 0) { return 0;
} unsignedint pos; for (pos = 0; ; pos++) { for (int i = 0; i < num_paths; i++) { if (rp_array->at(i)[pos] != '\0' && rp_array->at(i)[pos] == rp_array->at(0)[pos]) { continue;
}
// search backward for the pos before the file separator char while (pos > 0 && rp_array->at(0)[--pos] != *os::file_separator()); // return the file separator char position return pos + 1;
}
} return 0;
}
bool FileMapInfo::check_paths(int shared_path_start_idx, int num_paths, GrowableArray<constchar*>* rp_array, unsignedint dumptime_prefix_len, unsignedint runtime_prefix_len) { int i = 0; int j = shared_path_start_idx; while (i < num_paths) { while (shared_path(j)->from_class_path_attr()) { // shared_path(j) was expanded from the JAR file attribute "Class-Path:" // during dump time. It's not included in the -classpath VM argument.
j++;
}
assert(strlen(shared_path(j)->name()) > (size_t)dumptime_prefix_len, "sanity"); constchar* dumptime_path = shared_path(j)->name() + dumptime_prefix_len;
assert(strlen(rp_array->at(i)) > (size_t)runtime_prefix_len, "sanity"); constchar* runtime_path = rp_array->at(i) + runtime_prefix_len; if (!os::same_files(dumptime_path, runtime_path)) { returntrue;
}
i++;
j++;
} returnfalse;
}
bool FileMapInfo::validate_boot_class_paths() { // // - Archive contains boot classes only - relaxed boot path check: // Extra path elements appended to the boot path at runtime are allowed. // // - Archive contains application or platform classes - strict boot path check: // Validate the entire runtime boot path, which must be compatible // with the dump time boot path. Appending boot path at runtime is not // allowed. //
// The first entry in boot path is the modules_image (guaranteed by // ClassLoader::setup_boot_search_path()). Skip the first entry. The // path of the runtime modules_image may be different from the dump // time path (e.g. the JDK image is copied to a different location // after generating the shared archive), which is acceptable. For most // common cases, the dump time boot path might contain modules_image only. char* runtime_boot_path = Arguments::get_boot_class_path(); char* rp = skip_first_path_entry(runtime_boot_path);
assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); int dp_len = header()->app_class_paths_start_index() - 1; // ignore the first path to the module image bool mismatch = false;
bool relaxed_check = !header()->has_platform_or_app_classes(); if (dp_len == 0 && rp == NULL) { returntrue; // ok, both runtime and dump time boot paths have modules_images only
} elseif (dp_len == 0 && rp != NULL) { if (relaxed_check) { returntrue; // ok, relaxed check, runtime has extra boot append path entries
} else {
ResourceMark rm; if (check_paths_existence(rp)) { // If a path exists in the runtime boot paths, it is considered a mismatch // since there's no boot path specified during dump time.
mismatch = true;
}
}
} elseif (dp_len > 0 && rp != NULL) { int num;
ResourceMark rm;
GrowableArray<constchar*>* rp_array = create_path_array(rp); int rp_len = rp_array->length(); if (rp_len >= dp_len) { if (relaxed_check) { // only check the leading entries in the runtime boot path, up to // the length of the dump time boot path
num = dp_len;
} else { // check the full runtime boot path, must match with dump time
num = rp_len;
}
mismatch = check_paths(1, num, rp_array, 0, 0);
} else { // create_path_array() ignores non-existing paths. Although the dump time and runtime boot classpath lengths // are the same initially, after the call to create_path_array(), the runtime boot classpath length could become // shorter. We consider boot classpath mismatch in this case.
mismatch = true;
}
}
if (mismatch) { // The paths are different return classpath_failure("[BOOT classpath mismatch, actual =", runtime_boot_path);
} returntrue;
}
bool FileMapInfo::validate_app_class_paths(int shared_app_paths_len) { constchar *appcp = Arguments::get_appclasspath();
assert(appcp != NULL, "NULL app classpath"); int rp_len = num_paths(appcp); bool mismatch = false; if (rp_len < shared_app_paths_len) { return classpath_failure("Run time APP classpath is shorter than the one at dump time: ", appcp);
} if (shared_app_paths_len != 0 && rp_len != 0) { // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
ResourceMark rm;
GrowableArray<constchar*>* rp_array = create_path_array(appcp); if (rp_array->length() == 0) { // None of the jar file specified in the runtime -cp exists. return classpath_failure("None of the jar file specified in the runtime -cp exists: -Djava.class.path=", appcp);
} if (rp_array->length() < shared_app_paths_len) { // create_path_array() ignores non-existing paths. Although the dump time and runtime app classpath lengths // are the same initially, after the call to create_path_array(), the runtime app classpath length could become // shorter. We consider app classpath mismatch in this case. return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
}
// Handling of non-existent entries in the classpath: we eliminate all the non-existent // entries from both the dump time classpath (ClassLoader::update_class_path_entry_list) // and the runtime classpath (FileMapInfo::create_path_array), and check the remaining // entries. E.g.: // // dump : -cp a.jar:NE1:NE2:b.jar -> a.jar:b.jar -> recorded in archive. // run 1: -cp NE3:a.jar:NE4:b.jar -> a.jar:b.jar -> matched // run 2: -cp x.jar:NE4:b.jar -> x.jar:b.jar -> mismatched
int j = header()->app_class_paths_start_index();
mismatch = check_paths(j, shared_app_paths_len, rp_array, 0, 0); if (mismatch) { // To facilitate app deployment, we allow the JAR files to be moved *together* to // a different location, as long as they are still stored under the same directory // structure. E.g., the following is OK. // java -Xshare:dump -cp /a/Foo.jar:/a/b/Bar.jar ... // java -Xshare:auto -cp /x/y/Foo.jar:/x/y/b/Bar.jar ... unsignedint dumptime_prefix_len = header()->common_app_classpath_prefix_size(); unsignedint runtime_prefix_len = longest_common_app_classpath_prefix_len(shared_app_paths_len, rp_array);
mismatch = check_paths(j, shared_app_paths_len, rp_array,
dumptime_prefix_len, runtime_prefix_len); if (mismatch) { return classpath_failure("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
}
}
} returntrue;
}
void FileMapInfo::log_paths(constchar* msg, int start_idx, int end_idx) {
LogTarget(Info, class, path) lt; if (lt.is_enabled()) {
LogStream ls(lt);
ls.print("%s", msg); constchar* prefix = ""; for (int i = start_idx; i < end_idx; i++) {
ls.print("%s%s", prefix, shared_path(i)->name());
prefix = os::path_separator();
}
ls.cr();
}
}
// Load the shared path table info from the archive header
_shared_path_table = header()->shared_path_table(); if (DynamicDumpSharedSpaces) { // Only support dynamic dumping with the usage of the default CDS archive // or a simple base archive. // If the base layer archive contains additional path component besides // the runtime image and the -cp, dynamic dumping is disabled. // // When dynamic archiving is enabled, the _shared_path_table is overwritten // to include the application path and stored in the top layer archive.
assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image"); if (header()->app_class_paths_start_index() > 1) {
DynamicDumpSharedSpaces = false;
warning( "Dynamic archiving is disabled because base layer archive has appended boot classpath");
} if (header()->num_module_paths() > 0) { if (!check_module_paths()) {
DynamicDumpSharedSpaces = false;
warning( "Dynamic archiving is disabled because base layer archive has a different module path");
}
}
}
int module_paths_start_index = header()->app_module_paths_start_index(); int shared_app_paths_len = 0;
// validate the path entries up to the _max_used_path_index for (int i=0; i < header()->max_used_path_index() + 1; i++) { if (i < module_paths_start_index) { if (shared_path(i)->validate()) { // Only count the app class paths not from the "Class-path" attribute of a jar manifest. if (!shared_path(i)->from_class_path_attr() && i >= header()->app_class_paths_start_index()) {
shared_app_paths_len++;
}
log_info(class, path)("ok");
} else { if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
} returnfalse;
}
} elseif (i >= module_paths_start_index) { if (shared_path(i)->validate(false/* not a class path entry */)) {
log_info(class, path)("ok");
} else { if (_dynamic_archive_info != NULL && _dynamic_archive_info->_is_static) {
assert(!UseSharedSpaces, "UseSharedSpaces should be disabled");
} returnfalse;
}
}
}
if (header()->max_used_path_index() == 0) { // default archive only contains the module image in the bootclasspath
assert(shared_path(0)->is_modules_image(), "first shared_path must be the modules image");
} else { if (!validate_boot_class_paths() || !validate_app_class_paths(shared_app_paths_len)) { constchar* mismatch_msg = "shared class paths mismatch"; constchar* hint_msg = log_is_enabled(Info, class, path) ? "" : " (hint: enable -Xlog:class+path=info to diagnose the failure)";
fail_continue(LogLevel::Warning, "%s%s", mismatch_msg, hint_msg); returnfalse;
}
}
void FileMapInfo::validate_non_existent_class_paths() { // All of the recorded non-existent paths came from the Class-Path: attribute from the JAR // files on the app classpath. If any of these are found to exist during runtime, // it will change how classes are loading for the app loader. For safety, disable // loading of archived platform/app classes (currently there's no way to disable just the // app classes).
assert(UseSharedSpaces, "runtime only"); for (int i = header()->app_module_paths_start_index() + header()->num_module_paths();
i < get_number_of_shared_paths();
i++) {
SharedClassPathEntry* ent = shared_path(i); if (!ent->check_non_existent()) {
warning("Archived non-system classes are disabled because the " "file %s exists", ent->name());
header()->set_has_platform_or_app_classes(false);
}
}
}
// A utility class for reading/validating the GenericCDSFileMapHeader portion of // a CDS archive's header. The file header of all CDS archives with versions from // CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION (12) are guaranteed to always start // with GenericCDSFileMapHeader. This makes it possible to read important information // from a CDS archive created by a different version of HotSpot, so that we can // automatically regenerate the archive as necessary (JDK-8261455). class FileHeaderHelper { int _fd; bool _is_valid; bool _is_static;
GenericCDSFileMapHeader* _header; constchar* _archive_name; constchar* _base_archive_name;
~FileHeaderHelper() { if (_header != nullptr) {
FREE_C_HEAP_ARRAY(char, _header);
} if (_fd != -1) {
::close(_fd);
}
}
bool initialize() {
assert(_archive_name != nullptr, "Archive name is NULL");
_fd = os::open(_archive_name, O_RDONLY | O_BINARY, 0); if (_fd < 0) {
FileMapInfo::fail_continue("Specified shared archive not found (%s)", _archive_name); returnfalse;
} return initialize(_fd);
}
// for an already opened file, do not set _fd bool initialize(int fd) {
assert(_archive_name != nullptr, "Archive name is NULL");
assert(fd != -1, "Archive must be opened already"); // First read the generic header so we know the exact size of the actual header.
GenericCDSFileMapHeader gen_header;
size_t size = sizeof(GenericCDSFileMapHeader);
os::lseek(fd, 0, SEEK_SET);
size_t n = ::read(fd, (void*)&gen_header, (unsignedint)size); if (n != size) {
FileMapInfo::fail_continue("Unable to read generic CDS file map header from shared archive"); returnfalse;
}
if (gen_header._magic != CDS_ARCHIVE_MAGIC &&
gen_header._magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
FileMapInfo::fail_continue("The shared archive file has a bad magic number: %#x", gen_header._magic); returnfalse;
}
if (gen_header._version < CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION) {
FileMapInfo::fail_continue("Cannot handle shared archive file version 0x%x. Must be at least 0x%x.",
gen_header._version, CDS_GENERIC_HEADER_SUPPORTED_MIN_VERSION); returnfalse;
}
if (gen_header._version != CURRENT_CDS_ARCHIVE_VERSION) {
FileMapInfo::fail_continue("The shared archive file version 0x%x does not match the required version 0x%x.",
gen_header._version, CURRENT_CDS_ARCHIVE_VERSION);
}
size_t filelen = os::lseek(fd, 0, SEEK_END); if (gen_header._header_size >= filelen) {
FileMapInfo::fail_continue("Archive file header larger than archive file"); returnfalse;
}
// Read the actual header and perform more checks
size = gen_header._header_size;
_header = (GenericCDSFileMapHeader*)NEW_C_HEAP_ARRAY(char, size, mtInternal);
os::lseek(fd, 0, SEEK_SET);
n = ::read(fd, (void*)_header, (unsignedint)size); if (n != size) {
FileMapInfo::fail_continue("Unable to read actual CDS file map header from shared archive"); returnfalse;
}
if (!check_crc()) { returnfalse;
}
if (!check_and_init_base_archive_name()) { returnfalse;
}
// All fields in the GenericCDSFileMapHeader has been validated.
_is_valid = true; returntrue;
}
GenericCDSFileMapHeader* get_generic_file_header() {
assert(_header != nullptr && _is_valid, "must be a valid archive file"); return _header;
}
constchar* base_archive_name() {
assert(_header != nullptr && _is_valid, "must be a valid archive file"); return _base_archive_name;
}
if (name_offset + name_size < name_offset) {
FileMapInfo::fail_continue("base_archive_name offset/size overflow: " UINT32_FORMAT "/" UINT32_FORMAT,
name_offset, name_size); returnfalse;
} if (_header->_magic == CDS_ARCHIVE_MAGIC) { if (name_offset != 0) {
FileMapInfo::fail_continue("static shared archive must have zero _base_archive_name_offset"); returnfalse;
} if (name_size != 0) {
FileMapInfo::fail_continue("static shared archive must have zero _base_archive_name_size"); returnfalse;
}
} else {
assert(_header->_magic == CDS_DYNAMIC_ARCHIVE_MAGIC, "must be"); if ((name_size == 0 && name_offset != 0) ||
(name_size != 0 && name_offset == 0)) { // If either is zero, both must be zero. This indicates that we are using the default base archive.
FileMapInfo::fail_continue("Invalid base_archive_name offset/size: " UINT32_FORMAT "/" UINT32_FORMAT,
name_offset, name_size); returnfalse;
} if (name_size > 0) { if (name_offset + name_size > header_size) {
FileMapInfo::fail_continue("Invalid base_archive_name offset/size (out of range): "
UINT32_FORMAT " + " UINT32_FORMAT " > " UINT32_FORMAT ,
name_offset, name_size, header_size); returnfalse;
} constchar* name = ((constchar*)_header) + _header->_base_archive_name_offset; if (name[name_size - 1] != '\0' || strlen(name) != name_size - 1) {
FileMapInfo::fail_continue("Base archive name is damaged"); returnfalse;
} if (!os::file_exists(name)) {
FileMapInfo::fail_continue("Base archive %s does not exist", name); returnfalse;
}
_base_archive_name = name;
}
}
returntrue;
}
};
// Return value: // false: // <archive_name> is not a valid archive. *base_archive_name is set to null. // true && (*base_archive_name) == NULL: // <archive_name> is a valid static archive. // true && (*base_archive_name) != NULL: // <archive_name> is a valid dynamic archive. bool FileMapInfo::get_base_archive_name_from_header(constchar* archive_name, char** base_archive_name) {
FileHeaderHelper file_helper(archive_name, false);
*base_archive_name = NULL;
if (!file_helper.initialize()) { returnfalse;
}
GenericCDSFileMapHeader* header = file_helper.get_generic_file_header(); if (header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
assert(header->_magic == CDS_ARCHIVE_MAGIC, "must be"); if (AutoCreateSharedArchive) {
log_warning(cds)("AutoCreateSharedArchive is ignored because %s is a static archive", archive_name);
} returntrue;
}
constchar* base = file_helper.base_archive_name(); if (base == nullptr) {
*base_archive_name = Arguments::get_default_shared_archive_path();
} else {
*base_archive_name = os::strdup_check_oom(base);
}
returntrue;
}
// Read the FileMapInfo information from the file.
bool FileMapInfo::init_from_file(int fd) {
FileHeaderHelper file_helper(_full_path, _is_static); if (!file_helper.initialize(fd)) {
fail_continue("Unable to read the file header."); returnfalse;
}
GenericCDSFileMapHeader* gen_header = file_helper.get_generic_file_header();
if (_is_static) { if (gen_header->_magic != CDS_ARCHIVE_MAGIC) {
FileMapInfo::fail_continue("Not a base shared archive: %s", _full_path); returnfalse;
}
} else { if (gen_header->_magic != CDS_DYNAMIC_ARCHIVE_MAGIC) {
FileMapInfo::fail_continue("Not a top shared archive: %s", _full_path); returnfalse;
}
}
_header = (FileMapHeader*)os::malloc(gen_header->_header_size, mtInternal);
os::lseek(fd, 0, SEEK_SET); // reset to begin of the archive
size_t size = gen_header->_header_size;
size_t n = ::read(fd, (void*)_header, (unsignedint)size); if (n != size) {
fail_continue("Failed to read file header from the top archive file\n"); returnfalse;
}
if (header()->version() != CURRENT_CDS_ARCHIVE_VERSION) {
log_info(cds)("_version expected: 0x%x", CURRENT_CDS_ARCHIVE_VERSION);
log_info(cds)(" actual: 0x%x", header()->version());
fail_continue("The shared archive file has the wrong version."); returnfalse;
}
int common_path_size = header()->common_app_classpath_prefix_size(); if (common_path_size < 0) {
FileMapInfo::fail_continue("common app classpath prefix len < 0"); returnfalse;
}
if (actual_ident[JVM_IDENT_MAX-1] != 0) {
FileMapInfo::fail_continue("JVM version identifier is corrupted."); returnfalse;
}
char expected_ident[JVM_IDENT_MAX];
get_header_version(expected_ident); if (strncmp(actual_ident, expected_ident, JVM_IDENT_MAX-1) != 0) {
log_info(cds)("_jvm_ident expected: %s", expected_ident);
log_info(cds)(" actual: %s", actual_ident);
FileMapInfo::fail_continue("The shared archive file was created by a different" " version or build of HotSpot"); returnfalse;
}
_file_offset = header()->header_size(); // accounts for the size of _base_archive_name
size_t len = os::lseek(fd, 0, SEEK_END);
for (int i = 0; i <= MetaspaceShared::last_valid_region; i++) {
FileMapRegion* r = region_at(i); if (r->file_offset() > len || len - r->file_offset() < r->used()) {
fail_continue("The shared archive file has been truncated."); returnfalse;
}
}
returntrue;
}
void FileMapInfo::seek_to_position(size_t pos) { if (os::lseek(_fd, (long)pos, SEEK_SET) < 0) {
fail_stop("Unable to seek to position " SIZE_FORMAT, pos);
}
}
// Read the FileMapInfo information from the file. bool FileMapInfo::open_for_read() { if (_file_open) { returntrue;
}
log_info(cds)("trying to map %s", _full_path); int fd = os::open(_full_path, O_RDONLY | O_BINARY, 0); if (fd < 0) { if (errno == ENOENT) {
fail_continue("Specified shared archive not found (%s)", _full_path);
} else {
fail_continue("Failed to open shared archive file (%s)",
os::strerror(errno));
} returnfalse;
} else {
log_info(cds)("Opened archive %s.", _full_path);
}
_fd = fd;
_file_open = true; returntrue;
}
// Write the FileMapInfo information to the file.
void FileMapInfo::open_for_write() {
LogMessage(cds) msg; if (msg.is_info()) {
msg.info("Dumping shared data to file: ");
msg.info(" %s", _full_path);
}
#ifdef _WINDOWS // On Windows, need WRITE permission to remove the file.
chmod(_full_path, _S_IREAD | _S_IWRITE); #endif
// Use remove() to delete the existing file because, on Unix, this will // allow processes that have it open continued access to the file.
remove(_full_path); int fd = os::open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); if (fd < 0) {
fail_stop("Unable to create shared archive file %s: (%s).", _full_path,
os::strerror(errno));
}
_fd = fd;
_file_open = true;
// Seek past the header. We will write the header after all regions are written // and their CRCs computed.
size_t header_bytes = header()->header_size();
// Write out the given archive heap memory regions. GC code combines multiple // consecutive archive GC regions into one MemRegion whenever possible and // produces the 'regions' array. //
--> --------------------
--> maximum size reached
--> --------------------
Messung V0.5
¤ Dauer der Verarbeitung: 0.33 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.