Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/classfile/javaAssertions.cpp
32285 views
/*1* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "classfile/javaAssertions.hpp"26#include "classfile/javaClasses.hpp"27#include "classfile/systemDictionary.hpp"28#include "classfile/vmSymbols.hpp"29#include "memory/allocation.inline.hpp"30#include "memory/oopFactory.hpp"31#include "oops/oop.inline.hpp"32#include "runtime/handles.inline.hpp"3334bool JavaAssertions::_userDefault = false;35bool JavaAssertions::_sysDefault = false;36JavaAssertions::OptionList* JavaAssertions::_classes = 0;37JavaAssertions::OptionList* JavaAssertions::_packages = 0;3839JavaAssertions::OptionList::OptionList(const char* name, bool enabled,40OptionList* next) {41assert(name != 0, "need a name");42_name = name;43_enabled = enabled;44_next = next;45}4647int JavaAssertions::OptionList::count(OptionList* p) {48int rc;49for (rc = 0; p != 0; p = p->next(), ++rc) /* empty */;50return rc;51}5253void JavaAssertions::addOption(const char* name, bool enable) {54assert(name != 0, "must have a name");5556// Copy the name. The storage needs to exist for the the lifetime of the vm;57// it is never freed, so will be leaked (along with other option strings -58// e.g., bootclasspath) if a process creates/destroys multiple VMs.59int len = (int)strlen(name);60char *name_copy = NEW_C_HEAP_ARRAY(char, len + 1, mtClass);61strcpy(name_copy, name);6263// Figure out which list the new item should go on. Names that end in "..."64// go on the package tree list.65OptionList** head = &_classes;66if (len >= 3 && strcmp(name_copy + len - 3, "...") == 0) {67// Delete the "...".68len -= 3;69name_copy[len] = '\0';70head = &_packages;71}7273// Convert class/package names to internal format. Will have to convert back74// when copying to java in createJavaAssertionStatusDirectives, but that75// should happen only once. Alternative would require that76// JVM_DesiredAssertionStatus pass the external_name() to77// JavaAssertion::enabled(), but that is done once per loaded class.78for (int i = 0; i < len; ++i) {79if (name_copy[i] == '.') name_copy[i] = '/';80}8182if (TraceJavaAssertions) {83tty->print_cr("JavaAssertions: adding %s %s=%d",84head == &_classes ? "class" : "package",85name_copy[0] != '\0' ? name_copy : "'default'",86enable);87}8889// Prepend a new item to the list. Items added later take precedence, so90// prepending allows us to stop searching the list after the first match.91*head = new OptionList(name_copy, enable, *head);92}9394oop JavaAssertions::createAssertionStatusDirectives(TRAPS) {95Symbol* asd_sym = vmSymbols::java_lang_AssertionStatusDirectives();96Klass* k = SystemDictionary::resolve_or_fail(asd_sym, true, CHECK_NULL);97instanceKlassHandle asd_klass (THREAD, k);98asd_klass->initialize(CHECK_NULL);99Handle h = asd_klass->allocate_instance_handle(CHECK_NULL);100101int len;102typeArrayOop t;103len = OptionList::count(_packages);104objArrayOop pn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);105objArrayHandle pkgNames (THREAD, pn);106t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);107typeArrayHandle pkgEnabled(THREAD, t);108fillJavaArrays(_packages, len, pkgNames, pkgEnabled, CHECK_NULL);109110len = OptionList::count(_classes);111objArrayOop cn = oopFactory::new_objArray(SystemDictionary::String_klass(), len, CHECK_NULL);112objArrayHandle classNames (THREAD, cn);113t = oopFactory::new_typeArray(T_BOOLEAN, len, CHECK_NULL);114typeArrayHandle classEnabled(THREAD, t);115fillJavaArrays(_classes, len, classNames, classEnabled, CHECK_NULL);116117java_lang_AssertionStatusDirectives::set_packages(h(), pkgNames());118java_lang_AssertionStatusDirectives::set_packageEnabled(h(), pkgEnabled());119java_lang_AssertionStatusDirectives::set_classes(h(), classNames());120java_lang_AssertionStatusDirectives::set_classEnabled(h(), classEnabled());121java_lang_AssertionStatusDirectives::set_deflt(h(), userClassDefault());122return h();123}124125void JavaAssertions::fillJavaArrays(const OptionList* p, int len,126objArrayHandle names, typeArrayHandle enabled, TRAPS) {127// Fill in the parallel names and enabled (boolean) arrays. Start at the end128// of the array and work backwards, so the order of items in the arrays129// matches the order on the command line (the list is in reverse order, since130// it was created by prepending successive items from the command line).131int index;132for (index = len - 1; p != 0; p = p->next(), --index) {133assert(index >= 0, "length does not match list");134Handle s = java_lang_String::create_from_str(p->name(), CHECK);135s = java_lang_String::char_converter(s, '/', '.', CHECK);136names->obj_at_put(index, s());137enabled->bool_at_put(index, p->enabled());138}139assert(index == -1, "length does not match list");140}141142inline JavaAssertions::OptionList*143JavaAssertions::match_class(const char* classname) {144for (OptionList* p = _classes; p != 0; p = p->next()) {145if (strcmp(p->name(), classname) == 0) {146return p;147}148}149return 0;150}151152JavaAssertions::OptionList*153JavaAssertions::match_package(const char* classname) {154// Search the package list for any items that apply to classname. Each155// sub-package in classname is checked, from most-specific to least, until one156// is found.157if (_packages == 0) return 0;158159// Find the length of the "most-specific" package in classname. If classname160// does not include a package, length will be 0 which will match items for the161// default package (from options "-ea:..." or "-da:...").162size_t len = strlen(classname);163for (/* empty */; len > 0 && classname[len] != '/'; --len) /* empty */;164165do {166assert(len == 0 || classname[len] == '/', "not a package name");167for (OptionList* p = _packages; p != 0; p = p->next()) {168if (strncmp(p->name(), classname, len) == 0 && p->name()[len] == '\0') {169return p;170}171}172173// Find the length of the next package, taking care to avoid decrementing174// past 0 (len is unsigned).175while (len > 0 && classname[--len] != '/') /* empty */;176} while (len > 0);177178return 0;179}180181inline void JavaAssertions::trace(const char* name,182const char* typefound, const char* namefound, bool enabled) {183if (TraceJavaAssertions) {184tty->print_cr("JavaAssertions: search for %s found %s %s=%d",185name, typefound, namefound[0] != '\0' ? namefound : "'default'", enabled);186}187}188189bool JavaAssertions::enabled(const char* classname, bool systemClass) {190assert(classname != 0, "must have a classname");191192// This will be slow if the number of assertion options on the command line is193// large--it traverses two lists, one of them multiple times. Could use a194// single n-ary tree instead of lists if someone ever notices.195196// First check options that apply to classes. If we find a match we're done.197OptionList* p;198if (p = match_class(classname)) {199trace(classname, "class", p->name(), p->enabled());200return p->enabled();201}202203// Now check packages, from most specific to least.204if (p = match_package(classname)) {205trace(classname, "package", p->name(), p->enabled());206return p->enabled();207}208209// No match. Return the default status.210bool result = systemClass ? systemClassDefault() : userClassDefault();211trace(classname, systemClass ? "system" : "user", "default", result);212return result;213}214215216