1 /*
   2  * Copyright (c) 2003, 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  *
  23  */
  24 
  25 #include "precompiled.hpp"
  26 #include "classfile/dictionary.hpp"
  27 #include "classfile/classLoaderData.inline.hpp"
  28 #include "classfile/loaderConstraints.hpp"
  29 #include "logging/log.hpp"
  30 #include "memory/resourceArea.hpp"
  31 #include "oops/oop.inline.hpp"
  32 #include "runtime/handles.inline.hpp"
  33 #include "runtime/safepoint.hpp"
  34 #include "utilities/hashtable.inline.hpp"
  35 
  36 void LoaderConstraintEntry::set_loader(int i, oop p) {
  37   set_loader_data(i, ClassLoaderData::class_loader_data(p));
  38 }
  39 
  40 LoaderConstraintTable::LoaderConstraintTable(int table_size)
  41   : Hashtable<InstanceKlass*, mtClass>(table_size, sizeof(LoaderConstraintEntry)) {};
  42 
  43 
  44 LoaderConstraintEntry* LoaderConstraintTable::new_entry(
  45                                  unsigned int hash, Symbol* name,
  46                                  InstanceKlass* klass, int num_loaders,
  47                                  int max_loaders) {
  48   LoaderConstraintEntry* entry;
  49   entry = (LoaderConstraintEntry*)Hashtable<InstanceKlass*, mtClass>::new_entry(hash, klass);
  50   entry->set_name(name);
  51   entry->set_num_loaders(num_loaders);
  52   entry->set_max_loaders(max_loaders);
  53   return entry;
  54 }
  55 
  56 void LoaderConstraintTable::free_entry(LoaderConstraintEntry *entry) {
  57   // decrement name refcount before freeing
  58   entry->name()->decrement_refcount();
  59   Hashtable<InstanceKlass*, mtClass>::free_entry(entry);
  60 }
  61 
  62 // The loaderConstraintTable must always be accessed with the
  63 // SystemDictionary lock held. This is true even for readers as
  64 // entries in the table could be being dynamically resized.
  65 
  66 LoaderConstraintEntry** LoaderConstraintTable::find_loader_constraint(
  67                                     Symbol* name, Handle loader) {
  68 
  69   unsigned int hash = compute_hash(name);
  70   int index = hash_to_index(hash);
  71   LoaderConstraintEntry** pp = bucket_addr(index);
  72   ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());
  73 
  74   while (*pp) {
  75     LoaderConstraintEntry* p = *pp;
  76     if (p->hash() == hash) {
  77       if (p->name() == name) {
  78         for (int i = p->num_loaders() - 1; i >= 0; i--) {
  79           if (p->loader_data(i) == loader_data) {
  80             return pp;
  81           }
  82         }
  83       }
  84     }
  85     pp = p->next_addr();
  86   }
  87   return pp;
  88 }
  89 
  90 
  91 void LoaderConstraintTable::purge_loader_constraints() {
  92   assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
  93   LogTarget(Info, class, loader, constraints) lt;
  94   // Remove unloaded entries from constraint table
  95   for (int index = 0; index < table_size(); index++) {
  96     LoaderConstraintEntry** p = bucket_addr(index);
  97     while(*p) {
  98       LoaderConstraintEntry* probe = *p;
  99       InstanceKlass* klass = probe->klass();
 100       // Remove klass that is no longer alive
 101       if (klass != NULL &&
 102           !klass->is_loader_alive()) {
 103         probe->set_klass(NULL);
 104         if (lt.is_enabled()) {
 105           ResourceMark rm;
 106           lt.print("purging class object from constraint for name %s,"
 107                      " loader list:",
 108                      probe->name()->as_C_string());
 109           for (int i = 0; i < probe->num_loaders(); i++) {
 110             lt.print("    [%d]: %s", i,
 111                           probe->loader_data(i)->loader_name());
 112           }
 113         }
 114       }
 115       // Remove entries no longer alive from loader array
 116       int n = 0;
 117       while (n < probe->num_loaders()) {
 118         if (probe->loader_data(n)->is_unloading()) {
 119           if (lt.is_enabled()) {
 120             ResourceMark rm;
 121             lt.print("purging loader %s from constraint for name %s",
 122                      probe->loader_data(n)->loader_name(),
 123                      probe->name()->as_C_string()
 124                      );
 125           }
 126 
 127           // Compact array
 128           int num = probe->num_loaders() - 1;
 129           probe->set_num_loaders(num);
 130           probe->set_loader_data(n, probe->loader_data(num));
 131           probe->set_loader_data(num, NULL);
 132 
 133           if (lt.is_enabled()) {
 134             ResourceMark rm;
 135             lt.print("new loader list:");
 136             for (int i = 0; i < probe->num_loaders(); i++) {
 137               lt.print("    [%d]: %s", i,
 138                             probe->loader_data(i)->loader_name());
 139             }
 140           }
 141 
 142           continue;  // current element replaced, so restart without
 143                      // incrementing n
 144           }
 145         n++;
 146       }
 147       // Check whether entry should be purged
 148       if (probe->num_loaders() < 2) {
 149             if (lt.is_enabled()) {
 150               ResourceMark rm;
 151               lt.print("purging complete constraint for name %s",
 152                          probe->name()->as_C_string());
 153             }
 154 
 155         // Purge entry
 156         *p = probe->next();
 157         FREE_C_HEAP_ARRAY(oop, probe->loaders());
 158         free_entry(probe);
 159       } else {
 160 #ifdef ASSERT
 161         if (probe->klass() != NULL) {
 162           assert(probe->klass()->is_loader_alive(), "klass should be live");
 163         }
 164 #endif
 165         // Go to next entry
 166         p = probe->next_addr();
 167       }
 168     }
 169   }
 170 }
 171 
 172 void log_ldr_constraint_msg(Symbol* class_name, const char* reason,
 173                         Handle class_loader1, Handle class_loader2) {
 174   LogTarget(Info, class, loader, constraints) lt;
 175   if (lt.is_enabled()) {
 176     ResourceMark rm;
 177     lt.print("Failed to add constraint for name: %s, loader[0]: %s,"
 178                 " loader[1]: %s, Reason: %s",
 179                   class_name->as_C_string(),
 180                   SystemDictionary::loader_name(class_loader1()),
 181                   SystemDictionary::loader_name(class_loader2()),
 182                   reason);
 183   }
 184 }
 185 
 186 bool LoaderConstraintTable::add_entry(Symbol* class_name,
 187                                       InstanceKlass* klass1, Handle class_loader1,
 188                                       InstanceKlass* klass2, Handle class_loader2) {
 189   LogTarget(Info, class, loader, constraints) lt;
 190   if (klass1 != NULL && klass2 != NULL) {
 191     if (klass1 == klass2) {
 192       // Same type already loaded in both places.  There is no need for any constraint.
 193       return true;
 194     } else {
 195       log_ldr_constraint_msg(class_name,
 196                              "The class objects presented by loader[0] and loader[1] "
 197                              "are different",
 198                              class_loader1, class_loader2);
 199       return false;
 200     }
 201   }
 202 
 203   InstanceKlass* klass = klass1 != NULL ? klass1 : klass2;
 204   LoaderConstraintEntry** pp1 = find_loader_constraint(class_name, class_loader1);
 205   if (*pp1 != NULL && (*pp1)->klass() != NULL) {
 206     if (klass != NULL) {
 207       if (klass != (*pp1)->klass()) {
 208         log_ldr_constraint_msg(class_name,
 209                                "The class object presented by loader[0] does not match "
 210                                "the stored class object in the constraint",
 211                                class_loader1, class_loader2);
 212         return false;
 213       }
 214     } else {
 215       klass = (*pp1)->klass();
 216     }
 217   }
 218 
 219   LoaderConstraintEntry** pp2 = find_loader_constraint(class_name, class_loader2);
 220   if (*pp2 != NULL && (*pp2)->klass() != NULL) {
 221     if (klass != NULL) {
 222       if (klass != (*pp2)->klass()) {
 223         log_ldr_constraint_msg(class_name,
 224                                "The class object presented by loader[1] does not match "
 225                                "the stored class object in the constraint",
 226                                class_loader1, class_loader2);
 227         return false;
 228       }
 229     } else {
 230       klass = (*pp2)->klass();
 231     }
 232   }
 233 
 234   if (*pp1 == NULL && *pp2 == NULL) {
 235     unsigned int hash = compute_hash(class_name);
 236     int index = hash_to_index(hash);
 237     LoaderConstraintEntry* p;
 238     p = new_entry(hash, class_name, klass, 2, 2);
 239     p->set_loaders(NEW_C_HEAP_ARRAY(ClassLoaderData*, 2, mtClass));
 240     p->set_loader(0, class_loader1());
 241     p->set_loader(1, class_loader2());
 242     p->set_klass(klass);
 243     p->set_next(bucket(index));
 244     set_entry(index, p);
 245     if (lt.is_enabled()) {
 246       ResourceMark rm;
 247       lt.print("adding new constraint for name: %s, loader[0]: %s,"
 248                     " loader[1]: %s",
 249                     class_name->as_C_string(),
 250                     SystemDictionary::loader_name(class_loader1()),
 251                     SystemDictionary::loader_name(class_loader2())
 252                     );
 253     }
 254   } else if (*pp1 == *pp2) {
 255     /* constraint already imposed */
 256     if ((*pp1)->klass() == NULL) {
 257       (*pp1)->set_klass(klass);
 258       if (lt.is_enabled()) {
 259         ResourceMark rm;
 260         lt.print("setting class object in existing constraint for"
 261                       " name: %s and loader %s",
 262                       class_name->as_C_string(),
 263                       SystemDictionary::loader_name(class_loader1())
 264                       );
 265       }
 266     } else {
 267       assert((*pp1)->klass() == klass, "loader constraints corrupted");
 268     }
 269   } else if (*pp1 == NULL) {
 270     extend_loader_constraint(*pp2, class_loader1, klass);
 271   } else if (*pp2 == NULL) {
 272     extend_loader_constraint(*pp1, class_loader2, klass);
 273   } else {
 274     merge_loader_constraints(pp1, pp2, klass);
 275   }
 276 
 277   return true;
 278 }
 279 
 280 
 281 // return true if the constraint was updated, false if the constraint is
 282 // violated
 283 bool LoaderConstraintTable::check_or_update(InstanceKlass* k,
 284                                             Handle loader,
 285                                             Symbol* name) {
 286   LogTarget(Info, class, loader, constraints) lt;
 287   LoaderConstraintEntry* p = *(find_loader_constraint(name, loader));
 288   if (p && p->klass() != NULL && p->klass() != k) {
 289     if (lt.is_enabled()) {
 290       ResourceMark rm;
 291       lt.print("constraint check failed for name %s, loader %s: "
 292                  "the presented class object differs from that stored",
 293                  name->as_C_string(),
 294                  SystemDictionary::loader_name(loader()));
 295     }
 296     return false;
 297   } else {
 298     if (p && p->klass() == NULL) {
 299       p->set_klass(k);
 300       if (lt.is_enabled()) {
 301         ResourceMark rm;
 302         lt.print("updating constraint for name %s, loader %s, "
 303                    "by setting class object",
 304                    name->as_C_string(),
 305                    SystemDictionary::loader_name(loader()));
 306       }
 307     }
 308     return true;
 309   }
 310 }
 311 
 312 InstanceKlass* LoaderConstraintTable::find_constrained_klass(Symbol* name,
 313                                                        Handle loader) {
 314   LoaderConstraintEntry *p = *(find_loader_constraint(name, loader));
 315   if (p != NULL && p->klass() != NULL) {
 316     assert(p->klass()->is_instance_klass(), "sanity");
 317     if (!p->klass()->is_loaded()) {
 318       // Only return fully loaded classes.  Classes found through the
 319       // constraints might still be in the process of loading.
 320       return NULL;
 321     }
 322     return p->klass();
 323   }
 324 
 325   // No constraints, or else no klass loaded yet.
 326   return NULL;
 327 }
 328 
 329 void LoaderConstraintTable::ensure_loader_constraint_capacity(
 330                                                      LoaderConstraintEntry *p,
 331                                                     int nfree) {
 332     if (p->max_loaders() - p->num_loaders() < nfree) {
 333         int n = nfree + p->num_loaders();
 334         ClassLoaderData** new_loaders = NEW_C_HEAP_ARRAY(ClassLoaderData*, n, mtClass);
 335         memcpy(new_loaders, p->loaders(), sizeof(ClassLoaderData*) * p->num_loaders());
 336         p->set_max_loaders(n);
 337         FREE_C_HEAP_ARRAY(ClassLoaderData*, p->loaders());
 338         p->set_loaders(new_loaders);
 339     }
 340 }
 341 
 342 
 343 void LoaderConstraintTable::extend_loader_constraint(LoaderConstraintEntry* p,
 344                                                      Handle loader,
 345                                                      InstanceKlass* klass) {
 346   ensure_loader_constraint_capacity(p, 1);
 347   int num = p->num_loaders();
 348   p->set_loader(num, loader());
 349   p->set_num_loaders(num + 1);
 350   LogTarget(Info, class, loader, constraints) lt;
 351   if (lt.is_enabled()) {
 352     ResourceMark rm;
 353     lt.print("extending constraint for name %s by adding loader[%d]: %s %s",
 354                p->name()->as_C_string(),
 355                num,
 356                SystemDictionary::loader_name(loader()),
 357                (p->klass() == NULL ? " and setting class object" : "")
 358                );
 359   }
 360   if (p->klass() == NULL) {
 361     p->set_klass(klass);
 362   } else {
 363     assert(klass == NULL || p->klass() == klass, "constraints corrupted");
 364   }
 365 }
 366 
 367 
 368 void LoaderConstraintTable::merge_loader_constraints(
 369                                                    LoaderConstraintEntry** pp1,
 370                                                    LoaderConstraintEntry** pp2,
 371                                                    InstanceKlass* klass) {
 372   // make sure *pp1 has higher capacity
 373   if ((*pp1)->max_loaders() < (*pp2)->max_loaders()) {
 374     LoaderConstraintEntry** tmp = pp2;
 375     pp2 = pp1;
 376     pp1 = tmp;
 377   }
 378 
 379   LoaderConstraintEntry* p1 = *pp1;
 380   LoaderConstraintEntry* p2 = *pp2;
 381 
 382   ensure_loader_constraint_capacity(p1, p2->num_loaders());
 383 
 384   for (int i = 0; i < p2->num_loaders(); i++) {
 385     int num = p1->num_loaders();
 386     p1->set_loader_data(num, p2->loader_data(i));
 387     p1->set_num_loaders(num + 1);
 388   }
 389 
 390   LogTarget(Info, class, loader, constraints) lt;
 391   if (lt.is_enabled()) {
 392     ResourceMark rm;
 393     lt.print("merged constraints for name %s, new loader list:",
 394                   p1->name()->as_C_string()
 395                   );
 396 
 397     for (int i = 0; i < p1->num_loaders(); i++) {
 398       lt.print("    [%d]: %s", i,
 399                     p1->loader_data(i)->loader_name());
 400     }
 401     if (p1->klass() == NULL) {
 402       lt.print("... and setting class object");
 403     }
 404   }
 405 
 406   // p1->klass() will hold NULL if klass, p2->klass(), and old
 407   // p1->klass() are all NULL.  In addition, all three must have
 408   // matching non-NULL values, otherwise either the constraints would
 409   // have been violated, or the constraints had been corrupted (and an
 410   // assertion would fail).
 411   if (p2->klass() != NULL) {
 412     assert(p2->klass() == klass, "constraints corrupted");
 413   }
 414   if (p1->klass() == NULL) {
 415     p1->set_klass(klass);
 416   } else {
 417     assert(p1->klass() == klass, "constraints corrupted");
 418   }
 419 
 420   *pp2 = p2->next();
 421   FREE_C_HEAP_ARRAY(oop, p2->loaders());
 422   free_entry(p2);
 423   return;
 424 }
 425 
 426 
 427 void LoaderConstraintTable::verify(PlaceholderTable* placeholders) {
 428   Thread *thread = Thread::current();
 429   for (int cindex = 0; cindex < table_size(); cindex++) {
 430     for (LoaderConstraintEntry* probe = bucket(cindex);
 431                                 probe != NULL;
 432                                 probe = probe->next()) {
 433       if (probe->klass() != NULL) {
 434         InstanceKlass* ik = probe->klass();
 435         guarantee(ik->name() == probe->name(), "name should match");
 436         Symbol* name = ik->name();
 437         ClassLoaderData* loader_data = ik->class_loader_data();
 438         Dictionary* dictionary = loader_data->dictionary();
 439         unsigned int d_hash = dictionary->compute_hash(name);
 440         int d_index = dictionary->hash_to_index(d_hash);
 441         InstanceKlass* k = dictionary->find_class(d_index, d_hash, name);
 442         if (k != NULL) {
 443           // We found the class in the dictionary, so we should
 444           // make sure that the Klass* matches what we already have.
 445           guarantee(k == probe->klass(), "klass should be in dictionary");
 446         } else {
 447           // If we don't find the class in the dictionary, it
 448           // has to be in the placeholders table.
 449           unsigned int p_hash = placeholders->compute_hash(name);
 450           int p_index = placeholders->hash_to_index(p_hash);
 451           PlaceholderEntry* entry = placeholders->get_entry(p_index, p_hash,
 452                                                             name, loader_data);
 453 
 454           // The InstanceKlass might not be on the entry, so the only
 455           // thing we can check here is whether we were successful in
 456           // finding the class in the placeholders table.
 457           guarantee(entry != NULL, "klass should be in the placeholders");
 458         }
 459       }
 460       for (int n = 0; n< probe->num_loaders(); n++) {
 461         assert(ClassLoaderDataGraph::contains_loader_data(probe->loader_data(n)), "The loader is missing");
 462       }
 463     }
 464   }
 465 }
 466 
 467 // Called with the system dictionary lock held
 468 void LoaderConstraintTable::print_on(outputStream* st) const {
 469   ResourceMark rm;
 470   assert_locked_or_safepoint(SystemDictionary_lock);
 471   st->print_cr("Java loader constraints (table_size=%d, constraints=%d)",
 472                table_size(), number_of_entries());
 473   for (int cindex = 0; cindex < table_size(); cindex++) {
 474     for (LoaderConstraintEntry* probe = bucket(cindex);
 475                                 probe != NULL;
 476                                 probe = probe->next()) {
 477       st->print("%4d: ", cindex);
 478       probe->name()->print_on(st);
 479       st->print(" , loaders:");
 480       for (int n = 0; n < probe->num_loaders(); n++) {
 481         probe->loader_data(n)->print_value_on(st);
 482         st->print(", ");
 483       }
 484       st->cr();
 485     }
 486   }
 487 }