Skip to content

Commit b86f26a

Browse files
committed
Fix incorrect conditional predicting loop count
1 parent 5f4d0b0 commit b86f26a

File tree

2 files changed

+49
-1
lines changed

2 files changed

+49
-1
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// RUN: %clangxx -fsanitize=realtime %s -o %t -O3
2+
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty
3+
// RUN: %clangxx -fsanitize=realtime %s -o %t -O2
4+
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty
5+
// RUN: %clangxx -fsanitize=realtime %s -o %t -O1
6+
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty
7+
8+
// RUN: %clangxx -fsanitize=realtime %s -o %t -O0
9+
// RUN: not %run %t 2>&1 | FileCheck %s --allow-empty
10+
11+
// UNSUPPORTED: ios
12+
13+
// Intent: Ensure basic bound audio loops don't trigger rtsan.
14+
15+
#include <stdio.h>
16+
#include <stdlib.h>
17+
18+
#include <assert.h>
19+
20+
21+
void BadScalarEvolution(int* buffer, int sample_count, int channel_count) [[clang::nonblocking]] {
22+
23+
int sample = 0;
24+
while (sample < sample_count) {
25+
int channel = 0;
26+
while (channel < channel_count) {
27+
buffer[sample * channel_count + channel] = sample;
28+
channel++;
29+
}
30+
sample++;
31+
32+
// NOTE! Here is the "bug" that causes the loop to be unbounded.
33+
sample_count++;
34+
}
35+
}
36+
37+
int main() {
38+
const int sample_count = 10;
39+
const int channel_count = 2;
40+
int buffer[channel_count * sample_count];
41+
42+
BadScalarEvolution(buffer, sample_count, channel_count);
43+
44+
return 0;
45+
}
46+
47+
// CHECK: {{.*Real-time violation.*}}
48+
// CHECK-NEXT {{.*BadScalarEvolution.*}}

llvm/lib/Transforms/Instrumentation/RealtimeSanitizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ PreservedAnalyses RealtimeSanitizerLoopPass::run(Loop &L, LoopAnalysisManager &A
8484
assert(F && "Loop has no parent function");
8585

8686
const bool HasNoExits = L.hasNoExitBlocks();
87-
const bool CannotPredictLoopCount = isa<SCEVCouldNotCompute>(AR.SE.getConstantMaxBackedgeTakenCount(&L)) &&
87+
const bool CannotPredictLoopCount = isa<SCEVCouldNotCompute>(AR.SE.getConstantMaxBackedgeTakenCount(&L)) ||
8888
isa<SCEVCouldNotCompute>(AR.SE.getBackedgeTakenCount(&L));
8989
const bool LoopIsPotentiallyUnbound = HasNoExits || CannotPredictLoopCount;
9090

0 commit comments

Comments
 (0)