1 /*
   2  * Copyright (c) 2016, 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 "compiler/methodMatcher.hpp"
  27 #include "memory/oopFactory.hpp"
  28 #include "memory/resourceArea.hpp"
  29 #include "oops/oop.inline.hpp"
  30 
  31 // The JVM specification defines the allowed characters.
  32 // Tokens that are disallowed by the JVM specification can have
  33 // a meaning to the parser so we need to include them here.
  34 // The parser does not enforce all rules of the JVMS - a successful parse
  35 // does not mean that it is an allowed name. Illegal names will
  36 // be ignored since they never can match a class or method.
  37 //
  38 // '\0' and 0xf0-0xff are disallowed in constant string values
  39 // 0x20 ' ', 0x09 '\t' and, 0x2c ',' are used in the matching
  40 // 0x5b '[' and 0x5d ']' can not be used because of the matcher
  41 // 0x28 '(' and 0x29 ')' are used for the signature
  42 // 0x2e '.' is always replaced before the matching
  43 // 0x2f '/' is only used in the class name as package separator
  44 
  45 #define RANGEBASE "\x1\x2\x3\x4\x5\x6\x7\x8\xa\xb\xc\xd\xe\xf" \
  46     "\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19\x1a\x1b\x1c\x1d\x1e\x1f" \
  47     "\x21\x22\x23\x24\x25\x26\x27\x2a\x2b\x2c\x2d" \
  48     "\x30\x31\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b\x3c\x3d\x3e\x3f" \
  49     "\x40\x41\x42\x43\x44\x45\x46\x47\x48\x49\x4a\x4b\x4c\x4d\x4e\x4f" \
  50     "\x50\x51\x52\x53\x54\x55\x56\x57\x58\x59\x5a\x5c\x5e\x5f" \
  51     "\x60\x61\x62\x63\x64\x65\x66\x67\x68\x69\x6a\x6b\x6c\x6d\x6e\x6f" \
  52     "\x70\x71\x72\x73\x74\x75\x76\x77\x78\x79\x7a\x7b\x7c\x7d\x7e\x7f" \
  53     "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f" \
  54     "\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f" \
  55     "\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf" \
  56     "\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" \
  57     "\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf" \
  58     "\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf" \
  59     "\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef"
  60 
  61 #define RANGE0 "[*" RANGEBASE "]"
  62 #define RANGESLASH "[*" RANGEBASE "/]"
  63 
  64 MethodMatcher::MethodMatcher():
  65     _class_mode(Exact)
  66   , _method_mode(Exact)
  67   , _class_name(NULL)
  68   , _method_name(NULL)
  69   , _signature(NULL) {
  70 }
  71 
  72 MethodMatcher::~MethodMatcher() {
  73   if (_class_name != NULL) {
  74     _class_name->decrement_refcount();
  75   }
  76   if (_method_name != NULL) {
  77     _method_name->decrement_refcount();
  78   }
  79   if (_signature != NULL) {
  80     _signature->decrement_refcount();
  81   }
  82 }
  83 
  84 void MethodMatcher::init(Symbol* class_name, Mode class_mode,
  85                              Symbol* method_name, Mode method_mode,
  86                              Symbol* signature) {
  87  _class_mode = class_mode;
  88  _method_mode = method_mode;
  89  _class_name = class_name;
  90  _method_name = method_name;
  91  _signature = signature;
  92 }
  93 
  94 bool MethodMatcher::canonicalize(char * line, const char *& error_msg) {
  95   char* colon = strstr(line, "::");
  96   bool have_colon = (colon != NULL);
  97   if (have_colon) {
  98     // Don't allow multiple '::'
  99     if (colon + 2 != '\0') {
 100       if (strstr(colon+2, "::")) {
 101         error_msg = "Method pattern only allows one '::' allowed";
 102         return false;
 103       }
 104     }
 105 
 106     bool in_signature = false;
 107     char* pos = line;
 108     if (pos != NULL) {
 109       for (char* lp = pos + 1; *lp != '\0'; lp++) {
 110         if (*lp == '(') {
 111           break;
 112         }
 113 
 114         if (*lp == '/') {
 115           error_msg = "Method pattern uses '/' together with '::'";
 116           return false;
 117         }
 118       }
 119     }
 120   } else {
 121     // Don't allow mixed package separators
 122     char* pos = strchr(line, '.');
 123     bool in_signature = false;
 124     if (pos != NULL) {
 125       for (char* lp = pos + 1; *lp != '\0'; lp++) {
 126         if (*lp == '(') {
 127           in_signature = true;
 128         }
 129 
 130         // After any comma the method pattern has ended
 131         if (*lp == ',') {
 132           break;
 133         }
 134 
 135         if (!in_signature && (*lp == '/')) {
 136           error_msg = "Method pattern uses mixed '/' and '.' package separators";
 137           return false;
 138         }
 139 
 140         if (*lp == '.') {
 141           error_msg = "Method pattern uses multiple '.' in pattern";
 142           return false;
 143         }
 144       }
 145     }
 146   }
 147 
 148   for (char* lp = line; *lp != '\0'; lp++) {
 149     // Allow '.' to separate the class name from the method name.
 150     // This is the preferred spelling of methods:
 151     //      exclude java/lang/String.indexOf(I)I
 152     // Allow ',' for spaces (eases command line quoting).
 153     //      exclude,java/lang/String.indexOf
 154     // For backward compatibility, allow space as separator also.
 155     //      exclude java/lang/String indexOf
 156     //      exclude,java/lang/String,indexOf
 157     // For easy cut-and-paste of method names, allow VM output format
 158     // as produced by Method::print_short_name:
 159     //      exclude java.lang.String::indexOf
 160     // For simple implementation convenience here, convert them all to space.
 161 
 162     if (have_colon) {
 163       if (*lp == '.')  *lp = '/';   // dots build the package prefix
 164       if (*lp == ':')  *lp = ' ';
 165     }
 166     if (*lp == ',' || *lp == '.')  *lp = ' ';
 167   }
 168   return true;
 169 }
 170 
 171 bool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) const {
 172   if (match_mode == Any) {
 173     return true;
 174   }
 175 
 176   if (match_mode == Exact) {
 177     return candidate == match;
 178   }
 179 
 180   ResourceMark rm;
 181   const char * candidate_string = candidate->as_C_string();
 182   const char * match_string = match->as_C_string();
 183 
 184   switch (match_mode) {
 185   case Prefix:
 186     return strstr(candidate_string, match_string) == candidate_string;
 187 
 188   case Suffix: {
 189     size_t clen = strlen(candidate_string);
 190     size_t mlen = strlen(match_string);
 191     return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0;
 192   }
 193 
 194   case Substring:
 195     return strstr(candidate_string, match_string) != NULL;
 196 
 197   default:
 198     return false;
 199   }
 200 }
 201 
 202 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
 203   int match = MethodMatcher::Exact;
 204   if (name[0] == '*') {
 205     if (strlen(name) == 1) {
 206       return MethodMatcher::Any;
 207     }
 208     match |= MethodMatcher::Suffix;
 209     memmove(name, name + 1, strlen(name + 1) + 1);
 210   }
 211 
 212   size_t len = strlen(name);
 213   if (len > 0 && name[len - 1] == '*') {
 214     match |= MethodMatcher::Prefix;
 215     name[--len] = '\0';
 216   }
 217 
 218   if (strlen(name) == 0) {
 219     error_msg = "** Not a valid pattern";
 220     return MethodMatcher::Any;
 221   }
 222 
 223   if (strstr(name, "*") != NULL) {
 224     error_msg = " Embedded * not allowed";
 225     return MethodMatcher::Unknown;
 226   }
 227   return (MethodMatcher::Mode)match;
 228 }
 229 
 230 // Skip any leading spaces
 231 void skip_leading_spaces(char*& line, int* total_bytes_read ) {
 232   int bytes_read = 0;
 233   sscanf(line, "%*[ \t]%n", &bytes_read);
 234   if (bytes_read > 0) {
 235     line += bytes_read;
 236     *total_bytes_read += bytes_read;
 237   }
 238 }
 239 
 240 void MethodMatcher::parse_method_pattern(char*& line, const char*& error_msg, MethodMatcher* matcher) {
 241   MethodMatcher::Mode c_match;
 242   MethodMatcher::Mode m_match;
 243   char class_name[256] = {0};
 244   char method_name[256] = {0};
 245   char sig[1024] = {0};
 246   int bytes_read = 0;
 247   int total_bytes_read = 0;
 248 
 249   assert(error_msg == NULL, "Dont call here with error_msg already set");
 250 
 251   if (!MethodMatcher::canonicalize(line, error_msg)) {
 252     assert(error_msg != NULL, "Message must be set if parsing failed");
 253     return;
 254   }
 255 
 256   skip_leading_spaces(line, &total_bytes_read);
 257 
 258   if (2 == sscanf(line, "%255" RANGESLASH "%*[ ]" "%255"  RANGE0 "%n", class_name, method_name, &bytes_read)) {
 259     c_match = check_mode(class_name, error_msg);
 260     m_match = check_mode(method_name, error_msg);
 261 
 262     if ((strchr(class_name, '<') != NULL) || (strchr(class_name, '>') != NULL)) {
 263       error_msg = "Chars '<' and '>' not allowed in class name";
 264       return;
 265     }
 266     if ((strchr(method_name, '<') != NULL) || (strchr(method_name, '>') != NULL)) {
 267       if ((strncmp("<init>", method_name, 255) != 0) && (strncmp("<clinit>", method_name, 255) != 0)) {
 268         error_msg = "Chars '<' and '>' only allowed in <init> and <clinit>";
 269         return;
 270       }
 271     }
 272 
 273     if (c_match == MethodMatcher::Unknown || m_match == MethodMatcher::Unknown) {
 274       assert(error_msg != NULL, "Must have been set by check_mode()");
 275       return;
 276     }
 277 
 278     EXCEPTION_MARK;
 279     Symbol* signature = NULL;
 280     line += bytes_read;
 281     bytes_read = 0;
 282 
 283     skip_leading_spaces(line, &total_bytes_read);
 284 
 285     // there might be a signature following the method.
 286     // signatures always begin with ( so match that by hand
 287     if (line[0] == '(') {
 288       line++;
 289       sig[0] = '(';
 290       // scan the rest
 291       if (1 == sscanf(line, "%254[[);/" RANGEBASE "]%n", sig+1, &bytes_read)) {
 292         if (strchr(sig, '*') != NULL) {
 293           error_msg = " Wildcard * not allowed in signature";
 294           return;
 295         }
 296         line += bytes_read;
 297       }
 298       signature = SymbolTable::new_symbol(sig, CHECK);
 299     }
 300     Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK);
 301     Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK);
 302 
 303     matcher->init(c_name, c_match, m_name, m_match, signature);
 304     return;
 305   } else {
 306     error_msg = "Could not parse method pattern";
 307   }
 308 }
 309 
 310 bool MethodMatcher::matches(const methodHandle& method) const {
 311   Symbol* class_name  = method->method_holder()->name();
 312   Symbol* method_name = method->name();
 313   Symbol* signature = method->signature();
 314 
 315   if (match(class_name, this->class_name(), _class_mode) &&
 316       match(method_name, this->method_name(), _method_mode) &&
 317       ((this->signature() == NULL) || match(signature, this->signature(), Prefix))) {
 318     return true;
 319   }
 320   return false;
 321 }
 322 
 323 void MethodMatcher::print_symbol(outputStream* st, Symbol* h, Mode mode) {
 324   if (mode == Suffix || mode == Substring || mode == Any) {
 325     st->print("*");
 326   }
 327   if (mode != Any) {
 328     h->print_utf8_on(st);
 329   }
 330   if (mode == Prefix || mode == Substring) {
 331     st->print("*");
 332   }
 333 }
 334 
 335 void MethodMatcher::print_base(outputStream* st) {
 336   ResourceMark rm;
 337 
 338   print_symbol(st, class_name(), _class_mode);
 339   st->print(".");
 340   print_symbol(st, method_name(), _method_mode);
 341   if (signature() != NULL) {
 342     signature()->print_utf8_on(st);
 343   }
 344 }
 345 
 346 BasicMatcher* BasicMatcher::parse_method_pattern(char* line, const char*& error_msg) {
 347   assert(error_msg == NULL, "Don't call here with error_msg already set");
 348   BasicMatcher* bm = new BasicMatcher();
 349   MethodMatcher::parse_method_pattern(line, error_msg, bm);
 350   if (error_msg != NULL) {
 351     delete bm;
 352     return NULL;
 353   }
 354 
 355   // check for bad trailing characters
 356   int bytes_read = 0;
 357   sscanf(line, "%*[ \t]%n", &bytes_read);
 358   if (line[bytes_read] != '\0') {
 359     error_msg = "Unrecognized trailing text after method pattern";
 360     delete bm;
 361     return NULL;
 362   }
 363   return bm;
 364 }
 365 
 366 bool BasicMatcher::match(const methodHandle& method) {
 367   for (BasicMatcher* current = this; current != NULL; current = current->next()) {
 368     if (current->matches(method)) {
 369       return true;
 370     }
 371   }
 372   return false;
 373 }
 374 
 375 void InlineMatcher::print(outputStream* st) {
 376   if (_inline_action == InlineMatcher::force_inline) {
 377     st->print("+");
 378   } else {
 379     st->print("-");
 380   }
 381   print_base(st);
 382 }
 383 
 384 InlineMatcher* InlineMatcher::parse_method_pattern(char* line, const char*& error_msg) {
 385   assert(error_msg == NULL, "Dont call here with error_msg already set");
 386   InlineMatcher* im = new InlineMatcher();
 387   MethodMatcher::parse_method_pattern(line, error_msg, im);
 388   if (error_msg != NULL) {
 389     delete im;
 390     return NULL;
 391   }
 392   return im;
 393 }
 394 
 395 bool InlineMatcher::match(const methodHandle& method, int inline_action) {
 396   for (InlineMatcher* current = this; current != NULL; current = current->next()) {
 397     if (current->matches(method)) {
 398       return (current->_inline_action == inline_action);
 399     }
 400   }
 401   return false;
 402 }
 403 
 404 InlineMatcher* InlineMatcher::parse_inline_pattern(char* str, const char*& error_msg) {
 405   // check first token is +/-
 406   InlineType _inline_action;
 407    switch (str[0]) {
 408    case '-':
 409      _inline_action = InlineMatcher::dont_inline;
 410      break;
 411    case '+':
 412      _inline_action = InlineMatcher::force_inline;
 413      break;
 414    default:
 415      error_msg = "Missing leading inline type (+/-)";
 416      return NULL;
 417    }
 418    str++;
 419 
 420    int bytes_read = 0;
 421    assert(error_msg== NULL, "error_msg must not be set yet");
 422    InlineMatcher* im = InlineMatcher::parse_method_pattern(str, error_msg);
 423    if (im == NULL) {
 424      assert(error_msg != NULL, "Must have error message");
 425      return NULL;
 426    }
 427    im->set_action(_inline_action);
 428    return im;
 429 }
 430 
 431 InlineMatcher* InlineMatcher::clone() {
 432    InlineMatcher* m = new InlineMatcher();
 433    m->_class_mode =  _class_mode;
 434    m->_method_mode = _method_mode;
 435    m->_inline_action = _inline_action;
 436    m->_class_name = _class_name;
 437    if(_class_name != NULL) {
 438      _class_name->increment_refcount();
 439    }
 440    m->_method_name = _method_name;
 441    if (_method_name != NULL) {
 442      _method_name->increment_refcount();
 443    }
 444    m->_signature = _signature;
 445    if (_signature != NULL) {
 446      _signature->increment_refcount();
 447    }
 448    return m;
 449 }