1 /*
   2  * Copyright (c) 2015, 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/commandLineFlagRangeList.hpp"
  31 #include "runtime/os.hpp"
  32 #include "utilities/defaultStream.hpp"
  33 #include "utilities/macros.hpp"
  34 
  35 void CommandLineError::print(bool verbose, const char* msg, ...) {
  36   if (verbose) {
  37     va_list listPointer;
  38     va_start(listPointer, msg);
  39     jio_vfprintf(defaultStream::error_stream(), msg, listPointer);
  40     va_end(listPointer);
  41   }
  42 }
  43 
  44 class CommandLineFlagRange_int : public CommandLineFlagRange {
  45   int _min;
  46   int _max;
  47 
  48 public:
  49   // the "name" argument must be a string literal
  50   CommandLineFlagRange_int(const char* name, int min, int max) : CommandLineFlagRange(name) {
  51     _min=min, _max=max;
  52   }
  53 
  54   Flag::Error check_int(int value, bool verbose = true) {
  55     if ((value < _min) || (value > _max)) {
  56       CommandLineError::print(verbose,
  57                               "int %s=%d is outside the allowed range "
  58                               "[ %d ... %d ]\n",
  59                               name(), value, _min, _max);
  60       return Flag::OUT_OF_BOUNDS;
  61     } else {
  62       return Flag::SUCCESS;
  63     }
  64   }
  65 
  66   void print(outputStream* st) {
  67     st->print("[ %-25d ... %25d ]", _min, _max);
  68   }
  69 };
  70 
  71 class CommandLineFlagRange_intx : public CommandLineFlagRange {
  72   intx _min;
  73   intx _max;
  74 
  75 public:
  76   // the "name" argument must be a string literal
  77   CommandLineFlagRange_intx(const char* name, intx min, intx max) : CommandLineFlagRange(name) {
  78     _min=min, _max=max;
  79   }
  80 
  81   Flag::Error check_intx(intx value, bool verbose = true) {
  82     if ((value < _min) || (value > _max)) {
  83       CommandLineError::print(verbose,
  84                               "intx %s=" INTX_FORMAT " is outside the allowed range "
  85                               "[ " INTX_FORMAT " ... " INTX_FORMAT " ]\n",
  86                               name(), value, _min, _max);
  87       return Flag::OUT_OF_BOUNDS;
  88     } else {
  89       return Flag::SUCCESS;
  90     }
  91   }
  92 
  93   void print(outputStream* st) {
  94     st->print("[ " INTX_FORMAT_W(-25) " ... " INTX_FORMAT_W(25) " ]", _min, _max);
  95   }
  96 };
  97 
  98 class CommandLineFlagRange_uint : public CommandLineFlagRange {
  99   uint _min;
 100   uint _max;
 101 
 102 public:
 103   // the "name" argument must be a string literal
 104   CommandLineFlagRange_uint(const char* name, uint min, uint max) : CommandLineFlagRange(name) {
 105     _min=min, _max=max;
 106   }
 107 
 108   Flag::Error check_uint(uint value, bool verbose = true) {
 109     if ((value < _min) || (value > _max)) {
 110       CommandLineError::print(verbose,
 111                               "uint %s=%u is outside the allowed range "
 112                               "[ %u ... %u ]\n",
 113                               name(), value, _min, _max);
 114       return Flag::OUT_OF_BOUNDS;
 115     } else {
 116       return Flag::SUCCESS;
 117     }
 118   }
 119 
 120   void print(outputStream* st) {
 121     st->print("[ %-25u ... %25u ]", _min, _max);
 122   }
 123 };
 124 
 125 class CommandLineFlagRange_uintx : public CommandLineFlagRange {
 126   uintx _min;
 127   uintx _max;
 128 
 129 public:
 130   // the "name" argument must be a string literal
 131   CommandLineFlagRange_uintx(const char* name, uintx min, uintx max) : CommandLineFlagRange(name) {
 132     _min=min, _max=max;
 133   }
 134 
 135   Flag::Error check_uintx(uintx value, bool verbose = true) {
 136     if ((value < _min) || (value > _max)) {
 137       CommandLineError::print(verbose,
 138                               "uintx %s=" UINTX_FORMAT " is outside the allowed range "
 139                               "[ " UINTX_FORMAT " ... " UINTX_FORMAT " ]\n",
 140                               name(), value, _min, _max);
 141       return Flag::OUT_OF_BOUNDS;
 142     } else {
 143       return Flag::SUCCESS;
 144     }
 145   }
 146 
 147   void print(outputStream* st) {
 148     st->print("[ " UINTX_FORMAT_W(-25) " ... " UINTX_FORMAT_W(25) " ]", _min, _max);
 149   }
 150 };
 151 
 152 class CommandLineFlagRange_uint64_t : public CommandLineFlagRange {
 153   uint64_t _min;
 154   uint64_t _max;
 155 
 156 public:
 157   // the "name" argument must be a string literal
 158   CommandLineFlagRange_uint64_t(const char* name, uint64_t min, uint64_t max) : CommandLineFlagRange(name) {
 159     _min=min, _max=max;
 160   }
 161 
 162   Flag::Error check_uint64_t(uint64_t value, bool verbose = true) {
 163     if ((value < _min) || (value > _max)) {
 164       CommandLineError::print(verbose,
 165                               "uint64_t %s=" UINT64_FORMAT " is outside the allowed range "
 166                               "[ " UINT64_FORMAT " ... " UINT64_FORMAT " ]\n",
 167                               name(), value, _min, _max);
 168       return Flag::OUT_OF_BOUNDS;
 169     } else {
 170       return Flag::SUCCESS;
 171     }
 172   }
 173 
 174   void print(outputStream* st) {
 175     st->print("[ " UINT64_FORMAT_W(-25) " ... " UINT64_FORMAT_W(25) " ]", _min, _max);
 176   }
 177 };
 178 
 179 class CommandLineFlagRange_size_t : public CommandLineFlagRange {
 180   size_t _min;
 181   size_t _max;
 182 
 183 public:
 184   // the "name" argument must be a string literal
 185   CommandLineFlagRange_size_t(const char* name, size_t min, size_t max) : CommandLineFlagRange(name) {
 186     _min=min, _max=max;
 187   }
 188 
 189   Flag::Error check_size_t(size_t value, bool verbose = true) {
 190     if ((value < _min) || (value > _max)) {
 191       CommandLineError::print(verbose,
 192                               "size_t %s=" SIZE_FORMAT " is outside the allowed range "
 193                               "[ " SIZE_FORMAT " ... " SIZE_FORMAT " ]\n",
 194                               name(), value, _min, _max);
 195       return Flag::OUT_OF_BOUNDS;
 196     } else {
 197       return Flag::SUCCESS;
 198     }
 199   }
 200 
 201   void print(outputStream* st) {
 202     st->print("[ " SIZE_FORMAT_W(-25) " ... " SIZE_FORMAT_W(25) " ]", _min, _max);
 203   }
 204 };
 205 
 206 class CommandLineFlagRange_double : public CommandLineFlagRange {
 207   double _min;
 208   double _max;
 209 
 210 public:
 211   // the "name" argument must be a string literal
 212   CommandLineFlagRange_double(const char* name, double min, double max) : CommandLineFlagRange(name) {
 213     _min=min, _max=max;
 214   }
 215 
 216   Flag::Error check_double(double value, bool verbose = true) {
 217     if ((value < _min) || (value > _max)) {
 218       CommandLineError::print(verbose,
 219                               "double %s=%f is outside the allowed range "
 220                               "[ %f ... %f ]\n",
 221                               name(), value, _min, _max);
 222       return Flag::OUT_OF_BOUNDS;
 223     } else {
 224       return Flag::SUCCESS;
 225     }
 226   }
 227 
 228   void print(outputStream* st) {
 229     st->print("[ %-25.3f ... %25.3f ]", _min, _max);
 230   }
 231 };
 232 
 233 // No constraint emitting
 234 void emit_range_no(...)                         { /* NOP */ }
 235 
 236 // No constraint emitting if function argument is NOT provided
 237 void emit_range_bool(const char* /*name*/)      { /* NOP */ }
 238 void emit_range_ccstr(const char* /*name*/)     { /* NOP */ }
 239 void emit_range_ccstrlist(const char* /*name*/) { /* NOP */ }
 240 void emit_range_int(const char* /*name*/)       { /* NOP */ }
 241 void emit_range_intx(const char* /*name*/)      { /* NOP */ }
 242 void emit_range_uint(const char* /*name*/)      { /* NOP */ }
 243 void emit_range_uintx(const char* /*name*/)     { /* NOP */ }
 244 void emit_range_uint64_t(const char* /*name*/)  { /* NOP */ }
 245 void emit_range_size_t(const char* /*name*/)    { /* NOP */ }
 246 void emit_range_double(const char* /*name*/)    { /* NOP */ }
 247 
 248 // CommandLineFlagRange emitting code functions if range arguments are provided
 249 void emit_range_intx(const char* name, intx min, intx max) {
 250   CommandLineFlagRangeList::add(new CommandLineFlagRange_intx(name, min, max));
 251 }
 252 void emit_range_uintx(const char* name, uintx min, uintx max) {
 253   CommandLineFlagRangeList::add(new CommandLineFlagRange_uintx(name, min, max));
 254 }
 255 void emit_range_uint64_t(const char* name, uint64_t min, uint64_t max) {
 256   CommandLineFlagRangeList::add(new CommandLineFlagRange_uint64_t(name, min, max));
 257 }
 258 void emit_range_size_t(const char* name, size_t min, size_t max) {
 259   CommandLineFlagRangeList::add(new CommandLineFlagRange_size_t(name, min, max));
 260 }
 261 void emit_range_double(const char* name, double min, double max) {
 262   CommandLineFlagRangeList::add(new CommandLineFlagRange_double(name, min, max));
 263 }
 264 
 265 // Generate code to call emit_range_xxx function
 266 #define EMIT_RANGE_PRODUCT_FLAG(type, name, value, doc)      ); emit_range_##type(#name
 267 #define EMIT_RANGE_COMMERCIAL_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 268 #define EMIT_RANGE_DIAGNOSTIC_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 269 #define EMIT_RANGE_EXPERIMENTAL_FLAG(type, name, value, doc) ); emit_range_##type(#name
 270 #define EMIT_RANGE_MANAGEABLE_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 271 #define EMIT_RANGE_PRODUCT_RW_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 272 #define EMIT_RANGE_PD_PRODUCT_FLAG(type, name, doc)          ); emit_range_##type(#name
 273 #define EMIT_RANGE_DEVELOPER_FLAG(type, name, value, doc)    ); emit_range_##type(#name
 274 #define EMIT_RANGE_PD_DEVELOPER_FLAG(type, name, doc)        ); emit_range_##type(#name
 275 #define EMIT_RANGE_NOTPRODUCT_FLAG(type, name, value, doc)   ); emit_range_##type(#name
 276 #define EMIT_RANGE_LP64_PRODUCT_FLAG(type, name, value, doc) ); emit_range_##type(#name
 277 
 278 // Generate func argument to pass into emit_range_xxx functions
 279 #define EMIT_RANGE_CHECK(a, b)                               , a, b
 280 
 281 #define INITIAL_RANGES_SIZE 128
 282 GrowableArray<CommandLineFlagRange*>* CommandLineFlagRangeList::_ranges = NULL;
 283 
 284 // Check the ranges of all flags that have them
 285 void CommandLineFlagRangeList::init(void) {
 286 
 287   _ranges = new (ResourceObj::C_HEAP, mtInternal) GrowableArray<CommandLineFlagRange*>(INITIAL_RANGES_SIZE, true);
 288 
 289   emit_range_no(NULL RUNTIME_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 290                                    EMIT_RANGE_PD_DEVELOPER_FLAG,
 291                                    EMIT_RANGE_PRODUCT_FLAG,
 292                                    EMIT_RANGE_PD_PRODUCT_FLAG,
 293                                    EMIT_RANGE_DIAGNOSTIC_FLAG,
 294                                    EMIT_RANGE_EXPERIMENTAL_FLAG,
 295                                    EMIT_RANGE_NOTPRODUCT_FLAG,
 296                                    EMIT_RANGE_MANAGEABLE_FLAG,
 297                                    EMIT_RANGE_PRODUCT_RW_FLAG,
 298                                    EMIT_RANGE_LP64_PRODUCT_FLAG,
 299                                    EMIT_RANGE_CHECK,
 300                                    IGNORE_CONSTRAINT) );
 301 
 302   EMIT_RANGES_FOR_GLOBALS_EXT
 303 
 304   emit_range_no(NULL ARCH_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 305                                 EMIT_RANGE_PRODUCT_FLAG,
 306                                 EMIT_RANGE_DIAGNOSTIC_FLAG,
 307                                 EMIT_RANGE_EXPERIMENTAL_FLAG,
 308                                 EMIT_RANGE_NOTPRODUCT_FLAG,
 309                                 EMIT_RANGE_CHECK,
 310                                 IGNORE_CONSTRAINT));
 311 
 312 #if INCLUDE_JVMCI
 313   emit_range_no(NULL JVMCI_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 314                                  EMIT_RANGE_PD_DEVELOPER_FLAG,
 315                                  EMIT_RANGE_PRODUCT_FLAG,
 316                                  EMIT_RANGE_PD_PRODUCT_FLAG,
 317                                  EMIT_RANGE_DIAGNOSTIC_FLAG,
 318                                  EMIT_RANGE_EXPERIMENTAL_FLAG,
 319                                  EMIT_RANGE_NOTPRODUCT_FLAG,
 320                                  EMIT_RANGE_CHECK,
 321                                  IGNORE_CONSTRAINT));
 322 #endif // INCLUDE_JVMCI
 323 
 324 #ifdef COMPILER1
 325   emit_range_no(NULL C1_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 326                               EMIT_RANGE_PD_DEVELOPER_FLAG,
 327                               EMIT_RANGE_PRODUCT_FLAG,
 328                               EMIT_RANGE_PD_PRODUCT_FLAG,
 329                               EMIT_RANGE_DIAGNOSTIC_FLAG,
 330                               EMIT_RANGE_NOTPRODUCT_FLAG,
 331                               EMIT_RANGE_CHECK,
 332                               IGNORE_CONSTRAINT));
 333 #endif // COMPILER1
 334 
 335 #ifdef COMPILER2
 336   emit_range_no(NULL C2_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 337                               EMIT_RANGE_PD_DEVELOPER_FLAG,
 338                               EMIT_RANGE_PRODUCT_FLAG,
 339                               EMIT_RANGE_PD_PRODUCT_FLAG,
 340                               EMIT_RANGE_DIAGNOSTIC_FLAG,
 341                               EMIT_RANGE_EXPERIMENTAL_FLAG,
 342                               EMIT_RANGE_NOTPRODUCT_FLAG,
 343                               EMIT_RANGE_CHECK,
 344                               IGNORE_CONSTRAINT));
 345 #endif // COMPILER2
 346 
 347 #if INCLUDE_ALL_GCS
 348   emit_range_no(NULL G1_FLAGS(EMIT_RANGE_DEVELOPER_FLAG,
 349                               EMIT_RANGE_PD_DEVELOPER_FLAG,
 350                               EMIT_RANGE_PRODUCT_FLAG,
 351                               EMIT_RANGE_PD_PRODUCT_FLAG,
 352                               EMIT_RANGE_DIAGNOSTIC_FLAG,
 353                               EMIT_RANGE_EXPERIMENTAL_FLAG,
 354                               EMIT_RANGE_NOTPRODUCT_FLAG,
 355                               EMIT_RANGE_MANAGEABLE_FLAG,
 356                               EMIT_RANGE_PRODUCT_RW_FLAG,
 357                               EMIT_RANGE_CHECK,
 358                               IGNORE_CONSTRAINT));
 359 #endif // INCLUDE_ALL_GCS
 360 }
 361 
 362 CommandLineFlagRange* CommandLineFlagRangeList::find(const char* name) {
 363   CommandLineFlagRange* found = NULL;
 364   for (int i=0; i<length(); i++) {
 365     CommandLineFlagRange* range = at(i);
 366     if (strcmp(range->name(), name) == 0) {
 367       found = range;
 368       break;
 369     }
 370   }
 371   return found;
 372 }
 373 
 374 void CommandLineFlagRangeList::print(const char* name, outputStream* st, bool unspecified) {
 375   CommandLineFlagRange* range = CommandLineFlagRangeList::find(name);
 376   if (range != NULL) {
 377     range->print(st);
 378   } else if (unspecified == true) {
 379     st->print("[                           ...                           ]");
 380   }
 381 }
 382 
 383 bool CommandLineFlagRangeList::check_ranges() {
 384   // Check ranges.
 385   bool status = true;
 386   for (int i=0; i<length(); i++) {
 387     CommandLineFlagRange* range = at(i);
 388     const char* name = range->name();
 389     Flag* flag = Flag::find_flag(name, strlen(name), true, true);
 390     // We must check for NULL here as lp64_product flags on 32 bit architecture
 391     // can generate range check (despite that they are declared as constants),
 392     // but they will not be returned by Flag::find_flag()
 393     if (flag != NULL) {
 394       if (flag->is_int()) {
 395         int value = flag->get_int();
 396         if (range->check_int(value, true) != Flag::SUCCESS) status = false;
 397       } else if (flag->is_uint()) {
 398         uint value = flag->get_uint();
 399         if (range->check_uint(value, true) != Flag::SUCCESS) status = false;
 400       } else if (flag->is_intx()) {
 401         intx value = flag->get_intx();
 402         if (range->check_intx(value, true) != Flag::SUCCESS) status = false;
 403       } else if (flag->is_uintx()) {
 404         uintx value = flag->get_uintx();
 405         if (range->check_uintx(value, true) != Flag::SUCCESS) status = false;
 406       } else if (flag->is_uint64_t()) {
 407         uint64_t value = flag->get_uint64_t();
 408         if (range->check_uint64_t(value, true) != Flag::SUCCESS) status = false;
 409       } else if (flag->is_size_t()) {
 410         size_t value = flag->get_size_t();
 411         if (range->check_size_t(value, true) != Flag::SUCCESS) status = false;
 412       } else if (flag->is_double()) {
 413         double value = flag->get_double();
 414         if (range->check_double(value, true) != Flag::SUCCESS) status = false;
 415       }
 416     }
 417   }
 418   return status;
 419 }