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