hotspot/src/share/vm/classfile/classFileStream.cpp
author mgronlun
Tue, 08 Dec 2015 20:04:03 +0100
changeset 34666 1c7168ea0034
parent 26419 25abc4a3285c
child 37248 11a660dbbb8e
permissions -rw-r--r--
8140485: Class load and creation cleanup Reviewed-by: hseigel, coleenp, sspitsyn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
     2
 * Copyright (c) 1997, 2015, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "classfile/classFileStream.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "classfile/vmSymbols.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    29
const bool ClassFileStream::verify = true;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    30
const bool ClassFileStream::no_verification = false;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    31
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    32
void ClassFileStream::truncated_file_error(TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
  THROW_MSG(vmSymbols::java_lang_ClassFormatError(), "Truncated class file");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    36
ClassFileStream::ClassFileStream(const u1* buffer,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    37
                                 int length,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    38
                                 const char* source,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    39
                                 bool verify_stream) :
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    40
  _buffer_start(buffer),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    41
  _buffer_end(buffer + length),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    42
  _current(buffer),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    43
  _source(source),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    44
  _need_verify(verify_stream) {}
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    45
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    46
const u1* ClassFileStream::clone_buffer() const {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    47
  u1* const new_buffer_start = NEW_RESOURCE_ARRAY(u1, length());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    48
  memcpy(new_buffer_start, _buffer_start, length());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    49
  return new_buffer_start;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    52
const char* const ClassFileStream::clone_source() const {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    53
  const char* const src = source();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    54
  char* source_copy = NULL;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    55
  if (src != NULL) {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    56
    size_t source_len = strlen(src);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    57
    source_copy = NEW_RESOURCE_ARRAY(char, source_len + 1);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    58
    strncpy(source_copy, src, source_len + 1);
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    59
  }
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    60
  return source_copy;
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    61
}
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    62
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    63
// Caller responsible for ResourceMark
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    64
// clone stream with a rewound position
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    65
const ClassFileStream* ClassFileStream::clone() const {
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    66
  const u1* const new_buffer_start = clone_buffer();
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    67
  return new ClassFileStream(new_buffer_start,
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    68
                             length(),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    69
                             clone_source(),
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    70
                             need_verify());
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    71
}
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    72
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    73
u1 ClassFileStream::get_u1(TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    guarantee_more(1, CHECK_0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
    assert(1 <= _buffer_end - _current, "buffer overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  return *_current++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    82
u2 ClassFileStream::get_u2(TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
    guarantee_more(2, CHECK_0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
    assert(2 <= _buffer_end - _current, "buffer overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  }
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    88
  const u1* tmp = _current;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  _current += 2;
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    90
  return Bytes::get_Java_u2((address)tmp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    93
u4 ClassFileStream::get_u4(TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    guarantee_more(4, CHECK_0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    assert(4 <= _buffer_end - _current, "buffer overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  }
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
    99
  const u1* tmp = _current;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  _current += 4;
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   101
  return Bytes::get_Java_u4((address)tmp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   104
u8 ClassFileStream::get_u8(TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
    guarantee_more(8, CHECK_0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
    assert(8 <= _buffer_end - _current, "buffer overflow");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  }
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   110
  const u1* tmp = _current;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  _current += 8;
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   112
  return Bytes::get_Java_u8((address)tmp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   115
void ClassFileStream::skip_u1(int length, TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    guarantee_more(length, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  _current += length;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   122
void ClassFileStream::skip_u2(int length, TRAPS) const {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  if (_need_verify) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    guarantee_more(length * 2, CHECK);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  _current += length * 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
}
15102
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   128
34666
1c7168ea0034 8140485: Class load and creation cleanup
mgronlun
parents: 26419
diff changeset
   129
void ClassFileStream::skip_u4(int length, TRAPS) const {
15102
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   130
  if (_need_verify) {
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   131
    guarantee_more(length * 4, CHECK);
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   132
  }
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   133
  _current += length * 4;
0a86564e5f61 8004728: Add hotspot support for parameter reflection
coleenp
parents: 7397
diff changeset
   134
}