src/hotspot/share/classfile/sharedClassUtil.cpp
changeset 48138 78b2ecdd3c4b
child 48191 d8a62bea95d3
equal deleted inserted replaced
48137:0afc5f9eafef 48138:78b2ecdd3c4b
       
     1 /*
       
     2  * Copyright (c) 2014, 2017, 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.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   char* manifest = ClassLoaderExt::read_manifest(cpe, &manifest_size, CHECK);
       
   145   if (manifest != NULL) {
       
   146     ManifestStream* stream = new ManifestStream((u1*)manifest,
       
   147                                                 manifest_size);
       
   148     isSigned = stream->check_is_signed();
       
   149     if (isSigned) {
       
   150       ent->_is_signed = true;
       
   151     } else {
       
   152       // Copy the manifest into the shared archive
       
   153       manifest = ClassLoaderExt::read_raw_manifest(cpe, &manifest_size, CHECK);
       
   154       Array<u1>* buf = MetadataFactory::new_array<u1>(loader_data,
       
   155                                                       manifest_size,
       
   156                                                       THREAD);
       
   157       char* p = (char*)(buf->data());
       
   158       memcpy(p, manifest, manifest_size);
       
   159       ent->set_manifest(buf);
       
   160       ent->_is_signed = false;
       
   161     }
       
   162   }
       
   163 }
       
   164 
       
   165 void SharedClassUtil::initialize(TRAPS) {
       
   166   if (UseSharedSpaces) {
       
   167     int size = FileMapInfo::get_number_of_share_classpaths();
       
   168     if (size > 0) {
       
   169       SystemDictionaryShared::allocate_shared_data_arrays(size, THREAD);
       
   170       if (!DumpSharedSpaces) {
       
   171         FileMapHeaderExt* header = (FileMapHeaderExt*)FileMapInfo::current_info()->header();
       
   172         ClassLoaderExt::init_paths_start_index(header->_app_paths_start_index);
       
   173       }
       
   174     }
       
   175   }
       
   176 
       
   177   if (DumpSharedSpaces) {
       
   178     if (SharedArchiveConfigFile) {
       
   179       read_extra_data(SharedArchiveConfigFile, THREAD);
       
   180     }
       
   181   }
       
   182 }
       
   183 
       
   184 void SharedClassUtil::read_extra_data(const char* filename, TRAPS) {
       
   185   HashtableTextDump reader(filename);
       
   186   reader.check_version("VERSION: 1.0");
       
   187 
       
   188   while (reader.remain() > 0) {
       
   189     int utf8_length;
       
   190     int prefix_type = reader.scan_prefix(&utf8_length);
       
   191     ResourceMark rm(THREAD);
       
   192     char* utf8_buffer = NEW_RESOURCE_ARRAY(char, utf8_length);
       
   193     reader.get_utf8(utf8_buffer, utf8_length);
       
   194 
       
   195     if (prefix_type == HashtableTextDump::SymbolPrefix) {
       
   196       SymbolTable::new_symbol(utf8_buffer, utf8_length, THREAD);
       
   197     } else{
       
   198       assert(prefix_type == HashtableTextDump::StringPrefix, "Sanity");
       
   199       utf8_buffer[utf8_length] = '\0';
       
   200       oop s = StringTable::intern(utf8_buffer, THREAD);
       
   201     }
       
   202   }
       
   203 }
       
   204 
       
   205 bool SharedClassUtil::is_classpath_entry_signed(int classpath_index) {
       
   206   assert(classpath_index >= 0, "Sanity");
       
   207   SharedClassPathEntryExt* ent = (SharedClassPathEntryExt*)
       
   208     FileMapInfo::shared_classpath(classpath_index);
       
   209   return ent->_is_signed;
       
   210 }
       
   211 
       
   212 void FileMapHeaderExt::populate(FileMapInfo* mapinfo, size_t alignment) {
       
   213   FileMapInfo::FileMapHeader::populate(mapinfo, alignment);
       
   214 
       
   215   ClassLoaderExt::finalize_shared_paths_misc_info();
       
   216   _app_paths_start_index = ClassLoaderExt::app_paths_start_index();
       
   217 
       
   218   _verify_local = BytecodeVerificationLocal;
       
   219   _verify_remote = BytecodeVerificationRemote;
       
   220   _has_platform_or_app_classes = ClassLoaderExt::has_platform_or_app_classes();
       
   221 }
       
   222 
       
   223 bool FileMapHeaderExt::validate() {
       
   224   if (UseAppCDS) {
       
   225     const char* prop = Arguments::get_property("java.system.class.loader");
       
   226     if (prop != NULL) {
       
   227       warning("UseAppCDS is disabled because the java.system.class.loader property is specified (value = \"%s\"). "
       
   228               "To enable UseAppCDS, this property must be not be set", prop);
       
   229       UseAppCDS = false;
       
   230     }
       
   231   }
       
   232 
       
   233   if (!FileMapInfo::FileMapHeader::validate()) {
       
   234     return false;
       
   235   }
       
   236 
       
   237   // For backwards compatibility, we don't check the verification setting
       
   238   // if the archive only contains system classes.
       
   239   if (_has_platform_or_app_classes &&
       
   240       ((!_verify_local && BytecodeVerificationLocal) ||
       
   241        (!_verify_remote && BytecodeVerificationRemote))) {
       
   242     FileMapInfo::fail_continue("The shared archive file was created with less restrictive "
       
   243                   "verification setting than the current setting.");
       
   244     return false;
       
   245   }
       
   246 
       
   247   return true;
       
   248 }