src/share/vm/classfile/javaAssertions.cpp

Thu, 12 Oct 2017 21:27:07 +0800

author
aoqi
date
Thu, 12 Oct 2017 21:27:07 +0800
changeset 7535
7ae4e26cb1e0
parent 6876
710a3c8b516e
permissions
-rw-r--r--

merge

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

mercurial