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