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