hotspot/src/share/vm/classfile/sharedPathsMiscInfo.cpp
author iklam
Tue, 12 Aug 2014 17:29:00 -0700
changeset 26135 82b516c550f7
child 26419 25abc4a3285c
permissions -rw-r--r--
8046070: Class Data Sharing clean up and refactoring Summary: Cleaned up CDS to be more configurable, maintainable and extensible Reviewed-by: dholmes, coleenp, acorn, mchung
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
/*
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
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/classLoaderData.inline.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    28
#include "classfile/sharedPathsMiscInfo.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    29
#include "memory/allocation.inline.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    30
#include "memory/metaspaceShared.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    31
#include "runtime/arguments.hpp"
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    32
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    33
void SharedPathsMiscInfo::add_path(const char* path, int type) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    34
  if (TraceClassPaths) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    35
    tty->print("[type=%s] ", type_name(type));
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    36
    trace_class_path("[Add misc shared path ", path);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    37
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    38
  write(path, strlen(path) + 1);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    39
  write_jint(jint(type));
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    40
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    41
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    42
void SharedPathsMiscInfo::ensure_size(size_t needed_bytes) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    43
  assert(_allocated, "cannot modify buffer during validation.");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    44
  int used = get_used_bytes();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    45
  int target = used + int(needed_bytes);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    46
  if (target > _buf_size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    47
    _buf_size = _buf_size * 2 + (int)needed_bytes;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    48
    _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
    49
    _cur_ptr = _buf_start + used;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    50
    _end_ptr = _buf_start + _buf_size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    51
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    52
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    53
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    54
void SharedPathsMiscInfo::write(const void* ptr, size_t size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    55
  ensure_size(size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    56
  memcpy(_cur_ptr, ptr, size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    57
  _cur_ptr += size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    58
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    59
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    60
bool SharedPathsMiscInfo::read(void* ptr, size_t size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    61
  if (_cur_ptr + size <= _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    62
    memcpy(ptr, _cur_ptr, size);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    63
    _cur_ptr += size;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    64
    return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    65
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    66
  return false;
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
bool SharedPathsMiscInfo::fail(const char* msg, const char* name) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    70
  ClassLoader::trace_class_path(msg, name);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    71
  MetaspaceShared::set_archive_loading_failed();
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    72
  return false;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    73
}
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
bool SharedPathsMiscInfo::check() {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    76
  // 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
    77
  // without fear.
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    78
  _end_ptr -= sizeof(jint);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    79
  if (_cur_ptr >= _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    80
    return fail("Truncated archive file header");
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
  if (*_end_ptr != 0) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    83
    return fail("Corrupted archive file header");
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
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    86
  while (_cur_ptr < _end_ptr) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    87
    jint type;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    88
    const char* path = _cur_ptr;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    89
    _cur_ptr += strlen(path) + 1;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    90
    if (!read_jint(&type)) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    91
      return fail("Corrupted archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    92
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    93
    if (TraceClassPaths) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    94
      tty->print("[type=%s ", type_name(type));
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    95
      print_path(tty, type, path);
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    96
      tty->print_cr("]");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    97
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    98
    if (!check(type, path)) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
    99
      if (!PrintSharedArchiveAndExit) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   100
        return false;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   101
      }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   102
    } else {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   103
      trace_class_path("[ok");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   104
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   105
  }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   106
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   107
  return true;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   108
}
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   109
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   110
bool SharedPathsMiscInfo::check(jint type, const char* path) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   111
  switch (type) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   112
  case BOOT:
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   113
    if (strcmp(path, Arguments::get_sysclasspath()) != 0) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   114
      return fail("[BOOT classpath mismatch, actual: -Dsun.boot.class.path=", Arguments::get_sysclasspath());
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   115
    }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   116
    break;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   117
  case NON_EXIST: // fall-through
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   118
  case REQUIRED:
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   119
    {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   120
      struct stat st;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   121
      if (os::stat(path, &st) != 0) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   122
        // The file does not actually exist
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   123
        if (type == REQUIRED) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   124
          // but we require it to exist -> fail
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   125
          return fail("Required file doesn't exist");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   126
        }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   127
      } else {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   128
        // The file actually exists
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   129
        if (type == NON_EXIST) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   130
          // But we want it to not exist -> fail
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   131
          return fail("File must not exist");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   132
        }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   133
        time_t    timestamp;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   134
        long      filesize;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   135
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   136
        if (!read_time(&timestamp) || !read_long(&filesize)) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   137
          return fail("Corrupted archive file header");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   138
        }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   139
        if (timestamp != st.st_mtime) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   140
          return fail("Timestamp mismatch");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   141
        }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   142
        if (filesize  != st.st_size) {
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   143
          return fail("File size mismatch");
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   144
        }
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   145
      }
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
    break;
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   148
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   149
  default:
82b516c550f7 8046070: Class Data Sharing clean up and refactoring
iklam
parents:
diff changeset
   150
    return fail("Corrupted archive file header");
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
}