annotate src/hotspot/share/prims/cdsoffsets.cpp @ 55694:7b7df2be6219

8226406: JVM fails to detect mismatched or corrupt CDS archive Summary: Check important archive header fields such as _jvm_ident before processing other fields. Reviewed-by: iklam, jiangli
author ccheung
date Fri, 12 Jul 2019 08:40:37 -0700
parents 1512d88b24c6
children 5ddb746d45e0
rev   line source
iklam@48138 1 /*
ccheung@54927 2 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
iklam@48138 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
iklam@48138 4 *
iklam@48138 5 * This code is free software; you can redistribute it and/or modify it
iklam@48138 6 * under the terms of the GNU General Public License version 2 only, as
iklam@48138 7 * published by the Free Software Foundation.
iklam@48138 8 *
iklam@48138 9 * This code is distributed in the hope that it will be useful, but WITHOUT
iklam@48138 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
iklam@48138 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
iklam@48138 12 * version 2 for more details (a copy is included in the LICENSE file that
iklam@48138 13 * accompanied this code).
iklam@48138 14 *
iklam@48138 15 * You should have received a copy of the GNU General Public License version
iklam@48138 16 * 2 along with this work; if not, write to the Free Software Foundation,
iklam@48138 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
iklam@48138 18 *
iklam@48138 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
iklam@48138 20 * or visit www.oracle.com if you need additional information or have any
iklam@48138 21 * questions.
iklam@48138 22 *
iklam@48138 23 */
iklam@48138 24
iklam@48138 25 #include "precompiled.hpp"
iklam@48138 26 #include "utilities/macros.hpp"
iklam@48138 27 #if INCLUDE_CDS
iklam@48138 28 #include "runtime/os.hpp"
ccheung@54927 29 #include "memory/dynamicArchive.hpp"
iklam@48138 30 #include "memory/filemap.hpp"
iklam@48138 31 #include "memory/allocation.hpp"
iklam@48138 32 #include "memory/allocation.inline.hpp"
iklam@48138 33 #include "prims/cdsoffsets.hpp"
iklam@48138 34
stefank@49360 35 CDSOffsets::CDSOffsets(const char* name, int offset, CDSOffsets* next) {
stefank@49360 36 _name = NEW_C_HEAP_ARRAY(char, strlen(name) + 1, mtInternal);
stefank@49360 37 strcpy(_name, name);
stefank@49360 38 _offset = offset;
stefank@49360 39 _next = next;
stefank@49360 40 }
stefank@49360 41
iklam@48138 42 CDSOffsets* CDSOffsets::_all = NULL;
iklam@48138 43 #define ADD_NEXT(list, name, value) \
iklam@48138 44 list->add_end(new CDSOffsets(name, value, NULL))
iklam@48138 45
iklam@51477 46 #define CREATE_OFFSET_MAPS \
iklam@51477 47 _all = new CDSOffsets("size_t_size", sizeof(size_t), NULL); \
ccheung@54927 48 ADD_NEXT(_all, "int_size", sizeof(int)); \
iklam@51477 49 ADD_NEXT(_all, "FileMapHeader::_magic", offset_of(FileMapHeader, _magic)); \
iklam@51477 50 ADD_NEXT(_all, "FileMapHeader::_crc", offset_of(FileMapHeader, _crc)); \
iklam@51477 51 ADD_NEXT(_all, "FileMapHeader::_version", offset_of(FileMapHeader, _version)); \
ccheung@55694 52 ADD_NEXT(_all, "FileMapHeader::_jvm_ident", offset_of(FileMapHeader, _jvm_ident)); \
iklam@51477 53 ADD_NEXT(_all, "FileMapHeader::_space[0]", offset_of(FileMapHeader, _space)); \
iklam@51477 54 ADD_NEXT(_all, "CDSFileMapRegion::_crc", offset_of(CDSFileMapRegion, _crc)); \
iklam@51477 55 ADD_NEXT(_all, "CDSFileMapRegion::_used", offset_of(CDSFileMapRegion, _used)); \
iklam@51477 56 ADD_NEXT(_all, "FileMapHeader::_paths_misc_info_size", offset_of(FileMapHeader, _paths_misc_info_size)); \
iklam@51477 57 ADD_NEXT(_all, "file_header_size", sizeof(FileMapHeader)); \
ccheung@54927 58 ADD_NEXT(_all, "DynamicArchiveHeader::_base_archive_crc", offset_of(DynamicArchiveHeader, _base_archive_crc)); \
iklam@51477 59 ADD_NEXT(_all, "CDSFileMapRegion_size", sizeof(CDSFileMapRegion));
iklam@48138 60
iklam@48138 61 int CDSOffsets::find_offset(const char* name) {
iklam@48138 62 if (_all == NULL) {
iklam@48138 63 CREATE_OFFSET_MAPS
iklam@48138 64 }
iklam@48138 65 CDSOffsets* it = _all;
iklam@48138 66 while(it) {
iklam@48138 67 if (!strcmp(name, it->get_name())) {
iklam@48138 68 return it->get_offset();
iklam@48138 69 }
iklam@48138 70 it = it->next();
iklam@48138 71 }
iklam@48138 72 return -1; // not found
iklam@48138 73 }
iklam@48138 74
iklam@48138 75 void CDSOffsets::add_end(CDSOffsets* n) {
iklam@48138 76 CDSOffsets* p = this;
iklam@48138 77 while(p && p->_next) { p = p->_next; }
iklam@48138 78 p->_next = n;
iklam@48138 79 }
iklam@48138 80 #endif // INCLUDE_CDS