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