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 "utilities/ostream.hpp" 36 37 SharedPathsMiscInfo::SharedPathsMiscInfo() { 38 _buf_size = INITIAL_BUF_SIZE; 39 _cur_ptr = _buf_start = NEW_C_HEAP_ARRAY(char, _buf_size, mtClass); 40 _allocated = true; 41 } 42 43 SharedPathsMiscInfo::~SharedPathsMiscInfo() { 44 if (_allocated) { 45 FREE_C_HEAP_ARRAY(char, _buf_start); 46 } 47 } 48 49 void SharedPathsMiscInfo::add_path(const char* path, int type) { 50 log_info(class, path)("type=%s ", type_name(type)); 51 ClassLoader::trace_class_path("add misc shared path ", path); 52 write(path, strlen(path) + 1); 53 write_jint(jint(type)); 54 } 55 56 void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) { 57 assert(_allocated, "cannot modify buffer during validation."); 58 int used = get_used_bytes(); 59 int target = used + int(needed_bytes); 60 if (target > _buf_size) { 61 _buf_size = _buf_size * 2 + (int)needed_bytes; 62 _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass); 63 _cur_ptr = _buf_start + used; 64 _end_ptr = _buf_start + _buf_size; 65 } 66 } 67 68 void SharedPathsMiscInfo::write(const void* ptr, size_t size) { 69 ensure_size(size); 70 memcpy(_cur_ptr, ptr, size); 71 _cur_ptr += size; 72 } 73 74 bool SharedPathsMiscInfo::read(void* ptr, size_t size) { 75 if (_cur_ptr + size <= _end_ptr) { 76 memcpy(ptr, _cur_ptr, size); 77 _cur_ptr += size; 78 return true; 79 } 80 return false; 81 } 82 83 bool SharedPathsMiscInfo::fail(const char* msg, const char* name) { 84 ClassLoader::trace_class_path(msg, name); 85 MetaspaceShared::set_archive_loading_failed(); 86 return false; 87 } 88 89 void SharedPathsMiscInfo::print_path(outputStream* out, int type, const char* path) { 90 switch (type) { 91 case BOOT: 92 out->print("Expecting BOOT path=%s", path); 93 break; 94 case NON_EXIST: 95 out->print("Expecting that %s does not exist", path); 96 break; 97 default: 98 ShouldNotReachHere(); 99 } 100 } 101 102 bool SharedPathsMiscInfo::check() { 103 // The whole buffer must be 0 terminated so that we can use strlen and strcmp 104 // without fear. 105 _end_ptr -= sizeof(jint); 106 if (_cur_ptr >= _end_ptr) { 107 return fail("Truncated archive file header"); 108 } 109 if (*_end_ptr != 0) { 110 return fail("Corrupted archive file header"); 111 } 112 113 while (_cur_ptr < _end_ptr) { 114 jint type; 115 const char* path = _cur_ptr; 116 _cur_ptr += strlen(path) + 1; 117 if (!read_jint(&type)) { 118 return fail("Corrupted archive file header"); 119 } 120 LogTarget(Info, class, path) lt; 121 if (lt.is_enabled()) { 122 lt.print("type=%s ", type_name(type)); 123 LogStream ls(lt); 124 print_path(&ls, type, path); 125 ls.cr(); 126 } 127 if (!check(type, path)) { 128 if (!PrintSharedArchiveAndExit) { 129 return false; 130 } 131 } else { 132 ClassLoader::trace_class_path("ok"); 133 } 134 } 135 136 return true; 137 } 138 139 bool SharedPathsMiscInfo::check(jint type, const char* path) { 140 switch (type) { 141 case BOOT: 142 // In the future we should perform the check based on the content of the mapped archive. 143 if (UseAppCDS && os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) { 144 return fail("[BOOT classpath mismatch, actual =", Arguments::get_sysclasspath()); 145 } 146 break; 147 case NON_EXIST: 148 { 149 struct stat st; 150 if (os::stat(path, &st) == 0) { 151 // The file actually exists 152 // But we want it to not exist -> fail 153 return fail("File must not exist"); 154 } 155 } 156 break; 157 default: 158 return fail("Corrupted archive file header"); 159 } 160 161 return true; 162 }