1 /*
   2  * Copyright (c) 2014, 2015, 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 "memory/allocation.inline.hpp"
  31 #include "memory/metaspaceShared.hpp"
  32 #include "runtime/arguments.hpp"
  33 #include "utilities/ostream.hpp"
  34 
  35 void SharedPathsMiscInfo::add_path(const char* path, int type) {
  36   if (log_is_enabled(Info, classpath)) {
  37     ResourceMark rm;
  38     outputStream* log = LogHandle(classpath)::info_stream();
  39     log->print("type=%s ", type_name(type));
  40     ClassLoader::trace_class_path(log, "add misc shared path ", path);
  41   }
  42   write(path, strlen(path) + 1);
  43   write_jint(jint(type));
  44 }
  45 
  46 void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) {
  47   assert(_allocated, "cannot modify buffer during validation.");
  48   int used = get_used_bytes();
  49   int target = used + int(needed_bytes);
  50   if (target > _buf_size) {
  51     _buf_size = _buf_size * 2 + (int)needed_bytes;
  52     _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass);
  53     _cur_ptr = _buf_start + used;
  54     _end_ptr = _buf_start + _buf_size;
  55   }
  56 }
  57 
  58 void SharedPathsMiscInfo::write(const void* ptr, size_t size) {
  59   ensure_size(size);
  60   memcpy(_cur_ptr, ptr, size);
  61   _cur_ptr += size;
  62 }
  63 
  64 bool SharedPathsMiscInfo::read(void* ptr, size_t size) {
  65   if (_cur_ptr + size <= _end_ptr) {
  66     memcpy(ptr, _cur_ptr, size);
  67     _cur_ptr += size;
  68     return true;
  69   }
  70   return false;
  71 }
  72 
  73 bool SharedPathsMiscInfo::fail(const char* msg, const char* name) {
  74   ClassLoader::trace_class_path(tty, msg, name);
  75   MetaspaceShared::set_archive_loading_failed();
  76   return false;
  77 }
  78 
  79 bool SharedPathsMiscInfo::check() {
  80   // The whole buffer must be 0 terminated so that we can use strlen and strcmp
  81   // without fear.
  82   _end_ptr -= sizeof(jint);
  83   if (_cur_ptr >= _end_ptr) {
  84     return fail("Truncated archive file header");
  85   }
  86   if (*_end_ptr != 0) {
  87     return fail("Corrupted archive file header");
  88   }
  89 
  90   while (_cur_ptr < _end_ptr) {
  91     jint type;
  92     const char* path = _cur_ptr;
  93     _cur_ptr += strlen(path) + 1;
  94     if (!read_jint(&type)) {
  95       return fail("Corrupted archive file header");
  96     }
  97     if (log_is_enabled(Info, classpath)) {
  98       ResourceMark rm;
  99       outputStream* log = LogHandle(classpath)::info_stream();
 100       log->print("type=%s ", type_name(type));
 101       print_path(log, type, path);
 102     }
 103     if (!check(type, path)) {
 104       if (!PrintSharedArchiveAndExit) {
 105         return false;
 106       }
 107     } else {
 108       trace_class_path("[ok]");
 109     }
 110   }
 111 
 112   return true;
 113 }
 114 
 115 bool SharedPathsMiscInfo::check(jint type, const char* path) {
 116   switch (type) {
 117   case BOOT:
 118     if (os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) {
 119       return fail("[BOOT classpath mismatch, actual: -Dsun.boot.class.path=", Arguments::get_sysclasspath());
 120     }
 121     break;
 122   case NON_EXIST: // fall-through
 123   case REQUIRED:
 124     {
 125       struct stat st;
 126       if (os::stat(path, &st) != 0) {
 127         // The file does not actually exist
 128         if (type == REQUIRED) {
 129           // but we require it to exist -> fail
 130           return fail("Required file doesn't exist");
 131         }
 132       } else {
 133         // The file actually exists
 134         if (type == NON_EXIST) {
 135           // But we want it to not exist -> fail
 136           return fail("File must not exist");
 137         }
 138         time_t    timestamp;
 139         long      filesize;
 140 
 141         if (!read_time(&timestamp) || !read_long(&filesize)) {
 142           return fail("Corrupted archive file header");
 143         }
 144         if (timestamp != st.st_mtime) {
 145           return fail("Timestamp mismatch");
 146         }
 147         if (filesize != st.st_size) {
 148           return fail("File size mismatch");
 149         }
 150       }
 151     }
 152     break;
 153 
 154   default:
 155     return fail("Corrupted archive file header");
 156   }
 157 
 158   return true;
 159 }