src/hotspot/share/classfile/sharedPathsMiscInfo.cpp
author ccheung
Fri, 17 May 2019 08:29:55 -0700
changeset 54927 1512d88b24c6
parent 51439 0517bd2a0eda
child 55524 b279ae9843b8
permissions -rw-r--r--
8207812: Implement Dynamic CDS Archive Summary: Improve the usability of AppCDS Reviewed-by: acorn, jiangli, mseledtsov Contributed-by: ioi.lam@oracle.com, jianglizhou@google.com, calvin.cheung@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     1
/*
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
     2
 * Copyright (c) 2014, 2019, Oracle and/or its affiliates. All rights reserved.
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     4
 *
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     7
 * published by the Free Software Foundation.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     8
 *
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    13
 * accompanied this code).
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    14
 *
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    18
 *
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    21
 * questions.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    22
 *
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    23
 */
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    24
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    25
#include "precompiled.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    26
#include "classfile/classLoader.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    27
#include "classfile/sharedPathsMiscInfo.hpp"
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    28
#include "logging/log.hpp"
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
    29
#include "logging/logStream.hpp"
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    30
#include "memory/allocation.inline.hpp"
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
    31
#include "memory/filemap.hpp"
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    32
#include "memory/metaspaceShared.hpp"
37248
11a660dbbb8e 8132524: Missing includes to resourceArea.hpp
jprovino
parents: 37242
diff changeset
    33
#include "memory/resourceArea.hpp"
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    34
#include "runtime/arguments.hpp"
49593
4dd58ecc9912 8200105: Remove cyclic dependency between oop.inline.hpp and collectedHeap.inline.hpp
stefank
parents: 48171
diff changeset
    35
#include "runtime/os.inline.hpp"
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    36
#include "utilities/ostream.hpp"
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    37
48157
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    38
SharedPathsMiscInfo::SharedPathsMiscInfo() {
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
    39
  _app_offset = 0;
48157
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    40
  _buf_size = INITIAL_BUF_SIZE;
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    41
  _cur_ptr = _buf_start = NEW_C_HEAP_ARRAY(char, _buf_size, mtClass);
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    42
  _allocated = true;
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    43
}
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    44
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    45
SharedPathsMiscInfo::~SharedPathsMiscInfo() {
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    46
  if (_allocated) {
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    47
    FREE_C_HEAP_ARRAY(char, _buf_start);
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    48
  }
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    49
}
7c4d43c26352 8192061: Clean up allocation.inline.hpp includes
stefank
parents: 47216
diff changeset
    50
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    51
void SharedPathsMiscInfo::add_path(const char* path, int type) {
38151
fffedc5e5cf8 8154110: Update class* and safepoint* logging subsystems
mockner
parents: 37248
diff changeset
    52
  log_info(class, path)("type=%s ", type_name(type));
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    53
  ClassLoader::trace_class_path("add misc shared path ", path);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    54
  write(path, strlen(path) + 1);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    55
  write_jint(jint(type));
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    56
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    57
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    58
void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    59
  assert(_allocated, "cannot modify buffer during validation.");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    60
  int used = get_used_bytes();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    61
  int target = used + int(needed_bytes);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    62
  if (target > _buf_size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    63
    _buf_size = _buf_size * 2 + (int)needed_bytes;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    64
    _buf_start = REALLOC_C_HEAP_ARRAY(char, _buf_start, _buf_size, mtClass);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    65
    _cur_ptr = _buf_start + used;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    66
    _end_ptr = _buf_start + _buf_size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    67
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    68
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    69
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    70
void SharedPathsMiscInfo::write(const void* ptr, size_t size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    71
  ensure_size(size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    72
  memcpy(_cur_ptr, ptr, size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    73
  _cur_ptr += size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    74
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    75
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    76
bool SharedPathsMiscInfo::read(void* ptr, size_t size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    77
  if (_cur_ptr + size <= _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    78
    memcpy(ptr, _cur_ptr, size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    79
    _cur_ptr += size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    80
    return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    81
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    82
  return false;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    83
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    84
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    85
bool SharedPathsMiscInfo::fail(const char* msg, const char* name) {
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    86
  ClassLoader::trace_class_path(msg, name);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    87
  MetaspaceShared::set_archive_loading_failed();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    88
  return false;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    89
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    90
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
    91
void SharedPathsMiscInfo::print_path(outputStream* out, int type, const char* path) {
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    92
  switch (type) {
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
    93
  case BOOT_PATH:
36508
5f9eee6b383b 8142968: Module System implementation
alanb
parents: 36364
diff changeset
    94
    out->print("Expecting BOOT path=%s", path);
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    95
    break;
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    96
  case NON_EXIST:
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    97
    out->print("Expecting that %s does not exist", path);
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
    98
    break;
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
    99
  case APP_PATH:
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   100
    ClassLoader::trace_class_path("Expecting -Djava.class.path=", path);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   101
    break;
36364
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
   102
  default:
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
   103
    ShouldNotReachHere();
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
   104
  }
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
   105
}
5971776598e5 8150103: Convert TraceClassPaths to Unified Logging
mockner
parents: 34287
diff changeset
   106
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   107
bool SharedPathsMiscInfo::check(bool is_static) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   108
  // The whole buffer must be 0 terminated so that we can use strlen and strcmp
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   109
  // without fear.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   110
  _end_ptr -= sizeof(jint);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   111
  if (_cur_ptr >= _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   112
    return fail("Truncated archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   113
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   114
  if (*_end_ptr != 0) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   115
    return fail("Corrupted archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   116
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   117
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   118
  jshort cur_index = 0;
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   119
  FileMapHeader* header = is_static ? FileMapInfo::current_info()->header() :
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   120
                                      FileMapInfo::dynamic_info()->header();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   121
  jshort max_cp_index = header->max_used_path_index();
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   122
  jshort module_paths_start_index = header->app_module_paths_start_index();
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   123
  while (_cur_ptr < _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   124
    jint type;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   125
    const char* path = _cur_ptr;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   126
    _cur_ptr += strlen(path) + 1;
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   127
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   128
    if (!read_jint(&type)) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   129
      return fail("Corrupted archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   130
    }
46701
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   131
    LogTarget(Info, class, path) lt;
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   132
    if (lt.is_enabled()) {
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   133
      lt.print("type=%s ", type_name(type));
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   134
      LogStream ls(lt);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   135
      print_path(&ls, type, path);
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   136
      ls.cr();
f559541c0daa 8181917: Refactor UL LogStreams to avoid using resource area
stuefe
parents: 46365
diff changeset
   137
    }
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   138
    // skip checking the class path(s) which was not referenced during CDS dump
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   139
    if ((cur_index <= max_cp_index) || (cur_index >= module_paths_start_index)) {
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   140
      if (!check(type, path, is_static)) {
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   141
        if (!PrintSharedArchiveAndExit) {
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   142
          return false;
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   143
        }
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   144
      } else {
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   145
        ClassLoader::trace_class_path("ok");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   146
      }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   147
    } else {
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   148
      ClassLoader::trace_class_path("skipped check");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   149
    }
51439
0517bd2a0eda 8209385: CDS runtime classpath checking is too strict when only classes from the system modules are archived
ccheung
parents: 50199
diff changeset
   150
    cur_index++;
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   151
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   152
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   153
  return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   154
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   155
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   156
char* skip_first_path_entry(const char* path) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   157
  size_t path_sep_len = strlen(os::path_separator());
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   158
  char* p = strstr((char*)path, os::path_separator());
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   159
  if (p != NULL) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   160
    debug_only( {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   161
      size_t image_name_len = strlen(MODULES_IMAGE_NAME);
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   162
      assert(strncmp(p - image_name_len, MODULES_IMAGE_NAME, image_name_len) == 0,
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   163
             "first entry must be the modules image");
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   164
    } );
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   165
    p += path_sep_len;
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   166
  } else {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   167
    debug_only( {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   168
      assert(ClassLoader::string_ends_with(path, MODULES_IMAGE_NAME),
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   169
             "first entry must be the modules image");
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   170
    } );
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   171
  }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   172
  return p;
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   173
}
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   174
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   175
bool SharedPathsMiscInfo::check(jint type, const char* path, bool is_static) {
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   176
  assert(UseSharedSpaces, "runtime only");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   177
  switch (type) {
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   178
  case BOOT_PATH:
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   179
    {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   180
      //
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   181
      // - Archive contains boot classes only - relaxed boot path check:
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   182
      //   Extra path elements appended to the boot path at runtime are allowed.
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   183
      //
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   184
      // - Archive contains application or platform classes - strict boot path check:
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   185
      //   Validate the entire runtime boot path, which must be compactible
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   186
      //   with the dump time boot path. Appending boot path at runtime is not
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   187
      //   allowed.
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   188
      //
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   189
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   190
      // The first entry in boot path is the modules_image (guaranteed by
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   191
      // ClassLoader::setup_boot_search_path()). Skip the first entry. The
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   192
      // path of the runtime modules_image may be different from the dump
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   193
      // time path (e.g. the JDK image is copied to a different location
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   194
      // after generating the shared archive), which is acceptable. For most
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   195
      // common cases, the dump time boot path might contain modules_image only.
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   196
      char* runtime_boot_path = Arguments::get_sysclasspath();
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   197
      char* rp = skip_first_path_entry(runtime_boot_path);
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   198
      char* dp = skip_first_path_entry(path);
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   199
54927
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   200
      bool relaxed_check = is_static ?
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   201
                             !FileMapInfo::current_info()->header()->has_platform_or_app_classes() :
1512d88b24c6 8207812: Implement Dynamic CDS Archive
ccheung
parents: 51439
diff changeset
   202
                             !FileMapInfo::dynamic_info()->header()->has_platform_or_app_classes();
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   203
      if (dp == NULL && rp == NULL) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   204
        break;   // ok, both runtime and dump time boot paths have modules_images only
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   205
      } else if (dp == NULL && rp != NULL && relaxed_check) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   206
        break;   // ok, relaxed check, runtime has extra boot append path entries
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   207
      } else if (dp != NULL && rp != NULL) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   208
        size_t num;
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   209
        size_t dp_len = strlen(dp);
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   210
        size_t rp_len = strlen(rp);
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   211
        if (rp_len >= dp_len) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   212
          if (relaxed_check) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   213
            // only check the leading entries in the runtime boot path, up to
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   214
            // the length of the dump time boot path
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   215
            num = dp_len;
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   216
          } else {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   217
            // check the full runtime boot path, must match with dump time
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   218
            num = rp_len;
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   219
          }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   220
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   221
          if (os::file_name_strncmp(dp, rp, num) == 0) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   222
            // make sure it is the end of an entry in the runtime boot path
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   223
            if (rp[dp_len] == '\0' || rp[dp_len] == os::path_separator()[0]) {
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   224
              break; // ok, runtime and dump time paths match
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   225
            }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   226
          }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   227
        }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   228
      }
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   229
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   230
      // The paths are different
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   231
      return fail("[BOOT classpath mismatch, actual =", runtime_boot_path);
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   232
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   233
    break;
46365
d79745f72ae6 8171042: The 'REQUIRED' type in sharedPathsMiscInfo.hpp is not used and should be removed.
jiangli
parents: 42876
diff changeset
   234
  case NON_EXIST:
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   235
    {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   236
      struct stat st;
46365
d79745f72ae6 8171042: The 'REQUIRED' type in sharedPathsMiscInfo.hpp is not used and should be removed.
jiangli
parents: 42876
diff changeset
   237
      if (os::stat(path, &st) == 0) {
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   238
        // The file actually exists
46365
d79745f72ae6 8171042: The 'REQUIRED' type in sharedPathsMiscInfo.hpp is not used and should be removed.
jiangli
parents: 42876
diff changeset
   239
        // But we want it to not exist -> fail
d79745f72ae6 8171042: The 'REQUIRED' type in sharedPathsMiscInfo.hpp is not used and should be removed.
jiangli
parents: 42876
diff changeset
   240
        return fail("File must not exist");
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   241
      }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   242
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   243
    break;
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   244
  case APP_PATH:
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   245
    {
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   246
      size_t len = strlen(path);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   247
      const char *appcp = Arguments::get_appclasspath();
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   248
      assert(appcp != NULL, "NULL app classpath");
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   249
      size_t appcp_len = strlen(appcp);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   250
      if (appcp_len < len) {
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   251
        return fail("Run time APP classpath is shorter than the one at dump time: ", appcp);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   252
      }
50199
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   253
      // Prefix is OK: E.g., dump with -cp foo.jar, but run with -cp foo.jar:bar.jar.
83d8b3a25f25 8199807: AppCDS performs overly restrictive path matching check.
jiangli
parents: 50039
diff changeset
   254
      if (os::file_name_strncmp(path, appcp, len) != 0) {
50039
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   255
        return fail("[APP classpath mismatch, actual: -Djava.class.path=", appcp);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   256
      }
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   257
      if (appcp[len] != '\0' && appcp[len] != os::path_separator()[0]) {
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   258
        return fail("Dump time APP classpath is not a proper prefix of run time APP classpath: ", appcp);
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   259
      }
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   260
    }
9fec54fe663d 8197954: Remove unnecessary intermediary APIs from AppCDS implementation
iklam
parents: 49931
diff changeset
   261
    break;
26135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   262
  default:
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   263
    return fail("Corrupted archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   264
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   265
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   266
  return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   267
}