1 /* 2 * Copyright (c) 2014, 2016, 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/classLoaderData.inline.hpp" 28 #include "classfile/sharedPathsMiscInfo.hpp" 29 #include "logging/log.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/metaspaceShared.hpp" 32 #include "runtime/arguments.hpp" 33 #include "utilities/ostream.hpp" 34 35 void SharedPathsMiscInfo::add_path(const char* path, int type) { 36 log_info(classpath)("type=%s ", type_name(type)); 37 ClassLoader::trace_class_path("add misc shared path ", path); 38 write(path, strlen(path) + 1); 39 write_jint(jint(type)); 40 } 41 42 void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) { 43 assert(_allocated, "cannot modify buffer during validation."); 44 int used = get_used_bytes(); 45 int target = used + int(needed_bytes); 46 if (target > _buf_size) { 47 _buf_size = _buf_size * 2 + (int)needed_bytes; 48 _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass); 49 _cur_ptr = _buf_start + used; 50 _end_ptr = _buf_start + _buf_size; 51 } 52 } 53 54 void SharedPathsMiscInfo::write(const void* ptr, size_t size) { 55 ensure_size(size); 56 memcpy(_cur_ptr, ptr, size); 57 _cur_ptr += size; 58 } 59 60 bool SharedPathsMiscInfo::read(void* ptr, size_t size) { 61 if (_cur_ptr + size <= _end_ptr) { 62 memcpy(ptr, _cur_ptr, size); 63 _cur_ptr += size; 64 return true; 65 } 66 return false; 67 } 68 69 bool SharedPathsMiscInfo::fail(const char* msg, const char* name) { 70 ClassLoader::trace_class_path(msg, name); 71 MetaspaceShared::set_archive_loading_failed(); 72 return false; 73 } 74 75 void SharedPathsMiscInfo::print_path(int type, const char* path) { 76 ResourceMark rm; 77 outputStream* out = LogHandle(classpath)::info_stream(); 78 switch (type) { 79 case BOOT: 80 out->print("Expecting -Dsun.boot.class.path=%s", path); 81 break; 82 case NON_EXIST: 83 out->print("Expecting that %s does not exist", path); 84 break; 85 case REQUIRED: 86 out->print("Expecting that file %s must exist and is not altered", path); 87 break; 88 default: 89 ShouldNotReachHere(); 90 } 91 } 92 93 bool SharedPathsMiscInfo::check() { 94 // The whole buffer must be 0 terminated so that we can use strlen and strcmp 95 // without fear. 96 _end_ptr -= sizeof(jint); 97 if (_cur_ptr >= _end_ptr) { 98 return fail("Truncated archive file header"); 99 } 100 if (*_end_ptr != 0) { 101 return fail("Corrupted archive file header"); 102 } 103 104 while (_cur_ptr < _end_ptr) { 105 jint type; 106 const char* path = _cur_ptr; 107 _cur_ptr += strlen(path) + 1; 108 if (!read_jint(&type)) { 109 return fail("Corrupted archive file header"); 110 } 111 log_info(classpath)("type=%s ", type_name(type)); 112 print_path(type, path); 113 if (!check(type, path)) { 114 if (!PrintSharedArchiveAndExit) { 115 return false; 116 } 117 } else { 118 ClassLoader::trace_class_path("ok"); 119 } 120 } 121 122 return true; 123 } 124 125 bool SharedPathsMiscInfo::check(jint type, const char* path) { 126 switch (type) { 127 case BOOT: 128 if (os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) { 129 return fail("[BOOT classpath mismatch, actual: -Dsun.boot.class.path=", Arguments::get_sysclasspath()); 130 } 131 break; 132 case NON_EXIST: // fall-through 133 case REQUIRED: 134 { 135 struct stat st; 136 if (os::stat(path, &st) != 0) { 137 // The file does not actually exist 138 if (type == REQUIRED) { 139 // but we require it to exist -> fail 140 return fail("Required file doesn't exist"); 141 } 142 } else { 143 // The file actually exists 144 if (type == NON_EXIST) { 145 // But we want it to not exist -> fail 146 return fail("File must not exist"); 147 } 148 time_t timestamp; 149 long filesize; 150 151 if (!read_time(×tamp) || !read_long(&filesize)) { 152 return fail("Corrupted archive file header"); 153 } 154 if (timestamp != st.st_mtime) { 155 return fail("Timestamp mismatch"); 156 } 157 if (filesize != st.st_size) { 158 return fail("File size mismatch"); 159 } 160 } 161 } 162 break; 163 164 default: 165 return fail("Corrupted archive file header"); 166 } 167 168 return true; 169 }