iklam@7089: /* ccheung@8184: * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved. iklam@7089: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. iklam@7089: * iklam@7089: * This code is free software; you can redistribute it and/or modify it iklam@7089: * under the terms of the GNU General Public License version 2 only, as iklam@7089: * published by the Free Software Foundation. iklam@7089: * iklam@7089: * This code is distributed in the hope that it will be useful, but WITHOUT iklam@7089: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or iklam@7089: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License iklam@7089: * version 2 for more details (a copy is included in the LICENSE file that iklam@7089: * accompanied this code). iklam@7089: * iklam@7089: * You should have received a copy of the GNU General Public License version iklam@7089: * 2 along with this work; if not, write to the Free Software Foundation, iklam@7089: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. iklam@7089: * iklam@7089: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA iklam@7089: * or visit www.oracle.com if you need additional information or have any iklam@7089: * questions. iklam@7089: * iklam@7089: */ iklam@7089: iklam@7089: #include "precompiled.hpp" iklam@7089: #include "classfile/classLoader.hpp" iklam@7089: #include "classfile/classLoaderData.inline.hpp" iklam@7089: #include "classfile/sharedPathsMiscInfo.hpp" iklam@7089: #include "memory/allocation.inline.hpp" iklam@7089: #include "memory/metaspaceShared.hpp" iklam@7089: #include "runtime/arguments.hpp" iklam@7089: iklam@7089: void SharedPathsMiscInfo::add_path(const char* path, int type) { iklam@7089: if (TraceClassPaths) { iklam@7089: tty->print("[type=%s] ", type_name(type)); iklam@7089: trace_class_path("[Add misc shared path ", path); iklam@7089: } iklam@7089: write(path, strlen(path) + 1); iklam@7089: write_jint(jint(type)); iklam@7089: } iklam@7089: iklam@7089: void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) { iklam@7089: assert(_allocated, "cannot modify buffer during validation."); iklam@7089: int used = get_used_bytes(); iklam@7089: int target = used + int(needed_bytes); iklam@7089: if (target > _buf_size) { iklam@7089: _buf_size = _buf_size * 2 + (int)needed_bytes; iklam@7089: _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass); iklam@7089: _cur_ptr = _buf_start + used; iklam@7089: _end_ptr = _buf_start + _buf_size; iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: void SharedPathsMiscInfo::write(const void* ptr, size_t size) { iklam@7089: ensure_size(size); iklam@7089: memcpy(_cur_ptr, ptr, size); iklam@7089: _cur_ptr += size; iklam@7089: } iklam@7089: iklam@7089: bool SharedPathsMiscInfo::read(void* ptr, size_t size) { iklam@7089: if (_cur_ptr + size <= _end_ptr) { iklam@7089: memcpy(ptr, _cur_ptr, size); iklam@7089: _cur_ptr += size; iklam@7089: return true; iklam@7089: } iklam@7089: return false; iklam@7089: } iklam@7089: iklam@7089: bool SharedPathsMiscInfo::fail(const char* msg, const char* name) { ccheung@8184: ClassLoader::trace_class_path(tty, msg, name); iklam@7089: MetaspaceShared::set_archive_loading_failed(); iklam@7089: return false; iklam@7089: } iklam@7089: iklam@7089: bool SharedPathsMiscInfo::check() { iklam@7089: // The whole buffer must be 0 terminated so that we can use strlen and strcmp iklam@7089: // without fear. iklam@7089: _end_ptr -= sizeof(jint); iklam@7089: if (_cur_ptr >= _end_ptr) { iklam@7089: return fail("Truncated archive file header"); iklam@7089: } iklam@7089: if (*_end_ptr != 0) { iklam@7089: return fail("Corrupted archive file header"); iklam@7089: } iklam@7089: iklam@7089: while (_cur_ptr < _end_ptr) { iklam@7089: jint type; iklam@7089: const char* path = _cur_ptr; iklam@7089: _cur_ptr += strlen(path) + 1; iklam@7089: if (!read_jint(&type)) { iklam@7089: return fail("Corrupted archive file header"); iklam@7089: } iklam@7089: if (TraceClassPaths) { iklam@7089: tty->print("[type=%s ", type_name(type)); iklam@7089: print_path(tty, type, path); iklam@7089: tty->print_cr("]"); iklam@7089: } iklam@7089: if (!check(type, path)) { iklam@7089: if (!PrintSharedArchiveAndExit) { iklam@7089: return false; iklam@7089: } iklam@7089: } else { iklam@7089: trace_class_path("[ok"); iklam@7089: } iklam@7089: } iklam@7089: iklam@7089: return true; iklam@7089: } iklam@7089: iklam@7089: bool SharedPathsMiscInfo::check(jint type, const char* path) { iklam@7089: switch (type) { iklam@7089: case BOOT: iklam@7089: if (strcmp(path, Arguments::get_sysclasspath()) != 0) { iklam@7089: return fail("[BOOT classpath mismatch, actual: -Dsun.boot.class.path=", Arguments::get_sysclasspath()); iklam@7089: } iklam@7089: break; iklam@7089: case NON_EXIST: // fall-through iklam@7089: case REQUIRED: iklam@7089: { iklam@7089: struct stat st; iklam@7089: if (os::stat(path, &st) != 0) { iklam@7089: // The file does not actually exist iklam@7089: if (type == REQUIRED) { iklam@7089: // but we require it to exist -> fail iklam@7089: return fail("Required file doesn't exist"); iklam@7089: } iklam@7089: } else { iklam@7089: // The file actually exists iklam@7089: if (type == NON_EXIST) { iklam@7089: // But we want it to not exist -> fail iklam@7089: return fail("File must not exist"); iklam@7089: } iklam@7089: time_t timestamp; iklam@7089: long filesize; iklam@7089: iklam@7089: if (!read_time(×tamp) || !read_long(&filesize)) { iklam@7089: return fail("Corrupted archive file header"); iklam@7089: } iklam@7089: if (timestamp != st.st_mtime) { iklam@7089: return fail("Timestamp mismatch"); iklam@7089: } iklam@7090: if (filesize != st.st_size) { iklam@7089: return fail("File size mismatch"); iklam@7089: } iklam@7089: } iklam@7089: } iklam@7089: break; iklam@7089: iklam@7089: default: iklam@7089: return fail("Corrupted archive file header"); iklam@7089: } iklam@7089: iklam@7089: return true; iklam@7089: }