2019-08-01 01:51:05 +08:00
|
|
|
//===-- ubsan_flags.cpp ---------------------------------------------------===//
|
2014-07-24 02:44:54 +08:00
|
|
|
//
|
2019-01-19 16:50:56 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2014-07-24 02:44:54 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Runtime flags for UndefinedBehaviorSanitizer.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#include "ubsan_platform.h"
|
|
|
|
#if CAN_SANITIZE_UB
|
2014-07-24 02:44:54 +08:00
|
|
|
#include "ubsan_flags.h"
|
|
|
|
#include "sanitizer_common/sanitizer_common.h"
|
|
|
|
#include "sanitizer_common/sanitizer_flags.h"
|
2015-01-15 23:13:43 +08:00
|
|
|
#include "sanitizer_common/sanitizer_flag_parser.h"
|
2014-07-24 02:44:54 +08:00
|
|
|
|
2017-11-09 10:22:07 +08:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
2014-07-24 02:44:54 +08:00
|
|
|
namespace __ubsan {
|
|
|
|
|
[ubsan] Add preinit initializer for ubsan
Summary:
Now that ubsan does function interception (for signals), we
need to ensure that ubsan is initialized before any library
constructors are called. Otherwise, if a constructor calls
sigaction, ubsan will intercept in an unitialized state, which
will cause a crash.
This patch is a partial revert of r317757, which removed
preinit arrays for ubsan.
Reviewers: vitalybuka, eugenis, pcc
Subscribers: kubamracek, mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D42389
llvm-svn: 323249
2018-01-24 03:54:02 +08:00
|
|
|
static const char *GetFlag(const char *flag) {
|
|
|
|
// We cannot call getenv() from inside a preinit array initializer
|
|
|
|
if (SANITIZER_CAN_USE_PREINIT_ARRAY) {
|
|
|
|
return GetEnv(flag);
|
|
|
|
} else {
|
|
|
|
return getenv(flag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-24 02:44:54 +08:00
|
|
|
Flags ubsan_flags;
|
|
|
|
|
2015-01-07 08:38:00 +08:00
|
|
|
void Flags::SetDefaults() {
|
|
|
|
#define UBSAN_FLAG(Type, Name, DefaultValue, Description) Name = DefaultValue;
|
|
|
|
#include "ubsan_flags.inc"
|
|
|
|
#undef UBSAN_FLAG
|
|
|
|
}
|
|
|
|
|
2015-01-15 23:13:43 +08:00
|
|
|
void RegisterUbsanFlags(FlagParser *parser, Flags *f) {
|
|
|
|
#define UBSAN_FLAG(Type, Name, DefaultValue, Description) \
|
|
|
|
RegisterFlag(parser, #Name, Description, &f->Name);
|
2015-01-07 08:38:00 +08:00
|
|
|
#include "ubsan_flags.inc"
|
|
|
|
#undef UBSAN_FLAG
|
2014-07-30 07:49:20 +08:00
|
|
|
}
|
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
void InitializeFlags() {
|
|
|
|
SetCommonFlagsDefaults();
|
|
|
|
{
|
2015-01-15 23:13:43 +08:00
|
|
|
CommonFlags cf;
|
|
|
|
cf.CopyFrom(*common_flags());
|
[ubsan] Add preinit initializer for ubsan
Summary:
Now that ubsan does function interception (for signals), we
need to ensure that ubsan is initialized before any library
constructors are called. Otherwise, if a constructor calls
sigaction, ubsan will intercept in an unitialized state, which
will cause a crash.
This patch is a partial revert of r317757, which removed
preinit arrays for ubsan.
Reviewers: vitalybuka, eugenis, pcc
Subscribers: kubamracek, mgorny, llvm-commits, #sanitizers
Differential Revision: https://reviews.llvm.org/D42389
llvm-svn: 323249
2018-01-24 03:54:02 +08:00
|
|
|
cf.external_symbolizer_path = GetFlag("UBSAN_SYMBOLIZER_PATH");
|
2015-01-15 23:13:43 +08:00
|
|
|
OverrideCommonFlags(cf);
|
|
|
|
}
|
|
|
|
|
2015-04-02 06:42:36 +08:00
|
|
|
Flags *f = flags();
|
2015-01-07 08:38:00 +08:00
|
|
|
f->SetDefaults();
|
2015-04-02 06:42:36 +08:00
|
|
|
|
|
|
|
FlagParser parser;
|
|
|
|
RegisterCommonFlags(&parser);
|
|
|
|
RegisterUbsanFlags(&parser, f);
|
|
|
|
|
2014-09-20 02:54:52 +08:00
|
|
|
// Override from user-specified string.
|
2020-09-05 10:19:20 +08:00
|
|
|
parser.ParseString(__ubsan_default_options());
|
2014-07-30 07:49:20 +08:00
|
|
|
// Override from environment variable.
|
2019-06-15 09:37:14 +08:00
|
|
|
parser.ParseStringFromEnv("UBSAN_OPTIONS");
|
2016-03-19 03:28:07 +08:00
|
|
|
InitializeCommonFlags();
|
2015-08-14 04:34:00 +08:00
|
|
|
if (Verbosity()) ReportUnrecognizedFlags();
|
|
|
|
|
|
|
|
if (common_flags()->help) parser.PrintFlagDescriptions();
|
2014-07-24 02:44:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace __ubsan
|
2014-09-20 02:54:52 +08:00
|
|
|
|
2017-01-29 13:44:59 +08:00
|
|
|
SANITIZER_INTERFACE_WEAK_DEF(const char *, __ubsan_default_options, void) {
|
|
|
|
return "";
|
|
|
|
}
|
2015-07-02 09:44:34 +08:00
|
|
|
|
2015-03-27 01:26:04 +08:00
|
|
|
#endif // CAN_SANITIZE_UB
|