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

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 9 22:09:12 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-compiler-rt-sanitizer

Author: Thurston Dang (thurstond)

<details>
<summary>Changes</summary>

This test case demonstrates that ASan does not currently align FakeStack frames correctly. (https://github.com/llvm/llvm-project/pull/152819 will fix it.)

---
Full diff: https://github.com/llvm/llvm-project/pull/152889.diff


1 Files Affected:

- (added) compiler-rt/test/asan/TestCases/fakestack_alignment.cpp (+74) 


``````````diff
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..597a486e26534
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/fakestack_alignment.cpp
@@ -0,0 +1,74 @@
+// RUN: %clangxx_asan -fsanitize-address-use-after-return=always -O0 %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(4096) page {
+    int x;
+};
+
+struct alignas(16384) larry {
+    int x;
+};
+
+bool misaligned = false;
+
+void grandchild(void) {
+  larry l2;
+  uint alignment = (unsigned long)&l2 % alignof(larry);
+  if (alignment != 0)
+    misaligned = true;
+
+  printf ("Grandchild: address modulo alignment %u\n", alignment);
+}
+
+// Even if the FakeStack frame is aligned by chance to 16384, we can use an
+// intervening stack frame to knock it out of alignment.
+void child(void) {
+  page p1;
+  uint alignment = (unsigned long)&p1 % alignof(page);
+  printf ("Child: address modulo alignment is %u\n", alignment);
+  if (alignment != 0)
+    misaligned = true;
+
+  grandchild();
+}
+
+// Check whether the FakeStack frame is sufficiently aligned. Alignment can
+// happen by chance, so try this on many threads if you don't want
+void *Thread(void *unused)  {
+  larry l1;
+  uint alignment = (unsigned long)&l1 % alignof(larry);
+  printf ("Thread: address modulo alignment is %u\n", alignment);
+  if (alignment != 0)
+    misaligned = true;
+
+  child();
+
+  return NULL;
+}
+
+int main(int argc, char **argv) {
+  pthread_attr_t attr;
+  pthread_attr_init(&attr);
+
+  pthread_t t[10];
+  for (int i = 0; i < 10; i++) {
+     pthread_create(&t[i], &attr, Thread, 0);
+  }
+  pthread_attr_destroy(&attr);
+  for (int i = 0; i < 10; i++) {
+     pthread_join(t[i], 0);
+  }
+
+  if (misaligned) {
+    printf ("Test failed: not perfectly aligned\n");
+    exit(1);
+  }
+
+  return 0;
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/152889


More information about the llvm-commits mailing list