Skip to content

Commit 2e9b331

Browse files
authored
[compiler-rt][rtsan] Introduce RTSAN_OPTIONS and flags (#107174)
This matches much of the boilerplate found in the other sanitizers.
1 parent 0ffa377 commit 2e9b331

File tree

12 files changed

+149
-23
lines changed

12 files changed

+149
-23
lines changed

compiler-rt/lib/rtsan/CMakeLists.txt

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ include_directories(..)
33
set(RTSAN_CXX_SOURCES
44
rtsan.cpp
55
rtsan_context.cpp
6+
rtsan_flags.cpp
67
rtsan_stack.cpp
78
rtsan_interceptors.cpp)
89

@@ -12,7 +13,10 @@ set(RTSAN_PREINIT_SOURCES
1213
set(RTSAN_HEADERS
1314
rtsan.h
1415
rtsan_context.h
15-
rtsan_stack.h)
16+
rtsan_flags.h
17+
rtsan_flags.inc
18+
rtsan_stack.h
19+
)
1620

1721
set(RTSAN_DEPS)
1822

compiler-rt/lib/rtsan/rtsan.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010

1111
#include <rtsan/rtsan.h>
1212
#include <rtsan/rtsan_context.h>
13+
#include <rtsan/rtsan_flags.h>
1314
#include <rtsan/rtsan_interceptors.h>
1415

1516
#include "sanitizer_common/sanitizer_atomic.h"
17+
#include "sanitizer_common/sanitizer_common.h"
1618
#include "sanitizer_common/sanitizer_mutex.h"
1719

1820
using namespace __rtsan;
@@ -29,7 +31,11 @@ extern "C" {
2931

3032
SANITIZER_INTERFACE_ATTRIBUTE void __rtsan_init() {
3133
CHECK(!__rtsan_is_initialized());
34+
35+
SanitizerToolName = "RealtimeSanitizer";
36+
InitializeFlags();
3237
InitializeInterceptors();
38+
3339
SetInitialized();
3440
}
3541

compiler-rt/lib/rtsan/rtsan_context.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
//
99
//===----------------------------------------------------------------------===//
1010

11+
#include <rtsan/rtsan.h>
1112
#include <rtsan/rtsan_context.h>
1213

1314
#include <rtsan/rtsan_stack.h>
@@ -75,6 +76,7 @@ void __rtsan::Context::BypassPop() { bypass_depth_--; }
7576

7677
void __rtsan::ExpectNotRealtime(Context &context,
7778
const char *intercepted_function_name) {
79+
CHECK(__rtsan_is_initialized());
7880
if (context.InRealtimeContext() && !context.IsBypassed()) {
7981
context.BypassPush();
8082

compiler-rt/lib/rtsan/rtsan_flags.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
//===--- rtsan_flags.cpp - Realtime Sanitizer -------------------*- 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+
// This file is a part of RealtimeSanitizer.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "rtsan/rtsan_flags.h"
14+
#include "sanitizer_common/sanitizer_flag_parser.h"
15+
#include "sanitizer_common/sanitizer_flags.h"
16+
17+
using namespace __sanitizer;
18+
using namespace __rtsan;
19+
20+
Flags __rtsan::flags_data;
21+
22+
SANITIZER_INTERFACE_WEAK_DEF(const char *, __rtsan_default_options, void) {
23+
return "";
24+
}
25+
26+
static void RegisterRtsanFlags(FlagParser *parser, Flags *f) {
27+
#define RTSAN_FLAG(Type, Name, DefaultValue, Description) \
28+
RegisterFlag(parser, #Name, Description, &f->Name);
29+
#include "rtsan_flags.inc"
30+
#undef RTSAN_FLAG
31+
}
32+
33+
void __rtsan::InitializeFlags() {
34+
SetCommonFlagsDefaults();
35+
{
36+
CommonFlags cf;
37+
cf.CopyFrom(*common_flags());
38+
cf.external_symbolizer_path = GetEnv("RTSAN_SYMBOLIZER_PATH");
39+
OverrideCommonFlags(cf);
40+
}
41+
42+
FlagParser parser;
43+
RegisterRtsanFlags(&parser, &flags());
44+
RegisterCommonFlags(&parser);
45+
46+
// Override from user-specified string.
47+
parser.ParseString(__rtsan_default_options());
48+
49+
parser.ParseStringFromEnv("RTSAN_OPTIONS");
50+
51+
InitializeCommonFlags();
52+
53+
if (Verbosity())
54+
ReportUnrecognizedFlags();
55+
56+
if (common_flags()->help)
57+
parser.PrintFlagDescriptions();
58+
}

compiler-rt/lib/rtsan/rtsan_flags.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//===----------------------- rtsan_flags.h ----------------------*- 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+
// This file is a part of RealtimeSanitizer.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#pragma once
13+
14+
namespace __rtsan {
15+
16+
struct Flags {
17+
#define RTSAN_FLAG(Type, Name, DefaultValue, Description) \
18+
Type Name{DefaultValue};
19+
#include "rtsan_flags.inc"
20+
#undef RTSAN_FLAG
21+
};
22+
23+
extern Flags flags_data;
24+
inline Flags &flags() { return flags_data; }
25+
26+
void InitializeFlags();
27+
28+
} // namespace __rtsan

compiler-rt/lib/rtsan/rtsan_flags.inc

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
//===------------------------ rtsan_flags.inc -------------------*- 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+
// RTSan runtime flags.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
#ifndef RTSAN_FLAG
13+
#error "Define RTSAN_FLAG prior to including this file!"
14+
#endif
15+
16+
// RTSAN_FLAG(Type, Name, DefaultValue, Description)
17+
// See COMMON_FLAG in sanitizer_flags.inc for more details.
18+
19+
// Example flag, until we get a real one
20+
// RTSAN_FLAG(bool, halt_on_error, true, "If true, halt the program on error")

compiler-rt/lib/rtsan/rtsan_stack.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,9 @@ void BufferedStackTrace::UnwindImpl(uptr pc, uptr bp, void *context,
2929
}
3030
} // namespace __sanitizer
3131

32-
static void SetGlobalStackTraceFormat() {
33-
SetCommonFlagsDefaults();
34-
CommonFlags cf;
35-
cf.CopyFrom(*common_flags());
36-
cf.stack_trace_format = "DEFAULT";
37-
cf.external_symbolizer_path = GetEnv("RTSAN_SYMBOLIZER_PATH");
38-
OverrideCommonFlags(cf);
39-
}
40-
4132
void __rtsan::PrintStackTrace(uptr pc, uptr bp) {
42-
4333
BufferedStackTrace stack{};
4434

4535
stack.Unwind(pc, bp, nullptr, common_flags()->fast_unwind_on_fatal);
46-
47-
SetGlobalStackTraceFormat();
4836
stack.Print();
4937
}

compiler-rt/lib/rtsan/tests/rtsan_test_context.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,37 @@
1010

1111
#include "rtsan_test_utilities.h"
1212

13-
#include "rtsan_context.h"
13+
#include "rtsan/rtsan.h"
14+
#include "rtsan/rtsan_context.h"
1415

15-
TEST(TestRtsanContext, CanCreateContext) { __rtsan::Context context{}; }
16+
#include <gtest/gtest.h>
1617

17-
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieBeforeRealtimePush) {
18+
class TestRtsanContext : public ::testing::Test {
19+
protected:
20+
void SetUp() override { __rtsan_ensure_initialized(); }
21+
};
22+
23+
TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieBeforeRealtimePush) {
1824
__rtsan::Context context{};
1925
ExpectNotRealtime(context, "do_some_stuff");
2026
}
2127

22-
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterPushAndPop) {
28+
TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterPushAndPop) {
2329
__rtsan::Context context{};
2430
context.RealtimePush();
2531
context.RealtimePop();
2632
ExpectNotRealtime(context, "do_some_stuff");
2733
}
2834

29-
TEST(TestRtsanContext, ExpectNotRealtimeDiesAfterRealtimePush) {
35+
TEST_F(TestRtsanContext, ExpectNotRealtimeDiesAfterRealtimePush) {
3036
__rtsan::Context context{};
3137

3238
context.RealtimePush();
3339
EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
3440
}
3541

36-
TEST(TestRtsanContext,
37-
ExpectNotRealtimeDiesAfterRealtimeAfterMorePushesThanPops) {
42+
TEST_F(TestRtsanContext,
43+
ExpectNotRealtimeDiesAfterRealtimeAfterMorePushesThanPops) {
3844
__rtsan::Context context{};
3945

4046
context.RealtimePush();
@@ -45,16 +51,16 @@ TEST(TestRtsanContext,
4551
EXPECT_DEATH(ExpectNotRealtime(context, "do_some_stuff"), "");
4652
}
4753

48-
TEST(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
54+
TEST_F(TestRtsanContext, ExpectNotRealtimeDoesNotDieAfterBypassPush) {
4955
__rtsan::Context context{};
5056

5157
context.RealtimePush();
5258
context.BypassPush();
5359
ExpectNotRealtime(context, "do_some_stuff");
5460
}
5561

56-
TEST(TestRtsanContext,
57-
ExpectNotRealtimeDoesNotDieIfBypassDepthIsGreaterThanZero) {
62+
TEST_F(TestRtsanContext,
63+
ExpectNotRealtimeDoesNotDieIfBypassDepthIsGreaterThanZero) {
5864
__rtsan::Context context{};
5965

6066
context.RealtimePush();
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t
2+
// RUN: RTSAN_OPTIONS="verbosity=1,asdf=1" %run %t 2>&1 | FileCheck %s
3+
// UNSUPPORTED: ios
4+
5+
// Intent: Make sure we are respecting some basic common flags
6+
7+
int main() {
8+
return 0;
9+
// CHECK: WARNING: found 1 unrecognized flag(s):
10+
// CHECK-NEXT: {{.*asdf*}}
11+
}

llvm/test/Unit/lit.cfg.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
"ASAN_OPTIONS",
4848
"HWASAN_OPTIONS",
4949
"MSAN_OPTIONS",
50+
"RTSAN_OPTIONS",
5051
"TSAN_OPTIONS",
5152
"UBSAN_OPTIONS",
5253
]:

llvm/utils/lit/lit/TestingConfig.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ def fromdefaults(litConfig):
4141
"LSAN_OPTIONS",
4242
"HWASAN_OPTIONS",
4343
"MSAN_OPTIONS",
44+
"RTSAN_OPTIONS",
4445
"TSAN_OPTIONS",
4546
"UBSAN_OPTIONS",
4647
"ADB",

llvm/utils/lit/lit/llvm/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ def __init__(self, lit_config, config):
8383
"UBSAN_SYMBOLIZER_PATH" "ASAN_OPTIONS",
8484
"HWASAN_OPTIONS",
8585
"MSAN_OPTIONS",
86+
"RTSAN_OPTIONS",
8687
"TSAN_OPTIONS",
8788
"UBSAN_OPTIONS",
8889
]

0 commit comments

Comments
 (0)