1 /* 2 * Copyright (c) 2015, 2016, 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/referenceProcessor.hpp" 29 #include "runtime/arguments.hpp" 30 #include "runtime/commandLineFlagConstraintList.hpp" 31 #include "runtime/commandLineFlagConstraintsCompiler.hpp" 32 #include "runtime/commandLineFlagConstraintsGC.hpp" 33 #include "runtime/commandLineFlagConstraintsRuntime.hpp" 34 #include "runtime/os.hpp" 35 #include "utilities/macros.hpp" 36 37 class CommandLineFlagConstraint_bool : public CommandLineFlagConstraint { 38 CommandLineFlagConstraintFunc_bool _constraint; 39 40 public: 41 // the "name" argument must be a string literal 42 CommandLineFlagConstraint_bool(const char* name, 43 CommandLineFlagConstraintFunc_bool func, 44 ConstraintType type) : CommandLineFlagConstraint(name, type) { 45 _constraint=func; 46 } 47 48 Flag::Error apply_bool(bool value, bool verbose) { 49 return _constraint(value, verbose); 50 } 51 }; 52 53 class CommandLineFlagConstraint_int : public CommandLineFlagConstraint { 54 CommandLineFlagConstraintFunc_int _constraint; 55 56 public: 57 // the "name" argument must be a string literal 58 CommandLineFlagConstraint_int(const char* name, 59 CommandLineFlagConstraintFunc_int func, 60 ConstraintType type) : CommandLineFlagConstraint(name, type) { 61 _constraint=func; 62 } 63 64 Flag::Error apply_int(int value, bool verbose) { 65 return _constraint(value, verbose); 66 } 67 }; 68 69 class CommandLineFlagConstraint_intx : public CommandLineFlagConstraint { 70 CommandLineFlagConstraintFunc_intx _constraint; 71 72 public: 73 // the "name" argument must be a string literal 74 CommandLineFlagConstraint_intx(const char* name, 75 CommandLineFlagConstraintFunc_intx func, 76 ConstraintType type) : CommandLineFlagConstraint(name, type) { 77 _constraint=func; 78 } 79 80 Flag::Error apply_intx(intx value, bool verbose) { 81 return _constraint(value, verbose); 82 } 83 }; 84 85 class CommandLineFlagConstraint_uint : public CommandLineFlagConstraint { 86 CommandLineFlagConstraintFunc_uint _constraint; 87 88 public: 89 // the "name" argument must be a string literal 90 CommandLineFlagConstraint_uint(const char* name, 91 CommandLineFlagConstraintFunc_uint func, 92 ConstraintType type) : CommandLineFlagConstraint(name, type) { 93 _constraint=func; 94 } 95 96 Flag::Error apply_uint(uint value, bool verbose) { 97 return _constraint(value, verbose); 98 } 99 }; 100 101 class CommandLineFlagConstraint_uintx : public CommandLineFlagConstraint { 102 CommandLineFlagConstraintFunc_uintx _constraint; 103 104 public: 105 // the "name" argument must be a string literal 106 CommandLineFlagConstraint_uintx(const char* name, 107 CommandLineFlagConstraintFunc_uintx func, 108 ConstraintType type) : CommandLineFlagConstraint(name, type) { 109 _constraint=func; 110 } 111 112 Flag::Error apply_uintx(uintx value, bool verbose) { 113 return _constraint(value, verbose); 114 } 115 }; 116 117 class CommandLineFlagConstraint_uint64_t : public CommandLineFlagConstraint { 118 CommandLineFlagConstraintFunc_uint64_t _constraint; 119 120 public: 121 // the "name" argument must be a string literal 122 CommandLineFlagConstraint_uint64_t(const char* name, 123 CommandLineFlagConstraintFunc_uint64_t func, 124 ConstraintType type) : CommandLineFlagConstraint(name, type) { 125 _constraint=func; 126 } 127 128 Flag::Error apply_uint64_t(uint64_t value, bool verbose) { 129 return _constraint(value, verbose); 130 } 131 }; 132 133 class CommandLineFlagConstraint_size_t : public CommandLineFlagConstraint { 134 CommandLineFlagConstraintFunc_size_t _constraint; 135 136 public: 137 // the "name" argument must be a string literal 138 CommandLineFlagConstraint_size_t(const char* name, 139 CommandLineFlagConstraintFunc_size_t func, 140 ConstraintType type) : CommandLineFlagConstraint(name, type) { 141 _constraint=func; 142 } 143 144 Flag::Error apply_size_t(size_t value, bool verbose) { 145 return _constraint(value, verbose); 146 } 147 }; 148 149 class CommandLineFlagConstraint_double : public CommandLineFlagConstraint { 150 CommandLineFlagConstraintFunc_double _constraint; 151 152 public: 153 // the "name" argument must be a string literal 154 CommandLineFlagConstraint_double(const char* name, 155 CommandLineFlagConstraintFunc_double func, 156 ConstraintType type) : CommandLineFlagConstraint(name, type) { 157 _constraint=func; 158 } 159 160 Flag::Error apply_double(double value, bool verbose) { 161 return _constraint(value, verbose); 162 } 163 }; 164 165 // No constraint emitting 166 void emit_constraint_no(...) { /* NOP */ } 167 168 // No constraint emitting if function argument is NOT provided 169 void emit_constraint_bool(const char* /*name*/) { /* NOP */ } 170 void emit_constraint_ccstr(const char* /*name*/) { /* NOP */ } 171 void emit_constraint_ccstrlist(const char* /*name*/) { /* NOP */ } 172 void emit_constraint_int(const char* /*name*/) { /* NOP */ } 173 void emit_constraint_intx(const char* /*name*/) { /* NOP */ } 174 void emit_constraint_uint(const char* /*name*/) { /* NOP */ } 175 void emit_constraint_uintx(const char* /*name*/) { /* NOP */ } 176 void emit_constraint_uint64_t(const char* /*name*/) { /* NOP */ } 177 void emit_constraint_size_t(const char* /*name*/) { /* NOP */ } 178 void emit_constraint_double(const char* /*name*/) { /* NOP */ } 179 180 // CommandLineFlagConstraint emitting code functions if function argument is provided 181 void emit_constraint_bool(const char* name, CommandLineFlagConstraintFunc_bool func, CommandLineFlagConstraint::ConstraintType type) { 182 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_bool(name, func, type)); 183 } 184 void emit_constraint_int(const char* name, CommandLineFlagConstraintFunc_int func, CommandLineFlagConstraint::ConstraintType type) { 185 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_int(name, func, type)); 186 } 187 void emit_constraint_intx(const char* name, CommandLineFlagConstraintFunc_intx func, CommandLineFlagConstraint::ConstraintType type) { 188 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_intx(name, func, type)); 189 } 190 void emit_constraint_uint(const char* name, CommandLineFlagConstraintFunc_uint func, CommandLineFlagConstraint::ConstraintType type) { 191 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_uint(name, func, type)); 192 } 193 void emit_constraint_uintx(const char* name, CommandLineFlagConstraintFunc_uintx func, CommandLineFlagConstraint::ConstraintType type) { 194 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_uintx(name, func, type)); 195 } 196 void emit_constraint_uint64_t(const char* name, CommandLineFlagConstraintFunc_uint64_t func, CommandLineFlagConstraint::ConstraintType type) { 197 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_uint64_t(name, func, type)); 198 } 199 void emit_constraint_size_t(const char* name, CommandLineFlagConstraintFunc_size_t func, CommandLineFlagConstraint::ConstraintType type) { 200 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_size_t(name, func, type)); 201 } 202 void emit_constraint_double(const char* name, CommandLineFlagConstraintFunc_double func, CommandLineFlagConstraint::ConstraintType type) { 203 CommandLineFlagConstraintList::add(new CommandLineFlagConstraint_double(name, func, type)); 204 } 205 206 // Generate code to call emit_constraint_xxx function 207 #define EMIT_CONSTRAINT_PRODUCT_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 208 #define EMIT_CONSTRAINT_COMMERCIAL_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 209 #define EMIT_CONSTRAINT_DIAGNOSTIC_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 210 #define EMIT_CONSTRAINT_EXPERIMENTAL_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 211 #define EMIT_CONSTRAINT_MANAGEABLE_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 212 #define EMIT_CONSTRAINT_PRODUCT_RW_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 213 #define EMIT_CONSTRAINT_PD_PRODUCT_FLAG(type, name, doc) ); emit_constraint_##type(#name 214 #define EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG(type, name, doc) ); emit_constraint_##type(#name 215 #define EMIT_CONSTRAINT_DEVELOPER_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 216 #define EMIT_CONSTRAINT_PD_DEVELOPER_FLAG(type, name, doc) ); emit_constraint_##type(#name 217 #define EMIT_CONSTRAINT_NOTPRODUCT_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 218 #define EMIT_CONSTRAINT_LP64_PRODUCT_FLAG(type, name, value, doc) ); emit_constraint_##type(#name 219 220 // Generate func argument to pass into emit_constraint_xxx functions 221 #define EMIT_CONSTRAINT_CHECK(func, type) , func, CommandLineFlagConstraint::type 222 223 // the "name" argument must be a string literal 224 #define INITIAL_CONSTRAINTS_SIZE 72 225 GrowableArray<CommandLineFlagConstraint*>* CommandLineFlagConstraintList::_constraints = NULL; 226 CommandLineFlagConstraint::ConstraintType CommandLineFlagConstraintList::_validating_type = CommandLineFlagConstraint::AtParse; 227 228 // Check the ranges of all flags that have them or print them out and exit if requested 229 void CommandLineFlagConstraintList::init(void) { 230 _constraints = new (ResourceObj::C_HEAP, mtArguments) GrowableArray<CommandLineFlagConstraint*>(INITIAL_CONSTRAINTS_SIZE, true); 231 232 emit_constraint_no(NULL RUNTIME_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 233 EMIT_CONSTRAINT_PD_DEVELOPER_FLAG, 234 EMIT_CONSTRAINT_PRODUCT_FLAG, 235 EMIT_CONSTRAINT_PD_PRODUCT_FLAG, 236 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 237 EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG, 238 EMIT_CONSTRAINT_EXPERIMENTAL_FLAG, 239 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 240 EMIT_CONSTRAINT_MANAGEABLE_FLAG, 241 EMIT_CONSTRAINT_PRODUCT_RW_FLAG, 242 EMIT_CONSTRAINT_LP64_PRODUCT_FLAG, 243 IGNORE_RANGE, 244 EMIT_CONSTRAINT_CHECK, 245 IGNORE_WRITEABLE)); 246 247 EMIT_CONSTRAINTS_FOR_GLOBALS_EXT 248 249 emit_constraint_no(NULL ARCH_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 250 EMIT_CONSTRAINT_PRODUCT_FLAG, 251 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 252 EMIT_CONSTRAINT_EXPERIMENTAL_FLAG, 253 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 254 IGNORE_RANGE, 255 EMIT_CONSTRAINT_CHECK, 256 IGNORE_WRITEABLE)); 257 258 259 #ifdef COMPILER1 260 emit_constraint_no(NULL C1_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 261 EMIT_CONSTRAINT_PD_DEVELOPER_FLAG, 262 EMIT_CONSTRAINT_PRODUCT_FLAG, 263 EMIT_CONSTRAINT_PD_PRODUCT_FLAG, 264 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 265 EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG, 266 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 267 IGNORE_RANGE, 268 EMIT_CONSTRAINT_CHECK, 269 IGNORE_WRITEABLE)); 270 #endif // COMPILER1 271 272 #ifdef COMPILER2 273 emit_constraint_no(NULL C2_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 274 EMIT_CONSTRAINT_PD_DEVELOPER_FLAG, 275 EMIT_CONSTRAINT_PRODUCT_FLAG, 276 EMIT_CONSTRAINT_PD_PRODUCT_FLAG, 277 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 278 EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG, 279 EMIT_CONSTRAINT_EXPERIMENTAL_FLAG, 280 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 281 IGNORE_RANGE, 282 EMIT_CONSTRAINT_CHECK, 283 IGNORE_WRITEABLE)); 284 #endif // COMPILER2 285 286 #if INCLUDE_ALL_GCS 287 emit_constraint_no(NULL G1_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 288 EMIT_CONSTRAINT_PD_DEVELOPER_FLAG, 289 EMIT_CONSTRAINT_PRODUCT_FLAG, 290 EMIT_CONSTRAINT_PD_PRODUCT_FLAG, 291 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 292 EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG, 293 EMIT_CONSTRAINT_EXPERIMENTAL_FLAG, 294 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 295 EMIT_CONSTRAINT_MANAGEABLE_FLAG, 296 EMIT_CONSTRAINT_PRODUCT_RW_FLAG, 297 IGNORE_RANGE, 298 EMIT_CONSTRAINT_CHECK, 299 IGNORE_WRITEABLE)); 300 emit_constraint_no(NULL EPSILON_FLAGS(EMIT_CONSTRAINT_DEVELOPER_FLAG, 301 EMIT_CONSTRAINT_PD_DEVELOPER_FLAG, 302 EMIT_CONSTRAINT_PRODUCT_FLAG, 303 EMIT_CONSTRAINT_PD_PRODUCT_FLAG, 304 EMIT_CONSTRAINT_DIAGNOSTIC_FLAG, 305 EMIT_CONSTRAINT_PD_DIAGNOSTIC_FLAG, 306 EMIT_CONSTRAINT_EXPERIMENTAL_FLAG, 307 EMIT_CONSTRAINT_NOTPRODUCT_FLAG, 308 EMIT_CONSTRAINT_MANAGEABLE_FLAG, 309 EMIT_CONSTRAINT_PRODUCT_RW_FLAG, 310 IGNORE_RANGE, 311 EMIT_CONSTRAINT_CHECK, 312 IGNORE_WRITEABLE)); 313 #endif // INCLUDE_ALL_GCS 314 } 315 316 CommandLineFlagConstraint* CommandLineFlagConstraintList::find(const char* name) { 317 CommandLineFlagConstraint* found = NULL; 318 for (int i=0; i<length(); i++) { 319 CommandLineFlagConstraint* constraint = at(i); 320 if (strcmp(constraint->name(), name) == 0) { 321 found = constraint; 322 break; 323 } 324 } 325 return found; 326 } 327 328 // Find constraints by name and return only if found constraint's type is equal or lower than current validating type. 329 CommandLineFlagConstraint* CommandLineFlagConstraintList::find_if_needs_check(const char* name) { 330 CommandLineFlagConstraint* found = NULL; 331 CommandLineFlagConstraint* constraint = find(name); 332 if (constraint && (constraint->type() <= _validating_type)) { 333 found = constraint; 334 } 335 return found; 336 } 337 338 // Check constraints for specific constraint type. 339 bool CommandLineFlagConstraintList::check_constraints(CommandLineFlagConstraint::ConstraintType type) { 340 guarantee(type > _validating_type, "Constraint check is out of order."); 341 _validating_type = type; 342 343 bool status = true; 344 for (int i=0; i<length(); i++) { 345 CommandLineFlagConstraint* constraint = at(i); 346 if (type != constraint->type()) continue; 347 const char* name = constraint->name(); 348 Flag* flag = Flag::find_flag(name, strlen(name), true, true); 349 // We must check for NULL here as lp64_product flags on 32 bit architecture 350 // can generate constraint check (despite that they are declared as constants), 351 // but they will not be returned by Flag::find_flag() 352 if (flag != NULL) { 353 if (flag->is_bool()) { 354 bool value = flag->get_bool(); 355 if (constraint->apply_bool(value, true) != Flag::SUCCESS) status = false; 356 } else if (flag->is_int()) { 357 int value = flag->get_int(); 358 if (constraint->apply_int(value, true) != Flag::SUCCESS) status = false; 359 } else if (flag->is_uint()) { 360 uint value = flag->get_uint(); 361 if (constraint->apply_uint(value, true) != Flag::SUCCESS) status = false; 362 } else if (flag->is_intx()) { 363 intx value = flag->get_intx(); 364 if (constraint->apply_intx(value, true) != Flag::SUCCESS) status = false; 365 } else if (flag->is_uintx()) { 366 uintx value = flag->get_uintx(); 367 if (constraint->apply_uintx(value, true) != Flag::SUCCESS) status = false; 368 } else if (flag->is_uint64_t()) { 369 uint64_t value = flag->get_uint64_t(); 370 if (constraint->apply_uint64_t(value, true) != Flag::SUCCESS) status = false; 371 } else if (flag->is_size_t()) { 372 size_t value = flag->get_size_t(); 373 if (constraint->apply_size_t(value, true) != Flag::SUCCESS) status = false; 374 } else if (flag->is_double()) { 375 double value = flag->get_double(); 376 if (constraint->apply_double(value, true) != Flag::SUCCESS) status = false; 377 } 378 } 379 } 380 return status; 381 }