[compiler-rt] e36bd61 - [asan] Add test case for alignment of FakeStack frames (#152889)

via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 11 15:48:08 PDT 2025


Author: Thurston Dang
Date: 2025-08-11T15:48:05-07:00
New Revision: e36bd6158e695a369647fbc609ded323fbd3bc2f

URL: https://github.com/llvm/llvm-project/commit/e36bd6158e695a369647fbc609ded323fbd3bc2f
DIFF: https://github.com/llvm/llvm-project/commit/e36bd6158e695a369647fbc609ded323fbd3bc2f.diff

LOG: [asan] Add test case for alignment of FakeStack frames (#152889)

This test case demonstrates that ASan does not currently align FakeStack frames correctly:
- for 4KB objects on a 64KB stack, alignment is deterministically incorrect
- for objects larger than 4KB, even with large stack sizes, alignment is not guaranteed

https://github.com/llvm/llvm-project/pull/152819 will fix it.

Added: 
    compiler-rt/test/asan/TestCases/fakestack_alignment.cpp

Modified: 
    

Removed: 
    


################################################################################
diff  --git a/compiler-rt/test/asan/TestCases/fakestack_alignment.cpp b/compiler-rt/test/asan/TestCases/fakestack_alignment.cpp
new file mode 100644
index 0000000000000..01c073088694b
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/fakestack_alignment.cpp
@@ -0,0 +1,68 @@
+// Regression test 1:
+// This deterministically fails: when the stack size is 1<<16, FakeStack's
+// GetFrame() is out of alignment, because SizeRequiredForFlags(16) == 2K.
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096  -DTHREAD_COUNT=1 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&1
+
+// Regression test 2:
+// The FakeStack frame is not guaranteed to be aligned, but alignment can
+// happen by chance, so try this on many threads.
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192  -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&1
+
+// Extra tests:
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096  -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192  -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=65536 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=4096  -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=8192  -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&1
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 -DALIGNMENT=16384 -DTHREAD_COUNT=32 -DTHREAD_STACK_SIZE=131072 %s -o %t && %run %t 2>&1
+
+// XFAIL: *
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+struct alignas(ALIGNMENT) big_object {
+  int x;
+};
+
+bool misaligned = false;
+
+// Check whether the FakeStack frame is sufficiently aligned. Alignment can
+// happen by chance, so try this on many threads.
+void *Thread(void *unused) {
+  big_object x;
+  uint alignment = (unsigned long)&x % alignof(big_object);
+
+  if (alignment != 0)
+    misaligned = true;
+
+  return nullptr;
+}
+
+int main(int argc, char **argv) {
+  pthread_attr_t attr;
+  pthread_attr_init(&attr);
+#ifdef THREAD_STACK_SIZE
+  pthread_attr_setstacksize(&attr, THREAD_STACK_SIZE);
+#endif
+
+  pthread_t threads[THREAD_COUNT];
+  for (pthread_t &t : threads)
+    pthread_create(&t, &attr, Thread, 0);
+
+  pthread_attr_destroy(&attr);
+
+  for (pthread_t &t : threads)
+    pthread_join(t, 0);
+
+  if (misaligned) {
+    printf("Test failed: not perfectly aligned\n");
+    exit(1);
+  }
+
+  return 0;
+}


        


More information about the llvm-commits mailing list