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