1 /*
   2  * Copyright (c) 2014, 2015, 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 #ifndef SHARE_VM_CLASSFILE_SHAREDPATHSMISCINFO_HPP
  26 #define SHARE_VM_CLASSFILE_SHAREDPATHSMISCINFO_HPP
  27 
  28 #include "classfile/classLoader.hpp"
  29 #include "runtime/os.hpp"
  30 
  31 // During dumping time, when processing class paths, we build up the dump-time
  32 // classpath. The JAR files that exist are stored in the list ClassLoader::_first_entry.
  33 // However, we need to store other "misc" information for run-time checking, such as
  34 //
  35 // + The values of Arguments::get_sysclasspath() used during dumping.
  36 //
  37 // + The meta-index file(s) used during dumping (incl modification time and size)
  38 //
  39 // + The class path elements specified during dumping but did not exist --
  40 //   these elements must also be specified at run time, and they also must not
  41 //   exist at run time.
  42 //
  43 // These misc items are stored in a linear buffer in SharedPathsMiscInfo.
  44 // The storage format is stream oriented to minimize its size.
  45 //
  46 // When writing the information to the archive file, SharedPathsMiscInfo is stored in
  47 // the archive file header. At run-time, this information is used only during initialization
  48 // (accessed using read() instead of mmap()), and is deallocated afterwards to save space.
  49 //
  50 // The SharedPathsMiscInfo class is used for both creating the the information (during
  51 // dumping time) and validation (at run time). Different constructors are used in the
  52 // two situations. See below.
  53 
  54 class SharedPathsMiscInfo : public CHeapObj<mtClass> {
  55 protected:
  56   char* _buf_start;
  57   char* _cur_ptr;
  58   char* _end_ptr;
  59   int   _buf_size;
  60   bool  _allocated;   // was _buf_start allocated by me?
  61   void ensure_size(size_t needed_bytes);
  62   void add_path(const char* path, int type);
  63 
  64   void write(const void* ptr, size_t size);
  65   bool read(void* ptr, size_t size);
  66 
  67   static void trace_class_path(const char* msg, const char* name = NULL) {
  68     ClassLoader::trace_class_path(tty, msg, name);
  69   }
  70 protected:
  71   static bool fail(const char* msg, const char* name = NULL);
  72   virtual bool check(jint type, const char* path);
  73 
  74 public:
  75   enum {
  76     INITIAL_BUF_SIZE = 128
  77   };
  78   // This constructor is used when creating the misc information (during dump)
  79   SharedPathsMiscInfo() {
  80     _buf_size = INITIAL_BUF_SIZE;
  81     _cur_ptr = _buf_start = NEW_C_HEAP_ARRAY(char, _buf_size, mtClass);
  82     _allocated = true;
  83   }
  84   // This constructor is used when validating the misc info (during run time)
  85   SharedPathsMiscInfo(char *buff, int size) {
  86     _cur_ptr = _buf_start = buff;
  87     _end_ptr = _buf_start + size;
  88     _buf_size = size;
  89     _allocated = false;
  90   }
  91   ~SharedPathsMiscInfo() {
  92     if (_allocated) {
  93       FREE_C_HEAP_ARRAY(char, _buf_start);
  94     }
  95   }
  96   int get_used_bytes() {
  97     return _cur_ptr - _buf_start;
  98   }
  99   void* buffer() {
 100     return _buf_start;
 101   }
 102 
 103   // writing --
 104 
 105   // The path must not exist at run-time
 106   void add_nonexist_path(const char* path) {
 107     add_path(path, NON_EXIST);
 108   }
 109 
 110   // The path must exist, and must contain exactly <num_entries> files/dirs
 111   void add_boot_classpath(const char* path) {
 112     add_path(path, BOOT);
 113   }
 114   int write_jint(jint num) {
 115     write(&num, sizeof(num));
 116     return 0;
 117   }
 118   void write_time(time_t t) {
 119     write(&t, sizeof(t));
 120   }
 121   void write_long(long l) {
 122     write(&l, sizeof(l));
 123   }
 124 
 125   bool dump_to_file(int fd) {
 126     int n = get_used_bytes();
 127     return (os::write(fd, _buf_start, n) == (size_t)n);
 128   }
 129 
 130   // reading --
 131 
 132   enum {
 133     BOOT      = 1,
 134     NON_EXIST = 2,
 135     REQUIRED  = 3
 136   };
 137 
 138   virtual const char* type_name(int type) {
 139     switch (type) {
 140     case BOOT:      return "BOOT";
 141     case NON_EXIST: return "NON_EXIST";
 142     case REQUIRED:  return "REQUIRED";
 143     default:        ShouldNotReachHere(); return "?";
 144     }
 145   }
 146 
 147   virtual void print_path(outputStream* out, int type, const char* path) {
 148     switch (type) {
 149     case BOOT:
 150       out->print("Expecting -Dsun.boot.class.path=%s", path);
 151       break;
 152     case NON_EXIST:
 153       out->print("Expecting that %s does not exist", path);
 154       break;
 155     case REQUIRED:
 156       out->print("Expecting that file %s must exist and is not altered", path);
 157       break;
 158     default:
 159       ShouldNotReachHere();
 160     }
 161   }
 162 
 163   bool check();
 164   bool read_jint(jint *ptr) {
 165     return read(ptr, sizeof(jint));
 166   }
 167   bool read_long(long *ptr) {
 168     return read(ptr, sizeof(long));
 169   }
 170   bool read_time(time_t *ptr) {
 171     return read(ptr, sizeof(time_t));
 172   }
 173 };
 174 
 175 #endif // SHARE_VM_CLASSFILE_SHAREDPATHSMISCINFO_HPP