Path: blob/master/src/hotspot/share/compiler/methodMatcher.cpp
64440 views
/*1* Copyright (c) 2016, 2021, 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/symbolTable.hpp"26#include "classfile/vmSymbols.hpp"27#include "compiler/compilerOracle.hpp"28#include "compiler/methodMatcher.hpp"29#include "memory/oopFactory.hpp"30#include "memory/resourceArea.hpp"31#include "oops/method.hpp"32#include "oops/oop.inline.hpp"3334// The JVM specification defines the allowed characters.35// Tokens that are disallowed by the JVM specification can have36// a meaning to the parser so we need to include them here.37// The parser does not enforce all rules of the JVMS - a successful parse38// does not mean that it is an allowed name. Illegal names will39// be ignored since they never can match a class or method.40//41// '\0' and 0xf0-0xff are disallowed in constant string values42// 0x20 ' ', 0x09 '\t' and, 0x2c ',' are used in the matching43// 0x5b '[' and 0x5d ']' can not be used because of the matcher44// 0x28 '(' and 0x29 ')' are used for the signature45// 0x2e '.' is always replaced before the matching46// 0x2f '/' is only used in the class name as package separator47//48// It seems hard to get Non-ASCII characters to work in all circumstances due49// to limitations in Windows. So only ASCII characters are supported on Windows.5051#define RANGEBASE_ASCII "\x1\x2\x3\x4\x5\x6\x7\x8\xa\xb\xc\xd\xe\xf" \52"\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \53"\x21\x22\x23\x24\x25\x26\x27\x2a\x2b\x2c\x2d" \54"\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \55"\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \56"\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5c\x5e\x5f" \57"\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \58"\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f"5960#define RANGEBASE_NON_ASCII "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \61"\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \62"\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \63"\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \64"\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \65"\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \66"\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"6768#define RANGEBASE RANGEBASE_ASCII NOT_WINDOWS(RANGEBASE_NON_ASCII)6970#define RANGE0 "[*" RANGEBASE "]"71#define RANGESLASH "[*" RANGEBASE "/]"7273MethodMatcher::MethodMatcher():74_class_name(NULL)75, _method_name(NULL)76, _signature(NULL)77, _class_mode(Exact)78, _method_mode(Exact) {79}8081MethodMatcher::~MethodMatcher() {82if (_class_name != NULL) {83_class_name->decrement_refcount();84}85if (_method_name != NULL) {86_method_name->decrement_refcount();87}88if (_signature != NULL) {89_signature->decrement_refcount();90}91}9293void MethodMatcher::init(Symbol* class_name, Mode class_mode,94Symbol* method_name, Mode method_mode,95Symbol* signature) {96_class_mode = class_mode;97_method_mode = method_mode;98_class_name = class_name;99_method_name = method_name;100_signature = signature;101}102103bool MethodMatcher::canonicalize(char * line, const char *& error_msg) {104char* colon = strstr(line, "::");105bool have_colon = (colon != NULL);106if (have_colon) {107// Don't allow multiple '::'108if (colon[2] != '\0') {109if (strstr(colon+2, "::")) {110error_msg = "Method pattern only allows one '::' allowed";111return false;112}113}114115char* pos = line;116if (pos != NULL) {117for (char* lp = pos + 1; *lp != '\0'; lp++) {118if (*lp == '(') {119break;120}121122if (*lp == '/') {123error_msg = "Method pattern uses '/' together with '::'";124return false;125}126}127}128} else {129// Don't allow mixed package separators130char* pos = strchr(line, '.');131bool in_signature = false;132if (pos != NULL) {133for (char* lp = pos + 1; *lp != '\0'; lp++) {134if (*lp == '(') {135in_signature = true;136}137138// After any comma the method pattern has ended139if (*lp == ',') {140break;141}142143if (!in_signature && (*lp == '/')) {144error_msg = "Method pattern uses mixed '/' and '.' package separators";145return false;146}147148if (*lp == '.') {149error_msg = "Method pattern uses multiple '.' in pattern";150return false;151}152}153}154}155156for (char* lp = line; *lp != '\0'; lp++) {157// Allow '.' to separate the class name from the method name.158// This is the preferred spelling of methods:159// exclude java/lang/String.indexOf(I)I160// Allow ',' for spaces (eases command line quoting).161// exclude,java/lang/String.indexOf162// For backward compatibility, allow space as separator also.163// exclude java/lang/String indexOf164// exclude,java/lang/String,indexOf165// For easy cut-and-paste of method names, allow VM output format166// as produced by Method::print_short_name:167// exclude java.lang.String::indexOf168// For simple implementation convenience here, convert them all to space.169170if (have_colon) {171if (*lp == '.') *lp = '/'; // dots build the package prefix172if (*lp == ':') *lp = ' ';173}174if (*lp == ',' || *lp == '.') *lp = ' ';175176#ifdef _WINDOWS177// It seems hard to get Non-ASCII characters to work in all circumstances due178// to limitations in Windows. So only ASCII characters are supported on Windows.179if (!isascii(*lp)) {180error_msg = "Non-ASCII characters are not supported on Windows.";181return false;182}183#endif184}185return true;186}187188bool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) const {189if (match_mode == Any) {190return true;191}192193if (match_mode == Exact) {194return candidate == match;195}196197ResourceMark rm;198const char * candidate_string = candidate->as_C_string();199const char * match_string = match->as_C_string();200201switch (match_mode) {202case Prefix:203return strstr(candidate_string, match_string) == candidate_string;204205case Suffix: {206size_t clen = strlen(candidate_string);207size_t mlen = strlen(match_string);208return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0;209}210211case Substring:212return strstr(candidate_string, match_string) != NULL;213214default:215return false;216}217}218219static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {220int match = MethodMatcher::Exact;221if (name[0] == '*') {222if (strlen(name) == 1) {223return MethodMatcher::Any;224}225match |= MethodMatcher::Suffix;226memmove(name, name + 1, strlen(name + 1) + 1);227}228229size_t len = strlen(name);230if (len > 0 && name[len - 1] == '*') {231match |= MethodMatcher::Prefix;232name[--len] = '\0';233}234235if (strlen(name) == 0) {236error_msg = "** Not a valid pattern";237return MethodMatcher::Any;238}239240if (strstr(name, "*") != NULL) {241error_msg = " Embedded * not allowed";242return MethodMatcher::Unknown;243}244return (MethodMatcher::Mode)match;245}246247// Skip any leading spaces248void skip_leading_spaces(char*& line, int* total_bytes_read ) {249int bytes_read = 0;250sscanf(line, "%*[ \t]%n", &bytes_read);251if (bytes_read > 0) {252line += bytes_read;253*total_bytes_read += bytes_read;254}255}256257void MethodMatcher::parse_method_pattern(char*& line, const char*& error_msg, MethodMatcher* matcher) {258MethodMatcher::Mode c_match;259MethodMatcher::Mode m_match;260char class_name[256] = {0};261char method_name[256] = {0};262char sig[1024] = {0};263int bytes_read = 0;264int total_bytes_read = 0;265266assert(error_msg == NULL, "Dont call here with error_msg already set");267268if (!MethodMatcher::canonicalize(line, error_msg)) {269assert(error_msg != NULL, "Message must be set if parsing failed");270return;271}272273skip_leading_spaces(line, &total_bytes_read);274if (*line == '\0') {275error_msg = "Method pattern missing from command";276return;277}278279if (2 == sscanf(line, "%255" RANGESLASH "%*[ ]" "%255" RANGE0 "%n", class_name, method_name, &bytes_read)) {280c_match = check_mode(class_name, error_msg);281m_match = check_mode(method_name, error_msg);282283// Over-consumption284// method_name points to an option type or option name because the method name is not specified by users.285// In very rare case, the method name happens to be same as option type/name, so look ahead to make sure286// it doesn't show up again.287if ((OptionType::Unknown != CompilerOracle::parse_option_type(method_name) ||288CompileCommand::Unknown != CompilerOracle::parse_option_name(method_name)) &&289*(line + bytes_read) != '\0' &&290strstr(line + bytes_read, method_name) == NULL) {291error_msg = "Did not specify any method name";292method_name[0] = '\0';293return;294}295296if ((strchr(class_name, JVM_SIGNATURE_SPECIAL) != NULL) ||297(strchr(class_name, JVM_SIGNATURE_ENDSPECIAL) != NULL)) {298error_msg = "Chars '<' and '>' not allowed in class name";299return;300}301302if ((strchr(method_name, JVM_SIGNATURE_SPECIAL) != NULL) ||303(strchr(method_name, JVM_SIGNATURE_ENDSPECIAL) != NULL)) {304if (!vmSymbols::object_initializer_name()->equals(method_name) &&305!vmSymbols::class_initializer_name()->equals(method_name)) {306error_msg = "Chars '<' and '>' only allowed in <init> and <clinit>";307return;308}309}310311if (c_match == MethodMatcher::Unknown || m_match == MethodMatcher::Unknown) {312assert(error_msg != NULL, "Must have been set by check_mode()");313return;314}315316EXCEPTION_MARK;317Symbol* signature = NULL;318line += bytes_read;319bytes_read = 0;320321skip_leading_spaces(line, &total_bytes_read);322323// there might be a signature following the method.324// signatures always begin with ( so match that by hand325if (line[0] == '(') {326line++;327sig[0] = '(';328// scan the rest329if (1 == sscanf(line, "%1022[[);/" RANGEBASE "]%n", sig+1, &bytes_read)) {330if (strchr(sig, '*') != NULL) {331error_msg = " Wildcard * not allowed in signature";332return;333}334line += bytes_read;335}336signature = SymbolTable::new_symbol(sig);337}338Symbol* c_name = SymbolTable::new_symbol(class_name);339Symbol* m_name = SymbolTable::new_symbol(method_name);340341matcher->init(c_name, c_match, m_name, m_match, signature);342return;343} else {344error_msg = "Could not parse method pattern";345}346}347348bool MethodMatcher::matches(const methodHandle& method) const {349Symbol* class_name = method->method_holder()->name();350Symbol* method_name = method->name();351Symbol* signature = method->signature();352353if (match(class_name, this->class_name(), _class_mode) &&354match(method_name, this->method_name(), _method_mode) &&355((this->signature() == NULL) || match(signature, this->signature(), Prefix))) {356return true;357}358return false;359}360361void MethodMatcher::print_symbol(outputStream* st, Symbol* h, Mode mode) {362if (mode == Suffix || mode == Substring || mode == Any) {363st->print("*");364}365if (mode != Any) {366h->print_utf8_on(st);367}368if (mode == Prefix || mode == Substring) {369st->print("*");370}371}372373void MethodMatcher::print_base(outputStream* st) {374ResourceMark rm;375376print_symbol(st, class_name(), _class_mode);377st->print(".");378print_symbol(st, method_name(), _method_mode);379if (signature() != NULL) {380signature()->print_utf8_on(st);381}382}383384BasicMatcher* BasicMatcher::parse_method_pattern(char* line, const char*& error_msg, bool expect_trailing_chars) {385assert(error_msg == NULL, "Don't call here with error_msg already set");386BasicMatcher* bm = new BasicMatcher();387MethodMatcher::parse_method_pattern(line, error_msg, bm);388if (error_msg != NULL) {389delete bm;390return NULL;391}392if (!expect_trailing_chars) {393// check for bad trailing characters394int bytes_read = 0;395sscanf(line, "%*[ \t]%n", &bytes_read);396if (line[bytes_read] != '\0') {397error_msg = "Unrecognized trailing text after method pattern";398delete bm;399return NULL;400}401}402return bm;403}404405bool BasicMatcher::match(const methodHandle& method) {406for (BasicMatcher* current = this; current != NULL; current = current->next()) {407if (current->matches(method)) {408return true;409}410}411return false;412}413414void InlineMatcher::print(outputStream* st) {415if (_inline_action == InlineMatcher::force_inline) {416st->print("+");417} else {418st->print("-");419}420print_base(st);421}422423InlineMatcher* InlineMatcher::parse_method_pattern(char* line, const char*& error_msg) {424assert(error_msg == NULL, "Dont call here with error_msg already set");425InlineMatcher* im = new InlineMatcher();426MethodMatcher::parse_method_pattern(line, error_msg, im);427if (error_msg != NULL) {428delete im;429return NULL;430}431return im;432}433434bool InlineMatcher::match(const methodHandle& method, int inline_action) {435for (InlineMatcher* current = this; current != NULL; current = current->next()) {436if (current->matches(method)) {437return (current->_inline_action == inline_action);438}439}440return false;441}442443InlineMatcher* InlineMatcher::parse_inline_pattern(char* str, const char*& error_msg) {444// check first token is +/-445InlineType _inline_action;446switch (str[0]) {447case '-':448_inline_action = InlineMatcher::dont_inline;449break;450case '+':451_inline_action = InlineMatcher::force_inline;452break;453default:454error_msg = "Missing leading inline type (+/-)";455return NULL;456}457str++;458459assert(error_msg == NULL, "error_msg must not be set yet");460InlineMatcher* im = InlineMatcher::parse_method_pattern(str, error_msg);461if (im == NULL) {462assert(error_msg != NULL, "Must have error message");463return NULL;464}465im->set_action(_inline_action);466return im;467}468469InlineMatcher* InlineMatcher::clone() {470InlineMatcher* m = new InlineMatcher();471m->_class_mode = _class_mode;472m->_method_mode = _method_mode;473m->_inline_action = _inline_action;474m->_class_name = _class_name;475if(_class_name != NULL) {476_class_name->increment_refcount();477}478m->_method_name = _method_name;479if (_method_name != NULL) {480_method_name->increment_refcount();481}482m->_signature = _signature;483if (_signature != NULL) {484_signature->increment_refcount();485}486return m;487}488489490