Skip to content

[Support] Properly zero initialize CPU set when querying affinity #142924

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
Jun 6, 2025

Conversation

Dinistro
Copy link
Contributor

@Dinistro Dinistro commented Jun 5, 2025

This commit resolves a potential issue of working with uninitialized memory when querying the CPU's affinity. The man page of sched_getaffinity does not guarantee that the memory will be fully overwritten, so this change should ensure that issues are avoided.

This commit resolves a potential issue of working with uninitialized
memory when querying the CPU's affinity. The man page of
`sched_getaffinity` does not guarantee that the memory will be fully
overwritten, so this change should ensure that issues are avoided.
@Dinistro Dinistro marked this pull request as ready for review June 5, 2025 09:07
@llvmbot
Copy link
Member

llvmbot commented Jun 5, 2025

@llvm/pr-subscribers-llvm-support

Author: Christian Ulmann (Dinistro)

Changes

This commit resolves a potential issue of working with uninitialized memory when querying the CPU's affinity. The man page of sched_getaffinity does not guarantee that the memory will be fully overwritten, so this change should ensure that issues are avoided.


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

1 Files Affected:

  • (modified) llvm/lib/Support/Unix/Threading.inc (+1)
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 15a5b008604c3..a553230de054c 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -316,6 +316,7 @@ static int computeHostNumHardwareThreads() {
     return CPU_COUNT(&mask);
 #elif defined(__linux__)
   cpu_set_t Set;
+  CPU_ZERO(&Set);
   if (sched_getaffinity(0, sizeof(Set), &Set) == 0)
     return CPU_COUNT(&Set);
 #endif

@Dinistro Dinistro requested review from aganea and MaskRay June 5, 2025 09:08
@Dinistro Dinistro merged commit 052d588 into main Jun 6, 2025
15 checks passed
@Dinistro Dinistro deleted the users/dinistro/ensure-zeroing-of-thread-set branch June 6, 2025 09:24
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