1 /* 2 * Copyright (c) 2014, 2017, 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 "logging/logStream.hpp" 31 #include "memory/allocation.inline.hpp" 32 #include "memory/metaspaceShared.hpp" 33 #include "memory/resourceArea.hpp" 34 #include "runtime/arguments.hpp" 35 #include "runtime/os.inline.hpp" 36 #include "utilities/ostream.hpp" 37 38 SharedPathsMiscInfo::SharedPathsMiscInfo() { 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: 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 default: 99 ShouldNotReachHere(); 100 } 101 } 102 103 bool SharedPathsMiscInfo::check() { 104 // The whole buffer must be 0 terminated so that we can use strlen and strcmp 105 // without fear. 106 _end_ptr -= sizeof(jint); 107 if (_cur_ptr >= _end_ptr) { 108 return fail("Truncated archive file header"); 109 } 110 if (*_end_ptr != 0) { 111 return fail("Corrupted archive file header"); 112 } 113 114 while (_cur_ptr < _end_ptr) { 115 jint type; 116 const char* path = _cur_ptr; 117 _cur_ptr += strlen(path) + 1; 118 if (!read_jint(&type)) { 119 return fail("Corrupted archive file header"); 120 } 121 LogTarget(Info, class, path) lt; 122 if (lt.is_enabled()) { 123 lt.print("type=%s ", type_name(type)); 124 LogStream ls(lt); 125 print_path(&ls, type, path); 126 ls.cr(); 127 } 128 if (!check(type, path)) { 129 if (!PrintSharedArchiveAndExit) { 130 return false; 131 } 132 } else { 133 ClassLoader::trace_class_path("ok"); 134 } 135 } 136 137 return true; 138 } 139 140 bool SharedPathsMiscInfo::check(jint type, const char* path) { 141 switch (type) { 142 case BOOT: 143 // In the future we should perform the check based on the content of the mapped archive. 144 if (UseAppCDS && os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) { 145 return fail("[BOOT classpath mismatch, actual =", Arguments::get_sysclasspath()); 146 } 147 break; 148 case NON_EXIST: 149 { 150 struct stat st; 151 if (os::stat(path, &st) == 0) { 152 // The file actually exists 153 // But we want it to not exist -> fail 154 return fail("File must not exist"); 155 } 156 } 157 break; 158 default: 159 return fail("Corrupted archive file header"); 160 } 161 162 return true; 163 }