Skip to content

Commit fdac18c

Browse files
authored
[hwasan] Fix rare false negative (zero tag) in two more test cases (#69491)
stack-uas.c and stack-history-length.c both have -hwasan-record-stack-history=libcall, which makes the stack base tag fully randomized. They may therefore sometimes have a zero tag for a stack allocated variable, resulting in a false negative (#69221 (comment)). This patch applies the same workaround as used for deep-recursion.c (aa4dfd3) and stack-uar.c (ddf1de2): have two adjacent stack-allocated variables, and use whichever is not zero-tagged. These are the last remaining test cases that use -hwasan-record-stack-history=libcall. stack-uas flakiness spotted in the wild: https://lab.llvm.org/buildbot/#/builders/269/builds/549/steps/11/logs/stdio stack-history-length: https://lab.llvm.org/buildbot/#/builders/269/builds/537 Co-authored-by: Thurston Dang <[email protected]>
1 parent 553616a commit fdac18c

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

compiler-rt/test/hwasan/TestCases/stack-history-length.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
// Stack histories are currently not recorded on x86.
1212
// XFAIL: target=x86_64{{.*}}
1313

14+
#include <assert.h>
15+
#include <sanitizer/hwasan_interface.h>
1416
#include <stdlib.h>
1517

1618
void USE(void *x) { // pretend_to_do_something(void *x)
@@ -20,7 +22,24 @@ void USE(void *x) { // pretend_to_do_something(void *x)
2022
volatile int four = 4;
2123
__attribute__((noinline)) void FUNC0() { int x[4]; USE(&x[0]); }
2224
__attribute__((noinline)) void FUNC() { int x[4]; USE(&x[0]); }
23-
__attribute__((noinline)) void OOB() { int x[4]; x[four] = 0; USE(&x[0]); }
25+
__attribute__((noinline)) void OOB() {
26+
int x[4];
27+
int y[4];
28+
// With -hwasan-generate-tags-with-calls=false, stack tags can occasionally
29+
// be zero, leading to a false negative
30+
// (https://github.com/llvm/llvm-project/issues/69221). Work around it by
31+
// using the neighboring variable, which is guaranteed by
32+
// -hwasan-generate-tags-with-calls=false to have a different (hence
33+
// non-zero) tag.
34+
if (__hwasan_tag_pointer(x, 0) == x) {
35+
assert(__hwasan_tag_pointer(y, 0) != y);
36+
y[four] = 0;
37+
} else {
38+
x[four] = 0;
39+
}
40+
USE(&x[0]);
41+
USE(&y[0]);
42+
}
2443

2544
int main(int argc, char **argv) {
2645
int X = argc == 2 ? atoi(argv[1]) : 10;

compiler-rt/test/hwasan/TestCases/stack-uas.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313
// Stack histories currently are not recorded on x86.
1414
// XFAIL: target=x86_64{{.*}}
1515

16+
#include <assert.h>
17+
#include <sanitizer/hwasan_interface.h>
18+
#include <stdio.h>
19+
1620
void USE(void *x) { // pretend_to_do_something(void *x)
1721
__asm__ __volatile__(""
1822
:
@@ -36,8 +40,20 @@ __attribute__((noinline)) void Unrelated3() {
3640
__attribute__((noinline)) char buggy() {
3741
char *volatile p;
3842
{
39-
char zzz[0x1000] = {};
40-
p = zzz;
43+
char zzz[0x800] = {};
44+
char yyy[0x800] = {};
45+
// With -hwasan-generate-tags-with-calls=false, stack tags can occasionally
46+
// be zero, leading to a false negative
47+
// (https://github.com/llvm/llvm-project/issues/69221). Work around it by
48+
// using the neighboring variable, which is guaranteed by
49+
// -hwasan-generate-tags-with-calls=false to have a different (hence
50+
// non-zero) tag.
51+
if (__hwasan_tag_pointer(zzz, 0) == zzz) {
52+
assert(__hwasan_tag_pointer(yyy, 0) != yyy);
53+
p = yyy;
54+
} else {
55+
p = zzz;
56+
}
4157
}
4258
return *p;
4359
}
@@ -53,7 +69,7 @@ int main() {
5369
// CHECK: Cause: stack tag-mismatch
5470
// CHECK: is located in stack of thread
5571
// CHECK: Potentially referenced stack objects:
56-
// CHECK-NEXT: zzz in buggy {{.*}}stack-uas.c:[[@LINE-17]]
72+
// CHECK-NEXT: {{zzz|yyy}} in buggy {{.*}}stack-uas.c:
5773
// CHECK-NEXT: Memory tags around the buggy address
5874

5975
// NOSYM: Previously allocated frames:

0 commit comments

Comments
 (0)