Skip to content

[asan] Add test case for alignment of FakeStack frames for 4KB objects with smaller thread stack sizes #152892

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

thurstond
Copy link
Contributor

This test case demonstrates that ASan does not currently align FakeStack frames correctly for 4KB objects when using a smaller thread stack size (64KB), which forces the FakeStack frames to no longer be 4KB aligned.

This differs from #152889, which is a test case for objects >4KB, which relies on the fact that the default 4KB alignment for fake stack sizes >64KB is insufficient.

#152819 will fix both issues.

This test case demonstrates that ASan does not currently align FakeStack frames correctly for 4KB objects.

It deliberately uses a smaller thread stack size (64KB), which forces
the FakeStack frames to no longer be 4KB aligned.

This differs from llvm#152889,
which is a test case for objects >4KB, which relies on the fact that the
default 4KB alignment for fake stack sizes >64KB is insufficient.

llvm#152819 will fix both issues.
@llvmbot
Copy link
Member

llvmbot commented Aug 10, 2025

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

Author: Thurston Dang (thurstond)

Changes

This test case demonstrates that ASan does not currently align FakeStack frames correctly for 4KB objects when using a smaller thread stack size (64KB), which forces the FakeStack frames to no longer be 4KB aligned.

This differs from #152889, which is a test case for objects >4KB, which relies on the fact that the default 4KB alignment for fake stack sizes >64KB is insufficient.

#152819 will fix both issues.


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

1 Files Affected:

  • (added) compiler-rt/test/asan/TestCases/fakestack_alignment2.cpp (+37)
diff --git a/compiler-rt/test/asan/TestCases/fakestack_alignment2.cpp b/compiler-rt/test/asan/TestCases/fakestack_alignment2.cpp
new file mode 100644
index 0000000000000..3f0b77a5eb889
--- /dev/null
+++ b/compiler-rt/test/asan/TestCases/fakestack_alignment2.cpp
@@ -0,0 +1,37 @@
+// 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;
+};
+
+void *Thread(void *unused)  {
+  page p1;
+  uint alignment = (unsigned long)&p1 % alignof(page);
+  printf ("Thread: address modulo alignment is %u\n", alignment);
+  assert(alignment == 0);
+
+  return NULL;
+}
+
+int main(int argc, char **argv) {
+  pthread_attr_t attr;
+  pthread_attr_init(&attr);
+
+  // When the stack size is 1<<16, FakeStack's GetFrame() is out of alignment,
+  // because SizeRequiredForFlags(16) == 2K.
+  pthread_attr_setstacksize(&attr, 1<<16);
+
+  pthread_t t;
+  pthread_create(&t, &attr, Thread, 0);
+  pthread_attr_destroy(&attr);
+  pthread_join(t, 0);
+
+  return 0;
+}

Copy link

github-actions bot commented Aug 10, 2025

✅ With the latest revision this PR passed the C/C++ code formatter.

@fmayer
Copy link
Contributor

fmayer commented Aug 11, 2025

see the > 4K test. I think we should merge the two and use defines

@thurstond
Copy link
Contributor Author

see the > 4K test. I think we should merge the two and use defines

I shall consolidate the tests to provide durable savings.

@thurstond thurstond closed this Aug 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants