1 /*
2  * Copyright (c) 2015, 2018, 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/stringTable.hpp"
27 #include "classfile/symbolTable.hpp"
28 #include "gc/shared/jvmFlagConstraintsGC.hpp"
29 #include "runtime/arguments.hpp"
30 #include "runtime/flags/jvmFlag.hpp"
31 #include "runtime/flags/jvmFlagConstraintList.hpp"
32 #include "runtime/flags/jvmFlagConstraintsCompiler.hpp"
33 #include "runtime/flags/jvmFlagConstraintsRuntime.hpp"
34 #include "runtime/globals.hpp"
35 #include "runtime/os.hpp"
36 #include "utilities/macros.hpp"
37 
38 class JVMFlagConstraint_bool : public JVMFlagConstraint {
39   JVMFlagConstraintFunc_bool _constraint;
40   const bool* _ptr;
41 
42 public:
43   // the "name" argument must be a string literal
JVMFlagConstraint_bool(const char * name,const bool * ptr,JVMFlagConstraintFunc_bool func,ConstraintType type)44   JVMFlagConstraint_bool(const char* name, const bool* ptr,
45                                  JVMFlagConstraintFunc_bool func,
46                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
47 
apply(bool verbose)48   JVMFlag::Error apply(bool verbose) {
49     bool value = *_ptr;
50     return _constraint(value, verbose);
51   }
52 
apply_bool(bool value,bool verbose)53   JVMFlag::Error apply_bool(bool value, bool verbose) {
54     return _constraint(value, verbose);
55   }
56 };
57 
58 class JVMFlagConstraint_int : public JVMFlagConstraint {
59   JVMFlagConstraintFunc_int _constraint;
60   const int* _ptr;
61 
62 public:
63   // the "name" argument must be a string literal
JVMFlagConstraint_int(const char * name,const int * ptr,JVMFlagConstraintFunc_int func,ConstraintType type)64   JVMFlagConstraint_int(const char* name, const int* ptr,
65                                 JVMFlagConstraintFunc_int func,
66                                 ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
67 
apply(bool verbose)68   JVMFlag::Error apply(bool verbose) {
69     int value = *_ptr;
70     return _constraint(value, verbose);
71   }
72 
apply_int(int value,bool verbose)73   JVMFlag::Error apply_int(int value, bool verbose) {
74     return _constraint(value, verbose);
75   }
76 };
77 
78 class JVMFlagConstraint_intx : public JVMFlagConstraint {
79   JVMFlagConstraintFunc_intx _constraint;
80   const intx* _ptr;
81 
82 public:
83   // the "name" argument must be a string literal
JVMFlagConstraint_intx(const char * name,const intx * ptr,JVMFlagConstraintFunc_intx func,ConstraintType type)84   JVMFlagConstraint_intx(const char* name, const intx* ptr,
85                                  JVMFlagConstraintFunc_intx func,
86                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
87 
apply(bool verbose)88   JVMFlag::Error apply(bool verbose) {
89     intx value = *_ptr;
90     return _constraint(value, verbose);
91   }
92 
apply_intx(intx value,bool verbose)93   JVMFlag::Error apply_intx(intx value, bool verbose) {
94     return _constraint(value, verbose);
95   }
96 };
97 
98 class JVMFlagConstraint_uint : public JVMFlagConstraint {
99   JVMFlagConstraintFunc_uint _constraint;
100   const uint* _ptr;
101 
102 public:
103   // the "name" argument must be a string literal
JVMFlagConstraint_uint(const char * name,const uint * ptr,JVMFlagConstraintFunc_uint func,ConstraintType type)104   JVMFlagConstraint_uint(const char* name, const uint* ptr,
105                                  JVMFlagConstraintFunc_uint func,
106                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
107 
apply(bool verbose)108   JVMFlag::Error apply(bool verbose) {
109     uint value = *_ptr;
110     return _constraint(value, verbose);
111   }
112 
apply_uint(uint value,bool verbose)113   JVMFlag::Error apply_uint(uint value, bool verbose) {
114     return _constraint(value, verbose);
115   }
116 };
117 
118 class JVMFlagConstraint_uintx : public JVMFlagConstraint {
119   JVMFlagConstraintFunc_uintx _constraint;
120   const uintx* _ptr;
121 
122 public:
123   // the "name" argument must be a string literal
JVMFlagConstraint_uintx(const char * name,const uintx * ptr,JVMFlagConstraintFunc_uintx func,ConstraintType type)124   JVMFlagConstraint_uintx(const char* name, const uintx* ptr,
125                                   JVMFlagConstraintFunc_uintx func,
126                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
127 
apply(bool verbose)128   JVMFlag::Error apply(bool verbose) {
129     uintx value = *_ptr;
130     return _constraint(value, verbose);
131   }
132 
apply_uintx(uintx value,bool verbose)133   JVMFlag::Error apply_uintx(uintx value, bool verbose) {
134     return _constraint(value, verbose);
135   }
136 };
137 
138 class JVMFlagConstraint_uint64_t : public JVMFlagConstraint {
139   JVMFlagConstraintFunc_uint64_t _constraint;
140   const uint64_t* _ptr;
141 
142 public:
143   // the "name" argument must be a string literal
JVMFlagConstraint_uint64_t(const char * name,const uint64_t * ptr,JVMFlagConstraintFunc_uint64_t func,ConstraintType type)144   JVMFlagConstraint_uint64_t(const char* name, const uint64_t* ptr,
145                                      JVMFlagConstraintFunc_uint64_t func,
146                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
147 
apply(bool verbose)148   JVMFlag::Error apply(bool verbose) {
149     uint64_t value = *_ptr;
150     return _constraint(value, verbose);
151   }
152 
apply_uint64_t(uint64_t value,bool verbose)153   JVMFlag::Error apply_uint64_t(uint64_t value, bool verbose) {
154     return _constraint(value, verbose);
155   }
156 };
157 
158 class JVMFlagConstraint_size_t : public JVMFlagConstraint {
159   JVMFlagConstraintFunc_size_t _constraint;
160   const size_t* _ptr;
161 public:
162   // the "name" argument must be a string literal
JVMFlagConstraint_size_t(const char * name,const size_t * ptr,JVMFlagConstraintFunc_size_t func,ConstraintType type)163   JVMFlagConstraint_size_t(const char* name, const size_t* ptr,
164                                    JVMFlagConstraintFunc_size_t func,
165                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
166 
apply(bool verbose)167   JVMFlag::Error apply(bool verbose) {
168     size_t value = *_ptr;
169     return _constraint(value, verbose);
170   }
171 
apply_size_t(size_t value,bool verbose)172   JVMFlag::Error apply_size_t(size_t value, bool verbose) {
173     return _constraint(value, verbose);
174   }
175 };
176 
177 class JVMFlagConstraint_double : public JVMFlagConstraint {
178   JVMFlagConstraintFunc_double _constraint;
179   const double* _ptr;
180 
181 public:
182   // the "name" argument must be a string literal
JVMFlagConstraint_double(const char * name,const double * ptr,JVMFlagConstraintFunc_double func,ConstraintType type)183   JVMFlagConstraint_double(const char* name, const double* ptr,
184                                    JVMFlagConstraintFunc_double func,
185                                  ConstraintType type) : JVMFlagConstraint(name, type), _constraint(func), _ptr(ptr) {}
186 
apply(bool verbose)187   JVMFlag::Error apply(bool verbose) {
188     double value = *_ptr;
189     return _constraint(value, verbose);
190   }
191 
apply_double(double value,bool verbose)192   JVMFlag::Error apply_double(double value, bool verbose) {
193     return _constraint(value, verbose);
194   }
195 };
196 
197 // No constraint emitting
emit_constraint_no(...)198 void emit_constraint_no(...)                                                      { /* NOP */ }
199 
200 // No constraint emitting if function argument is NOT provided
emit_constraint_bool(const char *,const bool *)201 void emit_constraint_bool(const char* /*name*/, const bool* /*value*/)            { /* NOP */ }
emit_constraint_ccstr(const char *,const ccstr *)202 void emit_constraint_ccstr(const char* /*name*/, const ccstr* /*value*/)          { /* NOP */ }
emit_constraint_ccstrlist(const char *,const ccstrlist *)203 void emit_constraint_ccstrlist(const char* /*name*/, const ccstrlist* /*value*/)  { /* NOP */ }
emit_constraint_int(const char *,const int *)204 void emit_constraint_int(const char* /*name*/, const int* /*value*/)              { /* NOP */ }
emit_constraint_intx(const char *,const intx *)205 void emit_constraint_intx(const char* /*name*/, const intx* /*value*/)            { /* NOP */ }
emit_constraint_uint(const char *,const uint *)206 void emit_constraint_uint(const char* /*name*/, const uint* /*value*/)            { /* NOP */ }
emit_constraint_uintx(const char *,const uintx *)207 void emit_constraint_uintx(const char* /*name*/, const uintx* /*value*/)          { /* NOP */ }
emit_constraint_uint64_t(const char *,const uint64_t *)208 void emit_constraint_uint64_t(const char* /*name*/, const uint64_t* /*value*/)    { /* NOP */ }
emit_constraint_size_t(const char *,const size_t *)209 void emit_constraint_size_t(const char* /*name*/, const size_t* /*value*/)        { /* NOP */ }
emit_constraint_double(const char *,const double *)210 void emit_constraint_double(const char* /*name*/, const double* /*value*/)        { /* NOP */ }
211 
212 // JVMFlagConstraint emitting code functions if function argument is provided
emit_constraint_bool(const char * name,const bool * ptr,JVMFlagConstraintFunc_bool func,JVMFlagConstraint::ConstraintType type)213 void emit_constraint_bool(const char* name, const bool* ptr, JVMFlagConstraintFunc_bool func, JVMFlagConstraint::ConstraintType type) {
214   JVMFlagConstraintList::add(new JVMFlagConstraint_bool(name, ptr, func, type));
215 }
emit_constraint_int(const char * name,const int * ptr,JVMFlagConstraintFunc_int func,JVMFlagConstraint::ConstraintType type)216 void emit_constraint_int(const char* name, const int* ptr, JVMFlagConstraintFunc_int func, JVMFlagConstraint::ConstraintType type) {
217   JVMFlagConstraintList::add(new JVMFlagConstraint_int(name, ptr, func, type));
218 }
emit_constraint_intx(const char * name,const intx * ptr,JVMFlagConstraintFunc_intx func,JVMFlagConstraint::ConstraintType type)219 void emit_constraint_intx(const char* name, const intx* ptr, JVMFlagConstraintFunc_intx func, JVMFlagConstraint::ConstraintType type) {
220   JVMFlagConstraintList::add(new JVMFlagConstraint_intx(name, ptr, func, type));
221 }
emit_constraint_uint(const char * name,const uint * ptr,JVMFlagConstraintFunc_uint func,JVMFlagConstraint::ConstraintType type)222 void emit_constraint_uint(const char* name, const uint* ptr, JVMFlagConstraintFunc_uint func, JVMFlagConstraint::ConstraintType type) {
223   JVMFlagConstraintList::add(new JVMFlagConstraint_uint(name, ptr, func, type));
224 }
emit_constraint_uintx(const char * name,const uintx * ptr,JVMFlagConstraintFunc_uintx func,JVMFlagConstraint::ConstraintType type)225 void emit_constraint_uintx(const char* name, const uintx* ptr, JVMFlagConstraintFunc_uintx func, JVMFlagConstraint::ConstraintType type) {
226   JVMFlagConstraintList::add(new JVMFlagConstraint_uintx(name, ptr, func, type));
227 }
emit_constraint_uint64_t(const char * name,const uint64_t * ptr,JVMFlagConstraintFunc_uint64_t func,JVMFlagConstraint::ConstraintType type)228 void emit_constraint_uint64_t(const char* name, const uint64_t* ptr, JVMFlagConstraintFunc_uint64_t func, JVMFlagConstraint::ConstraintType type) {
229   JVMFlagConstraintList::add(new JVMFlagConstraint_uint64_t(name, ptr, func, type));
230 }
emit_constraint_size_t(const char * name,const size_t * ptr,JVMFlagConstraintFunc_size_t func,JVMFlagConstraint::ConstraintType type)231 void emit_constraint_size_t(const char* name, const size_t* ptr, JVMFlagConstraintFunc_size_t func, JVMFlagConstraint::ConstraintType type) {
232   JVMFlagConstraintList::add(new JVMFlagConstraint_size_t(name, ptr, func, type));
233 }
emit_constraint_double(const char * name,const double * ptr,JVMFlagConstraintFunc_double func,JVMFlagConstraint::ConstraintType type)234 void emit_constraint_double(const char* name, const double* ptr, JVMFlagConstraintFunc_double func, JVMFlagConstraint::ConstraintType type) {
235   JVMFlagConstraintList::add(new JVMFlagConstraint_double(name, ptr, func, type));
236 }
237 
238 // Generate code to call emit_constraint_xxx function
239 #define EMIT_CONSTRAINT_START       (void)(0
240 #define EMIT_CONSTRAINT(type, name) ); emit_constraint_##type(#name, &name
241 #define EMIT_CONSTRAINT_NO          ); emit_constraint_no(0
242 #define EMIT_CONSTRAINT_PRODUCT_FLAG(type, name, value, doc)      EMIT_CONSTRAINT(type, name)
243 #define EMIT_CONSTRAINT_DIAGNOSTIC_FLAG(type, name, value, doc)   EMIT_CONSTRAINT(type, name)
244 #define EMIT_CONSTRAINT_EXPERIMENTAL_FLAG(type, name, value, doc) EMIT_CONSTRAINT(type, name)
245 #define EMIT_CONSTRAINT_MANAGEABLE_FLAG(type, name, value, doc)   EMIT_CONSTRAINT(type, name)
246 #define EMIT_CONSTRAINT_PRODUCT_RW_FLAG(type, name, value, doc)   EMIT_CONSTRAINT(type, name)
247 #define EMIT_CONSTRAINT_PD_PRODUCT_FLAG(type, name, doc)          EMIT_CONSTRAINT(type, name)
248 #define EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG(type, name, doc)       EMIT_CONSTRAINT(type, name)
249 #ifndef PRODUCT
250 #define EMIT_CONSTRAINT_DEVELOPER_FLAG(type, name, value, doc)    EMIT_CONSTRAINT(type, name)
251 #define EMIT_CONSTRAINT_PD_DEVELOPER_FLAG(type, name, doc)        EMIT_CONSTRAINT(type, name)
252 #define EMIT_CONSTRAINT_NOTPRODUCT_FLAG(type, name, value, doc)   EMIT_CONSTRAINT(type, name)
253 #else
254 #define EMIT_CONSTRAINT_DEVELOPER_FLAG(type, name, value, doc)    EMIT_CONSTRAINT_NO
255 #define EMIT_CONSTRAINT_PD_DEVELOPER_FLAG(type, name, doc)        EMIT_CONSTRAINT_NO
256 #define EMIT_CONSTRAINT_NOTPRODUCT_FLAG(type, name, value, doc)   EMIT_CONSTRAINT_NO
257 #endif
258 #ifdef _LP64
259 #define EMIT_CONSTRAINT_LP64_PRODUCT_FLAG(type, name, value, doc) EMIT_CONSTRAINT(type, name)
260 #else
261 #define EMIT_CONSTRAINT_LP64_PRODUCT_FLAG(type, name, value, doc) EMIT_CONSTRAINT_NO
262 #endif
263 #define EMIT_CONSTRAINT_END         );
264 
265 // Generate func argument to pass into emit_constraint_xxx functions
266 #define EMIT_CONSTRAINT_CHECK(func, type)                         , func, JVMFlagConstraint::type
267 
268 // the "name" argument must be a string literal
269 #define INITIAL_CONSTRAINTS_SIZE 72
270 GrowableArray<JVMFlagConstraint*>* JVMFlagConstraintList::_constraints = NULL;
271 JVMFlagConstraint::ConstraintType JVMFlagConstraintList::_validating_type = JVMFlagConstraint::AtParse;
272 
273 // Check the ranges of all flags that have them or print them out and exit if requested
init(void)274 void JVMFlagConstraintList::init(void) {
275   _constraints = new (ResourceObj::C_HEAP, mtArguments) GrowableArray<JVMFlagConstraint*>(INITIAL_CONSTRAINTS_SIZE, true);
276 
277   EMIT_CONSTRAINT_START
278 
279   ALL_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG,
280             EMIT_CONSTRAINT_PD_DEVELOPER_FLAG,
281             EMIT_CONSTRAINT_PRODUCT_FLAG,
282             EMIT_CONSTRAINT_PD_PRODUCT_FLAG,
283             EMIT_CONSTRAINT_DIAGNOSTIC_FLAG,
284             EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG,
285             EMIT_CONSTRAINT_EXPERIMENTAL_FLAG,
286             EMIT_CONSTRAINT_NOTPRODUCT_FLAG,
287             EMIT_CONSTRAINT_MANAGEABLE_FLAG,
288             EMIT_CONSTRAINT_PRODUCT_RW_FLAG,
289             EMIT_CONSTRAINT_LP64_PRODUCT_FLAG,
290             IGNORE_RANGE,
291             EMIT_CONSTRAINT_CHECK,
292             IGNORE_WRITEABLE)
293 
294   EMIT_CONSTRAINTS_FOR_GLOBALS_EXT
295 
296   EMIT_CONSTRAINT_END
297 }
298 
find(const char * name)299 JVMFlagConstraint* JVMFlagConstraintList::find(const char* name) {
300   JVMFlagConstraint* found = NULL;
301   for (int i=0; i<length(); i++) {
302     JVMFlagConstraint* constraint = at(i);
303     if (strcmp(constraint->name(), name) == 0) {
304       found = constraint;
305       break;
306     }
307   }
308   return found;
309 }
310 
311 // Find constraints by name and return only if found constraint's type is equal or lower than current validating type.
find_if_needs_check(const char * name)312 JVMFlagConstraint* JVMFlagConstraintList::find_if_needs_check(const char* name) {
313   JVMFlagConstraint* found = NULL;
314   JVMFlagConstraint* constraint = find(name);
315   if (constraint && (constraint->type() <= _validating_type)) {
316     found = constraint;
317   }
318   return found;
319 }
320 
321 // Check constraints for specific constraint type.
check_constraints(JVMFlagConstraint::ConstraintType type)322 bool JVMFlagConstraintList::check_constraints(JVMFlagConstraint::ConstraintType type) {
323   guarantee(type > _validating_type, "Constraint check is out of order.");
324   _validating_type = type;
325 
326   bool status = true;
327   for (int i=0; i<length(); i++) {
328     JVMFlagConstraint* constraint = at(i);
329     if (type != constraint->type()) continue;
330     if (constraint->apply(true) != JVMFlag::SUCCESS) status = false;
331   }
332   return status;
333 }
334