138 const ClassFileParser& parser) {
139 assert(class_name != NULL, "invariant");
140
141 if (class_name == vmSymbols::java_lang_ClassLoader()) {
142 return true;
143 }
144
145 if (SystemDictionary::ClassLoader_klass_loaded()) {
146 const Klass* const super_klass = parser.super_klass();
147 if (super_klass != NULL) {
148 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
149 return true;
150 }
151 }
152 }
153 return false;
154 }
155
156 // called to verify that k is a member of this nest
157 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
158 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
159 if (log_is_enabled(Trace, class, nestmates)) {
160 ResourceMark rm(THREAD);
161 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
162 k->external_name(), this->external_name());
163 }
164 return false;
165 }
166
167 if (log_is_enabled(Trace, class, nestmates)) {
168 ResourceMark rm(THREAD);
169 log_trace(class, nestmates)("Checking nest membership of %s in %s",
170 k->external_name(), this->external_name());
171 }
172
173 // Check for a resolved cp entry , else fall back to a name check.
174 // We don't want to resolve any class other than the one being checked.
175 for (int i = 0; i < _nest_members->length(); i++) {
176 int cp_index = _nest_members->at(i);
177 if (_constants->tag_at(cp_index).is_klass()) {
195 }
196
197 Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
198 if (k2 == k) {
199 log_trace(class, nestmates)("- class is listed as a nest member");
200 return true;
201 }
202 else {
203 // same name but different klass!
204 log_trace(class, nestmates)(" - klass comparison failed!");
205 // can't have two names the same, so we're done
206 return false;
207 }
208 }
209 }
210 }
211 log_trace(class, nestmates)("- class is NOT a nest member!");
212 return false;
213 }
214
215 // Return nest-host class, resolving, validating and saving it if needed.
216 // In cases where this is called from a thread that can not do classloading
217 // (such as a native JIT thread) then we simply return NULL, which in turn
218 // causes the access check to return false. Such code will retry the access
219 // from a more suitable environment later.
220 InstanceKlass* InstanceKlass::nest_host(Symbol* validationException, TRAPS) {
221 InstanceKlass* nest_host_k = _nest_host;
222 if (nest_host_k == NULL) {
223 // need to resolve and save our nest-host class. This could be attempted
224 // concurrently but as the result is idempotent and we don't use the class
225 // then we do not need any synchronization beyond what is implicitly used
226 // during class loading.
227 if (_nest_host_index != 0) { // we have a real nest_host
228 // Before trying to resolve check if we're in a suitable context
229 if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
230 if (log_is_enabled(Trace, class, nestmates)) {
231 ResourceMark rm(THREAD);
232 log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
233 this->external_name());
234 }
235 return NULL;
236 }
237
238 if (log_is_enabled(Trace, class, nestmates)) {
239 ResourceMark rm(THREAD);
240 log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
241 this->external_name(),
242 _constants->klass_name_at(_nest_host_index)->as_C_string());
243 }
244
245 Klass* k = _constants->klass_at(_nest_host_index, THREAD);
246 if (HAS_PENDING_EXCEPTION) {
247 Handle exc_h = Handle(THREAD, PENDING_EXCEPTION);
248 if (exc_h->is_a(SystemDictionary::NoClassDefFoundError_klass())) {
249 // throw a new CDNFE with the original as its cause, and a clear msg
250 ResourceMark rm(THREAD);
251 char buf[200];
252 CLEAR_PENDING_EXCEPTION;
253 jio_snprintf(buf, sizeof(buf),
254 "Unable to load nest-host class (%s) of %s",
255 _constants->klass_name_at(_nest_host_index)->as_C_string(),
256 this->external_name());
257 log_trace(class, nestmates)("%s - NoClassDefFoundError", buf);
258 THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), buf, exc_h);
259 }
260 // All other exceptions pass through (OOME, StackOverflowError, LinkageErrors etc).
261 return NULL;
262 }
263
264 // A valid nest-host is an instance class in the current package that lists this
265 // class as a nest member. If any of these conditions are not met we post the
266 // requested exception type (if any) and return NULL
267
268 const char* error = NULL;
269
314 this->class_loader_data()->loader_name_and_id(),
315 k->external_name(),
316 k->class_loader_data()->loader_name_and_id(),
317 error
318 );
319 }
320 return NULL;
321 } else {
322 if (log_is_enabled(Trace, class, nestmates)) {
323 ResourceMark rm(THREAD);
324 log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
325 this->external_name());
326 }
327 // save resolved nest-host value
328 return (_nest_host = this);
329 }
330 }
331 return nest_host_k;
332 }
333
334 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
335 // or we are k's nest_host - all of which is covered by comparing the two
336 // resolved_nest_hosts
337 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
338
339 assert(this != k, "this should be handled by higher-level code");
340
341 // Per JVMS 5.4.4 we first resolve and validate the current class, then
342 // the target class k. Resolution exceptions will be passed on by upper
343 // layers. IncompatibleClassChangeErrors from membership validation failures
344 // will also be passed through.
345
346 Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
347 InstanceKlass* cur_host = nest_host(icce, CHECK_false);
348 if (cur_host == NULL) {
349 return false;
350 }
351
352 Klass* k_nest_host = k->nest_host(icce, CHECK_false);
353 if (k_nest_host == NULL) {
354 return false;
355 }
356
357 bool access = (cur_host == k_nest_host);
358
359 if (log_is_enabled(Trace, class, nestmates)) {
360 ResourceMark rm(THREAD);
361 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
362 this->external_name(),
363 access ? "" : "NOT ",
364 k->external_name());
365 }
366
367 return access;
368 }
369
370 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
371 const int size = InstanceKlass::size(parser.vtable_size(),
372 parser.itable_size(),
373 nonstatic_oop_map_size(parser.total_oop_map_count()),
374 parser.is_interface(),
375 parser.is_unsafe_anonymous(),
376 should_store_fingerprint(parser.is_unsafe_anonymous()));
377
378 const Symbol* const class_name = parser.class_name();
379 assert(class_name != NULL, "invariant");
380 ClassLoaderData* loader_data = parser.loader_data();
381 assert(loader_data != NULL, "invariant");
382
383 InstanceKlass* ik;
384
385 // Allocation
386 if (REF_NONE == parser.reference_type()) {
387 if (class_name == vmSymbols::java_lang_Class()) {
388 // mirror
389 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
390 }
391 else if (is_class_loader(class_name, parser)) {
392 // class loader
393 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
394 } else {
395 // normal
396 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
429 assert(default_vtable_indices() == NULL, "only create once");
430 set_default_vtable_indices(vtable_indices);
431 return vtable_indices;
432 }
433
434 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
435 Klass(id),
436 _nest_members(NULL),
437 _nest_host_index(0),
438 _nest_host(NULL),
439 _static_field_size(parser.static_field_size()),
440 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
441 _itable_len(parser.itable_size()),
442 _init_thread(NULL),
443 _init_state(allocated),
444 _reference_type(parser.reference_type())
445 {
446 set_vtable_length(parser.vtable_size());
447 set_kind(kind);
448 set_access_flags(parser.access_flags());
449 set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
450 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
451 false));
452
453 assert(NULL == _methods, "underlying memory not zeroed?");
454 assert(is_instance_klass(), "is layout incorrect?");
455 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
456
457 if (Arguments::is_dumping_archive()) {
458 SystemDictionaryShared::init_dumptime_info(this);
459 }
460
461 // Set biased locking bit for all instances of this class; it will be
462 // cleared if revocation occurs too often for this type
463 if (UseBiasedLocking && BiasedLocking::enabled()) {
464 set_prototype_header(markWord::biased_locking_prototype());
465 }
466 }
467
468 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
2231 log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
2232 return false;
2233 }
2234
2235 Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
2236 if (local_interfaces != NULL) {
2237 int length = local_interfaces->length();
2238 for (int i = 0; i < length; i++) {
2239 InstanceKlass* intf = local_interfaces->at(i);
2240 if (!intf->has_passed_fingerprint_check()) {
2241 ResourceMark rm;
2242 log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
2243 return false;
2244 }
2245 }
2246 }
2247
2248 return true;
2249 }
2250
2251 bool InstanceKlass::should_store_fingerprint(bool is_unsafe_anonymous) {
2252 #if INCLUDE_AOT
2253 // We store the fingerprint into the InstanceKlass only in the following 2 cases:
2254 if (CalculateClassFingerprint) {
2255 // (1) We are running AOT to generate a shared library.
2256 return true;
2257 }
2258 if (Arguments::is_dumping_archive()) {
2259 // (2) We are running -Xshare:dump or -XX:ArchiveClassesAtExit to create a shared archive
2260 return true;
2261 }
2262 if (UseAOT && is_unsafe_anonymous) {
2263 // (3) We are using AOT code from a shared library and see an unsafe anonymous class
2264 return true;
2265 }
2266 #endif
2267
2268 // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
2269 // but do not store the 64-bit fingerprint to save space.
2270 return false;
2271 }
2272
2273 bool InstanceKlass::has_stored_fingerprint() const {
2274 #if INCLUDE_AOT
2275 return should_store_fingerprint() || is_shared();
2276 #else
2277 return false;
2278 #endif
2279 }
2280
2281 uint64_t InstanceKlass::get_stored_fingerprint() const {
2282 address adr = adr_fingerprint();
2283 if (adr != NULL) {
2568
2569 assert(_dep_context == NULL,
2570 "dependencies should already be cleaned");
2571
2572 #if INCLUDE_JVMTI
2573 // Deallocate breakpoint records
2574 if (breakpoints() != 0x0) {
2575 methods_do(clear_all_breakpoints);
2576 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2577 }
2578
2579 // deallocate the cached class file
2580 if (_cached_class_file != NULL) {
2581 os::free(_cached_class_file);
2582 _cached_class_file = NULL;
2583 }
2584 #endif
2585
2586 // Decrement symbol reference counts associated with the unloaded class.
2587 if (_name != NULL) _name->decrement_refcount();
2588 // unreference array name derived from this class name (arrays of an unloaded
2589 // class can't be referenced anymore).
2590 if (_array_name != NULL) _array_name->decrement_refcount();
2591 FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2592 }
2593
2594 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2595 if (array == NULL) {
2596 _source_debug_extension = NULL;
2597 } else {
2598 // Adding one to the attribute length in order to store a null terminator
2599 // character could cause an overflow because the attribute length is
2600 // already coded with an u4 in the classfile, but in practice, it's
2601 // unlikely to happen.
2602 assert((length+1) > length, "Overflow checking");
2603 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2604 for (int i = 0; i < length; i++) {
2605 sde[i] = array[i];
2606 }
2607 sde[length] = '\0';
2654 if (name->utf8_length() <= 0) {
2655 return NULL;
2656 }
2657 ResourceMark rm;
2658 const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2659 if (package_name == NULL) {
2660 return NULL;
2661 }
2662 Symbol* pkg_name = SymbolTable::new_symbol(package_name);
2663 return pkg_name;
2664 }
2665 }
2666
2667 ModuleEntry* InstanceKlass::module() const {
2668 // For an unsafe anonymous class return the host class' module
2669 if (is_unsafe_anonymous()) {
2670 assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2671 return unsafe_anonymous_host()->module();
2672 }
2673
2674 // Class is in a named package
2675 if (!in_unnamed_package()) {
2676 return _package_entry->module();
2677 }
2678
2679 // Class is in an unnamed package, return its loader's unnamed module
2680 return class_loader_data()->unnamed_module();
2681 }
2682
2683 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2684
2685 // ensure java/ packages only loaded by boot or platform builtin loaders
2686 check_prohibited_package(name(), loader_data, CHECK);
2687
2688 TempNewSymbol pkg_name = package_from_name(name(), CHECK);
2689
2690 if (pkg_name != NULL && loader_data != NULL) {
2691
2692 // Find in class loader's package entry table.
2693 _package_entry = loader_data->packages()->lookup_only(pkg_name);
2865 }
2866 }
2867 }
2868 }
2869 return false;
2870 }
2871
2872 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
2873 InstanceKlass* outer_klass = NULL;
2874 *inner_is_member = false;
2875 int ooff = 0, noff = 0;
2876 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
2877 if (has_inner_classes_attr) {
2878 constantPoolHandle i_cp(THREAD, constants());
2879 if (ooff != 0) {
2880 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
2881 outer_klass = InstanceKlass::cast(ok);
2882 *inner_is_member = true;
2883 }
2884 if (NULL == outer_klass) {
2885 // It may be unsafe anonymous; try for that.
2886 int encl_method_class_idx = enclosing_method_class_index();
2887 if (encl_method_class_idx != 0) {
2888 Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
2889 outer_klass = InstanceKlass::cast(ok);
2890 *inner_is_member = false;
2891 }
2892 }
2893 }
2894
2895 // If no inner class attribute found for this class.
2896 if (NULL == outer_klass) return NULL;
2897
2898 // Throws an exception if outer klass has not declared k as an inner klass
2899 // We need evidence that each klass knows about the other, or else
2900 // the system could allow a spoof of an inner class to gain access rights.
2901 Reflection::check_for_inner_class(outer_klass, this, *inner_is_member, CHECK_NULL);
2902 return outer_klass;
2903 }
2904
2905 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
|
138 const ClassFileParser& parser) {
139 assert(class_name != NULL, "invariant");
140
141 if (class_name == vmSymbols::java_lang_ClassLoader()) {
142 return true;
143 }
144
145 if (SystemDictionary::ClassLoader_klass_loaded()) {
146 const Klass* const super_klass = parser.super_klass();
147 if (super_klass != NULL) {
148 if (super_klass->is_subtype_of(SystemDictionary::ClassLoader_klass())) {
149 return true;
150 }
151 }
152 }
153 return false;
154 }
155
156 // called to verify that k is a member of this nest
157 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
158 assert(!is_hidden(), "unexpected hidden class");
159 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
160 if (log_is_enabled(Trace, class, nestmates)) {
161 ResourceMark rm(THREAD);
162 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
163 k->external_name(), this->external_name());
164 }
165 return false;
166 }
167
168 if (log_is_enabled(Trace, class, nestmates)) {
169 ResourceMark rm(THREAD);
170 log_trace(class, nestmates)("Checking nest membership of %s in %s",
171 k->external_name(), this->external_name());
172 }
173
174 // Check for a resolved cp entry , else fall back to a name check.
175 // We don't want to resolve any class other than the one being checked.
176 for (int i = 0; i < _nest_members->length(); i++) {
177 int cp_index = _nest_members->at(i);
178 if (_constants->tag_at(cp_index).is_klass()) {
196 }
197
198 Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
199 if (k2 == k) {
200 log_trace(class, nestmates)("- class is listed as a nest member");
201 return true;
202 }
203 else {
204 // same name but different klass!
205 log_trace(class, nestmates)(" - klass comparison failed!");
206 // can't have two names the same, so we're done
207 return false;
208 }
209 }
210 }
211 }
212 log_trace(class, nestmates)("- class is NOT a nest member!");
213 return false;
214 }
215
216 InstanceKlass* InstanceKlass::runtime_nest_host(TRAPS) {
217 // TODO: nest_host returns NULL if validation fails. Need to follow up
218 // the specification when to evaluate the runtime nest host. Right now
219 // it's only determined when a dynamic nestmate is added.
220 InstanceKlass* nest_host_k = nest_host(NULL, CHECK_NULL);
221 if (nest_host_k == NULL) {
222 assert(_nest_host == NULL, "should fail to validate NestNost");
223 // drop the static nest information; set dynamic nest to this class
224 if (log_is_enabled(Trace, class, nestmates)) {
225 ResourceMark rm(THREAD);
226 log_trace(class, nestmates)("Fail to validate static nest host of %s: setting nest-host to self",
227 this->external_name());
228 }
229 _nest_host = nest_host_k = this;
230 }
231 return nest_host_k;
232 }
233
234 // Return nest-host class, resolving, validating and saving it if needed.
235 // In cases where this is called from a thread that can not do classloading
236 // (such as a native JIT thread) then we simply return NULL, which in turn
237 // causes the access check to return false. Such code will retry the access
238 // from a more suitable environment later.
239 InstanceKlass* InstanceKlass::nest_host(Symbol* validationException, TRAPS) {
240 InstanceKlass* nest_host_k = _nest_host;
241 if (nest_host_k == NULL) {
242 // need to resolve and save our nest-host class. This could be attempted
243 // concurrently but as the result is idempotent and we don't use the class
244 // then we do not need any synchronization beyond what is implicitly used
245 // during class loading.
246 if (_nest_host_index != 0) { // we have a real nest_host
247 // Before trying to resolve check if we're in a suitable context
248 if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
249 if (log_is_enabled(Trace, class, nestmates)) {
250 ResourceMark rm(THREAD);
251 log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
252 this->external_name());
253 }
254 return NULL;
255 }
256
257 if (log_is_enabled(Trace, class, nestmates)) {
258 ResourceMark rm(THREAD);
259 log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
260 this->external_name(),
261 _constants->klass_name_at(_nest_host_index)->as_C_string());
262 }
263
264 Klass* k = _constants->klass_at(_nest_host_index, THREAD);
265 if (HAS_PENDING_EXCEPTION) {
266 Handle exc_h = Handle(THREAD, PENDING_EXCEPTION);
267 if (validationException == NULL && exc_h->is_a(SystemDictionary::LinkageError_klass())) {
268 // clear exception if fails to resolve the nest host class
269 CLEAR_PENDING_EXCEPTION;
270 }
271 // throw a new NCDFE with the original as its cause, and a clear msg
272 if (exc_h->is_a(SystemDictionary::NoClassDefFoundError_klass()) && validationException != NULL) {
273 // throw a new NCDFE with the original as its cause, and a clear msg
274 ResourceMark rm(THREAD);
275 char buf[200];
276 CLEAR_PENDING_EXCEPTION;
277 jio_snprintf(buf, sizeof(buf),
278 "Unable to load nest-host class (%s) of %s",
279 _constants->klass_name_at(_nest_host_index)->as_C_string(),
280 this->external_name());
281 log_trace(class, nestmates)("%s - NoClassDefFoundError", buf);
282 THROW_MSG_CAUSE_NULL(vmSymbols::java_lang_NoClassDefFoundError(), buf, exc_h);
283 }
284 // All other exceptions pass through (OOME, StackOverflowError, LinkageErrors etc).
285 return NULL;
286 }
287
288 // A valid nest-host is an instance class in the current package that lists this
289 // class as a nest member. If any of these conditions are not met we post the
290 // requested exception type (if any) and return NULL
291
292 const char* error = NULL;
293
338 this->class_loader_data()->loader_name_and_id(),
339 k->external_name(),
340 k->class_loader_data()->loader_name_and_id(),
341 error
342 );
343 }
344 return NULL;
345 } else {
346 if (log_is_enabled(Trace, class, nestmates)) {
347 ResourceMark rm(THREAD);
348 log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
349 this->external_name());
350 }
351 // save resolved nest-host value
352 return (_nest_host = this);
353 }
354 }
355 return nest_host_k;
356 }
357
358
359 // Dynamic nest member support: set this class's nest host to the given class.
360 // This occurs as part of the class definition, as soon as the instanceKlass
361 // has been created and doesn't require further resolution. The code:
362 // lookup().defineHiddenClass(bytes_for_X, NESTMATE);
363 // results in:
364 // class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
365 // If it has an explicit _nest_host_index or _nest_members, these will be ignored.
366 // We also know the "host" is a valid nest-host in the same package so we can
367 // assert some of those facts.
368 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
369 assert(is_hidden(), "must be a hidden class");
370 assert(host != NULL, "NULL nest host specified");
371 assert(_nest_host == NULL, "current class has resolved nest-host");
372 assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
373 (host->_nest_host == host), "proposed host is not a valid nest-host");
374 // Can't assert this as package is not set yet:
375 // assert(is_same_class_package(host), "proposed host is in wrong package");
376
377 if (log_is_enabled(Trace, class, nestmates)) {
378 ResourceMark rm(THREAD);
379 // a hidden class does not expect a statically defined nest-host
380 if (_nest_host_index > 0) {
381 log_trace(class, nestmates)("Type %s is a dynamic nest member of %s: the NestHost attribute in the current class is ignored",
382 this->external_name(),
383 host->external_name());
384 } else if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
385 log_trace(class, nestmates)("Type %s is a dynamic nest member of %s: the NestMembers attribute in the current class is ignored",
386 this->external_name(),
387 host->external_name());
388 }
389 }
390 // set dynamic nest host
391 _nest_host = host;
392 }
393
394 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
395 // or we are k's nest_host - all of which is covered by comparing the two
396 // resolved_nest_hosts
397 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
398
399 assert(this != k, "this should be handled by higher-level code");
400
401 // Per JVMS 5.4.4 we first resolve and validate the current class, then
402 // the target class k. Resolution exceptions will be passed on by upper
403 // layers. IncompatibleClassChangeErrors from membership validation failures
404 // will also be passed through.
405
406 Symbol* icce = vmSymbols::java_lang_IncompatibleClassChangeError();
407 InstanceKlass* cur_host = nest_host(icce, CHECK_false);
408 if (cur_host == NULL) {
409 return false;
410 }
411
412 Klass* k_nest_host = k->nest_host(icce, CHECK_false);
413 if (k_nest_host == NULL) {
414 return false;
415 }
416
417 bool access = (cur_host == k_nest_host);
418
419 if (log_is_enabled(Trace, class, nestmates)) {
420 ResourceMark rm(THREAD);
421 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
422 this->external_name(),
423 access ? "" : "NOT ",
424 k->external_name());
425 }
426
427 return access;
428 }
429
430 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
431 bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
432 const int size = InstanceKlass::size(parser.vtable_size(),
433 parser.itable_size(),
434 nonstatic_oop_map_size(parser.total_oop_map_count()),
435 parser.is_interface(),
436 is_hidden_or_anonymous,
437 should_store_fingerprint(is_hidden_or_anonymous));
438
439 const Symbol* const class_name = parser.class_name();
440 assert(class_name != NULL, "invariant");
441 ClassLoaderData* loader_data = parser.loader_data();
442 assert(loader_data != NULL, "invariant");
443
444 InstanceKlass* ik;
445
446 // Allocation
447 if (REF_NONE == parser.reference_type()) {
448 if (class_name == vmSymbols::java_lang_Class()) {
449 // mirror
450 ik = new (loader_data, size, THREAD) InstanceMirrorKlass(parser);
451 }
452 else if (is_class_loader(class_name, parser)) {
453 // class loader
454 ik = new (loader_data, size, THREAD) InstanceClassLoaderKlass(parser);
455 } else {
456 // normal
457 ik = new (loader_data, size, THREAD) InstanceKlass(parser, InstanceKlass::_misc_kind_other);
490 assert(default_vtable_indices() == NULL, "only create once");
491 set_default_vtable_indices(vtable_indices);
492 return vtable_indices;
493 }
494
495 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
496 Klass(id),
497 _nest_members(NULL),
498 _nest_host_index(0),
499 _nest_host(NULL),
500 _static_field_size(parser.static_field_size()),
501 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
502 _itable_len(parser.itable_size()),
503 _init_thread(NULL),
504 _init_state(allocated),
505 _reference_type(parser.reference_type())
506 {
507 set_vtable_length(parser.vtable_size());
508 set_kind(kind);
509 set_access_flags(parser.access_flags());
510 if (parser.is_hidden()) set_is_hidden();
511 set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
512 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
513 false));
514
515 assert(NULL == _methods, "underlying memory not zeroed?");
516 assert(is_instance_klass(), "is layout incorrect?");
517 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
518
519 if (Arguments::is_dumping_archive()) {
520 SystemDictionaryShared::init_dumptime_info(this);
521 }
522
523 // Set biased locking bit for all instances of this class; it will be
524 // cleared if revocation occurs too often for this type
525 if (UseBiasedLocking && BiasedLocking::enabled()) {
526 set_prototype_header(markWord::biased_locking_prototype());
527 }
528 }
529
530 void InstanceKlass::deallocate_methods(ClassLoaderData* loader_data,
2293 log_trace(class, fingerprint)("%s : super %s not fingerprinted", external_name(), java_super()->external_name());
2294 return false;
2295 }
2296
2297 Array<InstanceKlass*>* local_interfaces = this->local_interfaces();
2298 if (local_interfaces != NULL) {
2299 int length = local_interfaces->length();
2300 for (int i = 0; i < length; i++) {
2301 InstanceKlass* intf = local_interfaces->at(i);
2302 if (!intf->has_passed_fingerprint_check()) {
2303 ResourceMark rm;
2304 log_trace(class, fingerprint)("%s : interface %s not fingerprinted", external_name(), intf->external_name());
2305 return false;
2306 }
2307 }
2308 }
2309
2310 return true;
2311 }
2312
2313 bool InstanceKlass::should_store_fingerprint(bool is_hidden_or_anonymous) {
2314 #if INCLUDE_AOT
2315 // We store the fingerprint into the InstanceKlass only in the following 2 cases:
2316 if (CalculateClassFingerprint) {
2317 // (1) We are running AOT to generate a shared library.
2318 return true;
2319 }
2320 if (Arguments::is_dumping_archive()) {
2321 // (2) We are running -Xshare:dump or -XX:ArchiveClassesAtExit to create a shared archive
2322 return true;
2323 }
2324 if (UseAOT && is_hidden_or_anonymous) {
2325 // (3) We are using AOT code from a shared library and see a hidden or unsafe anonymous class
2326 return true;
2327 }
2328 #endif
2329
2330 // In all other cases we might set the _misc_has_passed_fingerprint_check bit,
2331 // but do not store the 64-bit fingerprint to save space.
2332 return false;
2333 }
2334
2335 bool InstanceKlass::has_stored_fingerprint() const {
2336 #if INCLUDE_AOT
2337 return should_store_fingerprint() || is_shared();
2338 #else
2339 return false;
2340 #endif
2341 }
2342
2343 uint64_t InstanceKlass::get_stored_fingerprint() const {
2344 address adr = adr_fingerprint();
2345 if (adr != NULL) {
2630
2631 assert(_dep_context == NULL,
2632 "dependencies should already be cleaned");
2633
2634 #if INCLUDE_JVMTI
2635 // Deallocate breakpoint records
2636 if (breakpoints() != 0x0) {
2637 methods_do(clear_all_breakpoints);
2638 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2639 }
2640
2641 // deallocate the cached class file
2642 if (_cached_class_file != NULL) {
2643 os::free(_cached_class_file);
2644 _cached_class_file = NULL;
2645 }
2646 #endif
2647
2648 // Decrement symbol reference counts associated with the unloaded class.
2649 if (_name != NULL) _name->decrement_refcount();
2650
2651 // unreference array name derived from this class name (arrays of an unloaded
2652 // class can't be referenced anymore).
2653 if (_array_name != NULL) _array_name->decrement_refcount();
2654 FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2655 }
2656
2657 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2658 if (array == NULL) {
2659 _source_debug_extension = NULL;
2660 } else {
2661 // Adding one to the attribute length in order to store a null terminator
2662 // character could cause an overflow because the attribute length is
2663 // already coded with an u4 in the classfile, but in practice, it's
2664 // unlikely to happen.
2665 assert((length+1) > length, "Overflow checking");
2666 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2667 for (int i = 0; i < length; i++) {
2668 sde[i] = array[i];
2669 }
2670 sde[length] = '\0';
2717 if (name->utf8_length() <= 0) {
2718 return NULL;
2719 }
2720 ResourceMark rm;
2721 const char* package_name = ClassLoader::package_from_name((const char*) name->as_C_string());
2722 if (package_name == NULL) {
2723 return NULL;
2724 }
2725 Symbol* pkg_name = SymbolTable::new_symbol(package_name);
2726 return pkg_name;
2727 }
2728 }
2729
2730 ModuleEntry* InstanceKlass::module() const {
2731 // For an unsafe anonymous class return the host class' module
2732 if (is_unsafe_anonymous()) {
2733 assert(unsafe_anonymous_host() != NULL, "unsafe anonymous class must have a host class");
2734 return unsafe_anonymous_host()->module();
2735 }
2736
2737 if (is_hidden() &&
2738 in_unnamed_package() &&
2739 class_loader_data()->is_shortlived()) {
2740 // For a weak hidden class defined to an unnamed package,
2741 // the short-lived CLD will not have an unnamed module created for it.
2742 // Two choices to find the correct ModuleEntry:
2743 // 1. If hidden class is within a nest, use nest host's module
2744 // 2. Find the unnamed module off from the class loader
2745 // For now option #2 is used since a nest host is not set until
2746 // after the instance class is created in jvm_lookup_define_class().
2747 if (class_loader_data()->is_boot_class_loader_data()) {
2748 return ClassLoaderData::the_null_class_loader_data()->unnamed_module();
2749 } else {
2750 oop module = java_lang_ClassLoader::unnamedModule(class_loader_data()->class_loader());
2751 assert(java_lang_Module::is_instance(module), "Not an instance of java.lang.Module");
2752 return java_lang_Module::module_entry(module);
2753 }
2754 }
2755
2756 // Class is in a named package
2757 if (!in_unnamed_package()) {
2758 return _package_entry->module();
2759 }
2760
2761 // Class is in an unnamed package, return its loader's unnamed module
2762 return class_loader_data()->unnamed_module();
2763 }
2764
2765 void InstanceKlass::set_package(ClassLoaderData* loader_data, TRAPS) {
2766
2767 // ensure java/ packages only loaded by boot or platform builtin loaders
2768 check_prohibited_package(name(), loader_data, CHECK);
2769
2770 TempNewSymbol pkg_name = package_from_name(name(), CHECK);
2771
2772 if (pkg_name != NULL && loader_data != NULL) {
2773
2774 // Find in class loader's package entry table.
2775 _package_entry = loader_data->packages()->lookup_only(pkg_name);
2947 }
2948 }
2949 }
2950 }
2951 return false;
2952 }
2953
2954 InstanceKlass* InstanceKlass::compute_enclosing_class(bool* inner_is_member, TRAPS) const {
2955 InstanceKlass* outer_klass = NULL;
2956 *inner_is_member = false;
2957 int ooff = 0, noff = 0;
2958 bool has_inner_classes_attr = find_inner_classes_attr(&ooff, &noff, THREAD);
2959 if (has_inner_classes_attr) {
2960 constantPoolHandle i_cp(THREAD, constants());
2961 if (ooff != 0) {
2962 Klass* ok = i_cp->klass_at(ooff, CHECK_NULL);
2963 outer_klass = InstanceKlass::cast(ok);
2964 *inner_is_member = true;
2965 }
2966 if (NULL == outer_klass) {
2967 // It may be a local or anonymous class; try for that.
2968 int encl_method_class_idx = enclosing_method_class_index();
2969 if (encl_method_class_idx != 0) {
2970 Klass* ok = i_cp->klass_at(encl_method_class_idx, CHECK_NULL);
2971 outer_klass = InstanceKlass::cast(ok);
2972 *inner_is_member = false;
2973 }
2974 }
2975 }
2976
2977 // If no inner class attribute found for this class.
2978 if (NULL == outer_klass) return NULL;
2979
2980 // Throws an exception if outer klass has not declared k as an inner klass
2981 // We need evidence that each klass knows about the other, or else
2982 // the system could allow a spoof of an inner class to gain access rights.
2983 Reflection::check_for_inner_class(outer_klass, this, *inner_is_member, CHECK_NULL);
2984 return outer_klass;
2985 }
2986
2987 jint InstanceKlass::compute_modifier_flags(TRAPS) const {
|