comparison src/share/vm/memory/filemap.cpp @ 20730:8210e5f2e21b

8044269: Analysis of archive files. Summary: Add checksum verification. Reviewed-by: iklam, dholmes, mschoene
author jiangli
date Tue, 12 Aug 2014 17:46:16 -0400
parents 78bbf4d43a14
children 8cb56c8cb30d
comparison
equal deleted inserted replaced
20729:609faa407cfd 20730:8210e5f2e21b
175 } 175 }
176 if (_header._version != current_version()) { 176 if (_header._version != current_version()) {
177 fail_continue("The shared archive file has the wrong version."); 177 fail_continue("The shared archive file has the wrong version.");
178 return false; 178 return false;
179 } 179 }
180 _file_offset = (long)n; 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;
181 return true; 188 return true;
182 } 189 }
183 190
184 191
185 // Read the FileMapInfo information from the file. 192 // Read the FileMapInfo information from the file.
266 si->_base = base; 273 si->_base = base;
267 si->_used = size; 274 si->_used = size;
268 si->_capacity = capacity; 275 si->_capacity = capacity;
269 si->_read_only = read_only; 276 si->_read_only = read_only;
270 si->_allow_exec = allow_exec; 277 si->_allow_exec = allow_exec;
278 si->_crc = ClassLoader::crc32(0, base, (jint)size);
271 write_bytes_aligned(base, (int)size); 279 write_bytes_aligned(base, (int)size);
272 } 280 }
273 281
274 282
275 // Dump bytes to file -- at the current file position. 283 // Dump bytes to file -- at the current file position.
290 298
291 299
292 // Align file position to an allocation unit boundary. 300 // Align file position to an allocation unit boundary.
293 301
294 void FileMapInfo::align_file_position() { 302 void FileMapInfo::align_file_position() {
295 long new_file_offset = align_size_up(_file_offset, os::vm_allocation_granularity()); 303 size_t new_file_offset = align_size_up(_file_offset,
304 os::vm_allocation_granularity());
296 if (new_file_offset != _file_offset) { 305 if (new_file_offset != _file_offset) {
297 _file_offset = new_file_offset; 306 _file_offset = new_file_offset;
298 if (_file_open) { 307 if (_file_open) {
299 // Seek one byte back from the target and write a byte to insure 308 // Seek one byte back from the target and write a byte to insure
300 // that the written file is the correct length. 309 // that the written file is the correct length.
301 _file_offset -= 1; 310 _file_offset -= 1;
302 if (lseek(_fd, _file_offset, SEEK_SET) < 0) { 311 if (lseek(_fd, (long)_file_offset, SEEK_SET) < 0) {
303 fail_stop("Unable to seek.", NULL); 312 fail_stop("Unable to seek.", NULL);
304 } 313 }
305 char zero = 0; 314 char zero = 0;
306 write_bytes(&zero, 1); 315 write_bytes(&zero, 1);
307 } 316 }
404 MemTracker::record_virtual_memory_type((address)base, mtClassShared); 413 MemTracker::record_virtual_memory_type((address)base, mtClassShared);
405 #endif 414 #endif
406 return base; 415 return base;
407 } 416 }
408 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 }
409 431
410 // Unmap a memory region in the address space. 432 // Unmap a memory region in the address space.
411 433
412 void FileMapInfo::unmap_region(int i) { 434 void FileMapInfo::unmap_region(int i) {
413 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i]; 435 struct FileMapInfo::FileMapHeader::space_info* si = &_header._space[i];
455 SharedMiscDataSize = _header._space[2]._capacity; 477 SharedMiscDataSize = _header._space[2]._capacity;
456 SharedMiscCodeSize = _header._space[3]._capacity; 478 SharedMiscCodeSize = _header._space[3]._capacity;
457 return true; 479 return true;
458 } 480 }
459 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 }
460 490
461 bool FileMapInfo::validate() { 491 bool FileMapInfo::validate() {
492 if (VerifySharedSpaces && compute_header_crc() != _header._crc) {
493 fail_continue("Header checksum verification failed.");
494 return false;
495 }
462 if (_header._version != current_version()) { 496 if (_header._version != current_version()) {
463 fail_continue("The shared archive file is the wrong version."); 497 fail_continue("The shared archive file is the wrong version.");
464 return false; 498 return false;
465 } 499 }
466 if (_header._magic != (int)0xf00baba2) { 500 if (_header._magic != (int)0xf00baba2) {