| 1 | /* |
| 2 | * Copyright (c) 2014, 2019, 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/filemap.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 | _app_offset = 0; |
| 40 | _buf_size = INITIAL_BUF_SIZE; |
| 41 | _cur_ptr = _buf_start = NEW_C_HEAP_ARRAY(char, _buf_size, mtClass); |
| 42 | _allocated = true; |
| 43 | } |
| 44 | |
| 45 | SharedPathsMiscInfo::~SharedPathsMiscInfo() { |
| 46 | if (_allocated) { |
| 47 | FREE_C_HEAP_ARRAY(char, _buf_start); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | void SharedPathsMiscInfo::add_path(const char* path, int type) { |
| 52 | log_info(class, path)("type=%s " , type_name(type)); |
| 53 | ClassLoader::trace_class_path("add misc shared path " , path); |
| 54 | write(path, strlen(path) + 1); |
| 55 | write_jint(jint(type)); |
| 56 | } |
| 57 | |
| 58 | void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) { |
| 59 | assert(_allocated, "cannot modify buffer during validation." ); |
| 60 | int used = get_used_bytes(); |
| 61 | int target = used + int(needed_bytes); |
| 62 | if (target > _buf_size) { |
| 63 | _buf_size = _buf_size * 2 + (int)needed_bytes; |
| 64 | _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass); |
| 65 | _cur_ptr = _buf_start + used; |
| 66 | _end_ptr = _buf_start + _buf_size; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | void SharedPathsMiscInfo::write(const void* ptr, size_t size) { |
| 71 | ensure_size(size); |
| 72 | memcpy(_cur_ptr, ptr, size); |
| 73 | _cur_ptr += size; |
| 74 | } |
| 75 | |
| 76 | bool SharedPathsMiscInfo::read(void* ptr, size_t size) { |
| 77 | if (_cur_ptr + size <= _end_ptr) { |
| 78 | memcpy(ptr, _cur_ptr, size); |
| 79 | _cur_ptr += size; |
| 80 | return true; |
| 81 | } |
| 82 | return false; |
| 83 | } |
| 84 | |
| 85 | bool SharedPathsMiscInfo::fail(const char* msg, const char* name) { |
| 86 | ClassLoader::trace_class_path(msg, name); |
| 87 | MetaspaceShared::set_archive_loading_failed(); |
| 88 | return false; |
| 89 | } |
| 90 | |
| 91 | void SharedPathsMiscInfo::print_path(outputStream* out, int type, const char* path) { |
| 92 | switch (type) { |
| 93 | case BOOT_PATH: |
| 94 | out->print("Expecting BOOT path=%s" , path); |
| 95 | break; |
| 96 | case NON_EXIST: |
| 97 | out->print("Expecting that %s does not exist" , path); |
| 98 | break; |
| 99 | case APP_PATH: |
| 100 | ClassLoader::trace_class_path("Expecting -Djava.class.path=" , path); |
| 101 | break; |
| 102 | default: |
| 103 | ShouldNotReachHere(); |
| 104 | } |
| 105 | } |
| 106 | |
| 107 | bool SharedPathsMiscInfo::check(bool is_static) { |
| 108 | // The whole buffer must be 0 terminated so that we can use strlen and strcmp |
| 109 | // without fear. |
| 110 | _end_ptr -= sizeof(jint); |
| 111 | if (_cur_ptr >= _end_ptr) { |
| 112 | return fail("Truncated archive file header" ); |
| 113 | } |
| 114 | if (*_end_ptr != 0) { |
| 115 | return fail("Corrupted archive file header" ); |
| 116 | } |
| 117 | |
| 118 | jshort cur_index = 0; |
| 119 | FileMapHeader* = is_static ? FileMapInfo::current_info()->header() : |
| 120 | FileMapInfo::dynamic_info()->header(); |
| 121 | jshort max_cp_index = header->max_used_path_index(); |
| 122 | jshort module_paths_start_index = header->app_module_paths_start_index(); |
| 123 | while (_cur_ptr < _end_ptr) { |
| 124 | jint type; |
| 125 | const char* path = _cur_ptr; |
| 126 | _cur_ptr += strlen(path) + 1; |
| 127 | |
| 128 | if (!read_jint(&type)) { |
| 129 | return fail("Corrupted archive file header" ); |
| 130 | } |
| 131 | LogTarget(Info, class, path) lt; |
| 132 | if (lt.is_enabled()) { |
| 133 | lt.print("type=%s " , type_name(type)); |
| 134 | LogStream ls(lt); |
| 135 | print_path(&ls, type, path); |
| 136 | ls.cr(); |
| 137 | } |
| 138 | // skip checking the class path(s) which was not referenced during CDS dump |
| 139 | if ((cur_index <= max_cp_index) || (cur_index >= module_paths_start_index)) { |
| 140 | if (!check(type, path, is_static)) { |
| 141 | if (!PrintSharedArchiveAndExit) { |
| 142 | return false; |
| 143 | } |
| 144 | } else { |
| 145 | ClassLoader::trace_class_path("ok" ); |
| 146 | } |
| 147 | } else { |
| 148 | ClassLoader::trace_class_path("skipped check" ); |
| 149 | } |
| 150 | cur_index++; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| 156 | char* skip_first_path_entry(const char* path) { |
| 157 | size_t path_sep_len = strlen(os::path_separator()); |
| 158 | char* p = strstr((char*)path, os::path_separator()); |
| 159 | if (p != NULL) { |
| 160 | debug_only( { |
| 161 | size_t image_name_len = strlen(MODULES_IMAGE_NAME); |
| 162 | assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0, |
| 163 | "first entry must be the modules image" ); |
| 164 | } ); |
| 165 | p += path_sep_len; |
| 166 | } else { |
| 167 | debug_only( { |
| 168 | assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME), |
| 169 | "first entry must be the modules image" ); |
| 170 | } ); |
| 171 | } |
| 172 | return p; |
| 173 | } |
| 174 | |
| 175 | bool SharedPathsMiscInfo::check(jint type, const char* path, bool is_static) { |
| 176 | assert(UseSharedSpaces, "runtime only" ); |
| 177 | switch (type) { |
| 178 | case BOOT_PATH: |
| 179 | { |
| 180 | // |
| 181 | // - Archive contains boot classes only - relaxed boot path check: |
| 182 | // Extra path elements appended to the boot path at runtime are allowed. |
| 183 | // |
| 184 | // - Archive contains application or platform classes - strict boot path check: |
| 185 | // Validate the entire runtime boot path, which must be compactible |
| 186 | // with the dump time boot path. Appending boot path at runtime is not |
| 187 | // allowed. |
| 188 | // |
| 189 | |
| 190 | // The first entry in boot path is the modules_image (guaranteed by |
| 191 | // ClassLoader::setup_boot_search_path()). Skip the first entry. The |
| 192 | // path of the runtime modules_image may be different from the dump |
| 193 | // time path (e.g. the JDK image is copied to a different location |
| 194 | // after generating the shared archive), which is acceptable. For most |
| 195 | // common cases, the dump time boot path might contain modules_image only. |
| 196 | char* runtime_boot_path = Arguments::get_sysclasspath(); |
| 197 | char* rp = skip_first_path_entry(runtime_boot_path); |
| 198 | char* dp = skip_first_path_entry(path); |
| 199 | |
| 200 | bool relaxed_check = is_static ? |
| 201 | !FileMapInfo::current_info()->header()->has_platform_or_app_classes() : |
| 202 | !FileMapInfo::dynamic_info()->header()->has_platform_or_app_classes(); |
| 203 | if (dp == NULL && rp == NULL) { |
| 204 | break; // ok, both runtime and dump time boot paths have modules_images only |
| 205 | } else if (dp == NULL && rp != NULL && relaxed_check) { |
| 206 | break; // ok, relaxed check, runtime has extra boot append path entries |
| 207 | } else if (dp != NULL && rp != NULL) { |
| 208 | size_t num; |
| 209 | size_t dp_len = strlen(dp); |
| 210 | size_t rp_len = strlen(rp); |
| 211 | if (rp_len >= dp_len) { |
| 212 | if (relaxed_check) { |
| 213 | // only check the leading entries in the runtime boot path, up to |
| 214 | // the length of the dump time boot path |
| 215 | num = dp_len; |
| 216 | } else { |
| 217 | // check the full runtime boot path, must match with dump time |
| 218 | num = rp_len; |
| 219 | } |
| 220 | |
| 221 | if (os::file_name_strncmp(dp, rp, num) == 0) { |
| 222 | // make sure it is the end of an entry in the runtime boot path |
| 223 | if (rp[dp_len] == '\0' || rp[dp_len] == os::path_separator()[0]) { |
| 224 | break; // ok, runtime and dump time paths match |
| 225 | } |
| 226 | } |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | // The paths are different |
| 231 | return fail("[BOOT classpath mismatch, actual =" , runtime_boot_path); |
| 232 | } |
| 233 | break; |
| 234 | case NON_EXIST: |
| 235 | { |
| 236 | struct stat st; |
| 237 | if (os::stat(path, &st) == 0) { |
| 238 | // The file actually exists |
| 239 | // But we want it to not exist -> fail |
| 240 | return fail("File must not exist" ); |
| 241 | } |
| 242 | } |
| 243 | break; |
| 244 | case APP_PATH: |
| 245 | { |
| 246 | size_t len = strlen(path); |
| 247 | const char *appcp = Arguments::get_appclasspath(); |
| 248 | assert(appcp != NULL, "NULL app classpath" ); |
| 249 | size_t appcp_len = strlen(appcp); |
| 250 | if (appcp_len < len) { |
| 251 | return fail("Run time APP classpath is shorter than the one at dump time: " , appcp); |
| 252 | } |
| 253 | // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar. |
| 254 | if (os::file_name_strncmp(path, appcp, len) != 0) { |
| 255 | return fail("[APP classpath mismatch, actual: -Djava.class.path=" , appcp); |
| 256 | } |
| 257 | if (appcp[len] != '\0' && appcp[len] != os::path_separator()[0]) { |
| 258 | return fail("Dump time APP classpath is not a proper prefix of run time APP classpath: " , appcp); |
| 259 | } |
| 260 | } |
| 261 | break; |
| 262 | default: |
| 263 | return fail("Corrupted archive file header" ); |
| 264 | } |
| 265 | |
| 266 | return true; |
| 267 | } |
| 268 | |