1 /*
2 * Copyright (c) 2016, 2025, 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 "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"
33
34 // The JVM specification defines the allowed characters.
35 // Tokens that are disallowed by the JVM specification can have
36 // 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 parse
38 // does not mean that it is an allowed name. Illegal names will
39 // be ignored since they never can match a class or method.
40 //
41 // '\0' and 0xf0-0xff are disallowed in constant string values
42 // 0x20 ' ', 0x09 '\t' and, 0x2c ',' are used in the matching
43 // 0x5b '[' and 0x5d ']' can not be used because of the matcher
44 // 0x28 '(' and 0x29 ')' are used for the signature
45 // 0x2e '.' is always replaced before the matching
46 // 0x2f '/' is only used in the class name as package separator
47 //
48 // It seems hard to get Non-ASCII characters to work in all circumstances due
49 // to limitations in Windows. So only ASCII characters are supported on Windows.
50
51 #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"
59
60 #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"
67
68 #define RANGEBASE RANGEBASE_ASCII NOT_WINDOWS(RANGEBASE_NON_ASCII)
69
70 #define RANGE0 "[*" RANGEBASE "]"
71 #define RANGESLASH "[*" RANGEBASE "/]"
72
73 MethodMatcher::MethodMatcher():
74 _class_name(nullptr)
75 , _method_name(nullptr)
76 , _signature(nullptr)
77 , _class_mode(Exact)
78 , _method_mode(Exact) {
79 }
80
81 MethodMatcher::~MethodMatcher() {
82 if (_class_name != nullptr) {
83 _class_name->decrement_refcount();
84 }
85 if (_method_name != nullptr) {
86 _method_name->decrement_refcount();
87 }
88 if (_signature != nullptr) {
89 _signature->decrement_refcount();
90 }
91 }
92
93 void MethodMatcher::init(Symbol* class_name, Mode class_mode,
94 Symbol* method_name, Mode method_mode,
95 Symbol* 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 }
102
103 bool MethodMatcher::canonicalize(char * line, const char *& error_msg) {
104 char* colon = strstr(line, "::");
105 bool have_colon = (colon != nullptr);
106 if (have_colon) {
107 // Don't allow multiple '::'
108 if (colon[2] != '\0') {
109 if (strstr(colon+2, "::")) {
110 error_msg = "Method pattern only allows one '::' allowed";
111 return false;
112 }
113 }
114
115 char* pos = line;
116 if (pos != nullptr) {
117 for (char* lp = pos + 1; *lp != '\0'; lp++) {
118 if (*lp == '(') {
119 break;
120 }
121
122 if (*lp == '/') {
123 error_msg = "Method pattern uses '/' together with '::' (tips: replace '/' with '+' for hidden classes)";
124 return false;
125 }
126 }
127 }
128 } else {
129 // Don't allow mixed package separators
130 char* pos = strchr(line, '.');
131 bool in_signature = false;
132 if (pos != nullptr) {
133 for (char* lp = pos + 1; *lp != '\0'; lp++) {
134 if (*lp == '(') {
135 in_signature = true;
136 }
137
138 // After any comma the method pattern has ended
139 if (*lp == ',') {
140 break;
141 }
142
143 if (!in_signature && (*lp == '/')) {
144 error_msg = "Method pattern uses mixed '/' and '.' package separators";
145 return false;
146 }
147
148 if (*lp == '.') {
149 error_msg = "Method pattern uses multiple '.' in pattern";
150 return false;
151 }
152 }
153 }
154 }
155
156 for (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)I
160 // Allow ',' for spaces (eases command line quoting).
161 // exclude,java/lang/String.indexOf
162 // For backward compatibility, allow space as separator also.
163 // exclude java/lang/String indexOf
164 // exclude,java/lang/String,indexOf
165 // For easy cut-and-paste of method names, allow VM output format
166 // as produced by Method::print_short_name:
167 // exclude java.lang.String::indexOf
168 // For simple implementation convenience here, convert them all to space.
169
170 if (have_colon) {
171 if (*lp == '.') *lp = '/'; // dots build the package prefix
172 if (*lp == ':') *lp = ' ';
173 }
174 if (*lp == ',' || *lp == '.') *lp = ' ';
175
176 #ifdef _WINDOWS
177 // It seems hard to get Non-ASCII characters to work in all circumstances due
178 // to limitations in Windows. So only ASCII characters are supported on Windows.
179 if (!isascii(*lp)) {
180 error_msg = "Non-ASCII characters are not supported on Windows.";
181 return false;
182 }
183 #endif
184 }
185 return true;
186 }
187
188 bool MethodMatcher::match(Symbol* candidate, Symbol* match, Mode match_mode) const {
189 if (match_mode == Any) {
190 return true;
191 }
192
193 if (match_mode == Exact) {
194 return candidate == match;
195 }
196
197 ResourceMark rm;
198 const char * candidate_string = candidate->as_C_string();
199 const char * match_string = match->as_C_string();
200
201 switch (match_mode) {
202 case Prefix:
203 return strstr(candidate_string, match_string) == candidate_string;
204
205 case Suffix: {
206 size_t clen = strlen(candidate_string);
207 size_t mlen = strlen(match_string);
208 return clen >= mlen && strcmp(candidate_string + clen - mlen, match_string) == 0;
209 }
210
211 case Substring:
212 return strstr(candidate_string, match_string) != nullptr;
213
214 default:
215 return false;
216 }
217 }
218
219 static MethodMatcher::Mode check_mode(char name[], const char*& error_msg) {
220 int match = MethodMatcher::Exact;
221 size_t len = strlen(name);
222 if (name[0] == '*') {
223 if (len == 1) {
224 return MethodMatcher::Any;
225 }
226 match |= MethodMatcher::Suffix;
227 memmove(name, name + 1, len); // Include terminating nul in move.
228 len--;
229 }
230
231 if (len > 0 && name[len - 1] == '*') {
232 match |= MethodMatcher::Prefix;
233 name[--len] = '\0';
234 }
235
236 if (len == 0) {
237 error_msg = "** Not a valid pattern";
238 return MethodMatcher::Any;
239 }
240
241 if (strstr(name, "*") != nullptr) {
242 error_msg = " Embedded * not allowed";
243 return MethodMatcher::Unknown;
244 }
245 return (MethodMatcher::Mode)match;
246 }
247
248 // Skip any leading spaces
249 static void skip_leading_spaces(char*& line, int* total_bytes_read ) {
250 int bytes_read = 0;
251 sscanf(line, "%*[ \t]%n", &bytes_read);
252 if (bytes_read > 0) {
253 line += bytes_read;
254 *total_bytes_read += bytes_read;
255 }
256 }
257
258 void MethodMatcher::parse_method_pattern(char*& line, const char*& error_msg, MethodMatcher* matcher) {
259 MethodMatcher::Mode c_match;
260 MethodMatcher::Mode m_match;
261 char class_name[256] = {0};
262 char method_name[256] = {0};
263 char sig[1024] = {0};
264 int bytes_read = 0;
265 int total_bytes_read = 0;
266
267 assert(error_msg == nullptr, "Dont call here with error_msg already set");
268
269 if (!MethodMatcher::canonicalize(line, error_msg)) {
270 assert(error_msg != nullptr, "Message must be set if parsing failed");
271 return;
272 }
273
274 skip_leading_spaces(line, &total_bytes_read);
275 if (*line == '\0') {
276 error_msg = "Method pattern missing from command";
277 return;
278 }
279
280 if (2 == sscanf(line, "%255" RANGESLASH "%*[ ]" "%255" RANGE0 "%n", class_name, method_name, &bytes_read)) {
281 c_match = check_mode(class_name, error_msg);
282 m_match = check_mode(method_name, error_msg);
283
284 // Over-consumption
285 // method_name points to an option type or option name because the method name is not specified by users.
286 // In very rare case, the method name happens to be same as option type/name, so look ahead to make sure
287 // it doesn't show up again.
288 // !!! FIXME !!! rejects TooManyTrapsAtBCI,CLS::print()V,199 command
289 // if ((OptionType::Unknown != CompilerOracle::parse_option_type(method_name) ||
290 // CompileCommandEnum::Unknown != CompilerOracle::parse_option_name(method_name)) &&
291 // *(line + bytes_read) != '\0' &&
292 // strstr(line + bytes_read, method_name) == nullptr) {
293 // error_msg = "Did not specify any method name";
294 // method_name[0] = '\0';
295 // return;
296 // }
297
298 if ((strchr(class_name, JVM_SIGNATURE_SPECIAL) != nullptr) ||
299 (strchr(class_name, JVM_SIGNATURE_ENDSPECIAL) != nullptr)) {
300 error_msg = "Chars '<' and '>' not allowed in class name";
301 return;
302 }
303
304 if ((strchr(method_name, JVM_SIGNATURE_SPECIAL) != nullptr) ||
305 (strchr(method_name, JVM_SIGNATURE_ENDSPECIAL) != nullptr)) {
306 if (!vmSymbols::object_initializer_name()->equals(method_name) &&
307 !vmSymbols::class_initializer_name()->equals(method_name)) {
308 error_msg = "Chars '<' and '>' only allowed in <init> and <clinit>";
309 return;
310 }
311 }
312
313 if (c_match == MethodMatcher::Unknown || m_match == MethodMatcher::Unknown) {
314 assert(error_msg != nullptr, "Must have been set by check_mode()");
315 return;
316 }
317
318 EXCEPTION_MARK;
319 Symbol* signature = nullptr;
320 line += bytes_read;
321 bytes_read = 0;
322
323 skip_leading_spaces(line, &total_bytes_read);
324
325 // there might be a signature following the method.
326 // signatures always begin with ( so match that by hand
327 if (line[0] == '(') {
328 line++;
329 sig[0] = '(';
330 // scan the rest
331 if (1 == sscanf(line, "%1022[[);/" RANGEBASE "]%n", sig+1, &bytes_read)) {
332 if (strchr(sig, '*') != nullptr) {
333 error_msg = " Wildcard * not allowed in signature";
334 return;
335 }
336 line += bytes_read;
337 }
338 signature = SymbolTable::new_symbol(sig);
339 }
340 Symbol* c_name = SymbolTable::new_symbol(class_name);
341 Symbol* m_name = SymbolTable::new_symbol(method_name);
342
343 matcher->init(c_name, c_match, m_name, m_match, signature);
344 return;
345 } else {
346 error_msg = "Could not parse method pattern";
347 }
348 }
349
350 bool MethodMatcher::matches(const methodHandle& method) const {
351 Symbol* class_name = method->method_holder()->name();
352 Symbol* method_name = method->name();
353 Symbol* signature = method->signature();
354
355 if (match(class_name, this->class_name(), _class_mode) &&
356 match(method_name, this->method_name(), _method_mode) &&
357 ((this->signature() == nullptr) || match(signature, this->signature(), Prefix))) {
358 return true;
359 }
360 return false;
361 }
362
363 bool MethodMatcher::matches(MethodDetails& method_details) const {
364 if (match(method_details.class_name(), this->class_name(), _class_mode) &&
365 match(method_details.method_name(), this->method_name(), _method_mode) &&
366 ((this->signature() == nullptr) || match(method_details.signature(), this->signature(), Prefix))) {
367 return true;
368 }
369 return false;
370 }
371
372 void MethodMatcher::print_symbol(outputStream* st, Symbol* h, Mode mode) {
373 if (mode == Suffix || mode == Substring || mode == Any) {
374 st->print("*");
375 }
376 if (mode != Any) {
377 h->print_utf8_on(st);
378 }
379 if (mode == Prefix || mode == Substring) {
380 st->print("*");
381 }
382 }
383
384 void MethodMatcher::print_base(outputStream* st) {
385 ResourceMark rm;
386
387 print_symbol(st, class_name(), _class_mode);
388 st->print(".");
389 print_symbol(st, method_name(), _method_mode);
390 if (signature() != nullptr) {
391 signature()->print_utf8_on(st);
392 }
393 }
394
395 BasicMatcher* BasicMatcher::parse_method_pattern(char* line, const char*& error_msg, bool expect_trailing_chars) {
396 assert(error_msg == nullptr, "Don't call here with error_msg already set");
397 BasicMatcher* bm = new BasicMatcher();
398 MethodMatcher::parse_method_pattern(line, error_msg, bm);
399 if (error_msg != nullptr) {
400 delete bm;
401 return nullptr;
402 }
403 if (!expect_trailing_chars) {
404 // check for bad trailing characters
405 int bytes_read = 0;
406 sscanf(line, "%*[ \t]%n", &bytes_read);
407 if (line[bytes_read] != '\0') {
408 error_msg = "Unrecognized trailing text after method pattern";
409 delete bm;
410 return nullptr;
411 }
412 }
413 return bm;
414 }
415
416 bool BasicMatcher::match(MethodDetails& method_details) {
417 for (BasicMatcher* current = this; current != nullptr; current = current->next()) {
418 if (current->matches(method_details)) {
419 return true;
420 }
421 }
422 return false;
423 }
424
425 bool BasicMatcher::match(const methodHandle& method) {
426 for (BasicMatcher* current = this; current != nullptr; current = current->next()) {
427 if (current->matches(method)) {
428 return true;
429 }
430 }
431 return false;
432 }
433
434 void InlineMatcher::print(outputStream* st) {
435 if (_inline_action == InlineMatcher::force_inline) {
436 st->print("+");
437 } else {
438 st->print("-");
439 }
440 print_base(st);
441 }
442
443 InlineMatcher* InlineMatcher::parse_method_pattern(char* line, const char*& error_msg) {
444 assert(error_msg == nullptr, "Dont call here with error_msg already set");
445 InlineMatcher* im = new InlineMatcher();
446 MethodMatcher::parse_method_pattern(line, error_msg, im);
447 if (error_msg != nullptr) {
448 delete im;
449 return nullptr;
450 }
451 return im;
452 }
453
454 bool InlineMatcher::match(const methodHandle& method, int inline_action) {
455 for (InlineMatcher* current = this; current != nullptr; current = current->next()) {
456 if (current->matches(method)) {
457 return (current->_inline_action == inline_action);
458 }
459 }
460 return false;
461 }
462
463 InlineMatcher* InlineMatcher::parse_inline_pattern(char* str, const char*& error_msg) {
464 // check first token is +/-
465 InlineType _inline_action;
466 switch (str[0]) {
467 case '-':
468 _inline_action = InlineMatcher::dont_inline;
469 break;
470 case '+':
471 _inline_action = InlineMatcher::force_inline;
472 break;
473 default:
474 error_msg = "Missing leading inline type (+/-)";
475 return nullptr;
476 }
477 str++;
478
479 assert(error_msg == nullptr, "error_msg must not be set yet");
480 InlineMatcher* im = InlineMatcher::parse_method_pattern(str, error_msg);
481 if (im == nullptr) {
482 assert(error_msg != nullptr, "Must have error message");
483 return nullptr;
484 }
485 im->set_action(_inline_action);
486 return im;
487 }
488
489 InlineMatcher* InlineMatcher::clone() {
490 InlineMatcher* m = new InlineMatcher();
491 m->_class_mode = _class_mode;
492 m->_method_mode = _method_mode;
493 m->_inline_action = _inline_action;
494 m->_class_name = _class_name;
495 if(_class_name != nullptr) {
496 _class_name->increment_refcount();
497 }
498 m->_method_name = _method_name;
499 if (_method_name != nullptr) {
500 _method_name->increment_refcount();
501 }
502 m->_signature = _signature;
503 if (_signature != nullptr) {
504 _signature->increment_refcount();
505 }
506 return m;
507 }