1 /*
   2  * Copyright (c) 2014, 2018, 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/classLoaderExt.hpp"
  28 #include "classfile/dictionary.hpp"
  29 #include "classfile/javaClasses.hpp"
  30 #include "classfile/sharedClassUtil.hpp"
  31 #include "classfile/stringTable.hpp"
  32 #include "classfile/symbolTable.hpp"
  33 #include "classfile/systemDictionary.hpp"
  34 #include "classfile/systemDictionaryShared.hpp"
  35 #include "memory/filemap.hpp"
  36 #include "memory/metadataFactory.hpp"
  37 #include "memory/resourceArea.hpp"
  38 #include "oops/instanceKlass.hpp"
  39 #include "runtime/arguments.hpp"
  40 #include "runtime/java.hpp"
  41 #include "runtime/os.inline.hpp"
  42 
  43 class ManifestStream: public ResourceObj {
  44   private:
  45   u1*   _buffer_start; // Buffer bottom
  46   u1*   _buffer_end;   // Buffer top (one past last element)
  47   u1*   _current;      // Current buffer position
  48 
  49  public:
  50   // Constructor
  51   ManifestStream(u1* buffer, int length) : _buffer_start(buffer),
  52                                            _current(buffer) {
  53     _buffer_end = buffer + length;
  54   }
  55 
  56   static bool is_attr(u1* attr, const char* name) {
  57     return strncmp((const char*)attr, name, strlen(name)) == 0;
  58   }
  59 
  60   static char* copy_attr(u1* value, size_t len) {
  61     char* buf = NEW_RESOURCE_ARRAY(char, len + 1);
  62     strncpy(buf, (char*)value, len);
  63     buf[len] = 0;
  64     return buf;
  65   }
  66 
  67   // The return value indicates if the JAR is signed or not
  68   bool check_is_signed() {
  69     u1* attr = _current;
  70     bool isSigned = false;
  71     while (_current < _buffer_end) {
  72       if (*_current == '\n') {
  73         *_current = '\0';
  74         u1* value = (u1*)strchr((char*)attr, ':');
  75         if (value != NULL) {
  76           assert(*(value+1) == ' ', "Unrecognized format" );
  77           if (strstr((char*)attr, "-Digest") != NULL) {
  78             isSigned = true;
  79             break;
  80           }
  81         }
  82         *_current = '\n'; // restore
  83         attr = _current + 1;
  84       }
  85       _current ++;
  86     }
  87     return isSigned;
  88   }
  89 };
  90 
  91 void SharedPathsMiscInfoExt::print_path(outputStream* out, int type, const char* path) {
  92   switch(type) {
  93   case APP:
  94     ClassLoader::trace_class_path("Expecting -Djava.class.path=", path);
  95     break;
  96   default:
  97     SharedPathsMiscInfo::print_path(out, type, path);
  98   }
  99 }
 100 
 101 bool SharedPathsMiscInfoExt::check(jint type, const char* path) {
 102 
 103   switch (type) {
 104   case APP:
 105     {
 106       // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar
 107       size_t len = strlen(path);
 108       const char *appcp = Arguments::get_appclasspath();
 109       assert(appcp != NULL, "NULL app classpath");
 110       size_t appcp_len = strlen(appcp);
 111       if (appcp_len < len) {
 112         return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
 113       }
 114       ResourceMark rm;
 115       char* tmp_path;
 116       if (len == appcp_len) {
 117         tmp_path = (char*)appcp;
 118       } else {
 119         tmp_path = NEW_RESOURCE_ARRAY(char, len + 1);
 120         strncpy(tmp_path, appcp, len);
 121         tmp_path[len] = 0;
 122       }
 123       if (os::file_name_strcmp(path, tmp_path) != 0) {
 124         return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
 125       }
 126       if (appcp[len] != '\0' && appcp[len] != os::path_separator()[0]) {
 127         return fail("Dump time APP classpath is not a proper prefix of run time APP classpath: ", appcp);
 128       }
 129     }
 130     break;
 131   default:
 132     return SharedPathsMiscInfo::check(type, path);
 133   }
 134 
 135   return true;
 136 }
 137 
 138 void SharedClassUtil::update_shared_classpath(ClassPathEntry *cpe, SharedClassPathEntry* e, TRAPS) {
 139   ClassLoaderData* loader_data = ClassLoaderData::the_null_class_loader_data();
 140   SharedClassPathEntryExt* ent = (SharedClassPathEntryExt*)e;
 141   ResourceMark rm(THREAD);
 142   jint manifest_size;
 143   bool isSigned;
 144 
 145   if (cpe->is_jar_file()) {
 146     char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
 147     if (manifest != NULL) {
 148       ManifestStream* stream = new ManifestStream((u1*)manifest,
 149                                                   manifest_size);
 150       isSigned = stream->check_is_signed();
 151       if (isSigned) {
 152         ent->_is_signed = true;
 153       } else {
 154         // Copy the manifest into the shared archive
 155         manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
 156         Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
 157                                                         manifest_size,
 158                                                         THREAD);
 159         char* p = (char*)(buf->data());
 160         memcpy(p, manifest, manifest_size);
 161         ent->set_manifest(buf);
 162         ent->_is_signed = false;
 163       }
 164     }
 165   }
 166 }
 167 
 168 void SharedClassUtil::initialize(TRAPS) {
 169   if (UseSharedSpaces) {
 170     int size = FileMapInfo::get_number_of_share_classpaths();
 171     if (size > 0) {
 172       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);
 173       if (!DumpSharedSpaces) {
 174         FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
 175         ClassLoaderExt::init_paths_start_index(header->_app_paths_start_index);
 176       }
 177     }
 178   }
 179 
 180   if (DumpSharedSpaces) {
 181     if (SharedArchiveConfigFile) {
 182       read_extra_data(SharedArchiveConfigFile, THREAD);
 183     }
 184   }
 185 }
 186 
 187 void SharedClassUtil::read_extra_data(const char* filename, TRAPS) {
 188   HashtableTextDump reader(filename);
 189   reader.check_version("VERSION: 1.0");
 190 
 191   while (reader.remain() > 0) {
 192     int utf8_length;
 193     int prefix_type = reader.scan_prefix(&utf8_length);
 194     ResourceMark rm(THREAD);
 195     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, utf8_length);
 196     reader.get_utf8(utf8_buffer, utf8_length);
 197 
 198     if (prefix_type == HashtableTextDump::SymbolPrefix) {
 199       SymbolTable::new_symbol(utf8_buffer, utf8_length, THREAD);
 200     } else{
 201       assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity");
 202       utf8_buffer[utf8_length] = '\0';
 203       oop s = StringTable::intern(utf8_buffer, THREAD);
 204     }
 205   }
 206 }
 207 
 208 bool SharedClassUtil::is_classpath_entry_signed(int classpath_index) {
 209   assert(classpath_index >= 0, "Sanity");
 210   SharedClassPathEntryExt* ent = (SharedClassPathEntryExt*)
 211     FileMapInfo::shared_classpath(classpath_index);
 212   return ent->_is_signed;
 213 }
 214 
 215 void FileMapHeaderExt::populate(FileMapInfo* mapinfo, size_t alignment) {
 216   FileMapInfo::FileMapHeader::populate(mapinfo, alignment);
 217 
 218   ClassLoaderExt::finalize_shared_paths_misc_info();
 219   _app_paths_start_index = ClassLoaderExt::app_paths_start_index();
 220 
 221   _verify_local = BytecodeVerificationLocal;
 222   _verify_remote = BytecodeVerificationRemote;
 223   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
 224 }
 225 
 226 bool FileMapHeaderExt::validate() {
 227   if (UseAppCDS) {
 228     const char* prop = Arguments::get_property("java.system.class.loader");
 229     if (prop != NULL) {
 230       warning("UseAppCDS is disabled because the java.system.class.loader property is specified (value = \"%s\"). "
 231               "To enable UseAppCDS, this property must be not be set", prop);
 232       UseAppCDS = false;
 233     }
 234   }
 235 
 236   if (!FileMapInfo::FileMapHeader::validate()) {
 237     return false;
 238   }
 239 
 240   // For backwards compatibility, we don't check the verification setting
 241   // if the archive only contains system classes.
 242   if (_has_platform_or_app_classes &&
 243       ((!_verify_local && BytecodeVerificationLocal) ||
 244        (!_verify_remote && BytecodeVerificationRemote))) {
 245     FileMapInfo::fail_continue("The shared archive file was created with less restrictive "
 246                   "verification setting than the current setting.");
 247     return false;
 248   }
 249 
 250   return true;
 251 }