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