Skip to content

Commit b8dd31d

Browse files
committed
[asan] Add test case for alignment of FakeStack frames
This test case demonstrates that ASan does not currently align FakeStack frames correctly. (#152819 will fix it.)
1 parent 97f0ff0 commit b8dd31d

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 %s -o %t && %run %t 2>&1
2+
// XFAIL: *
3+
4+
#include <assert.h>
5+
#include <pthread.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
8+
#include <string.h>
9+
10+
struct alignas(4096) page {
11+
int x;
12+
};
13+
14+
struct alignas(16384) larry {
15+
int x;
16+
};
17+
18+
bool misaligned = false;
19+
20+
void grandchild(void) {
21+
larry l2;
22+
uint alignment = (unsigned long)&l2 % alignof(larry);
23+
if (alignment != 0)
24+
misaligned = true;
25+
26+
printf ("Grandchild: address modulo alignment %u\n", alignment);
27+
}
28+
29+
// Even if the FakeStack frame is aligned by chance to 16384, we can use an
30+
// intervening stack frame to knock it out of alignment.
31+
void child(void) {
32+
page p1;
33+
uint alignment = (unsigned long)&p1 % alignof(page);
34+
printf ("Child: address modulo alignment is %u\n", alignment);
35+
if (alignment != 0)
36+
misaligned = true;
37+
38+
grandchild();
39+
}
40+
41+
// Check whether the FakeStack frame is sufficiently aligned. Alignment can
42+
// happen by chance, so try this on many threads if you don't want
43+
void *Thread(void *unused) {
44+
larry l1;
45+
uint alignment = (unsigned long)&l1 % alignof(larry);
46+
printf ("Thread: address modulo alignment is %u\n", alignment);
47+
if (alignment != 0)
48+
misaligned = true;
49+
50+
child();
51+
52+
return NULL;
53+
}
54+
55+
int main(int argc, char **argv) {
56+
pthread_attr_t attr;
57+
pthread_attr_init(&attr);
58+
59+
pthread_t t[10];
60+
for (int i = 0; i < 10; i++) {
61+
pthread_create(&t[i], &attr, Thread, 0);
62+
}
63+
pthread_attr_destroy(&attr);
64+
for (int i = 0; i < 10; i++) {
65+
pthread_join(t[i], 0);
66+
}
67+
68+
if (misaligned) {
69+
printf ("Test failed: not perfectly aligned\n");
70+
exit(1);
71+
}
72+
73+
return 0;
74+
}

0 commit comments

Comments
 (0)