Skip to content

[compiler-rt] Destroy pthread attrs after use in tests #114923

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

Merged
merged 1 commit into from
Jan 21, 2025

Conversation

kevans91
Copy link
Contributor

@kevans91 kevans91 commented Nov 5, 2024

The attr typically located on the stack is of an opaque pthread_attr_t type, which may be a pointer that gets initialized by pthread_attr_init(). Explicitly clean up the attr with pthread_attr_destroy() to avoid a leak on such platforms to avoid unexpected test failures with lsan enabled.

This primarily affects FreeBSD; NetBSD, musl, and glibc will seemingly all use a full-sized pthread_attr_t.

The attr typically located on the stack is of an opaque pthread_attr_t
type, which may be a pointer that gets initialized by pthread_attr_init().
Explicitly clean up the attr with pthread_attr_destroy() to avoid a leak on
such platforms to avoid unexpected test failures with lsan enabled.

This primarily affects FreeBSD; NetBSD, musl, and glibc will seemingly all
use a full-sized pthread_attr_t.
@llvmbot
Copy link
Member

llvmbot commented Nov 5, 2024

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

Author: Kyle Evans (kevans91)

Changes

The attr typically located on the stack is of an opaque pthread_attr_t type, which may be a pointer that gets initialized by pthread_attr_init(). Explicitly clean up the attr with pthread_attr_destroy() to avoid a leak on such platforms to avoid unexpected test failures with lsan enabled.

This primarily affects FreeBSD; NetBSD, musl, and glibc will seemingly all use a full-sized pthread_attr_t.


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

3 Files Affected:

  • (modified) compiler-rt/test/asan/TestCases/Posix/fake_stack_gc.cpp (+1)
  • (modified) compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp (+1)
  • (modified) compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp (+1)
diff --git a/compiler-rt/test/asan/TestCases/Posix/fake_stack_gc.cpp b/compiler-rt/test/asan/TestCases/Posix/fake_stack_gc.cpp
index 8c368b9b1b947f..36fdf81120b59d 100644
--- a/compiler-rt/test/asan/TestCases/Posix/fake_stack_gc.cpp
+++ b/compiler-rt/test/asan/TestCases/Posix/fake_stack_gc.cpp
@@ -89,6 +89,7 @@ int main(void) {
 
   pthread_t tid;
   assert(pthread_create(&tid, &attr, Thread, alt_stack) == 0);
+  assert(pthread_attr_destroy(&attr) == 0);
 
   pthread_join(tid, nullptr);
 
diff --git a/compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp b/compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp
index 593114bdf2e8df..8e7d5082d0b5d9 100644
--- a/compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp
+++ b/compiler-rt/test/asan/TestCases/Posix/unpoison-alternate-stack.cpp
@@ -159,6 +159,7 @@ int main() {
   pthread_attr_init(&ThreadAttr);
   pthread_attr_setstack(&ThreadAttr, Mapping, DefaultStackSize);
   pthread_create(&Thread, &ThreadAttr, &threadFun, (void *)&AltStack);
+  pthread_attr_destroy(&ThreadAttr);
 
   pthread_join(Thread, nullptr);
 
diff --git a/compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp b/compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp
index 68eea93a81e57e..d0363a0bf85aeb 100644
--- a/compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp
+++ b/compiler-rt/test/lsan/TestCases/leak_check_before_thread_started.cpp
@@ -37,6 +37,7 @@ void create_detached_thread() {
   pthread_mutex_lock(&mutex);
   int res = pthread_create(&thread_id, &attr, func, arg);
   assert(res == 0);
+  pthread_attr_destroy(&attr);
 }
 
 int main() {

@kevans91
Copy link
Contributor Author

kevans91 commented Nov 5, 2024

I don't anticipate having many more of these small nitpicky things- mostly trying to declutter my FreeBSD/lsan branch to better see what I have left to understand/resolve with the integration.

@kevans91
Copy link
Contributor Author

kevans91 commented Nov 7, 2024

CC @jrtc27 maybe, this one's fairly trivial

Copy link
Contributor

@fmayer fmayer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Specifically https://pubs.opengroup.org/onlinepubs/007904875/functions/pthread_create.html:

If the attributes specified by attr are modified later, the thread's attributes shall not be affected

@kevans91
Copy link
Contributor Author

LGTM.

Specifically https://pubs.opengroup.org/onlinepubs/007904875/functions/pthread_create.html:

If the attributes specified by attr are modified later, the thread's attributes shall not be affected

Right, the spec doesn't really allow for any possibility that isn't copying the attr passed in out of the harm's way.

@kevans91
Copy link
Contributor Author

Ping... what else is needed here?

@jrtc27 jrtc27 merged commit a79098b into llvm:main Jan 21, 2025
12 checks passed
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.

5 participants