1 //===-- options_parser.cpp --------------------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "gwp_asan/optional/options_parser.h" 10 11 #include <stdarg.h> 12 #include <stdint.h> 13 #include <stdlib.h> 14 #include <string.h> 15 16 #include "gwp_asan/options.h" 17 #include "sanitizer_common/sanitizer_common.h" 18 #include "sanitizer_common/sanitizer_flag_parser.h" 19 #include "sanitizer_common/sanitizer_flags.h" 20 21 namespace gwp_asan { 22 namespace options { 23 namespace { 24 void registerGwpAsanFlags(__sanitizer::FlagParser *parser, Options *o) { 25 #define GWP_ASAN_OPTION(Type, Name, DefaultValue, Description) \ 26 RegisterFlag(parser, #Name, Description, &o->Name); 27 #include "gwp_asan/options.inc" 28 #undef GWP_ASAN_OPTION 29 } 30 31 const char *getCompileDefinitionGwpAsanDefaultOptions() { 32 #ifdef GWP_ASAN_DEFAULT_OPTIONS 33 return SANITIZER_STRINGIFY(GWP_ASAN_DEFAULT_OPTIONS); 34 #else 35 return ""; 36 #endif 37 } 38 39 const char *getGwpAsanDefaultOptions() { 40 return (__gwp_asan_default_options) ? __gwp_asan_default_options() : ""; 41 } 42 43 Options *getOptionsInternal() { 44 static Options GwpAsanFlags; 45 return &GwpAsanFlags; 46 } 47 } // anonymous namespace 48 49 void initOptions() { 50 Options *o = getOptionsInternal(); 51 o->setDefaults(); 52 53 __sanitizer::FlagParser Parser; 54 registerGwpAsanFlags(&Parser, o); 55 56 // Override from compile definition. 57 Parser.ParseString(getCompileDefinitionGwpAsanDefaultOptions()); 58 59 // Override from user-specified string. 60 Parser.ParseString(getGwpAsanDefaultOptions()); 61 62 // Override from environment. 63 Parser.ParseString(__sanitizer::GetEnv("GWP_ASAN_OPTIONS")); 64 65 __sanitizer::InitializeCommonFlags(); 66 if (__sanitizer::Verbosity()) 67 __sanitizer::ReportUnrecognizedFlags(); 68 69 if (!o->Enabled) 70 return; 71 72 // Sanity checks for the parameters. 73 if (o->MaxSimultaneousAllocations <= 0) { 74 __sanitizer::Printf("GWP-ASan ERROR: MaxSimultaneousAllocations must be > " 75 "0 when GWP-ASan is enabled.\n"); 76 exit(EXIT_FAILURE); 77 } 78 79 if (o->SampleRate < 1) { 80 __sanitizer::Printf( 81 "GWP-ASan ERROR: SampleRate must be > 0 when GWP-ASan is enabled.\n"); 82 exit(EXIT_FAILURE); 83 } 84 85 o->Printf = __sanitizer::Printf; 86 } 87 88 const Options &getOptions() { return *getOptionsInternal(); } 89 90 } // namespace options 91 } // namespace gwp_asan 92