Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/sharedPathsMiscInfo.cpp
32285 views
/*1* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/classLoader.hpp"26#include "classfile/classLoaderData.inline.hpp"27#include "classfile/sharedPathsMiscInfo.hpp"28#include "memory/allocation.inline.hpp"29#include "memory/metaspaceShared.hpp"30#include "runtime/arguments.hpp"3132void SharedPathsMiscInfo::add_path(const char* path, int type) {33if (TraceClassPaths) {34tty->print("[type=%s] ", type_name(type));35trace_class_path("[Add misc shared path ", path);36}37write(path, strlen(path) + 1);38write_jint(jint(type));39}4041void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) {42assert(_allocated, "cannot modify buffer during validation.");43int used = get_used_bytes();44int target = used + int(needed_bytes);45if (target > _buf_size) {46_buf_size = _buf_size * 2 + (int)needed_bytes;47_buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass);48_cur_ptr = _buf_start + used;49_end_ptr = _buf_start + _buf_size;50}51}5253void SharedPathsMiscInfo::write(const void* ptr, size_t size) {54ensure_size(size);55memcpy(_cur_ptr, ptr, size);56_cur_ptr += size;57}5859bool SharedPathsMiscInfo::read(void* ptr, size_t size) {60if (_cur_ptr + size <= _end_ptr) {61memcpy(ptr, _cur_ptr, size);62_cur_ptr += size;63return true;64}65return false;66}6768bool SharedPathsMiscInfo::fail(const char* msg, const char* name) {69ClassLoader::trace_class_path(tty, msg, name);70MetaspaceShared::set_archive_loading_failed();71return false;72}7374bool SharedPathsMiscInfo::check() {75// The whole buffer must be 0 terminated so that we can use strlen and strcmp76// without fear.77_end_ptr -= sizeof(jint);78if (_cur_ptr >= _end_ptr) {79return fail("Truncated archive file header");80}81if (*_end_ptr != 0) {82return fail("Corrupted archive file header");83}8485while (_cur_ptr < _end_ptr) {86jint type;87const char* path = _cur_ptr;88_cur_ptr += strlen(path) + 1;89if (!read_jint(&type)) {90return fail("Corrupted archive file header");91}92if (TraceClassPaths) {93tty->print("[type=%s ", type_name(type));94print_path(tty, type, path);95tty->print_cr("]");96}97if (!check(type, path)) {98if (!PrintSharedArchiveAndExit) {99return false;100}101} else {102trace_class_path("[ok");103}104}105106return true;107}108109bool SharedPathsMiscInfo::check(jint type, const char* path) {110switch (type) {111case BOOT:112if (strcmp(path, Arguments::get_sysclasspath()) != 0) {113return fail("[BOOT classpath mismatch, actual: -Dsun.boot.class.path=", Arguments::get_sysclasspath());114}115break;116case NON_EXIST: // fall-through117case REQUIRED:118{119struct stat st;120if (os::stat(path, &st) != 0) {121// The file does not actually exist122if (type == REQUIRED) {123// but we require it to exist -> fail124return fail("Required file doesn't exist");125}126} else {127// The file actually exists128if (type == NON_EXIST) {129// But we want it to not exist -> fail130return fail("File must not exist");131}132time_t timestamp;133long filesize;134135if (!read_time(×tamp) || !read_long(&filesize)) {136return fail("Corrupted archive file header");137}138if (timestamp != st.st_mtime) {139return fail("Timestamp mismatch");140}141if (filesize != st.st_size) {142return fail("File size mismatch");143}144}145}146break;147148default:149return fail("Corrupted archive file header");150}151152return true;153}154155156