136
137 static inline bool is_class_loader(const Symbol* class_name,
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()) {
179 Klass* k2 = _constants->klass_at(cp_index, CHECK_false);
180 if (k2 == k) {
181 log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
182 return true;
183 }
184 }
185 else {
186 Symbol* name = _constants->klass_name_at(cp_index);
187 if (name == k->name()) {
188 log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
189
190 // Names match so check actual klass - this may trigger class loading if
191 // it doesn't match (though that should be impossible). But to be safe we
192 // have to check for a compiler thread executing here.
193 if (!THREAD->can_call_java() && !_constants->tag_at(cp_index).is_klass()) {
194 log_trace(class, nestmates)("- validation required resolution in an unsuitable thread");
195 return false;
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
294 // JVMS 5.4.4 indicates package check comes first
295 if (is_same_class_package(k)) {
296
297 // Now check actual membership. We can't be a member if our "host" is
298 // not an instance class.
299 if (k->is_instance_klass()) {
300 nest_host_k = InstanceKlass::cast(k);
301
302 bool is_member = nest_host_k->has_nest_member(this, CHECK_NULL);
303 if (is_member) {
304 // save resolved nest-host value
305 _nest_host = nest_host_k;
306
307 if (log_is_enabled(Trace, class, nestmates)) {
308 ResourceMark rm(THREAD);
309 log_trace(class, nestmates)("Resolved nest-host of %s to %s",
310 this->external_name(), k->external_name());
311 }
312 return nest_host_k;
313 }
314 }
315 error = "current type is not listed as a nest member";
316 } else {
317 error = "types are in different packages";
318 }
319
320 if (log_is_enabled(Trace, class, nestmates)) {
321 ResourceMark rm(THREAD);
322 log_trace(class, nestmates)
323 ("Type %s (loader: %s) is not a nest member of "
324 "resolved type %s (loader: %s): %s",
325 this->external_name(),
326 this->class_loader_data()->loader_name_and_id(),
327 k->external_name(),
328 k->class_loader_data()->loader_name_and_id(),
329 error);
330 }
331
332 if (validationException != NULL && THREAD->can_call_java()) {
333 ResourceMark rm(THREAD);
334 Exceptions::fthrow(THREAD_AND_LOCATION,
335 validationException,
336 "Type %s (loader: %s) is not a nest member of %s (loader: %s): %s",
337 this->external_name(),
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(),
480 _method_ordering->at_put(i, m->at(i));
481 }
482 } else {
483 _method_ordering = Universe::the_empty_int_array();
484 }
485 }
486
487 // create a new array of vtable_indices for default methods
488 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
489 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
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 _record_components(NULL),
501 _static_field_size(parser.static_field_size()),
502 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
503 _itable_len(parser.itable_size()),
504 _init_thread(NULL),
505 _init_state(allocated),
506 _reference_type(parser.reference_type())
507 {
508 set_vtable_length(parser.vtable_size());
509 set_kind(kind);
510 set_access_flags(parser.access_flags());
511 if (parser.is_hidden()) set_is_hidden();
512 set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
513 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
514 false));
515
516 assert(NULL == _methods, "underlying memory not zeroed?");
517 assert(is_instance_klass(), "is layout incorrect?");
518 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
519
2465 array_klasses()->remove_unshareable_info();
2466 }
2467
2468 // These are not allocated from metaspace. They are safe to set to NULL.
2469 _source_debug_extension = NULL;
2470 _dep_context = NULL;
2471 _osr_nmethods_head = NULL;
2472 #if INCLUDE_JVMTI
2473 _breakpoints = NULL;
2474 _previous_versions = NULL;
2475 _cached_class_file = NULL;
2476 _jvmti_cached_class_field_map = NULL;
2477 #endif
2478
2479 _init_thread = NULL;
2480 _methods_jmethod_ids = NULL;
2481 _jni_ids = NULL;
2482 _oop_map_cache = NULL;
2483 // clear _nest_host to ensure re-load at runtime
2484 _nest_host = NULL;
2485 _package_entry = NULL;
2486 _dep_context_last_cleaned = 0;
2487 }
2488
2489 void InstanceKlass::remove_java_mirror() {
2490 Klass::remove_java_mirror();
2491
2492 // do array classes also.
2493 if (array_klasses() != NULL) {
2494 array_klasses()->remove_java_mirror();
2495 }
2496 }
2497
2498 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2499 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2500 // before the InstanceKlass is added to the SystemDictionary. Make
2501 // sure the current state is <loaded.
2502 assert(!is_loaded(), "invalid init state");
2503 set_package(loader_data, CHECK);
2504 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2630 // Deallocate breakpoint records
2631 if (breakpoints() != 0x0) {
2632 methods_do(clear_all_breakpoints);
2633 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2634 }
2635
2636 // deallocate the cached class file
2637 if (_cached_class_file != NULL) {
2638 os::free(_cached_class_file);
2639 _cached_class_file = NULL;
2640 }
2641 #endif
2642
2643 // Decrement symbol reference counts associated with the unloaded class.
2644 if (_name != NULL) _name->decrement_refcount();
2645
2646 // unreference array name derived from this class name (arrays of an unloaded
2647 // class can't be referenced anymore).
2648 if (_array_name != NULL) _array_name->decrement_refcount();
2649 FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2650 }
2651
2652 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2653 if (array == NULL) {
2654 _source_debug_extension = NULL;
2655 } else {
2656 // Adding one to the attribute length in order to store a null terminator
2657 // character could cause an overflow because the attribute length is
2658 // already coded with an u4 in the classfile, but in practice, it's
2659 // unlikely to happen.
2660 assert((length+1) > length, "Overflow checking");
2661 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2662 for (int i = 0; i < length; i++) {
2663 sde[i] = array[i];
2664 }
2665 sde[length] = '\0';
2666 _source_debug_extension = sde;
2667 }
2668 }
2669
|
136
137 static inline bool is_class_loader(const Symbol* class_name,
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 // private: called to verify that k is a static member of this nest.
157 // We know that k is an instance class in the same package and hence the
158 // same classloader.
159 bool InstanceKlass::has_nest_member(InstanceKlass* k, TRAPS) const {
160 assert(!is_hidden(), "unexpected hidden class");
161 if (_nest_members == NULL || _nest_members == Universe::the_empty_short_array()) {
162 if (log_is_enabled(Trace, class, nestmates)) {
163 ResourceMark rm(THREAD);
164 log_trace(class, nestmates)("Checked nest membership of %s in non-nest-host class %s",
165 k->external_name(), this->external_name());
166 }
167 return false;
168 }
169
170 if (log_is_enabled(Trace, class, nestmates)) {
171 ResourceMark rm(THREAD);
172 log_trace(class, nestmates)("Checking nest membership of %s in %s",
173 k->external_name(), this->external_name());
174 }
175
176 // Check for a resolved cp entry , else fall back to a name check.
177 // We don't want to resolve any class other than the one being checked.
178 for (int i = 0; i < _nest_members->length(); i++) {
179 int cp_index = _nest_members->at(i);
180 if (_constants->tag_at(cp_index).is_klass()) {
181 Klass* k2 = _constants->klass_at(cp_index, THREAD);
182 assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
183 "Exceptions should not be possible here");
184 if (k2 == k) {
185 log_trace(class, nestmates)("- class is listed at nest_members[%d] => cp[%d]", i, cp_index);
186 return true;
187 }
188 }
189 else {
190 Symbol* name = _constants->klass_name_at(cp_index);
191 if (name == k->name()) {
192 log_trace(class, nestmates)("- Found it at nest_members[%d] => cp[%d]", i, cp_index);
193
194 // Names match so check actual klass. This may trigger class loading if
195 // it doesn't match though that should be impossible as it means one classloader
196 // has defined two different classes with the same name! A compiler thread won't be
197 // able to perform that loading but we can't exclude the compiler threads from
198 // executing this logic. But it should actually be impossible to trigger loading here.
199 Klass* k2 = _constants->klass_at(cp_index, THREAD);
200 assert(!HAS_PENDING_EXCEPTION || PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass()),
201 "Exceptions should not be possible here");
202 if (k2 == k) {
203 log_trace(class, nestmates)("- class is listed as a nest member");
204 return true;
205 }
206 else {
207 // same name but different klass!
208 log_trace(class, nestmates)(" - klass comparison failed!");
209 // can't have two names the same, so we're done
210 return false;
211 }
212 }
213 }
214 }
215 log_trace(class, nestmates)("- class is NOT a nest member!");
216 return false;
217 }
218
219 // Return nest-host class, resolving, validating and saving it if needed.
220 // In cases where this is called from a thread that cannot do classloading
221 // (such as a native JIT thread) then we simply return NULL, which in turn
222 // causes the access check to return false. Such code will retry the access
223 // from a more suitable environment later. Otherwise the _nest_host is always
224 // set once this method returns.
225 // Any errors from nest-host resolution must be preserved so they can be queried
226 // from higher-level access checking code, and reported as part of access checking
227 // exceptions.
228 // VirtualMachineErrors are propagated with a NULL return.
229 // Under any conditions where the _nest_host can be set to non-NULL the resulting value
230 // of it and, if applicable, _nest_host_res_error, are idempotent. But as we can be
231 // executing this code concurrently we need to ensure ordering is maintained so that
232 // errors messages can safely be read.
233 InstanceKlass* InstanceKlass::nest_host(TRAPS) {
234 InstanceKlass* nest_host_k = _nest_host;
235 if (nest_host_k != NULL) {
236 return nest_host_k;
237 }
238
239 const bool doLog = log_is_enabled(Trace, class, nestmates);
240
241 // need to resolve and save our nest-host class.
242 if (_nest_host_index != 0) { // we have a real nest_host
243 // Before trying to resolve check if we're in a suitable context
244 if (!THREAD->can_call_java() && !_constants->tag_at(_nest_host_index).is_klass()) {
245 if (doLog) {
246 ResourceMark rm(THREAD);
247 log_trace(class, nestmates)("Rejected resolution of nest-host of %s in unsuitable thread",
248 this->external_name());
249 }
250 return NULL; // sentinel to say "try again from a different context"
251 }
252
253 if (doLog) {
254 ResourceMark rm(THREAD);
255 log_trace(class, nestmates)("Resolving nest-host of %s using cp entry for %s",
256 this->external_name(),
257 _constants->klass_name_at(_nest_host_index)->as_C_string());
258 }
259
260 Klass* k = _constants->klass_at(_nest_host_index, THREAD);
261 if (HAS_PENDING_EXCEPTION) {
262 if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
263 return NULL; // propagate VMEs
264 }
265 ResourceMark rm(THREAD);
266 stringStream ss;
267 char* target_host_class = _constants->klass_name_at(_nest_host_index)->as_C_string();
268 ss.print("Nest host resolution of %s with host %s failed: ",
269 this->external_name(), target_host_class);
270 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
271 _nest_host_res_error = ss.as_string(true /* on C-heap */);
272 // ensure we see _nest_host_res_error is set if _nest_host is non-NULL
273 OrderAccess::storestore();
274 CLEAR_PENDING_EXCEPTION;
275 if (doLog) {
276 log_trace(class, nestmates)("%s", _nest_host_res_error);
277 }
278 } else {
279 // A valid nest-host is an instance class in the current package that lists this
280 // class as a nest member. If any of these conditions are not met the class is
281 // its own nest-host.
282 const char* error = NULL;
283
284 // JVMS 5.4.4 indicates package check comes first
285 if (is_same_class_package(k)) {
286 // Now check actual membership. We can't be a member if our "host" is
287 // not an instance class.
288 if (k->is_instance_klass()) {
289 nest_host_k = InstanceKlass::cast(k);
290 bool is_member = nest_host_k->has_nest_member(this, THREAD);
291 // exception is rare, perhaps impossible
292 if (!HAS_PENDING_EXCEPTION) {
293 if (is_member) {
294 _nest_host = nest_host_k; // save resolved nest-host value
295 if (doLog) {
296 ResourceMark rm(THREAD);
297 log_trace(class, nestmates)("Resolved nest-host of %s to %s",
298 this->external_name(), k->external_name());
299 }
300 return nest_host_k;
301 } else {
302 error = "current type is not listed as a nest member";
303 }
304 } else {
305 if (PENDING_EXCEPTION->is_a(SystemDictionary::VirtualMachineError_klass())) {
306 return NULL; // propagate VMEs
307 }
308 stringStream ss;
309 ss.print("exception on member check: ");
310 java_lang_Throwable::print(PENDING_EXCEPTION, &ss);
311 error = ss.as_string();
312 }
313 } else {
314 error = "host is not an instance class";
315 }
316 } else {
317 error = "types are in different packages";
318 }
319
320 // something went wrong, so record what and log it
321 {
322 ResourceMark rm(THREAD);
323 stringStream ss;
324 ss.print("Type %s (loader: %s) is not a nest member of type %s (loader: %s): %s",
325 this->external_name(),
326 this->class_loader_data()->loader_name_and_id(),
327 k->external_name(),
328 k->class_loader_data()->loader_name_and_id(),
329 error);
330 _nest_host_res_error = ss.as_string(true /* on C-heap */);
331 // ensure we see _nest_host_res_error is set if _nest_host is non-NULL
332 OrderAccess::storestore();
333
334 if (doLog) {
335 log_trace(class, nestmates)("%s", _nest_host_res_error);
336 }
337 }
338 }
339 } else {
340 if (doLog) {
341 ResourceMark rm(THREAD);
342 log_trace(class, nestmates)("Type %s is not part of a nest: setting nest-host to self",
343 this->external_name());
344 }
345 }
346
347 // Either not in an explicit nest, or else an error occurred, so
348 // the nest-host is set to `this`. Any thread that sees this assignment
349 // will also see any setting of _nest_host_res_error, if applicable.
350 return (_nest_host = this);
351 }
352
353 // Dynamic nest member support: set this class's nest host to the given class.
354 // This occurs as part of the class definition, as soon as the instanceKlass
355 // has been created and doesn't require further resolution. The code:
356 // lookup().defineHiddenClass(bytes_for_X, NESTMATE);
357 // results in:
358 // class_of_X.set_nest_host(lookup().lookupClass().getNestHost())
359 // If it has an explicit _nest_host_index or _nest_members, these will be ignored.
360 // We also know the "host" is a valid nest-host in the same package so we can
361 // assert some of those facts.
362 void InstanceKlass::set_nest_host(InstanceKlass* host, TRAPS) {
363 assert(is_hidden(), "must be a hidden class");
364 assert(host != NULL, "NULL nest host specified");
365 assert(_nest_host == NULL, "current class has resolved nest-host");
366 assert(_nest_host_res_error == NULL, "unexpected nest host resolution error exists: %s",
367 _nest_host_res_error);
368 assert((host->_nest_host == NULL && host->_nest_host_index == 0) ||
369 (host->_nest_host == host), "proposed host is not a valid nest-host");
370 // Can't assert this as package is not set yet:
371 // assert(is_same_class_package(host), "proposed host is in wrong package");
372
373 if (log_is_enabled(Trace, class, nestmates)) {
374 ResourceMark rm(THREAD);
375 const char* msg = "";
376 // a hidden class does not expect a statically defined nest-host
377 if (_nest_host_index > 0) {
378 msg = "(the NestHost attribute in the current class is ignored)";
379 } else if (_nest_members != NULL && _nest_members != Universe::the_empty_short_array()) {
380 msg = "(the NestMembers attribute in the current class is ignored)";
381 }
382 log_trace(class, nestmates)("Injected type %s into the nest of %s %s",
383 this->external_name(),
384 host->external_name(),
385 msg);
386 }
387 // set dynamic nest host
388 _nest_host = host;
389 }
390
391 // check if 'this' and k are nestmates (same nest_host), or k is our nest_host,
392 // or we are k's nest_host - all of which is covered by comparing the two
393 // resolved_nest_hosts.
394 // Any exceptions (i.e. VMEs) are propagated.
395 bool InstanceKlass::has_nestmate_access_to(InstanceKlass* k, TRAPS) {
396
397 assert(this != k, "this should be handled by higher-level code");
398
399 // Per JVMS 5.4.4 we first resolve and validate the current class, then
400 // the target class k.
401
402 InstanceKlass* cur_host = nest_host(CHECK_false);
403 if (cur_host == NULL) {
404 return false;
405 }
406
407 Klass* k_nest_host = k->nest_host(CHECK_false);
408 if (k_nest_host == NULL) {
409 return false;
410 }
411
412 bool access = (cur_host == k_nest_host);
413
414 if (log_is_enabled(Trace, class, nestmates)) {
415 ResourceMark rm(THREAD);
416 log_trace(class, nestmates)("Class %s does %shave nestmate access to %s",
417 this->external_name(),
418 access ? "" : "NOT ",
419 k->external_name());
420 }
421
422 return access;
423 }
424
425 InstanceKlass* InstanceKlass::allocate_instance_klass(const ClassFileParser& parser, TRAPS) {
426 bool is_hidden_or_anonymous = parser.is_hidden() || parser.is_unsafe_anonymous();
427 const int size = InstanceKlass::size(parser.vtable_size(),
475 _method_ordering->at_put(i, m->at(i));
476 }
477 } else {
478 _method_ordering = Universe::the_empty_int_array();
479 }
480 }
481
482 // create a new array of vtable_indices for default methods
483 Array<int>* InstanceKlass::create_new_default_vtable_indices(int len, TRAPS) {
484 Array<int>* vtable_indices = MetadataFactory::new_array<int>(class_loader_data(), len, CHECK_NULL);
485 assert(default_vtable_indices() == NULL, "only create once");
486 set_default_vtable_indices(vtable_indices);
487 return vtable_indices;
488 }
489
490 InstanceKlass::InstanceKlass(const ClassFileParser& parser, unsigned kind, KlassID id) :
491 Klass(id),
492 _nest_members(NULL),
493 _nest_host_index(0),
494 _nest_host(NULL),
495 _nest_host_res_error(NULL),
496 _record_components(NULL),
497 _static_field_size(parser.static_field_size()),
498 _nonstatic_oop_map_size(nonstatic_oop_map_size(parser.total_oop_map_count())),
499 _itable_len(parser.itable_size()),
500 _init_thread(NULL),
501 _init_state(allocated),
502 _reference_type(parser.reference_type())
503 {
504 set_vtable_length(parser.vtable_size());
505 set_kind(kind);
506 set_access_flags(parser.access_flags());
507 if (parser.is_hidden()) set_is_hidden();
508 set_is_unsafe_anonymous(parser.is_unsafe_anonymous());
509 set_layout_helper(Klass::instance_layout_helper(parser.layout_size(),
510 false));
511
512 assert(NULL == _methods, "underlying memory not zeroed?");
513 assert(is_instance_klass(), "is layout incorrect?");
514 assert(size_helper() == parser.layout_size(), "incorrect size_helper?");
515
2461 array_klasses()->remove_unshareable_info();
2462 }
2463
2464 // These are not allocated from metaspace. They are safe to set to NULL.
2465 _source_debug_extension = NULL;
2466 _dep_context = NULL;
2467 _osr_nmethods_head = NULL;
2468 #if INCLUDE_JVMTI
2469 _breakpoints = NULL;
2470 _previous_versions = NULL;
2471 _cached_class_file = NULL;
2472 _jvmti_cached_class_field_map = NULL;
2473 #endif
2474
2475 _init_thread = NULL;
2476 _methods_jmethod_ids = NULL;
2477 _jni_ids = NULL;
2478 _oop_map_cache = NULL;
2479 // clear _nest_host to ensure re-load at runtime
2480 _nest_host = NULL;
2481 _nest_host_res_error = NULL;
2482 _package_entry = NULL;
2483 _dep_context_last_cleaned = 0;
2484 }
2485
2486 void InstanceKlass::remove_java_mirror() {
2487 Klass::remove_java_mirror();
2488
2489 // do array classes also.
2490 if (array_klasses() != NULL) {
2491 array_klasses()->remove_java_mirror();
2492 }
2493 }
2494
2495 void InstanceKlass::restore_unshareable_info(ClassLoaderData* loader_data, Handle protection_domain, TRAPS) {
2496 // SystemDictionary::add_to_hierarchy() sets the init_state to loaded
2497 // before the InstanceKlass is added to the SystemDictionary. Make
2498 // sure the current state is <loaded.
2499 assert(!is_loaded(), "invalid init state");
2500 set_package(loader_data, CHECK);
2501 Klass::restore_unshareable_info(loader_data, protection_domain, CHECK);
2627 // Deallocate breakpoint records
2628 if (breakpoints() != 0x0) {
2629 methods_do(clear_all_breakpoints);
2630 assert(breakpoints() == 0x0, "should have cleared breakpoints");
2631 }
2632
2633 // deallocate the cached class file
2634 if (_cached_class_file != NULL) {
2635 os::free(_cached_class_file);
2636 _cached_class_file = NULL;
2637 }
2638 #endif
2639
2640 // Decrement symbol reference counts associated with the unloaded class.
2641 if (_name != NULL) _name->decrement_refcount();
2642
2643 // unreference array name derived from this class name (arrays of an unloaded
2644 // class can't be referenced anymore).
2645 if (_array_name != NULL) _array_name->decrement_refcount();
2646 FREE_C_HEAP_ARRAY(char, _source_debug_extension);
2647
2648 // deallocate memoized nest-host resolution error
2649 if (_nest_host_res_error != NULL) {
2650 FREE_C_HEAP_ARRAY(char, _nest_host_res_error);
2651 _nest_host_res_error = NULL;
2652 }
2653 }
2654
2655 void InstanceKlass::set_source_debug_extension(const char* array, int length) {
2656 if (array == NULL) {
2657 _source_debug_extension = NULL;
2658 } else {
2659 // Adding one to the attribute length in order to store a null terminator
2660 // character could cause an overflow because the attribute length is
2661 // already coded with an u4 in the classfile, but in practice, it's
2662 // unlikely to happen.
2663 assert((length+1) > length, "Overflow checking");
2664 char* sde = NEW_C_HEAP_ARRAY(char, (length + 1), mtClass);
2665 for (int i = 0; i < length; i++) {
2666 sde[i] = array[i];
2667 }
2668 sde[length] = '\0';
2669 _source_debug_extension = sde;
2670 }
2671 }
2672
|