1 /*
2 * Copyright (c) 1997, 2018, 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 *
44 #include "memory/referenceType.hpp"
45 #include "memory/universe.inline.hpp"
46 #include "oops/constantPool.hpp"
47 #include "oops/fieldStreams.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/instanceMirrorKlass.hpp"
50 #include "oops/klass.inline.hpp"
51 #include "oops/klassVtable.hpp"
52 #include "oops/method.hpp"
53 #include "oops/symbol.hpp"
54 #include "prims/jvm.h"
55 #include "prims/jvmtiExport.hpp"
56 #include "prims/jvmtiThreadState.hpp"
57 #include "runtime/javaCalls.hpp"
58 #include "runtime/perfData.hpp"
59 #include "runtime/reflection.hpp"
60 #include "runtime/signature.hpp"
61 #include "runtime/timer.hpp"
62 #include "services/classLoadingService.hpp"
63 #include "services/threadService.hpp"
64 #include "utilities/array.hpp"
65 #include "utilities/globalDefinitions.hpp"
66 #include "utilities/ostream.hpp"
67
68 // We generally try to create the oops directly when parsing, rather than
69 // allocating temporary data structures and copying the bytes twice. A
70 // temporary area is only needed when parsing utf8 entries in the constant
71 // pool and when parsing line number tables.
72
73 // We add assert in debug mode when class format is not checked.
74
75 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
76 #define JAVA_MIN_SUPPORTED_VERSION 45
77 #define JAVA_MAX_SUPPORTED_VERSION 52
78 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
79
80 // Used for two backward compatibility reasons:
81 // - to check for new additions to the class file format in JDK1.5
82 // - to check for bug fixes in the format checker in JDK1.5
83 #define JAVA_1_5_VERSION 49
3868 // Constant pool
3869 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
3870
3871 int cp_size = cp->length();
3872
3873 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
3874
3875 // Access flags
3876 AccessFlags access_flags;
3877 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
3878
3879 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3880 // Set abstract bit for old class files for backward compatibility
3881 flags |= JVM_ACC_ABSTRACT;
3882 }
3883 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
3884 access_flags.set_flags(flags);
3885
3886 // This class and superclass
3887 u2 this_class_index = cfs->get_u2_fast();
3888 check_property(
3889 valid_cp_range(this_class_index, cp_size) &&
3890 cp->tag_at(this_class_index).is_unresolved_klass(),
3891 "Invalid this class index %u in constant pool in class file %s",
3892 this_class_index, CHECK_(nullHandle));
3893
3894 Symbol* class_name = cp->unresolved_klass_at(this_class_index);
3895 assert(class_name != NULL, "class_name can't be null");
3896
3897 // It's important to set parsed_name *before* resolving the super class.
3898 // (it's used for cleanup by the caller if parsing fails)
3899 parsed_name = class_name;
3900 // parsed_name is returned and can be used if there's an error, so add to
3901 // its reference count. Caller will decrement the refcount.
3902 parsed_name->increment_refcount();
3903
3904 // Update _class_name which could be null previously to be class_name
3905 _class_name = class_name;
3906
3907 // Don't need to check whether this class name is legal or not.
4198 check_illegal_static_method(this_klass, CHECK_(nullHandle));
4199 }
4200 }
4201
4202 // Allocate mirror and initialize static fields
4203 java_lang_Class::create_mirror(this_klass, class_loader, protection_domain,
4204 CHECK_(nullHandle));
4205
4206 // Generate any default methods - default methods are interface methods
4207 // that have a default implementation. This is new with Lambda project.
4208 if (has_default_methods ) {
4209 DefaultMethods::generate_default_methods(
4210 this_klass(), &all_mirandas, CHECK_(nullHandle));
4211 }
4212
4213 // Update the loader_data graph.
4214 record_defined_class_dependencies(this_klass, CHECK_NULL);
4215
4216 ClassLoadingService::notify_class_loaded(InstanceKlass::cast(this_klass()),
4217 false /* not shared class */);
4218
4219 if (TraceClassLoading) {
4220 ResourceMark rm;
4221 // print in a single call to reduce interleaving of output
4222 if (cfs->source() != NULL) {
4223 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
4224 cfs->source());
4225 } else if (class_loader.is_null()) {
4226 Klass* caller =
4227 THREAD->is_Java_thread()
4228 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
4229 : NULL;
4230 // caller can be NULL, for example, during a JVMTI VM_Init hook
4231 if (caller != NULL) {
4232 tty->print("[Loaded %s by instance of %s]\n",
4233 this_klass->external_name(),
4234 InstanceKlass::cast(caller)->external_name());
4235 } else {
4236 tty->print("[Loaded %s]\n", this_klass->external_name());
4237 }
5283 return NULL;
5284 }
5285 case JVM_SIGNATURE_ARRAY:
5286 array_dim++;
5287 if (array_dim > 255) {
5288 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
5289 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
5290 }
5291 // The rest of what's there better be a legal signature
5292 signature++;
5293 length--;
5294 void_ok = false;
5295 break;
5296
5297 default:
5298 return NULL;
5299 }
5300 }
5301 return NULL;
5302 }
|
1 /*
2 * Copyright (c) 1997, 2019, 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 *
44 #include "memory/referenceType.hpp"
45 #include "memory/universe.inline.hpp"
46 #include "oops/constantPool.hpp"
47 #include "oops/fieldStreams.hpp"
48 #include "oops/instanceKlass.hpp"
49 #include "oops/instanceMirrorKlass.hpp"
50 #include "oops/klass.inline.hpp"
51 #include "oops/klassVtable.hpp"
52 #include "oops/method.hpp"
53 #include "oops/symbol.hpp"
54 #include "prims/jvm.h"
55 #include "prims/jvmtiExport.hpp"
56 #include "prims/jvmtiThreadState.hpp"
57 #include "runtime/javaCalls.hpp"
58 #include "runtime/perfData.hpp"
59 #include "runtime/reflection.hpp"
60 #include "runtime/signature.hpp"
61 #include "runtime/timer.hpp"
62 #include "services/classLoadingService.hpp"
63 #include "services/threadService.hpp"
64 #include "trace/traceMacros.hpp"
65 #include "utilities/array.hpp"
66 #include "utilities/globalDefinitions.hpp"
67 #include "utilities/ostream.hpp"
68
69 // We generally try to create the oops directly when parsing, rather than
70 // allocating temporary data structures and copying the bytes twice. A
71 // temporary area is only needed when parsing utf8 entries in the constant
72 // pool and when parsing line number tables.
73
74 // We add assert in debug mode when class format is not checked.
75
76 #define JAVA_CLASSFILE_MAGIC 0xCAFEBABE
77 #define JAVA_MIN_SUPPORTED_VERSION 45
78 #define JAVA_MAX_SUPPORTED_VERSION 52
79 #define JAVA_MAX_SUPPORTED_MINOR_VERSION 0
80
81 // Used for two backward compatibility reasons:
82 // - to check for new additions to the class file format in JDK1.5
83 // - to check for bug fixes in the format checker in JDK1.5
84 #define JAVA_1_5_VERSION 49
3869 // Constant pool
3870 constantPoolHandle cp = parse_constant_pool(CHECK_(nullHandle));
3871
3872 int cp_size = cp->length();
3873
3874 cfs->guarantee_more(8, CHECK_(nullHandle)); // flags, this_class, super_class, infs_len
3875
3876 // Access flags
3877 AccessFlags access_flags;
3878 jint flags = cfs->get_u2_fast() & JVM_RECOGNIZED_CLASS_MODIFIERS;
3879
3880 if ((flags & JVM_ACC_INTERFACE) && _major_version < JAVA_6_VERSION) {
3881 // Set abstract bit for old class files for backward compatibility
3882 flags |= JVM_ACC_ABSTRACT;
3883 }
3884 verify_legal_class_modifiers(flags, CHECK_(nullHandle));
3885 access_flags.set_flags(flags);
3886
3887 // This class and superclass
3888 u2 this_class_index = cfs->get_u2_fast();
3889 _this_class_index = this_class_index; //used by jfr
3890 check_property(
3891 valid_cp_range(this_class_index, cp_size) &&
3892 cp->tag_at(this_class_index).is_unresolved_klass(),
3893 "Invalid this class index %u in constant pool in class file %s",
3894 this_class_index, CHECK_(nullHandle));
3895
3896 Symbol* class_name = cp->unresolved_klass_at(this_class_index);
3897 assert(class_name != NULL, "class_name can't be null");
3898
3899 // It's important to set parsed_name *before* resolving the super class.
3900 // (it's used for cleanup by the caller if parsing fails)
3901 parsed_name = class_name;
3902 // parsed_name is returned and can be used if there's an error, so add to
3903 // its reference count. Caller will decrement the refcount.
3904 parsed_name->increment_refcount();
3905
3906 // Update _class_name which could be null previously to be class_name
3907 _class_name = class_name;
3908
3909 // Don't need to check whether this class name is legal or not.
4200 check_illegal_static_method(this_klass, CHECK_(nullHandle));
4201 }
4202 }
4203
4204 // Allocate mirror and initialize static fields
4205 java_lang_Class::create_mirror(this_klass, class_loader, protection_domain,
4206 CHECK_(nullHandle));
4207
4208 // Generate any default methods - default methods are interface methods
4209 // that have a default implementation. This is new with Lambda project.
4210 if (has_default_methods ) {
4211 DefaultMethods::generate_default_methods(
4212 this_klass(), &all_mirandas, CHECK_(nullHandle));
4213 }
4214
4215 // Update the loader_data graph.
4216 record_defined_class_dependencies(this_klass, CHECK_NULL);
4217
4218 ClassLoadingService::notify_class_loaded(InstanceKlass::cast(this_klass()),
4219 false /* not shared class */);
4220 TRACE_INIT_ID(InstanceKlass::cast(this_klass()));
4221
4222 if (TraceClassLoading) {
4223 ResourceMark rm;
4224 // print in a single call to reduce interleaving of output
4225 if (cfs->source() != NULL) {
4226 tty->print("[Loaded %s from %s]\n", this_klass->external_name(),
4227 cfs->source());
4228 } else if (class_loader.is_null()) {
4229 Klass* caller =
4230 THREAD->is_Java_thread()
4231 ? ((JavaThread*)THREAD)->security_get_caller_class(1)
4232 : NULL;
4233 // caller can be NULL, for example, during a JVMTI VM_Init hook
4234 if (caller != NULL) {
4235 tty->print("[Loaded %s by instance of %s]\n",
4236 this_klass->external_name(),
4237 InstanceKlass::cast(caller)->external_name());
4238 } else {
4239 tty->print("[Loaded %s]\n", this_klass->external_name());
4240 }
5286 return NULL;
5287 }
5288 case JVM_SIGNATURE_ARRAY:
5289 array_dim++;
5290 if (array_dim > 255) {
5291 // 4277370: array descriptor is valid only if it represents 255 or fewer dimensions.
5292 classfile_parse_error("Array type descriptor has more than 255 dimensions in class file %s", CHECK_0);
5293 }
5294 // The rest of what's there better be a legal signature
5295 signature++;
5296 length--;
5297 void_ok = false;
5298 break;
5299
5300 default:
5301 return NULL;
5302 }
5303 }
5304 return NULL;
5305 }
5306
5307 const ClassFileStream* ClassFileParser::clone_stream() const {
5308 assert(_stream != NULL, "invariant");
5309 return _stream->clone();
5310 }
5311
5312 void ClassFileParser::set_klass_to_deallocate(InstanceKlass* klass) {
5313
5314 #ifdef ASSERT
5315 if (klass != NULL) {
5316 assert(NULL == _klass, "leaking?");
5317 }
5318 #endif
5319
5320 _klass = klass;
5321 }
|