src/share/vm/memory/filemap.cpp

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

mercurial