Skip to content

Commit beab949

Browse files
committed
[X86] Add test suite for stack realignment.
Reland patch D145926. Constrain the test to X86 only.
1 parent b5b649b commit beab949

File tree

6 files changed

+126
-0
lines changed

6 files changed

+126
-0
lines changed

SingleSource/UnitTests/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ add_subdirectory(SignlessTypes)
77
add_subdirectory(Threads)
88
add_subdirectory(Vector)
99
add_subdirectory(Vectorizer)
10+
add_subdirectory(X86)
1011

1112
list(APPEND CFLAGS -Wno-implicit-function-declaration -Wno-implicit-int)
1213

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if(CMAKE_C_COMPILER_ID STREQUAL "Clang")
2+
if(ARCH STREQUAL "x86")
3+
llvm_singlesource(PREFIX "x86-")
4+
endif()
5+
endif()
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int checkInt(int *I, int Align) {
5+
*I = 20;
6+
if ((((size_t)I) & (Align - 1)) != 0) {
7+
printf("\nUnalign address (%d): %p!\n", Align, I);
8+
abort();
9+
}
10+
return *I;
11+
}
12+
13+
#ifndef ALIGNMENT
14+
#define ALIGNMENT 64
15+
#endif
16+
17+
typedef int aligned __attribute__((aligned(ALIGNMENT)));
18+
19+
void bar(char *p, int size) { __builtin_strncpy(p, "good", size); }
20+
21+
class Base {};
22+
23+
struct A : virtual public Base {
24+
A() {}
25+
};
26+
27+
struct B {};
28+
29+
void foo(int size) {
30+
aligned Var;
31+
char *Ptr = (char *)__builtin_alloca(size + 1);
32+
aligned I;
33+
34+
// clobber 32-bit base pointer.
35+
asm volatile("nop" ::"S"(405) :);
36+
// clobber 64-bit base pointer.
37+
asm volatile("nop" ::"b"(405) :);
38+
bar(Ptr, size);
39+
if (__builtin_strncmp(Ptr, "good", size) != 0) {
40+
Ptr[size] = '\0';
41+
printf("Failed: %s != good\n", Ptr);
42+
abort();
43+
}
44+
45+
if (checkInt(&I, __alignof__(I)) != I)
46+
abort();
47+
48+
// access argument and local variable.
49+
asm volatile("movl %0, %1" ::"r"(size), "m"(Var) :);
50+
throw A();
51+
}
52+
53+
int main() {
54+
try {
55+
foo(5);
56+
} catch (A &a) {
57+
}
58+
return 0;
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exit 0
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
4+
int checkInt(int *I, int Align) {
5+
*I = 20;
6+
if ((((size_t)I) & (Align - 1)) != 0) {
7+
printf("\nUnalign address (%d): %p!\n", Align, I);
8+
abort();
9+
}
10+
return *I;
11+
}
12+
13+
#ifndef ALIGNMENT
14+
#define ALIGNMENT 64
15+
#endif
16+
17+
typedef int aligned __attribute__((aligned(ALIGNMENT)));
18+
19+
void bar(char *p, int size) { __builtin_strncpy(p, "good", size); }
20+
21+
class Base {};
22+
23+
struct A : virtual public Base {
24+
A() {}
25+
};
26+
27+
struct B {};
28+
29+
__attribute__((noinline))
30+
void foo(int size) {
31+
aligned Var;
32+
char *Ptr = (char *)__builtin_alloca(size + 1);
33+
aligned I;
34+
35+
// clobber 32-bit base pointer.
36+
asm volatile("nop" ::"S"(405) :);
37+
// clobber 64-bit base pointer.
38+
asm volatile("nop" ::"b"(405) :);
39+
bar(Ptr, size);
40+
if (__builtin_strncmp(Ptr, "good", size) != 0) {
41+
Ptr[size] = '\0';
42+
printf("Failed: %s != good\n", Ptr);
43+
abort();
44+
}
45+
46+
if (checkInt(&I, __alignof__(I)) != I)
47+
abort();
48+
49+
// access argument and local variable.
50+
asm volatile("movl %0, %1" ::"r"(size), "m"(Var) :);
51+
}
52+
53+
int main() {
54+
try {
55+
foo(5);
56+
} catch (A &a) {
57+
}
58+
return 0;
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exit 0

0 commit comments

Comments
 (0)