Skip to content

Commit 44d9bee

Browse files
authored
[rtsan][test] Prevent test check from being optimized out in LTO builds (#122524)
In LTO builds, some test checks can be optimized away, since the compiler can see through the memory accesses after inlining across TUs. This causes the existing death tests to fail, since the functions are completely optimized out and things like copying a lambda will no longer occur and trigger the sanitizer. To prevent that, we can use an empty inline assembly block to tell the compiler that memory is modified, and prevent it from doing that.
1 parent 31e9d39 commit 44d9bee

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,13 +145,23 @@ TEST(TestRtsan, LaunchingAThreadDiesWhenRealtime) {
145145

146146
namespace {
147147
void InvokeStdFunction(std::function<void()> &&function) { function(); }
148+
149+
template <typename T> void HideMemoryFromCompiler(T *memory) {
150+
// Pass the pointer to an empty assembly block as an input, and inform
151+
// the compiler that memory is read to and possibly modified. This should not
152+
// be architecture specific, since the asm block is empty.
153+
__asm__ __volatile__("" ::"r"(memory) : "memory");
154+
}
148155
} // namespace
149156

150157
TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
151158
std::array<float, 16> lots_of_data;
152159
auto LargeLambda = [lots_of_data]() mutable {
153-
// Stop everything getting optimised out
154160
lots_of_data[3] = 0.25f;
161+
// In LTO builds, this lambda can be optimized away, since the compiler can
162+
// see through the memory accesses after inlining across TUs. Ensure it can
163+
// no longer reason about the memory access, so that won't happen.
164+
HideMemoryFromCompiler(&lots_of_data[3]);
155165
EXPECT_EQ(16u, lots_of_data.size());
156166
EXPECT_EQ(0.25f, lots_of_data[3]);
157167
};

0 commit comments

Comments
 (0)