src/share/vm/memory/filemap.cpp

Fri, 27 Feb 2009 13:27:09 -0800

author
twisti
date
Fri, 27 Feb 2009 13:27:09 -0800
changeset 1040
98cb887364d3
parent 435
a61af66fc99e
child 1907
c18cbe5936b8
permissions
-rw-r--r--

6810672: Comment typos
Summary: I have collected some typos I have found while looking at the code.
Reviewed-by: kvn, never

duke@435 1 /*
duke@435 2 * Copyright 2003-2006 Sun Microsystems, Inc. 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 *
duke@435 19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@435 20 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@435 21 * have any questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 # include "incls/_precompiled.incl"
duke@435 26 # include "incls/_filemap.cpp.incl"
duke@435 27 # include <sys/stat.h>
duke@435 28 # include <errno.h>
duke@435 29
duke@435 30 #ifndef O_BINARY // if defined (Win32) use binary files.
duke@435 31 #define O_BINARY 0 // otherwise do nothing.
duke@435 32 #endif
duke@435 33
duke@435 34
duke@435 35 extern address JVM_FunctionAtStart();
duke@435 36 extern address JVM_FunctionAtEnd();
duke@435 37
twisti@1040 38 // Complain and stop. All error conditions occurring during the writing of
duke@435 39 // an archive file should stop the process. Unrecoverable errors during
duke@435 40 // the reading of the archive file should stop the process.
duke@435 41
duke@435 42 static void fail(const char *msg, va_list ap) {
duke@435 43 // This occurs very early during initialization: tty is not initialized.
duke@435 44 jio_fprintf(defaultStream::error_stream(),
twisti@1040 45 "An error has occurred while processing the"
duke@435 46 " shared archive file.\n");
duke@435 47 jio_vfprintf(defaultStream::error_stream(), msg, ap);
duke@435 48 jio_fprintf(defaultStream::error_stream(), "\n");
duke@435 49 vm_exit_during_initialization("Unable to use shared archive.", NULL);
duke@435 50 }
duke@435 51
duke@435 52
duke@435 53 void FileMapInfo::fail_stop(const char *msg, ...) {
duke@435 54 va_list ap;
duke@435 55 va_start(ap, msg);
duke@435 56 fail(msg, ap); // Never returns.
duke@435 57 va_end(ap); // for completeness.
duke@435 58 }
duke@435 59
duke@435 60
duke@435 61 // Complain and continue. Recoverable errors during the reading of the
duke@435 62 // archive file may continue (with sharing disabled).
duke@435 63 //
duke@435 64 // If we continue, then disable shared spaces and close the file.
duke@435 65
duke@435 66 void FileMapInfo::fail_continue(const char *msg, ...) {
duke@435 67 va_list ap;
duke@435 68 va_start(ap, msg);
duke@435 69 if (RequireSharedSpaces) {
duke@435 70 fail(msg, ap);
duke@435 71 }
duke@435 72 va_end(ap);
duke@435 73 UseSharedSpaces = false;
duke@435 74 close();
duke@435 75 }
duke@435 76
duke@435 77
duke@435 78 // Fill in the fileMapInfo structure with data about this VM instance.
duke@435 79
duke@435 80 void FileMapInfo::populate_header(size_t alignment) {
duke@435 81 _header._magic = 0xf00baba2;
duke@435 82 _header._version = _current_version;
duke@435 83 _header._alignment = alignment;
duke@435 84
duke@435 85 // The following fields are for sanity checks for whether this archive
duke@435 86 // will function correctly with this JVM and the bootclasspath it's
duke@435 87 // invoked with.
duke@435 88
duke@435 89 // JVM version string ... changes on each build.
duke@435 90 const char *vm_version = VM_Version::internal_vm_info_string();
duke@435 91 if (strlen(vm_version) < (JVM_IDENT_MAX-1)) {
duke@435 92 strcpy(_header._jvm_ident, vm_version);
duke@435 93 } else {
duke@435 94 fail_stop("JVM Ident field for shared archive is too long"
duke@435 95 " - truncated to <%s>", _header._jvm_ident);
duke@435 96 }
duke@435 97
duke@435 98 // Build checks on classpath and jar files
duke@435 99 _header._num_jars = 0;
duke@435 100 ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
duke@435 101 for ( ; cpe != NULL; cpe = cpe->next()) {
duke@435 102
duke@435 103 if (cpe->is_jar_file()) {
duke@435 104 if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
duke@435 105 fail_stop("Too many jar files to share.", NULL);
duke@435 106 }
duke@435 107
duke@435 108 // Jar file - record timestamp and file size.
duke@435 109 struct stat st;
duke@435 110 const char *path = cpe->name();
duke@435 111 if (os::stat(path, &st) != 0) {
duke@435 112 // If we can't access a jar file in the boot path, then we can't
duke@435 113 // make assumptions about where classes get loaded from.
duke@435 114 fail_stop("Unable to open jar file %s.", path);
duke@435 115 }
duke@435 116 _header._jar[_header._num_jars]._timestamp = st.st_mtime;
duke@435 117 _header._jar[_header._num_jars]._filesize = st.st_size;
duke@435 118 _header._num_jars++;
duke@435 119 } else {
duke@435 120
duke@435 121 // If directories appear in boot classpath, they must be empty to
duke@435 122 // avoid having to verify each individual class file.
duke@435 123 const char* name = ((ClassPathDirEntry*)cpe)->name();
duke@435 124 if (!os::dir_is_empty(name)) {
duke@435 125 fail_stop("Boot classpath directory %s is not empty.", name);
duke@435 126 }
duke@435 127 }
duke@435 128 }
duke@435 129 }
duke@435 130
duke@435 131
duke@435 132 // Read the FileMapInfo information from the file.
duke@435 133
duke@435 134 bool FileMapInfo::init_from_file(int fd) {
duke@435 135
duke@435 136 size_t n = read(fd, &_header, sizeof(struct FileMapHeader));
duke@435 137 if (n != sizeof(struct FileMapHeader)) {
duke@435 138 fail_continue("Unable to read the file header.");
duke@435 139 return false;
duke@435 140 }
duke@435 141 if (_header._version != current_version()) {
duke@435 142 fail_continue("The shared archive file has the wrong version.");
duke@435 143 return false;
duke@435 144 }
duke@435 145 _file_offset = (long)n;
duke@435 146 return true;
duke@435 147 }
duke@435 148
duke@435 149
duke@435 150 // Read the FileMapInfo information from the file.
duke@435 151 bool FileMapInfo::open_for_read() {
duke@435 152 _full_path = Arguments::GetSharedArchivePath();
duke@435 153 int fd = open(_full_path, O_RDONLY | O_BINARY, 0);
duke@435 154 if (fd < 0) {
duke@435 155 if (errno == ENOENT) {
duke@435 156 // Not locating the shared archive is ok.
duke@435 157 fail_continue("Specified shared archive not found.");
duke@435 158 } else {
duke@435 159 fail_continue("Failed to open shared archive file (%s).",
duke@435 160 strerror(errno));
duke@435 161 }
duke@435 162 return false;
duke@435 163 }
duke@435 164
duke@435 165 _fd = fd;
duke@435 166 _file_open = true;
duke@435 167 return true;
duke@435 168 }
duke@435 169
duke@435 170
duke@435 171 // Write the FileMapInfo information to the file.
duke@435 172
duke@435 173 void FileMapInfo::open_for_write() {
duke@435 174 _full_path = Arguments::GetSharedArchivePath();
duke@435 175 if (PrintSharedSpaces) {
duke@435 176 tty->print_cr("Dumping shared data to file: ");
duke@435 177 tty->print_cr(" %s", _full_path);
duke@435 178 }
duke@435 179
duke@435 180 // Remove the existing file in case another process has it open.
duke@435 181 remove(_full_path);
duke@435 182 int fd = open(_full_path, O_RDWR | O_CREAT | O_TRUNC | O_BINARY, 0444);
duke@435 183 if (fd < 0) {
duke@435 184 fail_stop("Unable to create shared archive file %s.", _full_path);
duke@435 185 }
duke@435 186 _fd = fd;
duke@435 187 _file_offset = 0;
duke@435 188 _file_open = true;
duke@435 189 }
duke@435 190
duke@435 191
duke@435 192 // Write the header to the file, seek to the next allocation boundary.
duke@435 193
duke@435 194 void FileMapInfo::write_header() {
duke@435 195 write_bytes_aligned(&_header, sizeof(FileMapHeader));
duke@435 196 }
duke@435 197
duke@435 198
duke@435 199 // Dump shared spaces to file.
duke@435 200
duke@435 201 void FileMapInfo::write_space(int i, CompactibleSpace* space, bool read_only) {
duke@435 202 align_file_position();
duke@435 203 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
duke@435 204 write_region(i, (char*)space->bottom(), space->used(),
duke@435 205 space->capacity(), read_only, false);
duke@435 206 }
duke@435 207
duke@435 208
duke@435 209 // Dump region to file.
duke@435 210
duke@435 211 void FileMapInfo::write_region(int region, char* base, size_t size,
duke@435 212 size_t capacity, bool read_only,
duke@435 213 bool allow_exec) {
duke@435 214 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[region];
duke@435 215
duke@435 216 if (_file_open) {
duke@435 217 guarantee(si->_file_offset == _file_offset, "file offset mismatch.");
duke@435 218 if (PrintSharedSpaces) {
duke@435 219 tty->print_cr("Shared file region %d: 0x%x bytes, addr 0x%x,"
duke@435 220 " file offset 0x%x", region, size, base, _file_offset);
duke@435 221 }
duke@435 222 } else {
duke@435 223 si->_file_offset = _file_offset;
duke@435 224 }
duke@435 225 si->_base = base;
duke@435 226 si->_used = size;
duke@435 227 si->_capacity = capacity;
duke@435 228 si->_read_only = read_only;
duke@435 229 si->_allow_exec = allow_exec;
duke@435 230 write_bytes_aligned(base, (int)size);
duke@435 231 }
duke@435 232
duke@435 233
duke@435 234 // Dump bytes to file -- at the current file position.
duke@435 235
duke@435 236 void FileMapInfo::write_bytes(const void* buffer, int nbytes) {
duke@435 237 if (_file_open) {
duke@435 238 int n = ::write(_fd, buffer, nbytes);
duke@435 239 if (n != nbytes) {
duke@435 240 // It is dangerous to leave the corrupted shared archive file around,
duke@435 241 // close and remove the file. See bug 6372906.
duke@435 242 close();
duke@435 243 remove(_full_path);
duke@435 244 fail_stop("Unable to write to shared archive file.", NULL);
duke@435 245 }
duke@435 246 }
duke@435 247 _file_offset += nbytes;
duke@435 248 }
duke@435 249
duke@435 250
duke@435 251 // Align file position to an allocation unit boundary.
duke@435 252
duke@435 253 void FileMapInfo::align_file_position() {
duke@435 254 long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity());
duke@435 255 if (new_file_offset != _file_offset) {
duke@435 256 _file_offset = new_file_offset;
duke@435 257 if (_file_open) {
duke@435 258 // Seek one byte back from the target and write a byte to insure
duke@435 259 // that the written file is the correct length.
duke@435 260 _file_offset -= 1;
duke@435 261 if (lseek(_fd, _file_offset, SEEK_SET) < 0) {
duke@435 262 fail_stop("Unable to seek.", NULL);
duke@435 263 }
duke@435 264 char zero = 0;
duke@435 265 write_bytes(&zero, 1);
duke@435 266 }
duke@435 267 }
duke@435 268 }
duke@435 269
duke@435 270
duke@435 271 // Dump bytes to file -- at the current file position.
duke@435 272
duke@435 273 void FileMapInfo::write_bytes_aligned(const void* buffer, int nbytes) {
duke@435 274 align_file_position();
duke@435 275 write_bytes(buffer, nbytes);
duke@435 276 align_file_position();
duke@435 277 }
duke@435 278
duke@435 279
duke@435 280 // Close the shared archive file. This does NOT unmap mapped regions.
duke@435 281
duke@435 282 void FileMapInfo::close() {
duke@435 283 if (_file_open) {
duke@435 284 if (::close(_fd) < 0) {
duke@435 285 fail_stop("Unable to close the shared archive file.");
duke@435 286 }
duke@435 287 _file_open = false;
duke@435 288 _fd = -1;
duke@435 289 }
duke@435 290 }
duke@435 291
duke@435 292
duke@435 293 // Memory map a shared space from the archive file.
duke@435 294
duke@435 295 bool FileMapInfo::map_space(int i, ReservedSpace rs, ContiguousSpace* space) {
duke@435 296 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
duke@435 297 if (space != NULL) {
duke@435 298 if (si->_base != (char*)space->bottom() ||
duke@435 299 si->_capacity != space->capacity()) {
duke@435 300 fail_continue("Shared space base address does not match.");
duke@435 301 return false;
duke@435 302 }
duke@435 303 }
duke@435 304 bool result = (map_region(i, rs) != NULL);
duke@435 305 if (space != NULL && result) {
duke@435 306 space->set_top((HeapWord*)(si->_base + si->_used));
duke@435 307 space->set_saved_mark();
duke@435 308 }
duke@435 309 return result;
duke@435 310 }
duke@435 311
duke@435 312
duke@435 313 // JVM/TI RedefineClasses() support:
duke@435 314 // Remap the shared readonly space to shared readwrite, private.
duke@435 315 bool FileMapInfo::remap_shared_readonly_as_readwrite() {
duke@435 316 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[0];
duke@435 317 if (!si->_read_only) {
duke@435 318 // the space is already readwrite so we are done
duke@435 319 return true;
duke@435 320 }
duke@435 321 size_t used = si->_used;
duke@435 322 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 323 if (!open_for_read()) {
duke@435 324 return false;
duke@435 325 }
duke@435 326 char *base = os::remap_memory(_fd, _full_path, si->_file_offset,
duke@435 327 si->_base, size, false /* !read_only */,
duke@435 328 si->_allow_exec);
duke@435 329 close();
duke@435 330 if (base == NULL) {
duke@435 331 fail_continue("Unable to remap shared readonly space (errno=%d).", errno);
duke@435 332 return false;
duke@435 333 }
duke@435 334 if (base != si->_base) {
duke@435 335 fail_continue("Unable to remap shared readonly space at required address.");
duke@435 336 return false;
duke@435 337 }
duke@435 338 si->_read_only = false;
duke@435 339 return true;
duke@435 340 }
duke@435 341
duke@435 342
duke@435 343 // Memory map a region in the address space.
duke@435 344
duke@435 345 char* FileMapInfo::map_region(int i, ReservedSpace rs) {
duke@435 346 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
duke@435 347 size_t used = si->_used;
duke@435 348 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 349
duke@435 350 ReservedSpace mapped_rs = rs.first_part(size, true, true);
duke@435 351 ReservedSpace unmapped_rs = rs.last_part(size);
duke@435 352 mapped_rs.release();
duke@435 353
duke@435 354 return map_region(i, true);
duke@435 355 }
duke@435 356
duke@435 357
duke@435 358 // Memory map a region in the address space.
duke@435 359
duke@435 360 char* FileMapInfo::map_region(int i, bool address_must_match) {
duke@435 361 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
duke@435 362 size_t used = si->_used;
duke@435 363 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 364 char *requested_addr = 0;
duke@435 365 if (address_must_match) {
duke@435 366 requested_addr = si->_base;
duke@435 367 }
duke@435 368 char *base = os::map_memory(_fd, _full_path, si->_file_offset,
duke@435 369 requested_addr, size, si->_read_only,
duke@435 370 si->_allow_exec);
duke@435 371 if (base == NULL) {
duke@435 372 fail_continue("Unable to map shared space.");
duke@435 373 return NULL;
duke@435 374 }
duke@435 375 if (address_must_match) {
duke@435 376 if (base != si->_base) {
duke@435 377 fail_continue("Unable to map shared space at required address.");
duke@435 378 return NULL;
duke@435 379 }
duke@435 380 } else {
duke@435 381 si->_base = base; // save mapped address for unmapping.
duke@435 382 }
duke@435 383 return base;
duke@435 384 }
duke@435 385
duke@435 386
duke@435 387 // Unmap a memory region in the address space.
duke@435 388
duke@435 389 void FileMapInfo::unmap_region(int i) {
duke@435 390 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
duke@435 391 size_t used = si->_used;
duke@435 392 size_t size = align_size_up(used, os::vm_allocation_granularity());
duke@435 393 if (!os::unmap_memory(si->_base, size)) {
duke@435 394 fail_stop("Unable to unmap shared space.");
duke@435 395 }
duke@435 396 }
duke@435 397
duke@435 398
duke@435 399 void FileMapInfo::assert_mark(bool check) {
duke@435 400 if (!check) {
duke@435 401 fail_stop("Mark mismatch while restoring from shared file.", NULL);
duke@435 402 }
duke@435 403 }
duke@435 404
duke@435 405
duke@435 406 FileMapInfo* FileMapInfo::_current_info = NULL;
duke@435 407
duke@435 408
duke@435 409 // Open the shared archive file, read and validate the header
duke@435 410 // information (version, boot classpath, etc.). If initialization
duke@435 411 // fails, shared spaces are disabled and the file is closed. [See
duke@435 412 // fail_continue.]
duke@435 413
duke@435 414
duke@435 415 bool FileMapInfo::initialize() {
duke@435 416 assert(UseSharedSpaces, "UseSharedSpaces expected.");
duke@435 417
duke@435 418 if (JvmtiExport::can_modify_any_class() || JvmtiExport::can_walk_any_space()) {
duke@435 419 fail_continue("Tool agent requires sharing to be disabled.");
duke@435 420 return false;
duke@435 421 }
duke@435 422
duke@435 423 if (!open_for_read()) {
duke@435 424 return false;
duke@435 425 }
duke@435 426
duke@435 427 init_from_file(_fd);
duke@435 428 if (!validate()) {
duke@435 429 return false;
duke@435 430 }
duke@435 431
duke@435 432 SharedReadOnlySize = _header._space[0]._capacity;
duke@435 433 SharedReadWriteSize = _header._space[1]._capacity;
duke@435 434 SharedMiscDataSize = _header._space[2]._capacity;
duke@435 435 SharedMiscCodeSize = _header._space[3]._capacity;
duke@435 436 return true;
duke@435 437 }
duke@435 438
duke@435 439
duke@435 440 bool FileMapInfo::validate() {
duke@435 441 if (_header._version != current_version()) {
duke@435 442 fail_continue("The shared archive file is the wrong version.");
duke@435 443 return false;
duke@435 444 }
duke@435 445 if (_header._magic != (int)0xf00baba2) {
duke@435 446 fail_continue("The shared archive file has a bad magic number.");
duke@435 447 return false;
duke@435 448 }
duke@435 449 if (strncmp(_header._jvm_ident, VM_Version::internal_vm_info_string(),
duke@435 450 JVM_IDENT_MAX-1) != 0) {
duke@435 451 fail_continue("The shared archive file was created by a different"
duke@435 452 " version or build of HotSpot.");
duke@435 453 return false;
duke@435 454 }
duke@435 455
duke@435 456 // Cannot verify interpreter yet, as it can only be created after the GC
duke@435 457 // heap has been initialized.
duke@435 458
duke@435 459 if (_header._num_jars >= JVM_SHARED_JARS_MAX) {
duke@435 460 fail_continue("Too many jar files to share.");
duke@435 461 return false;
duke@435 462 }
duke@435 463
duke@435 464 // Build checks on classpath and jar files
duke@435 465 int num_jars_now = 0;
duke@435 466 ClassPathEntry *cpe = ClassLoader::classpath_entry(0);
duke@435 467 for ( ; cpe != NULL; cpe = cpe->next()) {
duke@435 468
duke@435 469 if (cpe->is_jar_file()) {
duke@435 470 if (num_jars_now < _header._num_jars) {
duke@435 471
duke@435 472 // Jar file - verify timestamp and file size.
duke@435 473 struct stat st;
duke@435 474 const char *path = cpe->name();
duke@435 475 if (os::stat(path, &st) != 0) {
duke@435 476 fail_continue("Unable to open jar file %s.", path);
duke@435 477 return false;
duke@435 478 }
duke@435 479 if (_header._jar[num_jars_now]._timestamp != st.st_mtime ||
duke@435 480 _header._jar[num_jars_now]._filesize != st.st_size) {
duke@435 481 fail_continue("A jar file is not the one used while building"
duke@435 482 " the shared archive file.");
duke@435 483 return false;
duke@435 484 }
duke@435 485 }
duke@435 486 ++num_jars_now;
duke@435 487 } else {
duke@435 488
duke@435 489 // If directories appear in boot classpath, they must be empty to
duke@435 490 // avoid having to verify each individual class file.
duke@435 491 const char* name = ((ClassPathDirEntry*)cpe)->name();
duke@435 492 if (!os::dir_is_empty(name)) {
duke@435 493 fail_continue("Boot classpath directory %s is not empty.", name);
duke@435 494 return false;
duke@435 495 }
duke@435 496 }
duke@435 497 }
duke@435 498 if (num_jars_now < _header._num_jars) {
duke@435 499 fail_continue("The number of jar files in the boot classpath is"
duke@435 500 " less than the number the shared archive was created with.");
duke@435 501 return false;
duke@435 502 }
duke@435 503
duke@435 504 return true;
duke@435 505 }
duke@435 506
duke@435 507 // The following method is provided to see whether a given pointer
duke@435 508 // falls in the mapped shared space.
duke@435 509 // Param:
duke@435 510 // p, The given pointer
duke@435 511 // Return:
duke@435 512 // True if the p is within the mapped shared space, otherwise, false.
duke@435 513 bool FileMapInfo::is_in_shared_space(const void* p) {
duke@435 514 for (int i = 0; i < CompactingPermGenGen::n_regions; i++) {
duke@435 515 if (p >= _header._space[i]._base &&
duke@435 516 p < _header._space[i]._base + _header._space[i]._used) {
duke@435 517 return true;
duke@435 518 }
duke@435 519 }
duke@435 520
duke@435 521 return false;
duke@435 522 }

mercurial