xref: /freebsd/contrib/llvm-project/compiler-rt/lib/ubsan/ubsan_flags.cpp (revision e8d8bef961a50d4dc22501cde4fb9fb0be1b2532)
168d75effSDimitry Andric //===-- ubsan_flags.cpp ---------------------------------------------------===//
268d75effSDimitry Andric //
368d75effSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
468d75effSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
568d75effSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
668d75effSDimitry Andric //
768d75effSDimitry Andric //===----------------------------------------------------------------------===//
868d75effSDimitry Andric //
968d75effSDimitry Andric // Runtime flags for UndefinedBehaviorSanitizer.
1068d75effSDimitry Andric //
1168d75effSDimitry Andric //===----------------------------------------------------------------------===//
1268d75effSDimitry Andric 
1368d75effSDimitry Andric #include "ubsan_platform.h"
1468d75effSDimitry Andric #if CAN_SANITIZE_UB
1568d75effSDimitry Andric #include "ubsan_flags.h"
1668d75effSDimitry Andric #include "sanitizer_common/sanitizer_common.h"
1768d75effSDimitry Andric #include "sanitizer_common/sanitizer_flags.h"
1868d75effSDimitry Andric #include "sanitizer_common/sanitizer_flag_parser.h"
1968d75effSDimitry Andric 
2068d75effSDimitry Andric #include <stdlib.h>
2168d75effSDimitry Andric 
2268d75effSDimitry Andric namespace __ubsan {
2368d75effSDimitry Andric 
GetFlag(const char * flag)2468d75effSDimitry Andric static const char *GetFlag(const char *flag) {
2568d75effSDimitry Andric   // We cannot call getenv() from inside a preinit array initializer
2668d75effSDimitry Andric   if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
2768d75effSDimitry Andric     return GetEnv(flag);
2868d75effSDimitry Andric   } else {
2968d75effSDimitry Andric     return getenv(flag);
3068d75effSDimitry Andric   }
3168d75effSDimitry Andric }
3268d75effSDimitry Andric 
3368d75effSDimitry Andric Flags ubsan_flags;
3468d75effSDimitry Andric 
SetDefaults()3568d75effSDimitry Andric void Flags::SetDefaults() {
3668d75effSDimitry Andric #define UBSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
3768d75effSDimitry Andric #include "ubsan_flags.inc"
3868d75effSDimitry Andric #undef UBSAN_FLAG
3968d75effSDimitry Andric }
4068d75effSDimitry Andric 
RegisterUbsanFlags(FlagParser * parser,Flags * f)4168d75effSDimitry Andric void RegisterUbsanFlags(FlagParser *parser, Flags *f) {
4268d75effSDimitry Andric #define UBSAN_FLAG(Type, Name, DefaultValue, Description) \
4368d75effSDimitry Andric   RegisterFlag(parser, #Name, Description, &f->Name);
4468d75effSDimitry Andric #include "ubsan_flags.inc"
4568d75effSDimitry Andric #undef UBSAN_FLAG
4668d75effSDimitry Andric }
4768d75effSDimitry Andric 
InitializeFlags()4868d75effSDimitry Andric void InitializeFlags() {
4968d75effSDimitry Andric   SetCommonFlagsDefaults();
5068d75effSDimitry Andric   {
5168d75effSDimitry Andric     CommonFlags cf;
5268d75effSDimitry Andric     cf.CopyFrom(*common_flags());
5368d75effSDimitry Andric     cf.external_symbolizer_path = GetFlag("UBSAN_SYMBOLIZER_PATH");
5468d75effSDimitry Andric     OverrideCommonFlags(cf);
5568d75effSDimitry Andric   }
5668d75effSDimitry Andric 
5768d75effSDimitry Andric   Flags *f = flags();
5868d75effSDimitry Andric   f->SetDefaults();
5968d75effSDimitry Andric 
6068d75effSDimitry Andric   FlagParser parser;
6168d75effSDimitry Andric   RegisterCommonFlags(&parser);
6268d75effSDimitry Andric   RegisterUbsanFlags(&parser, f);
6368d75effSDimitry Andric 
6468d75effSDimitry Andric   // Override from user-specified string.
65*e8d8bef9SDimitry Andric   parser.ParseString(__ubsan_default_options());
6668d75effSDimitry Andric   // Override from environment variable.
6768d75effSDimitry Andric   parser.ParseStringFromEnv("UBSAN_OPTIONS");
6868d75effSDimitry Andric   InitializeCommonFlags();
6968d75effSDimitry Andric   if (Verbosity()) ReportUnrecognizedFlags();
7068d75effSDimitry Andric 
7168d75effSDimitry Andric   if (common_flags()->help) parser.PrintFlagDescriptions();
7268d75effSDimitry Andric }
7368d75effSDimitry Andric 
7468d75effSDimitry Andric }  // namespace __ubsan
7568d75effSDimitry Andric 
SANITIZER_INTERFACE_WEAK_DEF(const char *,__ubsan_default_options,void)7668d75effSDimitry Andric SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_options, void) {
7768d75effSDimitry Andric   return "";
7868d75effSDimitry Andric }
7968d75effSDimitry Andric 
8068d75effSDimitry Andric #endif  // CAN_SANITIZE_UB
81