1 /* 2 * Copyright (c) 2014, 2018, 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/sharedPathsMiscInfo.hpp" 28 #include "logging/log.hpp" 29 #include "logging/logStream.hpp" 30 #include "memory/allocation.inline.hpp" 31 #include "memory/metaspaceShared.hpp" 32 #include "memory/resourceArea.hpp" 33 #include "runtime/arguments.hpp" 34 #include "runtime/os.inline.hpp" 35 #include "utilities/ostream.hpp" 36 37 SharedPathsMiscInfo::SharedPathsMiscInfo() { 38 _app_offset = 0; 39 _buf_size = INITIAL_BUF_SIZE; 40 _cur_ptr = _buf_start = NEW_C_HEAP_ARRAY(char, _buf_size, mtClass); 41 _allocated = true; 42 } 43 44 SharedPathsMiscInfo::~SharedPathsMiscInfo() { 45 if (_allocated) { 46 FREE_C_HEAP_ARRAY(char, _buf_start); 47 } 48 } 49 50 void SharedPathsMiscInfo::add_path(const char* path, int type) { 51 log_info(class, path)("type=%s ", type_name(type)); 52 ClassLoader::trace_class_path("add misc shared path ", path); 53 write(path, strlen(path) + 1); 54 write_jint(jint(type)); 55 } 56 57 void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) { 58 assert(_allocated, "cannot modify buffer during validation."); 59 int used = get_used_bytes(); 60 int target = used + int(needed_bytes); 61 if (target > _buf_size) { 62 _buf_size = _buf_size * 2 + (int)needed_bytes; 63 _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass); 64 _cur_ptr = _buf_start + used; 65 _end_ptr = _buf_start + _buf_size; 66 } 67 } 68 69 void SharedPathsMiscInfo::write(const void* ptr, size_t size) { 70 ensure_size(size); 71 memcpy(_cur_ptr, ptr, size); 72 _cur_ptr += size; 73 } 74 75 bool SharedPathsMiscInfo::read(void* ptr, size_t size) { 76 if (_cur_ptr + size <= _end_ptr) { 77 memcpy(ptr, _cur_ptr, size); 78 _cur_ptr += size; 79 return true; 80 } 81 return false; 82 } 83 84 bool SharedPathsMiscInfo::fail(const char* msg, const char* name) { 85 ClassLoader::trace_class_path(msg, name); 86 MetaspaceShared::set_archive_loading_failed(); 87 return false; 88 } 89 90 void SharedPathsMiscInfo::print_path(outputStream* out, int type, const char* path) { 91 switch (type) { 92 case BOOT_PATH: 93 out->print("Expecting BOOT path=%s", path); 94 break; 95 case NON_EXIST: 96 out->print("Expecting that %s does not exist", path); 97 break; 98 case APP_PATH: 99 ClassLoader::trace_class_path("Expecting -Djava.class.path=", path); 100 break; 101 case MODULE_PATH: 102 ClassLoader::trace_class_path("Checking module path: ", path); 103 break; 104 default: 105 ShouldNotReachHere(); 106 } 107 } 108 109 bool SharedPathsMiscInfo::check() { 110 // The whole buffer must be 0 terminated so that we can use strlen and strcmp 111 // without fear. 112 _end_ptr -= sizeof(jint); 113 if (_cur_ptr >= _end_ptr) { 114 return fail("Truncated archive file header"); 115 } 116 if (*_end_ptr != 0) { 117 return fail("Corrupted archive file header"); 118 } 119 120 while (_cur_ptr < _end_ptr) { 121 jint type; 122 const char* path = _cur_ptr; 123 _cur_ptr += strlen(path) + 1; 124 if (!read_jint(&type)) { 125 return fail("Corrupted archive file header"); 126 } 127 LogTarget(Info, class, path) lt; 128 if (lt.is_enabled()) { 129 lt.print("type=%s ", type_name(type)); 130 LogStream ls(lt); 131 print_path(&ls, type, path); 132 ls.cr(); 133 } 134 if (!check(type, path)) { 135 if (!PrintSharedArchiveAndExit) { 136 return false; 137 } 138 } else { 139 ClassLoader::trace_class_path("ok"); 140 } 141 } 142 143 return true; 144 } 145 146 bool SharedPathsMiscInfo::check(jint type, const char* path) { 147 switch (type) { 148 case BOOT_PATH: 149 // In the future we should perform the check based on the content of the mapped archive. 150 if (os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) { 151 return fail("[BOOT classpath mismatch, actual =", Arguments::get_sysclasspath()); 152 } 153 break; 154 case NON_EXIST: 155 { 156 struct stat st; 157 if (os::stat(path, &st) == 0) { 158 // The file actually exists 159 // But we want it to not exist -> fail 160 return fail("File must not exist"); 161 } 162 } 163 break; 164 case APP_PATH: 165 { 166 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar 167 size_t len = strlen(path); 168 const char *appcp = Arguments::get_appclasspath(); 169 assert(appcp != NULL, "NULL app classpath"); 170 size_t appcp_len = strlen(appcp); 171 if (appcp_len < len) { 172 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp); 173 } 174 ResourceMark rm; 175 char* tmp_path; 176 if (len == appcp_len) { 177 tmp_path = (char*)appcp; 178 } else { 179 tmp_path = NEW_RESOURCE_ARRAY(char, len + 1); 180 strncpy(tmp_path, appcp, len); 181 tmp_path[len] = 0; 182 } 183 if (os::file_name_strcmp(path, tmp_path) != 0) { 184 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp); 185 } 186 if (appcp[len] != '\0' && appcp[len] != os::path_separator()[0]) { 187 return fail("Dump time APP classpath is not a proper prefix of run time APP classpath: ", appcp); 188 } 189 } 190 break; 191 case MODULE_PATH: 192 // The path check for archived module classes is handled differently than the classes 193 // from the -cp or -Xbootclasspath/a because we can quickly determine if an archived 194 // module class's origin matches with the runtime module source path (see 195 // SystemDictionaryShared::is_shared_class_visible_for_classloader()). No need to 196 // do strict string matching here. 197 ShouldNotReachHere(); 198 default: 199 return fail("Corrupted archive file header"); 200 } 201 202 return true; 203 }