[llvm] [BOLT][runtime] harden hugify THP support check. (PR #192042)

David CARLIER via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 04:58:46 PDT 2026


https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/192042

None

>From 06f75b492688dd2cfa768afbd4c1ded4f89eed90 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Tue, 14 Apr 2026 12:57:49 +0100
Subject: [PATCH] [BOLT][runtime] harden hugify THP support check.

---
 bolt/runtime/hugify.cpp | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/bolt/runtime/hugify.cpp b/bolt/runtime/hugify.cpp
index de896307f24fc..4a6d44efca4ea 100644
--- a/bolt/runtime/hugify.cpp
+++ b/bolt/runtime/hugify.cpp
@@ -65,20 +65,25 @@ static bool hasPagecacheTHPSupport() {
   char Buf[64];
 
   int FD = __open("/sys/kernel/mm/transparent_hugepage/enabled",
-                  0 /* O_RDONLY */, 0);
+                  O_CLOEXEC /* O_RDONLY | O_CLOEXEC */, 0);
   if (FD < 0)
     return false;
 
   memset(Buf, 0, sizeof(Buf));
-  const size_t Res = __read(FD, Buf, sizeof(Buf));
-  if (Res < 0)
+  const size_t Res = __read(FD, Buf, sizeof(Buf) - 1);
+  if (static_cast<int64_t>(Res) < 0) {
+    __close(FD);
     return false;
+  }
 
   if (!strStr(Buf, "[always]") && !strStr(Buf, "[madvise]")) {
     DEBUG(report("[hugify] THP support is not enabled.\n");)
+    __close(FD);
     return false;
   }
 
+  __close(FD);
+
   struct KernelVersionTy {
     uint32_t major;
     uint32_t minor;



More information about the llvm-commits mailing list