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 default: 102 ShouldNotReachHere(); 103 } 104 } 105 106 bool SharedPathsMiscInfo::check() { 107 // The whole buffer must be 0 terminated so that we can use strlen and strcmp 108 // without fear. 109 _end_ptr -= sizeof(jint); 110 if (_cur_ptr >= _end_ptr) { 111 return fail("Truncated archive file header"); 112 } 113 if (*_end_ptr != 0) { 114 return fail("Corrupted archive file header"); 115 } 116 117 while (_cur_ptr < _end_ptr) { 118 jint type; 119 const char* path = _cur_ptr; 120 _cur_ptr += strlen(path) + 1; 121 if (!read_jint(&type)) { 122 return fail("Corrupted archive file header"); 123 } 124 LogTarget(Info, class, path) lt; 125 if (lt.is_enabled()) { 126 lt.print("type=%s ", type_name(type)); 127 LogStream ls(lt); 128 print_path(&ls, type, path); 129 ls.cr(); 130 } 131 if (!check(type, path)) { 132 if (!PrintSharedArchiveAndExit) { 133 return false; 134 } 135 } else { 136 ClassLoader::trace_class_path("ok"); 137 } 138 } 139 140 return true; 141 } 142 143 bool SharedPathsMiscInfo::check(jint type, const char* path) { 144 switch (type) { 145 case BOOT_PATH: 146 // In the future we should perform the check based on the content of the mapped archive. 147 if (os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) { 148 return fail("[BOOT classpath mismatch, actual =", Arguments::get_sysclasspath()); 149 } 150 break; 151 case NON_EXIST: 152 { 153 struct stat st; 154 if (os::stat(path, &st) == 0) { 155 // The file actually exists 156 // But we want it to not exist -> fail 157 return fail("File must not exist"); 158 } 159 } 160 break; 161 case APP_PATH: 162 { 163 // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar 164 size_t len = strlen(path); 165 const char *appcp = Arguments::get_appclasspath(); 166 assert(appcp != NULL, "NULL app classpath"); 167 size_t appcp_len = strlen(appcp); 168 if (appcp_len < len) { 169 return fail("Run time APP classpath is shorter than the one at dump time: ", appcp); 170 } 171 ResourceMark rm; 172 char* tmp_path; 173 if (len == appcp_len) { 174 tmp_path = (char*)appcp; 175 } else { 176 tmp_path = NEW_RESOURCE_ARRAY(char, len + 1); 177 strncpy(tmp_path, appcp, len); 178 tmp_path[len] = 0; 179 } 180 if (os::file_name_strcmp(path, tmp_path) != 0) { 181 return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp); 182 } 183 if (appcp[len] != '\0' && appcp[len] != os::path_separator()[0]) { 184 return fail("Dump time APP classpath is not a proper prefix of run time APP classpath: ", appcp); 185 } 186 } 187 break; 188 default: 189 return fail("Corrupted archive file header"); 190 } 191 192 return true; 193 }