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