loaderConstraints.cpp 17.8 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25
#include "precompiled.hpp"
26
#include "classfile/classLoaderData.inline.hpp"
27 28 29 30 31 32
#include "classfile/loaderConstraints.hpp"
#include "memory/resourceArea.hpp"
#include "oops/oop.inline.hpp"
#include "runtime/handles.inline.hpp"
#include "runtime/safepoint.hpp"
#include "utilities/hashtable.inline.hpp"
D
duke 已提交
33

34 35 36 37
void LoaderConstraintEntry::set_loader(int i, oop p) {
  set_loader_data(i, ClassLoaderData::class_loader_data(p));
}

D
duke 已提交
38
LoaderConstraintTable::LoaderConstraintTable(int nof_buckets)
39
  : Hashtable<Klass*, mtClass>(nof_buckets, sizeof(LoaderConstraintEntry)) {};
D
duke 已提交
40 41 42


LoaderConstraintEntry* LoaderConstraintTable::new_entry(
43
                                 unsigned int hash, Symbol* name,
44
                                 Klass* klass, int num_loaders,
D
duke 已提交
45 46
                                 int max_loaders) {
  LoaderConstraintEntry* entry;
47
  entry = (LoaderConstraintEntry*)Hashtable<Klass*, mtClass>::new_entry(hash, klass);
D
duke 已提交
48 49 50 51 52 53
  entry->set_name(name);
  entry->set_num_loaders(num_loaders);
  entry->set_max_loaders(max_loaders);
  return entry;
}

54 55 56
void LoaderConstraintTable::free_entry(LoaderConstraintEntry *entry) {
  // decrement name refcount before freeing
  entry->name()->decrement_refcount();
57
  Hashtable<Klass*, mtClass>::free_entry(entry);
58 59
}

60 61
// Enhanced Class Redefinition support
void LoaderConstraintTable::classes_do(KlassClosure* f) {
D
duke 已提交
62 63 64 65 66
  for (int index = 0; index < table_size(); index++) {
    for (LoaderConstraintEntry* probe = bucket(index);
                                probe != NULL;
                                probe = probe->next()) {
      if (probe->klass() != NULL) {
67
        f->do_klass(probe->klass());
D
duke 已提交
68 69 70 71 72 73 74 75 76 77
      }
        }
      }
    }

// The loaderConstraintTable must always be accessed with the
// SystemDictionary lock held. This is true even for readers as
// entries in the table could be being dynamically resized.

LoaderConstraintEntry** LoaderConstraintTable::find_loader_constraint(
78
                                    Symbol* name, Handle loader) {
D
duke 已提交
79 80 81 82

  unsigned int hash = compute_hash(name);
  int index = hash_to_index(hash);
  LoaderConstraintEntry** pp = bucket_addr(index);
83 84
  ClassLoaderData* loader_data = ClassLoaderData::class_loader_data(loader());

D
duke 已提交
85 86 87
  while (*pp) {
    LoaderConstraintEntry* p = *pp;
    if (p->hash() == hash) {
88
      if (p->name() == name) {
D
duke 已提交
89
        for (int i = p->num_loaders() - 1; i >= 0; i--) {
90
          if (p->loader_data(i) == loader_data) {
D
duke 已提交
91 92 93 94 95 96 97 98 99 100 101
            return pp;
          }
        }
      }
    }
    pp = p->next_addr();
  }
  return pp;
}


102
void LoaderConstraintTable::purge_loader_constraints() {
103
  assert(SafepointSynchronize::is_at_safepoint(), "must be at safepoint");
D
duke 已提交
104 105 106 107 108
  // Remove unloaded entries from constraint table
  for (int index = 0; index < table_size(); index++) {
    LoaderConstraintEntry** p = bucket_addr(index);
    while(*p) {
      LoaderConstraintEntry* probe = *p;
109
      Klass* klass = probe->klass();
D
duke 已提交
110
      // Remove klass that is no longer alive
111 112
      if (klass != NULL &&
          klass->class_loader_data()->is_unloading()) {
D
duke 已提交
113 114 115 116 117 118 119 120
        probe->set_klass(NULL);
        if (TraceLoaderConstraints) {
          ResourceMark rm;
          tty->print_cr("[Purging class object from constraint for name %s,"
                     " loader list:",
                     probe->name()->as_C_string());
          for (int i = 0; i < probe->num_loaders(); i++) {
            tty->print_cr("[   [%d]: %s", i,
121
                          probe->loader_data(i)->loader_name());
D
duke 已提交
122 123 124 125 126 127
          }
        }
      }
      // Remove entries no longer alive from loader array
      int n = 0;
      while (n < probe->num_loaders()) {
128
        if (probe->loader_data(n)->is_unloading()) {
D
duke 已提交
129 130 131
            if (TraceLoaderConstraints) {
              ResourceMark rm;
              tty->print_cr("[Purging loader %s from constraint for name %s",
132
                            probe->loader_data(n)->loader_name(),
D
duke 已提交
133 134 135 136 137 138 139
                            probe->name()->as_C_string()
                            );
            }

            // Compact array
            int num = probe->num_loaders() - 1;
            probe->set_num_loaders(num);
140 141
          probe->set_loader_data(n, probe->loader_data(num));
          probe->set_loader_data(num, NULL);
D
duke 已提交
142 143 144 145 146 147

            if (TraceLoaderConstraints) {
              ResourceMark rm;
              tty->print_cr("[New loader list:");
              for (int i = 0; i < probe->num_loaders(); i++) {
                tty->print_cr("[   [%d]: %s", i,
148
                              probe->loader_data(i)->loader_name());
D
duke 已提交
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
              }
            }

            continue;  // current element replaced, so restart without
                       // incrementing n
          }
        n++;
      }
      // Check whether entry should be purged
      if (probe->num_loaders() < 2) {
            if (TraceLoaderConstraints) {
              ResourceMark rm;
              tty->print("[Purging complete constraint for name %s\n",
                         probe->name()->as_C_string());
            }

        // Purge entry
        *p = probe->next();
Z
zgu 已提交
167
        FREE_C_HEAP_ARRAY(oop, probe->loaders(), mtClass);
D
duke 已提交
168 169 170 171
        free_entry(probe);
      } else {
#ifdef ASSERT
        if (probe->klass() != NULL) {
172 173 174
          ClassLoaderData* loader_data =
            probe->klass()->class_loader_data();
          assert(!loader_data->is_unloading(), "klass should be live");
D
duke 已提交
175 176 177 178 179 180 181 182 183
        }
#endif
        // Go to next entry
        p = probe->next_addr();
      }
    }
  }
}

184
bool LoaderConstraintTable::add_entry(Symbol* class_name,
185 186
                                      Klass* klass1, Handle class_loader1,
                                      Klass* klass2, Handle class_loader2) {
D
duke 已提交
187 188 189 190 191
  int failure_code = 0; // encode different reasons for failing

  if (klass1 != NULL && klass2 != NULL && klass1 != klass2) {
    failure_code = 1;
  } else {
192
    Klass* klass = klass1 != NULL ? klass1 : klass2;
D
duke 已提交
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222

    LoaderConstraintEntry** pp1 = find_loader_constraint(class_name,
                                                         class_loader1);
    if (*pp1 != NULL && (*pp1)->klass() != NULL) {
      if (klass != NULL) {
        if (klass != (*pp1)->klass()) {
          failure_code = 2;
        }
      } else {
        klass = (*pp1)->klass();
      }
    }

    LoaderConstraintEntry** pp2 = find_loader_constraint(class_name,
                                                         class_loader2);
    if (*pp2 != NULL && (*pp2)->klass() != NULL) {
      if (klass != NULL) {
        if (klass != (*pp2)->klass()) {
          failure_code = 3;
        }
      } else {
        klass = (*pp2)->klass();
      }
    }

    if (failure_code == 0) {
      if (*pp1 == NULL && *pp2 == NULL) {
        unsigned int hash = compute_hash(class_name);
        int index = hash_to_index(hash);
        LoaderConstraintEntry* p;
223
        p = new_entry(hash, class_name, klass, 2, 2);
224
        p->set_loaders(NEW_C_HEAP_ARRAY(ClassLoaderData*, 2, mtClass));
D
duke 已提交
225 226 227 228 229 230 231 232 233
        p->set_loader(0, class_loader1());
        p->set_loader(1, class_loader2());
        p->set_klass(klass);
        p->set_next(bucket(index));
        set_entry(index, p);
        if (TraceLoaderConstraints) {
          ResourceMark rm;
          tty->print("[Adding new constraint for name: %s, loader[0]: %s,"
                     " loader[1]: %s ]\n",
234
                     class_name->as_C_string(),
D
duke 已提交
235 236 237 238 239 240 241 242 243 244 245 246
                     SystemDictionary::loader_name(class_loader1()),
                     SystemDictionary::loader_name(class_loader2())
                     );
        }
      } else if (*pp1 == *pp2) {
        /* constraint already imposed */
        if ((*pp1)->klass() == NULL) {
          (*pp1)->set_klass(klass);
          if (TraceLoaderConstraints) {
            ResourceMark rm;
            tty->print("[Setting class object in existing constraint for"
                       " name: %s and loader %s ]\n",
247
                       class_name->as_C_string(),
D
duke 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277
                       SystemDictionary::loader_name(class_loader1())
                       );
          }
        } else {
          assert((*pp1)->klass() == klass, "loader constraints corrupted");
        }
      } else if (*pp1 == NULL) {
        extend_loader_constraint(*pp2, class_loader1, klass);
      } else if (*pp2 == NULL) {
        extend_loader_constraint(*pp1, class_loader2, klass);
      } else {
        merge_loader_constraints(pp1, pp2, klass);
      }
    }
  }

  if (failure_code != 0 && TraceLoaderConstraints) {
    ResourceMark rm;
    const char* reason = "";
    switch(failure_code) {
    case 1: reason = "the class objects presented by loader[0] and loader[1]"
              " are different"; break;
    case 2: reason = "the class object presented by loader[0] does not match"
              " the stored class object in the constraint"; break;
    case 3: reason = "the class object presented by loader[1] does not match"
              " the stored class object in the constraint"; break;
    default: reason = "unknown reason code";
    }
    tty->print("[Failed to add constraint for name: %s, loader[0]: %s,"
               " loader[1]: %s, Reason: %s ]\n",
278
               class_name->as_C_string(),
D
duke 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292
               SystemDictionary::loader_name(class_loader1()),
               SystemDictionary::loader_name(class_loader2()),
               reason
               );
  }

  return failure_code == 0;
}


// return true if the constraint was updated, false if the constraint is
// violated
bool LoaderConstraintTable::check_or_update(instanceKlassHandle k,
                                                   Handle loader,
293
                                                   Symbol* name) {
D
duke 已提交
294 295 296 297 298 299
  LoaderConstraintEntry* p = *(find_loader_constraint(name, loader));
  if (p && p->klass() != NULL && p->klass() != k()) {
    if (TraceLoaderConstraints) {
      ResourceMark rm;
      tty->print("[Constraint check failed for name %s, loader %s: "
                 "the presented class object differs from that stored ]\n",
300
                 name->as_C_string(),
D
duke 已提交
301 302 303 304 305 306 307 308 309 310
                 SystemDictionary::loader_name(loader()));
    }
    return false;
  } else {
    if (p && p->klass() == NULL) {
      p->set_klass(k());
      if (TraceLoaderConstraints) {
        ResourceMark rm;
        tty->print("[Updating constraint for name %s, loader %s, "
                   "by setting class object ]\n",
311
                   name->as_C_string(),
D
duke 已提交
312 313 314 315 316 317 318
                   SystemDictionary::loader_name(loader()));
      }
    }
    return true;
  }
}

319
Klass* LoaderConstraintTable::find_constrained_klass(Symbol* name,
D
duke 已提交
320 321
                                                       Handle loader) {
  LoaderConstraintEntry *p = *(find_loader_constraint(name, loader));
322
  if (p != NULL && p->klass() != NULL) {
H
hseigel 已提交
323
    if (p->klass()->oop_is_instance() && !InstanceKlass::cast(p->klass())->is_loaded()) {
324 325 326 327
      // Only return fully loaded classes.  Classes found through the
      // constraints might still be in the process of loading.
      return NULL;
    }
D
duke 已提交
328
    return p->klass();
329
  }
D
duke 已提交
330 331 332 333 334 335 336 337 338 339

  // No constraints, or else no klass loaded yet.
  return NULL;
}

void LoaderConstraintTable::ensure_loader_constraint_capacity(
                                                     LoaderConstraintEntry *p,
                                                    int nfree) {
    if (p->max_loaders() - p->num_loaders() < nfree) {
        int n = nfree + p->num_loaders();
340 341
        ClassLoaderData** new_loaders = NEW_C_HEAP_ARRAY(ClassLoaderData*, n, mtClass);
        memcpy(new_loaders, p->loaders(), sizeof(ClassLoaderData*) * p->num_loaders());
D
duke 已提交
342
        p->set_max_loaders(n);
343
        FREE_C_HEAP_ARRAY(ClassLoaderData*, p->loaders(), mtClass);
D
duke 已提交
344 345 346 347 348 349 350
        p->set_loaders(new_loaders);
    }
}


void LoaderConstraintTable::extend_loader_constraint(LoaderConstraintEntry* p,
                                                     Handle loader,
351
                                                     Klass* klass) {
D
duke 已提交
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
  ensure_loader_constraint_capacity(p, 1);
  int num = p->num_loaders();
  p->set_loader(num, loader());
  p->set_num_loaders(num + 1);
  if (TraceLoaderConstraints) {
    ResourceMark rm;
    tty->print("[Extending constraint for name %s by adding loader[%d]: %s %s",
               p->name()->as_C_string(),
               num,
               SystemDictionary::loader_name(loader()),
               (p->klass() == NULL ? " and setting class object ]\n" : " ]\n")
               );
  }
  if (p->klass() == NULL) {
    p->set_klass(klass);
  } else {
    assert(klass == NULL || p->klass() == klass, "constraints corrupted");
  }
}


void LoaderConstraintTable::merge_loader_constraints(
                                                   LoaderConstraintEntry** pp1,
                                                   LoaderConstraintEntry** pp2,
376
                                                   Klass* klass) {
D
duke 已提交
377 378 379 380 381 382 383 384 385 386 387 388 389 390
  // make sure *pp1 has higher capacity
  if ((*pp1)->max_loaders() < (*pp2)->max_loaders()) {
    LoaderConstraintEntry** tmp = pp2;
    pp2 = pp1;
    pp1 = tmp;
  }

  LoaderConstraintEntry* p1 = *pp1;
  LoaderConstraintEntry* p2 = *pp2;

  ensure_loader_constraint_capacity(p1, p2->num_loaders());

  for (int i = 0; i < p2->num_loaders(); i++) {
    int num = p1->num_loaders();
391
    p1->set_loader_data(num, p2->loader_data(i));
D
duke 已提交
392 393 394 395 396 397 398 399 400 401 402
    p1->set_num_loaders(num + 1);
  }

  if (TraceLoaderConstraints) {
    ResourceMark rm;
    tty->print_cr("[Merged constraints for name %s, new loader list:",
                  p1->name()->as_C_string()
                  );

    for (int i = 0; i < p1->num_loaders(); i++) {
      tty->print_cr("[   [%d]: %s", i,
403
                    p1->loader_data(i)->loader_name());
D
duke 已提交
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424
    }
    if (p1->klass() == NULL) {
      tty->print_cr("[... and setting class object]");
    }
  }

  // p1->klass() will hold NULL if klass, p2->klass(), and old
  // p1->klass() are all NULL.  In addition, all three must have
  // matching non-NULL values, otherwise either the constraints would
  // have been violated, or the constraints had been corrupted (and an
  // assertion would fail).
  if (p2->klass() != NULL) {
    assert(p2->klass() == klass, "constraints corrupted");
  }
  if (p1->klass() == NULL) {
    p1->set_klass(klass);
  } else {
    assert(p1->klass() == klass, "constraints corrupted");
  }

  *pp2 = p2->next();
Z
zgu 已提交
425
  FREE_C_HEAP_ARRAY(oop, p2->loaders(), mtClass);
D
duke 已提交
426 427 428 429 430
  free_entry(p2);
  return;
}


431 432
void LoaderConstraintTable::verify(Dictionary* dictionary,
                                   PlaceholderTable* placeholders) {
D
duke 已提交
433 434 435 436 437 438
  Thread *thread = Thread::current();
  for (int cindex = 0; cindex < _loader_constraint_size; cindex++) {
    for (LoaderConstraintEntry* probe = bucket(cindex);
                                probe != NULL;
                                probe = probe->next()) {
      if (probe->klass() != NULL) {
439
        InstanceKlass* ik = InstanceKlass::cast(probe->klass());
D
duke 已提交
440
        guarantee(ik->name() == probe->name(), "name should match");
441
        Symbol* name = ik->name();
442 443
        ClassLoaderData* loader_data = ik->class_loader_data();
        unsigned int d_hash = dictionary->compute_hash(name, loader_data);
D
duke 已提交
444
        int d_index = dictionary->hash_to_index(d_hash);
445
        Klass* k = dictionary->find_class(d_index, d_hash, name, loader_data);
446 447
        if (k != NULL) {
          // We found the class in the system dictionary, so we should
448
          // make sure that the Klass* matches what we already have.
449 450 451 452
          guarantee(k == probe->klass(), "klass should be in dictionary");
        } else {
          // If we don't find the class in the system dictionary, it
          // has to be in the placeholders table.
453
          unsigned int p_hash = placeholders->compute_hash(name, loader_data);
454 455
          int p_index = placeholders->hash_to_index(p_hash);
          PlaceholderEntry* entry = placeholders->get_entry(p_index, p_hash,
456
                                                            name, loader_data);
457

458
          // The InstanceKlass might not be on the entry, so the only
459 460 461 462
          // thing we can check here is whether we were successful in
          // finding the class in the placeholders table.
          guarantee(entry != NULL, "klass should be in the placeholders");
        }
D
duke 已提交
463 464
      }
      for (int n = 0; n< probe->num_loaders(); n++) {
465
        assert(ClassLoaderDataGraph::contains_loader_data(probe->loader_data(n)), "The loader is missing");
D
duke 已提交
466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
      }
    }
  }
}

#ifndef PRODUCT

// Called with the system dictionary lock held
void LoaderConstraintTable::print() {
  ResourceMark rm;

  assert_locked_or_safepoint(SystemDictionary_lock);
  tty->print_cr("Java loader constraints (entries=%d)", _loader_constraint_size);
  for (int cindex = 0; cindex < _loader_constraint_size; cindex++) {
    for (LoaderConstraintEntry* probe = bucket(cindex);
                                probe != NULL;
                                probe = probe->next()) {
      tty->print("%4d: ", cindex);
      probe->name()->print();
      tty->print(" , loaders:");
      for (int n = 0; n < probe->num_loaders(); n++) {
487
        probe->loader_data(n)->print_value();
D
duke 已提交
488 489 490 491 492 493 494
        tty->print(", ");
      }
      tty->cr();
    }
  }
}
#endif