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