1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/vm/memory/filemap.cpp Sat Dec 01 00:00:00 2007 +0000 1.3 @@ -0,0 +1,522 @@ 1.4 +/* 1.5 + * Copyright 2003-2006 Sun Microsystems, Inc. All Rights Reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, 1.23 + * CA 95054 USA or visit www.sun.com if you need additional information or 1.24 + * have any questions. 1.25 + * 1.26 + */ 1.27 + 1.28 +# include "incls/_precompiled.incl" 1.29 +# include "incls/_filemap.cpp.incl" 1.30 +# include <sys/stat.h> 1.31 +# include <errno.h> 1.32 + 1.33 +#ifndef O_BINARY // if defined (Win32) use binary files. 1.34 +#define O_BINARY 0 // otherwise do nothing. 1.35 +#endif 1.36 + 1.37 + 1.38 +extern address JVM_FunctionAtStart(); 1.39 +extern address JVM_FunctionAtEnd(); 1.40 + 1.41 +// Complain and stop. All error conditions occuring during the writing of 1.42 +// an archive file should stop the process. Unrecoverable errors during 1.43 +// the reading of the archive file should stop the process. 1.44 + 1.45 +static void fail(const char *msg, va_list ap) { 1.46 + // This occurs very early during initialization: tty is not initialized. 1.47 + jio_fprintf(defaultStream::error_stream(), 1.48 + "An error has occured while processing the" 1.49 + " shared archive file.\n"); 1.50 + jio_vfprintf(defaultStream::error_stream(), msg, ap); 1.51 + jio_fprintf(defaultStream::error_stream(), "\n"); 1.52 + vm_exit_during_initialization("Unable to use shared archive.", NULL); 1.53 +} 1.54 + 1.55 + 1.56 +void FileMapInfo::fail_stop(const char *msg, ...) { 1.57 + va_list ap; 1.58 + va_start(ap, msg); 1.59 + fail(msg, ap); // Never returns. 1.60 + va_end(ap); // for completeness. 1.61 +} 1.62 + 1.63 + 1.64 +// Complain and continue. Recoverable errors during the reading of the 1.65 +// archive file may continue (with sharing disabled). 1.66 +// 1.67 +// If we continue, then disable shared spaces and close the file. 1.68 + 1.69 +void FileMapInfo::fail_continue(const char *msg, ...) { 1.70 + va_list ap; 1.71 + va_start(ap, msg); 1.72 + if (RequireSharedSpaces) { 1.73 + fail(msg, ap); 1.74 + } 1.75 + va_end(ap); 1.76 + UseSharedSpaces = false; 1.77 + close(); 1.78 +} 1.79 + 1.80 + 1.81 +// Fill in the fileMapInfo structure with data about this VM instance. 1.82 + 1.83 +void FileMapInfo::populate_header(size_t alignment) { 1.84 + _header._magic = 0xf00baba2; 1.85 + _header._version = _current_version; 1.86 + _header._alignment = alignment; 1.87 + 1.88 + // The following fields are for sanity checks for whether this archive 1.89 + // will function correctly with this JVM and the bootclasspath it's 1.90 + // invoked with. 1.91 + 1.92 + // JVM version string ... changes on each build. 1.93 + const char *vm_version = VM_Version::internal_vm_info_string(); 1.94 + if (strlen(vm_version) < (JVM_IDENT_MAX-1)) { 1.95 + strcpy(_header._jvm_ident, vm_version); 1.96 + } else { 1.97 + fail_stop("JVM Ident field for shared archive is too long" 1.98 + " - truncated to <%s>", _header._jvm_ident); 1.99 + } 1.100 + 1.101 + // Build checks on classpath and jar files 1.102 + _header._num_jars = 0; 1.103 + ClassPathEntry *cpe = ClassLoader::classpath_entry(0); 1.104 + for ( ; cpe != NULL; cpe = cpe->next()) { 1.105 + 1.106 + if (cpe->is_jar_file()) { 1.107 + if (_header._num_jars >= JVM_SHARED_JARS_MAX) { 1.108 + fail_stop("Too many jar files to share.", NULL); 1.109 + } 1.110 + 1.111 + // Jar file - record timestamp and file size. 1.112 + struct stat st; 1.113 + const char *path = cpe->name(); 1.114 + if (os::stat(path, &st) != 0) { 1.115 + // If we can't access a jar file in the boot path, then we can't 1.116 + // make assumptions about where classes get loaded from. 1.117 + fail_stop("Unable to open jar file %s.", path); 1.118 + } 1.119 + _header._jar[_header._num_jars]._timestamp = st.st_mtime; 1.120 + _header._jar[_header._num_jars]._filesize = st.st_size; 1.121 + _header._num_jars++; 1.122 + } else { 1.123 + 1.124 + // If directories appear in boot classpath, they must be empty to 1.125 + // avoid having to verify each individual class file. 1.126 + const char* name = ((ClassPathDirEntry*)cpe)->name(); 1.127 + if (!os::dir_is_empty(name)) { 1.128 + fail_stop("Boot classpath directory %s is not empty.", name); 1.129 + } 1.130 + } 1.131 + } 1.132 +} 1.133 + 1.134 + 1.135 +// Read the FileMapInfo information from the file. 1.136 + 1.137 +bool FileMapInfo::init_from_file(int fd) { 1.138 + 1.139 + size_t n = read(fd, &_header, sizeof(struct FileMapHeader)); 1.140 + if (n != sizeof(struct FileMapHeader)) { 1.141 + fail_continue("Unable to read the file header."); 1.142 + return false; 1.143 + } 1.144 + if (_header._version != current_version()) { 1.145 + fail_continue("The shared archive file has the wrong version."); 1.146 + return false; 1.147 + } 1.148 + _file_offset = (long)n; 1.149 + return true; 1.150 +} 1.151 + 1.152 + 1.153 +// Read the FileMapInfo information from the file. 1.154 +bool FileMapInfo::open_for_read() { 1.155 + _full_path = Arguments::GetSharedArchivePath(); 1.156 + int fd = open(_full_path, O_RDONLY | O_BINARY, 0); 1.157 + if (fd < 0) { 1.158 + if (errno == ENOENT) { 1.159 + // Not locating the shared archive is ok. 1.160 + fail_continue("Specified shared archive not found."); 1.161 + } else { 1.162 + fail_continue("Failed to open shared archive file (%s).", 1.163 + strerror(errno)); 1.164 + } 1.165 + return false; 1.166 + } 1.167 + 1.168 + _fd = fd; 1.169 + _file_open = true; 1.170 + return true; 1.171 +} 1.172 + 1.173 + 1.174 +// Write the FileMapInfo information to the file. 1.175 + 1.176 +void FileMapInfo::open_for_write() { 1.177 + _full_path = Arguments::GetSharedArchivePath(); 1.178 + if (PrintSharedSpaces) { 1.179 + tty->print_cr("Dumping shared data to file: "); 1.180 + tty->print_cr(" %s", _full_path); 1.181 + } 1.182 + 1.183 + // Remove the existing file in case another process has it open. 1.184 + remove(_full_path); 1.185 + int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444); 1.186 + if (fd < 0) { 1.187 + fail_stop("Unable to create shared archive file %s.", _full_path); 1.188 + } 1.189 + _fd = fd; 1.190 + _file_offset = 0; 1.191 + _file_open = true; 1.192 +} 1.193 + 1.194 + 1.195 +// Write the header to the file, seek to the next allocation boundary. 1.196 + 1.197 +void FileMapInfo::write_header() { 1.198 + write_bytes_aligned(&_header, sizeof(FileMapHeader)); 1.199 +} 1.200 + 1.201 + 1.202 +// Dump shared spaces to file. 1.203 + 1.204 +void FileMapInfo::write_space(int i, CompactibleSpace* space, bool read_only) { 1.205 + align_file_position(); 1.206 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 1.207 + write_region(i, (char*)space->bottom(), space->used(), 1.208 + space->capacity(), read_only, false); 1.209 +} 1.210 + 1.211 + 1.212 +// Dump region to file. 1.213 + 1.214 +void FileMapInfo::write_region(int region, char* base, size_t size, 1.215 + size_t capacity, bool read_only, 1.216 + bool allow_exec) { 1.217 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region]; 1.218 + 1.219 + if (_file_open) { 1.220 + guarantee(si->_file_offset == _file_offset, "file offset mismatch."); 1.221 + if (PrintSharedSpaces) { 1.222 + tty->print_cr("Shared file region %d: 0x%x bytes, addr 0x%x," 1.223 + " file offset 0x%x", region, size, base, _file_offset); 1.224 + } 1.225 + } else { 1.226 + si->_file_offset = _file_offset; 1.227 + } 1.228 + si->_base = base; 1.229 + si->_used = size; 1.230 + si->_capacity = capacity; 1.231 + si->_read_only = read_only; 1.232 + si->_allow_exec = allow_exec; 1.233 + write_bytes_aligned(base, (int)size); 1.234 +} 1.235 + 1.236 + 1.237 +// Dump bytes to file -- at the current file position. 1.238 + 1.239 +void FileMapInfo::write_bytes(const void* buffer, int nbytes) { 1.240 + if (_file_open) { 1.241 + int n = ::write(_fd, buffer, nbytes); 1.242 + if (n != nbytes) { 1.243 + // It is dangerous to leave the corrupted shared archive file around, 1.244 + // close and remove the file. See bug 6372906. 1.245 + close(); 1.246 + remove(_full_path); 1.247 + fail_stop("Unable to write to shared archive file.", NULL); 1.248 + } 1.249 + } 1.250 + _file_offset += nbytes; 1.251 +} 1.252 + 1.253 + 1.254 +// Align file position to an allocation unit boundary. 1.255 + 1.256 +void FileMapInfo::align_file_position() { 1.257 + long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity()); 1.258 + if (new_file_offset != _file_offset) { 1.259 + _file_offset = new_file_offset; 1.260 + if (_file_open) { 1.261 + // Seek one byte back from the target and write a byte to insure 1.262 + // that the written file is the correct length. 1.263 + _file_offset -= 1; 1.264 + if (lseek(_fd, _file_offset, SEEK_SET) < 0) { 1.265 + fail_stop("Unable to seek.", NULL); 1.266 + } 1.267 + char zero = 0; 1.268 + write_bytes(&zero, 1); 1.269 + } 1.270 + } 1.271 +} 1.272 + 1.273 + 1.274 +// Dump bytes to file -- at the current file position. 1.275 + 1.276 +void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) { 1.277 + align_file_position(); 1.278 + write_bytes(buffer, nbytes); 1.279 + align_file_position(); 1.280 +} 1.281 + 1.282 + 1.283 +// Close the shared archive file. This does NOT unmap mapped regions. 1.284 + 1.285 +void FileMapInfo::close() { 1.286 + if (_file_open) { 1.287 + if (::close(_fd) < 0) { 1.288 + fail_stop("Unable to close the shared archive file."); 1.289 + } 1.290 + _file_open = false; 1.291 + _fd = -1; 1.292 + } 1.293 +} 1.294 + 1.295 + 1.296 +// Memory map a shared space from the archive file. 1.297 + 1.298 +bool FileMapInfo::map_space(int i, ReservedSpace rs, ContiguousSpace* space) { 1.299 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 1.300 + if (space != NULL) { 1.301 + if (si->_base != (char*)space->bottom() || 1.302 + si->_capacity != space->capacity()) { 1.303 + fail_continue("Shared space base address does not match."); 1.304 + return false; 1.305 + } 1.306 + } 1.307 + bool result = (map_region(i, rs) != NULL); 1.308 + if (space != NULL && result) { 1.309 + space->set_top((HeapWord*)(si->_base + si->_used)); 1.310 + space->set_saved_mark(); 1.311 + } 1.312 + return result; 1.313 +} 1.314 + 1.315 + 1.316 +// JVM/TI RedefineClasses() support: 1.317 +// Remap the shared readonly space to shared readwrite, private. 1.318 +bool FileMapInfo::remap_shared_readonly_as_readwrite() { 1.319 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0]; 1.320 + if (!si->_read_only) { 1.321 + // the space is already readwrite so we are done 1.322 + return true; 1.323 + } 1.324 + size_t used = si->_used; 1.325 + size_t size = align_size_up(used, os::vm_allocation_granularity()); 1.326 + if (!open_for_read()) { 1.327 + return false; 1.328 + } 1.329 + char *base = os::remap_memory(_fd, _full_path, si->_file_offset, 1.330 + si->_base, size, false /* !read_only */, 1.331 + si->_allow_exec); 1.332 + close(); 1.333 + if (base == NULL) { 1.334 + fail_continue("Unable to remap shared readonly space (errno=%d).", errno); 1.335 + return false; 1.336 + } 1.337 + if (base != si->_base) { 1.338 + fail_continue("Unable to remap shared readonly space at required address."); 1.339 + return false; 1.340 + } 1.341 + si->_read_only = false; 1.342 + return true; 1.343 +} 1.344 + 1.345 + 1.346 +// Memory map a region in the address space. 1.347 + 1.348 +char* FileMapInfo::map_region(int i, ReservedSpace rs) { 1.349 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 1.350 + size_t used = si->_used; 1.351 + size_t size = align_size_up(used, os::vm_allocation_granularity()); 1.352 + 1.353 + ReservedSpace mapped_rs = rs.first_part(size, true, true); 1.354 + ReservedSpace unmapped_rs = rs.last_part(size); 1.355 + mapped_rs.release(); 1.356 + 1.357 + return map_region(i, true); 1.358 +} 1.359 + 1.360 + 1.361 +// Memory map a region in the address space. 1.362 + 1.363 +char* FileMapInfo::map_region(int i, bool address_must_match) { 1.364 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 1.365 + size_t used = si->_used; 1.366 + size_t size = align_size_up(used, os::vm_allocation_granularity()); 1.367 + char *requested_addr = 0; 1.368 + if (address_must_match) { 1.369 + requested_addr = si->_base; 1.370 + } 1.371 + char *base = os::map_memory(_fd, _full_path, si->_file_offset, 1.372 + requested_addr, size, si->_read_only, 1.373 + si->_allow_exec); 1.374 + if (base == NULL) { 1.375 + fail_continue("Unable to map shared space."); 1.376 + return NULL; 1.377 + } 1.378 + if (address_must_match) { 1.379 + if (base != si->_base) { 1.380 + fail_continue("Unable to map shared space at required address."); 1.381 + return NULL; 1.382 + } 1.383 + } else { 1.384 + si->_base = base; // save mapped address for unmapping. 1.385 + } 1.386 + return base; 1.387 +} 1.388 + 1.389 + 1.390 +// Unmap a memory region in the address space. 1.391 + 1.392 +void FileMapInfo::unmap_region(int i) { 1.393 + struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 1.394 + size_t used = si->_used; 1.395 + size_t size = align_size_up(used, os::vm_allocation_granularity()); 1.396 + if (!os::unmap_memory(si->_base, size)) { 1.397 + fail_stop("Unable to unmap shared space."); 1.398 + } 1.399 +} 1.400 + 1.401 + 1.402 +void FileMapInfo::assert_mark(bool check) { 1.403 + if (!check) { 1.404 + fail_stop("Mark mismatch while restoring from shared file.", NULL); 1.405 + } 1.406 +} 1.407 + 1.408 + 1.409 +FileMapInfo* FileMapInfo::_current_info = NULL; 1.410 + 1.411 + 1.412 +// Open the shared archive file, read and validate the header 1.413 +// information (version, boot classpath, etc.). If initialization 1.414 +// fails, shared spaces are disabled and the file is closed. [See 1.415 +// fail_continue.] 1.416 + 1.417 + 1.418 +bool FileMapInfo::initialize() { 1.419 + assert(UseSharedSpaces, "UseSharedSpaces expected."); 1.420 + 1.421 + if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) { 1.422 + fail_continue("Tool agent requires sharing to be disabled."); 1.423 + return false; 1.424 + } 1.425 + 1.426 + if (!open_for_read()) { 1.427 + return false; 1.428 + } 1.429 + 1.430 + init_from_file(_fd); 1.431 + if (!validate()) { 1.432 + return false; 1.433 + } 1.434 + 1.435 + SharedReadOnlySize = _header._space[0]._capacity; 1.436 + SharedReadWriteSize = _header._space[1]._capacity; 1.437 + SharedMiscDataSize = _header._space[2]._capacity; 1.438 + SharedMiscCodeSize = _header._space[3]._capacity; 1.439 + return true; 1.440 +} 1.441 + 1.442 + 1.443 +bool FileMapInfo::validate() { 1.444 + if (_header._version != current_version()) { 1.445 + fail_continue("The shared archive file is the wrong version."); 1.446 + return false; 1.447 + } 1.448 + if (_header._magic != (int)0xf00baba2) { 1.449 + fail_continue("The shared archive file has a bad magic number."); 1.450 + return false; 1.451 + } 1.452 + if (strncmp(_header._jvm_ident, VM_Version::internal_vm_info_string(), 1.453 + JVM_IDENT_MAX-1) != 0) { 1.454 + fail_continue("The shared archive file was created by a different" 1.455 + " version or build of HotSpot."); 1.456 + return false; 1.457 + } 1.458 + 1.459 + // Cannot verify interpreter yet, as it can only be created after the GC 1.460 + // heap has been initialized. 1.461 + 1.462 + if (_header._num_jars >= JVM_SHARED_JARS_MAX) { 1.463 + fail_continue("Too many jar files to share."); 1.464 + return false; 1.465 + } 1.466 + 1.467 + // Build checks on classpath and jar files 1.468 + int num_jars_now = 0; 1.469 + ClassPathEntry *cpe = ClassLoader::classpath_entry(0); 1.470 + for ( ; cpe != NULL; cpe = cpe->next()) { 1.471 + 1.472 + if (cpe->is_jar_file()) { 1.473 + if (num_jars_now < _header._num_jars) { 1.474 + 1.475 + // Jar file - verify timestamp and file size. 1.476 + struct stat st; 1.477 + const char *path = cpe->name(); 1.478 + if (os::stat(path, &st) != 0) { 1.479 + fail_continue("Unable to open jar file %s.", path); 1.480 + return false; 1.481 + } 1.482 + if (_header._jar[num_jars_now]._timestamp != st.st_mtime || 1.483 + _header._jar[num_jars_now]._filesize != st.st_size) { 1.484 + fail_continue("A jar file is not the one used while building" 1.485 + " the shared archive file."); 1.486 + return false; 1.487 + } 1.488 + } 1.489 + ++num_jars_now; 1.490 + } else { 1.491 + 1.492 + // If directories appear in boot classpath, they must be empty to 1.493 + // avoid having to verify each individual class file. 1.494 + const char* name = ((ClassPathDirEntry*)cpe)->name(); 1.495 + if (!os::dir_is_empty(name)) { 1.496 + fail_continue("Boot classpath directory %s is not empty.", name); 1.497 + return false; 1.498 + } 1.499 + } 1.500 + } 1.501 + if (num_jars_now < _header._num_jars) { 1.502 + fail_continue("The number of jar files in the boot classpath is" 1.503 + " less than the number the shared archive was created with."); 1.504 + return false; 1.505 + } 1.506 + 1.507 + return true; 1.508 +} 1.509 + 1.510 +// The following method is provided to see whether a given pointer 1.511 +// falls in the mapped shared space. 1.512 +// Param: 1.513 +// p, The given pointer 1.514 +// Return: 1.515 +// True if the p is within the mapped shared space, otherwise, false. 1.516 +bool FileMapInfo::is_in_shared_space(const void* p) { 1.517 + for (int i = 0; i < CompactingPermGenGen::n_regions; i++) { 1.518 + if (p >= _header._space[i]._base && 1.519 + p < _header._space[i]._base + _header._space[i]._used) { 1.520 + return true; 1.521 + } 1.522 + } 1.523 + 1.524 + return false; 1.525 +}