annotate src/share/vm/classfile/javaAssertions.cpp @ 7694:2194fdf3fbd9

8047125: (ref) More phantom object references Reviewed-by: mchung, dfuchs, ahgross, jmasa, brutisso, mgerdin Contributed-by: kim.barrett@oracle.com
author jmasa
date Fri, 26 Sep 2014 17:48:10 -0400
parents d2a62e0f25eb
children 5a375300c073
rev   line source
duke@0 1 /*
coleenp@3602 2 * Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.
duke@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@0 4 *
duke@0 5 * This code is free software; you can redistribute it and/or modify it
duke@0 6 * under the terms of the GNU General Public License version 2 only, as
duke@0 7 * published by the Free Software Foundation.
duke@0 8 *
duke@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@0 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@0 13 * accompanied this code).
duke@0 14 *
duke@0 15 * You should have received a copy of the GNU General Public License version
duke@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@0 18 *
trims@1472 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1472 20 * or visit www.oracle.com if you need additional information or have any
trims@1472 21 * questions.
duke@0 22 *
duke@0 23 */
duke@0 24
stefank@1879 25 #include "precompiled.hpp"
stefank@1879 26 #include "classfile/javaAssertions.hpp"
stefank@1879 27 #include "classfile/javaClasses.hpp"
stefank@1879 28 #include "classfile/systemDictionary.hpp"
stefank@1879 29 #include "classfile/vmSymbols.hpp"
stefank@1879 30 #include "memory/allocation.inline.hpp"
stefank@1879 31 #include "memory/oopFactory.hpp"
stefank@1879 32 #include "oops/oop.inline.hpp"
stefank@1879 33 #include "runtime/handles.inline.hpp"
duke@0 34
duke@0 35 bool JavaAssertions::_userDefault = false;
duke@0 36 bool JavaAssertions::_sysDefault = false;
duke@0 37 JavaAssertions::OptionList* JavaAssertions::_classes = 0;
duke@0 38 JavaAssertions::OptionList* JavaAssertions::_packages = 0;
duke@0 39
duke@0 40 JavaAssertions::OptionList::OptionList(const char* name, bool enabled,
duke@0 41 OptionList* next) {
duke@0 42 assert(name != 0, "need a name");
duke@0 43 _name = name;
duke@0 44 _enabled = enabled;
duke@0 45 _next = next;
duke@0 46 }
duke@0 47
duke@0 48 int JavaAssertions::OptionList::count(OptionList* p) {
duke@0 49 int rc;
duke@0 50 for (rc = 0; p != 0; p = p->next(), ++rc) /* empty */;
duke@0 51 return rc;
duke@0 52 }
duke@0 53
duke@0 54 void JavaAssertions::addOption(const char* name, bool enable) {
duke@0 55 assert(name != 0, "must have a name");
duke@0 56
duke@0 57 // Copy the name. The storage needs to exist for the the lifetime of the vm;
duke@0 58 // it is never freed, so will be leaked (along with other option strings -
duke@0 59 // e.g., bootclasspath) if a process creates/destroys multiple VMs.
duke@0 60 int len = (int)strlen(name);
zgu@3465 61 char *name_copy = NEW_C_HEAP_ARRAY(char, len + 1, mtClass);
duke@0 62 strcpy(name_copy, name);
duke@0 63
duke@0 64 // Figure out which list the new item should go on. Names that end in "..."
duke@0 65 // go on the package tree list.
duke@0 66 OptionList** head = &_classes;
duke@0 67 if (len >= 3 && strcmp(name_copy + len - 3, "...") == 0) {
duke@0 68 // Delete the "...".
duke@0 69 len -= 3;
duke@0 70 name_copy[len] = '\0';
duke@0 71 head = &_packages;
duke@0 72 }
duke@0 73
duke@0 74 // Convert class/package names to internal format. Will have to convert back
duke@0 75 // when copying to java in createJavaAssertionStatusDirectives, but that
duke@0 76 // should happen only once. Alternative would require that
duke@0 77 // JVM_DesiredAssertionStatus pass the external_name() to
duke@0 78 // JavaAssertion::enabled(), but that is done once per loaded class.
duke@0 79 for (int i = 0; i < len; ++i) {
duke@0 80 if (name_copy[i] == '.') name_copy[i] = '/';
duke@0 81 }
duke@0 82
duke@0 83 if (TraceJavaAssertions) {
duke@0 84 tty->print_cr("JavaAssertions: adding %s %s=%d",
duke@0 85 head == &_classes ? "class" : "package",
duke@0 86 name_copy[0] != '\0' ? name_copy : "'default'",
duke@0 87 enable);
duke@0 88 }
duke@0 89
duke@0 90 // Prepend a new item to the list. Items added later take precedence, so
duke@0 91 // prepending allows us to stop searching the list after the first match.
duke@0 92 *head = new OptionList(name_copy, enable, *head);
duke@0 93 }
duke@0 94
duke@0 95 oop JavaAssertions::createAssertionStatusDirectives(TRAPS) {
coleenp@2062 96 Symbol* asd_sym = vmSymbols::java_lang_AssertionStatusDirectives();
coleenp@3602 97 Klass* k = SystemDictionary::resolve_or_fail(asd_sym, true, CHECK_NULL);
duke@0 98 instanceKlassHandle asd_klass (THREAD, k);
duke@0 99 asd_klass->initialize(CHECK_NULL);
duke@0 100 Handle h = asd_klass->allocate_instance_handle(CHECK_NULL);
duke@0 101
duke@0 102 int len;
duke@0 103 typeArrayOop t;
duke@0 104 len = OptionList::count(_packages);
never@1142 105 objArrayOop pn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);
duke@0 106 objArrayHandle pkgNames (THREAD, pn);
duke@0 107 t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
duke@0 108 typeArrayHandle pkgEnabled(THREAD, t);
duke@0 109 fillJavaArrays(_packages, len, pkgNames, pkgEnabled, CHECK_NULL);
duke@0 110
duke@0 111 len = OptionList::count(_classes);
never@1142 112 objArrayOop cn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);
duke@0 113 objArrayHandle classNames (THREAD, cn);
duke@0 114 t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);
duke@0 115 typeArrayHandle classEnabled(THREAD, t);
duke@0 116 fillJavaArrays(_classes, len, classNames, classEnabled, CHECK_NULL);
duke@0 117
duke@0 118 java_lang_AssertionStatusDirectives::set_packages(h(), pkgNames());
duke@0 119 java_lang_AssertionStatusDirectives::set_packageEnabled(h(), pkgEnabled());
duke@0 120 java_lang_AssertionStatusDirectives::set_classes(h(), classNames());
duke@0 121 java_lang_AssertionStatusDirectives::set_classEnabled(h(), classEnabled());
duke@0 122 java_lang_AssertionStatusDirectives::set_deflt(h(), userClassDefault());
duke@0 123 return h();
duke@0 124 }
duke@0 125
duke@0 126 void JavaAssertions::fillJavaArrays(const OptionList* p, int len,
duke@0 127 objArrayHandle names, typeArrayHandle enabled, TRAPS) {
duke@0 128 // Fill in the parallel names and enabled (boolean) arrays. Start at the end
duke@0 129 // of the array and work backwards, so the order of items in the arrays
duke@0 130 // matches the order on the command line (the list is in reverse order, since
duke@0 131 // it was created by prepending successive items from the command line).
duke@0 132 int index;
duke@0 133 for (index = len - 1; p != 0; p = p->next(), --index) {
duke@0 134 assert(index >= 0, "length does not match list");
duke@0 135 Handle s = java_lang_String::create_from_str(p->name(), CHECK);
duke@0 136 s = java_lang_String::char_converter(s, '/', '.', CHECK);
duke@0 137 names->obj_at_put(index, s());
duke@0 138 enabled->bool_at_put(index, p->enabled());
duke@0 139 }
duke@0 140 assert(index == -1, "length does not match list");
duke@0 141 }
duke@0 142
duke@0 143 inline JavaAssertions::OptionList*
duke@0 144 JavaAssertions::match_class(const char* classname) {
duke@0 145 for (OptionList* p = _classes; p != 0; p = p->next()) {
duke@0 146 if (strcmp(p->name(), classname) == 0) {
duke@0 147 return p;
duke@0 148 }
duke@0 149 }
duke@0 150 return 0;
duke@0 151 }
duke@0 152
duke@0 153 JavaAssertions::OptionList*
duke@0 154 JavaAssertions::match_package(const char* classname) {
duke@0 155 // Search the package list for any items that apply to classname. Each
duke@0 156 // sub-package in classname is checked, from most-specific to least, until one
duke@0 157 // is found.
duke@0 158 if (_packages == 0) return 0;
duke@0 159
duke@0 160 // Find the length of the "most-specific" package in classname. If classname
duke@0 161 // does not include a package, length will be 0 which will match items for the
duke@0 162 // default package (from options "-ea:..." or "-da:...").
duke@0 163 size_t len = strlen(classname);
duke@0 164 for (/* empty */; len > 0 && classname[len] != '/'; --len) /* empty */;
duke@0 165
duke@0 166 do {
duke@0 167 assert(len == 0 || classname[len] == '/', "not a package name");
duke@0 168 for (OptionList* p = _packages; p != 0; p = p->next()) {
duke@0 169 if (strncmp(p->name(), classname, len) == 0 && p->name()[len] == '\0') {
duke@0 170 return p;
duke@0 171 }
duke@0 172 }
duke@0 173
duke@0 174 // Find the length of the next package, taking care to avoid decrementing
duke@0 175 // past 0 (len is unsigned).
duke@0 176 while (len > 0 && classname[--len] != '/') /* empty */;
duke@0 177 } while (len > 0);
duke@0 178
duke@0 179 return 0;
duke@0 180 }
duke@0 181
duke@0 182 inline void JavaAssertions::trace(const char* name,
duke@0 183 const char* typefound, const char* namefound, bool enabled) {
duke@0 184 if (TraceJavaAssertions) {
duke@0 185 tty->print_cr("JavaAssertions: search for %s found %s %s=%d",
duke@0 186 name, typefound, namefound[0] != '\0' ? namefound : "'default'", enabled);
duke@0 187 }
duke@0 188 }
duke@0 189
duke@0 190 bool JavaAssertions::enabled(const char* classname, bool systemClass) {
duke@0 191 assert(classname != 0, "must have a classname");
duke@0 192
duke@0 193 // This will be slow if the number of assertion options on the command line is
duke@0 194 // large--it traverses two lists, one of them multiple times. Could use a
duke@0 195 // single n-ary tree instead of lists if someone ever notices.
duke@0 196
duke@0 197 // First check options that apply to classes. If we find a match we're done.
duke@0 198 OptionList* p;
duke@0 199 if (p = match_class(classname)) {
duke@0 200 trace(classname, "class", p->name(), p->enabled());
duke@0 201 return p->enabled();
duke@0 202 }
duke@0 203
duke@0 204 // Now check packages, from most specific to least.
duke@0 205 if (p = match_package(classname)) {
duke@0 206 trace(classname, "package", p->name(), p->enabled());
duke@0 207 return p->enabled();
duke@0 208 }
duke@0 209
duke@0 210 // No match. Return the default status.
duke@0 211 bool result = systemClass ? systemClassDefault() : userClassDefault();
duke@0 212 trace(classname, systemClass ? "system" : "user", "default", result);
duke@0 213 return result;
duke@0 214 }