1/*
2 * Copyright (c) 2015, 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/compileBroker.hpp"
27#include "compiler/directivesParser.hpp"
28#include "memory/allocation.inline.hpp"
29#include "memory/resourceArea.hpp"
30#include "runtime/os.inline.hpp"
31#include <string.h>
32
33void DirectivesParser::push_tmp(CompilerDirectives* dir) {
34 _tmp_depth++;
35 dir->set_next(_tmp_top);
36 _tmp_top = dir;
37}
38
39CompilerDirectives* DirectivesParser::pop_tmp() {
40 if (_tmp_top == NULL) {
41 return NULL;
42 }
43 CompilerDirectives* tmp = _tmp_top;
44 _tmp_top = _tmp_top->next();
45 tmp->set_next(NULL);
46 _tmp_depth--;
47 return tmp;
48}
49
50void DirectivesParser::clean_tmp() {
51 CompilerDirectives* tmp = pop_tmp();
52 while (tmp != NULL) {
53 delete tmp;
54 tmp = pop_tmp();
55 }
56 assert(_tmp_depth == 0, "Consistency");
57}
58
59int DirectivesParser::parse_string(const char* text, outputStream* st) {
60 DirectivesParser cd(text, st, false);
61 if (cd.valid()) {
62 return cd.install_directives();
63 } else {
64 cd.clean_tmp();
65 st->flush();
66 st->print_cr("Parsing of compiler directives failed");
67 return -1;
68 }
69}
70
71bool DirectivesParser::has_file() {
72 return CompilerDirectivesFile != NULL;
73}
74
75bool DirectivesParser::parse_from_flag() {
76 return parse_from_file(CompilerDirectivesFile, tty);
77}
78
79bool DirectivesParser::parse_from_file(const char* filename, outputStream* st) {
80 assert(filename != NULL, "Test before calling this");
81 if (!parse_from_file_inner(filename, st)) {
82 st->print_cr("Could not load file: %s", filename);
83 return false;
84 }
85 return true;
86}
87
88bool DirectivesParser::parse_from_file_inner(const char* filename, outputStream* stream) {
89 struct stat st;
90 ResourceMark rm;
91 if (os::stat(filename, &st) == 0) {
92 // found file, open it
93 int file_handle = os::open(filename, 0, 0);
94 if (file_handle != -1) {
95 // read contents into resource array
96 char* buffer = NEW_RESOURCE_ARRAY(char, st.st_size+1);
97 ssize_t num_read = os::read(file_handle, (char*) buffer, st.st_size);
98 if (num_read >= 0) {
99 buffer[num_read] = '\0';
100 // close file
101 os::close(file_handle);
102 return parse_string(buffer, stream) > 0;
103 }
104 }
105 }
106 return false;
107}
108
109int DirectivesParser::install_directives() {
110 // Check limit
111 if (!DirectivesStack::check_capacity(_tmp_depth, _st)) {
112 clean_tmp();
113 return 0;
114 }
115
116 // Pop from internal temporary stack and push to compileBroker.
117 CompilerDirectives* tmp = pop_tmp();
118 int i = 0;
119 while (tmp != NULL) {
120 i++;
121 DirectivesStack::push(tmp);
122 tmp = pop_tmp();
123 }
124 if (i == 0) {
125 _st->print_cr("No directives in file");
126 return 0;
127 } else {
128 _st->print_cr("%i compiler directives added", i);
129 if (CompilerDirectivesPrint) {
130 // Print entire directives stack after new has been pushed.
131 DirectivesStack::print(_st);
132 }
133 return i;
134 }
135}
136
137DirectivesParser::DirectivesParser(const char* text, outputStream* st, bool silent)
138: JSON(text, silent, st), depth(0), current_directive(NULL), current_directiveset(NULL), _tmp_top(NULL), _tmp_depth(0) {
139#ifndef PRODUCT
140 memset(stack, 0, MAX_DEPTH * sizeof(stack[0]));
141#endif
142 parse();
143}
144
145DirectivesParser::~DirectivesParser() {
146 assert(_tmp_top == NULL, "Consistency");
147 assert(_tmp_depth == 0, "Consistency");
148}
149
150const DirectivesParser::key DirectivesParser::keys[] = {
151 // name, keytype, allow_array, allowed_mask, set_function
152 { "c1", type_c1, 0, mask(type_directives), NULL, UnknownFlagType },
153 { "c2", type_c2, 0, mask(type_directives), NULL, UnknownFlagType },
154 { "match", type_match, 1, mask(type_directives), NULL, UnknownFlagType },
155 { "inline", type_inline, 1, mask(type_directives) | mask(type_c1) | mask(type_c2), NULL, UnknownFlagType },
156
157 // Global flags
158 #define common_flag_key(name, type, dvalue, compiler) \
159 { #name, type_flag, 0, mask(type_directives) | mask(type_c1) | mask(type_c2), &DirectiveSet::set_##name, type##Flag},
160 compilerdirectives_common_flags(common_flag_key)
161 compilerdirectives_c2_flags(common_flag_key)
162 compilerdirectives_c1_flags(common_flag_key)
163 #undef common_flag_key
164};
165
166const DirectivesParser::key DirectivesParser::dir_array_key = {
167 "top level directives array", type_dir_array, 0, 1 // Lowest bit means allow at top level
168};
169const DirectivesParser::key DirectivesParser::dir_key = {
170 "top level directive", type_directives, 0, mask(type_dir_array) | 1 // Lowest bit means allow at top level
171};
172const DirectivesParser::key DirectivesParser::value_array_key = {
173 "value array", type_value_array, 0, UINT_MAX // Allow all, checked by allow_array on other keys, not by allowed_mask from this key
174};
175
176const DirectivesParser::key* DirectivesParser::lookup_key(const char* str, size_t len) {
177 for (size_t i = 0; i < (sizeof(keys) / sizeof(keys[0])); i++) {
178 if (strncasecmp(keys[i].name, str, len) == 0) {
179 return &keys[i];
180 }
181 }
182 return NULL;
183}
184
185uint DirectivesParser::mask(keytype kt) {
186 return 1 << (kt + 1);
187}
188
189bool DirectivesParser::push_key(const char* str, size_t len) {
190 bool result = true;
191 const key* k = lookup_key(str, len);
192
193 if (k == NULL) {
194 // os::strdup
195 char* s = NEW_C_HEAP_ARRAY(char, len + 1, mtCompiler);
196 strncpy(s, str, len);
197 s[len] = '\0';
198 error(KEY_ERROR, "No such key: '%s'.", s);
199 FREE_C_HEAP_ARRAY(char, s);
200 return false;
201 }
202
203 return push_key(k);
204}
205
206bool DirectivesParser::push_key(const key* k) {
207 assert(k->allowedmask != 0, "not allowed anywhere?");
208
209 // Exceeding the stack should not be possible with a valid compiler directive,
210 // and an invalid should abort before this happens
211 assert(depth < MAX_DEPTH, "exceeded stack depth");
212 if (depth >= MAX_DEPTH) {
213 error(INTERNAL_ERROR, "Stack depth exceeded.");
214 return false;
215 }
216
217 assert(stack[depth] == NULL, "element not nulled, something is wrong");
218
219 if (depth == 0 && !(k->allowedmask & 1)) {
220 error(KEY_ERROR, "Key '%s' not allowed at top level.", k->name);
221 return false;
222 }
223
224 if (depth > 0) {
225 const key* prev = stack[depth - 1];
226 if (!(k->allowedmask & mask(prev->type))) {
227 error(KEY_ERROR, "Key '%s' not allowed after '%s' key.", k->name, prev->name);
228 return false;
229 }
230 }
231
232 stack[depth] = k;
233 depth++;
234 return true;
235}
236
237const DirectivesParser::key* DirectivesParser::current_key() {
238 assert(depth > 0, "getting key from empty stack");
239 if (depth == 0) {
240 return NULL;
241 }
242 return stack[depth - 1];
243}
244
245const DirectivesParser::key* DirectivesParser::pop_key() {
246 assert(depth > 0, "popping empty stack");
247 if (depth == 0) {
248 error(INTERNAL_ERROR, "Popping empty stack.");
249 return NULL;
250 }
251 depth--;
252
253 const key* k = stack[depth];
254#ifndef PRODUCT
255 stack[depth] = NULL;
256#endif
257
258 return k;
259}
260
261bool DirectivesParser::set_option_flag(JSON_TYPE t, JSON_VAL* v, const key* option_key, DirectiveSet* set) {
262
263 void (DirectiveSet::*test)(void *args);
264 test = option_key->set;
265
266 switch (t) {
267 case JSON_TRUE:
268 if (option_key->flag_type != boolFlag) {
269 error(VALUE_ERROR, "Cannot use bool value for an %s flag", flag_type_names[option_key->flag_type]);
270 return false;
271 } else {
272 bool val = true;
273 (set->*test)((void *)&val);
274 }
275 break;
276
277 case JSON_FALSE:
278 if (option_key->flag_type != boolFlag) {
279 error(VALUE_ERROR, "Cannot use bool value for an %s flag", flag_type_names[option_key->flag_type]);
280 return false;
281 } else {
282 bool val = false;
283 (set->*test)((void *)&val);
284 }
285 break;
286
287 case JSON_NUMBER_INT:
288 if (option_key->flag_type == intxFlag) {
289 intx ival = v->int_value;
290 (set->*test)((void *)&ival);
291 } else if (option_key->flag_type == uintxFlag) {
292 uintx ival = v->uint_value;
293 (set->*test)((void *)&ival);
294 } else if (option_key->flag_type == doubleFlag) {
295 double dval = (double)v->int_value;
296 (set->*test)((void *)&dval);
297 } else {
298 error(VALUE_ERROR, "Cannot use int value for an %s flag", flag_type_names[option_key->flag_type]);
299 return false;
300 }
301 break;
302
303 case JSON_NUMBER_FLOAT:
304 if (option_key->flag_type != doubleFlag) {
305 error(VALUE_ERROR, "Cannot use double value for an %s flag", flag_type_names[option_key->flag_type]);
306 return false;
307 } else {
308 double dval = v->double_value;
309 (set->*test)((void *)&dval);
310 }
311 break;
312
313 case JSON_STRING:
314 if (option_key->flag_type != ccstrFlag && option_key->flag_type != ccstrlistFlag) {
315 error(VALUE_ERROR, "Cannot use string value for a %s flag", flag_type_names[option_key->flag_type]);
316 return false;
317 } else {
318 char* s = NEW_C_HEAP_ARRAY(char, v->str.length+1, mtCompiler);
319 strncpy(s, v->str.start, v->str.length + 1);
320 s[v->str.length] = '\0';
321 (set->*test)((void *)&s);
322 }
323 break;
324
325 default:
326 assert(0, "Should not reach here.");
327 }
328 return true;
329}
330
331bool DirectivesParser::set_option(JSON_TYPE t, JSON_VAL* v) {
332
333 const key* option_key = pop_key();
334 const key* enclosing_key = current_key();
335
336 if (option_key->type == value_array_key.type) {
337 // Multi value array, we are really setting the value
338 // for the key one step further up.
339 option_key = pop_key();
340 enclosing_key = current_key();
341
342 // Repush option_key and multi value marker, since
343 // we need to keep them until all multi values are set.
344 push_key(option_key);
345 push_key(&value_array_key);
346 }
347
348 switch (option_key->type) {
349 case type_flag:
350 {
351 if (current_directiveset == NULL) {
352 assert(depth == 2, "Must not have active directive set");
353
354 if (!set_option_flag(t, v, option_key, current_directive->_c1_store)) {
355 return false;
356 }
357 if (!set_option_flag(t, v, option_key, current_directive->_c2_store)) {
358 return false;
359 }
360 } else {
361 assert(depth > 2, "Must have active current directive set");
362 if (!set_option_flag(t, v, option_key, current_directiveset)) {
363 return false;
364 }
365 }
366 break;
367 }
368
369 case type_match:
370 if (t != JSON_STRING) {
371 error(VALUE_ERROR, "Key of type %s needs a value of type string", option_key->name);
372 return false;
373 }
374 if (enclosing_key->type != type_directives) {
375 error(SYNTAX_ERROR, "Match keyword can only exist inside a directive");
376 return false;
377 }
378 {
379 char* s = NEW_C_HEAP_ARRAY(char, v->str.length + 1, mtCompiler);
380 strncpy(s, v->str.start, v->str.length);
381 s[v->str.length] = '\0';
382
383 const char* error_msg = NULL;
384 if (!current_directive->add_match(s, error_msg)) {
385 assert (error_msg != NULL, "Must have valid error message");
386 error(VALUE_ERROR, "Method pattern error: %s", error_msg);
387 }
388 FREE_C_HEAP_ARRAY(char, s);
389 }
390 break;
391
392 case type_inline:
393 if (t != JSON_STRING) {
394 error(VALUE_ERROR, "Key of type %s needs a value of type string", option_key->name);
395 return false;
396 }
397 {
398 //char* s = strndup(v->str.start, v->str.length);
399 char* s = NEW_C_HEAP_ARRAY(char, v->str.length + 1, mtCompiler);
400 strncpy(s, v->str.start, v->str.length);
401 s[v->str.length] = '\0';
402
403 const char* error_msg = NULL;
404 if (current_directiveset == NULL) {
405 if (current_directive->_c1_store->parse_and_add_inline(s, error_msg)) {
406 if (!current_directive->_c2_store->parse_and_add_inline(s, error_msg)) {
407 assert (error_msg != NULL, "Must have valid error message");
408 error(VALUE_ERROR, "Method pattern error: %s", error_msg);
409 }
410 } else {
411 assert (error_msg != NULL, "Must have valid error message");
412 error(VALUE_ERROR, "Method pattern error: %s", error_msg);
413 }
414 } else {
415 if (!current_directiveset->parse_and_add_inline(s, error_msg)) {
416 assert (error_msg != NULL, "Must have valid error message");
417 error(VALUE_ERROR, "Method pattern error: %s", error_msg);
418 }
419 }
420 FREE_C_HEAP_ARRAY(char, s);
421 }
422 break;
423
424 case type_c1:
425 current_directiveset = current_directive->_c1_store;
426 if (t != JSON_TRUE && t != JSON_FALSE) {
427 error(VALUE_ERROR, "Key of type %s needs a true or false value", option_key->name);
428 return false;
429 }
430 break;
431
432 case type_c2:
433 current_directiveset = current_directive->_c2_store;
434 if (t != JSON_TRUE && t != JSON_FALSE) {
435 error(VALUE_ERROR, "Key of type %s needs a true or false value", option_key->name);
436 return false;
437 }
438 break;
439
440 default:
441 break;
442 }
443
444 return true;
445}
446
447bool DirectivesParser::callback(JSON_TYPE t, JSON_VAL* v, uint rlimit) {
448 const key* k;
449
450 if (depth == 0) {
451 switch (t) {
452 case JSON_ARRAY_BEGIN:
453 return push_key(&dir_array_key);
454
455 case JSON_OBJECT_BEGIN:
456 // push synthetic dir_array
457 push_key(&dir_array_key);
458 assert(depth == 1, "Make sure the stack are aligned with the directives");
459 break;
460
461 default:
462 error(SYNTAX_ERROR, "DirectivesParser can only start with an array containing directive objects, or one single directive.");
463 return false;
464 }
465 }
466 if (depth == 1) {
467 switch (t) {
468 case JSON_OBJECT_BEGIN:
469 // Parsing a new directive.
470 current_directive = new CompilerDirectives();
471 return push_key(&dir_key);
472
473 case JSON_ARRAY_END:
474 k = pop_key();
475
476 if (k->type != type_dir_array) {
477 error(SYNTAX_ERROR, "Expected end of directives array");
478 return false;
479 }
480 return true;
481
482 default:
483 error(SYNTAX_ERROR, "DirectivesParser can only start with an array containing directive objects, or one single directive.");
484 return false;
485 }
486 } else {
487 switch (t) {
488 case JSON_OBJECT_BEGIN:
489 k = current_key();
490 switch (k->type) {
491 case type_c1:
492 current_directiveset = current_directive->_c1_store;
493 return true;
494 case type_c2:
495 current_directiveset = current_directive->_c2_store;
496 return true;
497
498 case type_dir_array:
499 return push_key(&dir_key);
500
501 default:
502 error(SYNTAX_ERROR, "The key '%s' does not allow an object to follow.", k->name);
503 return false;
504 }
505 return false;
506
507 case JSON_OBJECT_END:
508 k = pop_key();
509 switch (k->type) {
510 case type_c1:
511 case type_c2:
512 // This is how we now if options apply to a single or both directive sets
513 current_directiveset = NULL;
514 break;
515
516 case type_directives:
517 // Check, finish and push to stack!
518 if (current_directive->match() == NULL) {
519 error(INTERNAL_ERROR, "Directive missing required match.");
520 return false;
521 }
522 current_directive->finalize(_st);
523 push_tmp(current_directive);
524 current_directive = NULL;
525 break;
526
527 default:
528 error(INTERNAL_ERROR, "Object end with wrong key type on stack: %s.", k->name);
529 ShouldNotReachHere();
530 return false;
531 }
532 return true;
533
534 case JSON_ARRAY_BEGIN:
535 k = current_key();
536 if (!(k->allow_array_value)) {
537 if (k->type == type_dir_array) {
538 error(SYNTAX_ERROR, "Array not allowed inside top level array, expected directive object.");
539 } else {
540 error(VALUE_ERROR, "The key '%s' does not allow an array of values.", k->name);
541 }
542 return false;
543 }
544 return push_key(&value_array_key);
545
546 case JSON_ARRAY_END:
547 k = pop_key(); // Pop multi value marker
548 assert(k->type == value_array_key.type, "array end for level != 0 should terminate multi value");
549 k = pop_key(); // Pop key for option that was set
550 return true;
551
552 case JSON_KEY:
553 return push_key(v->str.start, v->str.length);
554
555 case JSON_STRING:
556 case JSON_NUMBER_INT:
557 case JSON_NUMBER_FLOAT:
558 case JSON_TRUE:
559 case JSON_FALSE:
560 case JSON_NULL:
561 return set_option(t, v);
562
563 default:
564 error(INTERNAL_ERROR, "Unknown JSON type: %d.", t);
565 ShouldNotReachHere();
566 return false;
567 }
568 }
569}
570
571