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 "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_name(NULL) 66 , _method_name(NULL) 67 , _signature(NULL) 68 , _class_mode(Exact) 69 , _method_mode(Exact) { 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 #ifdef _MSC_VER 241 #pragma warning(push) 242 #pragma warning(disable : 4819) 243 #endif 244 void MethodMatcher::parse_method_pattern(char*& line, const char*& error_msg, MethodMatcher* matcher) { 245 MethodMatcher::Mode c_match; 246 MethodMatcher::Mode m_match; 247 char class_name[256] = {0}; 248 char method_name[256] = {0}; 249 char sig[1024] = {0}; 250 int bytes_read = 0; 251 int total_bytes_read = 0; 252 253 assert(error_msg == NULL, "Dont call here with error_msg already set"); 254 255 if (!MethodMatcher::canonicalize(line, error_msg)) { 256 assert(error_msg != NULL, "Message must be set if parsing failed"); 257 return; 258 } 259 260 skip_leading_spaces(line, &total_bytes_read); 261 262 if (2 == sscanf(line, "%255" RANGESLASH "%*[ ]" "%255" RANGE0 "%n", class_name, method_name, &bytes_read)) { 263 c_match = check_mode(class_name, error_msg); 264 m_match = check_mode(method_name, error_msg); 265 266 if ((strchr(class_name, '<') != NULL) || (strchr(class_name, '>') != NULL)) { 267 error_msg = "Chars '<' and '>' not allowed in class name"; 268 return; 269 } 270 if ((strchr(method_name, '<') != NULL) || (strchr(method_name, '>') != NULL)) { 271 if ((strncmp("<init>", method_name, 255) != 0) && (strncmp("<clinit>", method_name, 255) != 0)) { 272 error_msg = "Chars '<' and '>' only allowed in <init> and <clinit>"; 273 return; 274 } 275 } 276 277 if (c_match == MethodMatcher::Unknown || m_match == MethodMatcher::Unknown) { 278 assert(error_msg != NULL, "Must have been set by check_mode()"); 279 return; 280 } 281 282 EXCEPTION_MARK; 283 Symbol* signature = NULL; 284 line += bytes_read; 285 bytes_read = 0; 286 287 skip_leading_spaces(line, &total_bytes_read); 288 289 // there might be a signature following the method. 290 // signatures always begin with ( so match that by hand 291 if (line[0] == '(') { 292 line++; 293 sig[0] = '('; 294 // scan the rest 295 if (1 == sscanf(line, "%1022[[);/" RANGEBASE "]%n", sig+1, &bytes_read)) { 296 if (strchr(sig, '*') != NULL) { 297 error_msg = " Wildcard * not allowed in signature"; 298 return; 299 } 300 line += bytes_read; 301 } 302 signature = SymbolTable::new_symbol(sig, CHECK); 303 } 304 Symbol* c_name = SymbolTable::new_symbol(class_name, CHECK); 305 Symbol* m_name = SymbolTable::new_symbol(method_name, CHECK); 306 307 matcher->init(c_name, c_match, m_name, m_match, signature); 308 return; 309 } else { 310 error_msg = "Could not parse method pattern"; 311 } 312 } 313 #ifdef _MSC_VER 314 #pragma warning(pop) 315 #endif 316 317 bool MethodMatcher::matches(const methodHandle& method) const { 318 Symbol* class_name = method->method_holder()->name(); 319 Symbol* method_name = method->name(); 320 Symbol* signature = method->signature(); 321 322 if (match(class_name, this->class_name(), _class_mode) && 323 match(method_name, this->method_name(), _method_mode) && 324 ((this->signature() == NULL) || match(signature, this->signature(), Prefix))) { 325 return true; 326 } 327 return false; 328 } 329 330 void MethodMatcher::print_symbol(outputStream* st, Symbol* h, Mode mode) { 331 if (mode == Suffix || mode == Substring || mode == Any) { 332 st->print("*"); 333 } 334 if (mode != Any) { 335 h->print_utf8_on(st); 336 } 337 if (mode == Prefix || mode == Substring) { 338 st->print("*"); 339 } 340 } 341 342 void MethodMatcher::print_base(outputStream* st) { 343 ResourceMark rm; 344 345 print_symbol(st, class_name(), _class_mode); 346 st->print("."); 347 print_symbol(st, method_name(), _method_mode); 348 if (signature() != NULL) { 349 signature()->print_utf8_on(st); 350 } 351 } 352 353 BasicMatcher* BasicMatcher::parse_method_pattern(char* line, const char*& error_msg) { 354 assert(error_msg == NULL, "Don't call here with error_msg already set"); 355 BasicMatcher* bm = new BasicMatcher(); 356 MethodMatcher::parse_method_pattern(line, error_msg, bm); 357 if (error_msg != NULL) { 358 delete bm; 359 return NULL; 360 } 361 362 // check for bad trailing characters 363 int bytes_read = 0; 364 sscanf(line, "%*[ \t]%n", &bytes_read); 365 if (line[bytes_read] != '\0') { 366 error_msg = "Unrecognized trailing text after method pattern"; 367 delete bm; 368 return NULL; 369 } 370 return bm; 371 } 372 373 bool BasicMatcher::match(const methodHandle& method) { 374 for (BasicMatcher* current = this; current != NULL; current = current->next()) { 375 if (current->matches(method)) { 376 return true; 377 } 378 } 379 return false; 380 } 381 382 void InlineMatcher::print(outputStream* st) { 383 if (_inline_action == InlineMatcher::force_inline) { 384 st->print("+"); 385 } else { 386 st->print("-"); 387 } 388 print_base(st); 389 } 390 391 InlineMatcher* InlineMatcher::parse_method_pattern(char* line, const char*& error_msg) { 392 assert(error_msg == NULL, "Dont call here with error_msg already set"); 393 InlineMatcher* im = new InlineMatcher(); 394 MethodMatcher::parse_method_pattern(line, error_msg, im); 395 if (error_msg != NULL) { 396 delete im; 397 return NULL; 398 } 399 return im; 400 } 401 402 bool InlineMatcher::match(const methodHandle& method, int inline_action) { 403 for (InlineMatcher* current = this; current != NULL; current = current->next()) { 404 if (current->matches(method)) { 405 return (current->_inline_action == inline_action); 406 } 407 } 408 return false; 409 } 410 411 InlineMatcher* InlineMatcher::parse_inline_pattern(char* str, const char*& error_msg) { 412 // check first token is +/- 413 InlineType _inline_action; 414 switch (str[0]) { 415 case '-': 416 _inline_action = InlineMatcher::dont_inline; 417 break; 418 case '+': 419 _inline_action = InlineMatcher::force_inline; 420 break; 421 default: 422 error_msg = "Missing leading inline type (+/-)"; 423 return NULL; 424 } 425 str++; 426 427 int bytes_read = 0; 428 assert(error_msg== NULL, "error_msg must not be set yet"); 429 InlineMatcher* im = InlineMatcher::parse_method_pattern(str, error_msg); 430 if (im == NULL) { 431 assert(error_msg != NULL, "Must have error message"); 432 return NULL; 433 } 434 im->set_action(_inline_action); 435 return im; 436 } 437 438 InlineMatcher* InlineMatcher::clone() { 439 InlineMatcher* m = new InlineMatcher(); 440 m->_class_mode = _class_mode; 441 m->_method_mode = _method_mode; 442 m->_inline_action = _inline_action; 443 m->_class_name = _class_name; 444 if(_class_name != NULL) { 445 _class_name->increment_refcount(); 446 } 447 m->_method_name = _method_name; 448 if (_method_name != NULL) { 449 _method_name->increment_refcount(); 450 } 451 m->_signature = _signature; 452 if (_signature != NULL) { 453 _signature->increment_refcount(); 454 } 455 return m; 456 }