1 /*
   2  * Copyright (c) 2014, 2016, 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 "memory/resourceArea.hpp"
  33 #include "runtime/arguments.hpp"
  34 #include "utilities/ostream.hpp"
  35 
  36 void SharedPathsMiscInfo::add_path(const char* path, int type) {
  37   log_info(class, path)("type=%s ", type_name(type));
  38   ClassLoader::trace_class_path("add misc shared path ", path);
  39   write(path, strlen(path) + 1);
  40   write_jint(jint(type));
  41 }
  42 
  43 void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) {
  44   assert(_allocated, "cannot modify buffer during validation.");
  45   int used = get_used_bytes();
  46   int target = used + int(needed_bytes);
  47   if (target > _buf_size) {
  48     _buf_size = _buf_size * 2 + (int)needed_bytes;
  49     _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass);
  50     _cur_ptr = _buf_start + used;
  51     _end_ptr = _buf_start + _buf_size;
  52   }
  53 }
  54 
  55 void SharedPathsMiscInfo::write(const void* ptr, size_t size) {
  56   ensure_size(size);
  57   memcpy(_cur_ptr, ptr, size);
  58   _cur_ptr += size;
  59 }
  60 
  61 bool SharedPathsMiscInfo::read(void* ptr, size_t size) {
  62   if (_cur_ptr + size <= _end_ptr) {
  63     memcpy(ptr, _cur_ptr, size);
  64     _cur_ptr += size;
  65     return true;
  66   }
  67   return false;
  68 }
  69 
  70 bool SharedPathsMiscInfo::fail(const char* msg, const char* name) {
  71   ClassLoader::trace_class_path(msg, name);
  72   MetaspaceShared::set_archive_loading_failed();
  73   return false;
  74 }
  75 
  76 void SharedPathsMiscInfo::print_path(int type, const char* path) {
  77   ResourceMark rm;
  78   outputStream* out = Log(class, path)::info_stream();
  79   switch (type) {
  80   case BOOT:
  81     out->print("Expecting BOOT path=%s", path);
  82     break;
  83   case NON_EXIST:
  84     out->print("Expecting that %s does not exist", path);
  85     break;
  86   case REQUIRED:
  87     out->print("Expecting that file %s must exist and is not altered", path);
  88     break;
  89   case PATCH_MOD:
  90     out->print("Expecting --patch-module=%s", path);
  91     break;
  92   default:
  93     ShouldNotReachHere();
  94   }
  95 }
  96 
  97 bool SharedPathsMiscInfo::check() {
  98   // The whole buffer must be 0 terminated so that we can use strlen and strcmp
  99   // without fear.
 100   _end_ptr -= sizeof(jint);
 101   if (_cur_ptr >= _end_ptr) {
 102     return fail("Truncated archive file header");
 103   }
 104   if (*_end_ptr != 0) {
 105     return fail("Corrupted archive file header");
 106   }
 107 
 108   while (_cur_ptr < _end_ptr) {
 109     jint type;
 110     const char* path = _cur_ptr;
 111     _cur_ptr += strlen(path) + 1;
 112     if (!read_jint(&type)) {
 113       return fail("Corrupted archive file header");
 114     }
 115     log_info(class, path)("type=%s ", type_name(type));
 116     print_path(type, path);
 117     if (!check(type, path)) {
 118       if (!PrintSharedArchiveAndExit) {
 119         return false;
 120       }
 121     } else {
 122       ClassLoader::trace_class_path("ok");
 123     }
 124   }
 125 
 126   return true;
 127 }
 128 
 129 bool SharedPathsMiscInfo::check(jint type, const char* path) {
 130   switch (type) {
 131   case BOOT:
 132     if (os::file_name_strcmp(path, Arguments::get_sysclasspath()) != 0) {
 133       return fail("[BOOT classpath mismatch, actual =", Arguments::get_sysclasspath());
 134     }
 135     break;
 136   case NON_EXIST: // fall-through
 137   case REQUIRED:
 138     {
 139       struct stat st;
 140       if (os::stat(path, &st) != 0) {
 141         // The file does not actually exist
 142         if (type == REQUIRED) {
 143           // but we require it to exist -> fail
 144           return fail("Required file doesn't exist");
 145         }
 146       } else {
 147         // The file actually exists
 148         if (type == NON_EXIST) {
 149           // But we want it to not exist -> fail
 150           return fail("File must not exist");
 151         }
 152         if ((st.st_mode & S_IFMT) != S_IFREG) {
 153           return fail("Did not get a regular file as expected.");
 154         }
 155         time_t    timestamp;
 156         long      filesize;
 157 
 158         if (!read_time(&timestamp) || !read_long(&filesize)) {
 159           return fail("Corrupted archive file header");
 160         }
 161         if (timestamp != st.st_mtime) {
 162           return fail("Timestamp mismatch");
 163         }
 164         if (filesize != st.st_size) {
 165           return fail("File size mismatch");
 166         }
 167       }
 168     }
 169     break;
 170   case PATCH_MOD:
 171     {
 172       GrowableArray<ModulePatchPath*>* patch_mod_args = Arguments::get_patch_mod_prefix();
 173       if (patch_mod_args != NULL) {
 174         int num_of_entries = patch_mod_args->length();
 175         for (int i = 0; i < num_of_entries; i++) {
 176           const char* module_name = (patch_mod_args->at(i))->module_name();
 177           const char* path_string = (patch_mod_args->at(i))->path_string();
 178           size_t n = strlen(module_name);
 179           // path contains the module name, followed by '=', and one or more entries.
 180           // E.g.: "java.base=foo" or "java.naming=dir1:dir2:dir3"
 181           if ((strncmp(module_name, path, n) != 0) ||
 182               (path[n] != '=') ||
 183               (strcmp(path + n + 1, path_string) != 0)) {
 184             return fail("--patch-module mismatch, path not found in run time: ", path);
 185           }
 186         }
 187       }
 188     }
 189     break;
 190   default:
 191     return fail("Corrupted archive file header");
 192   }
 193 
 194   return true;
 195 }