[compiler-rt] 5deadc6 - [NFC][sanitizer] Extract `LoadStatus` (#111909)

via llvm-commits llvm-commits at lists.llvm.org
Thu Oct 10 14:52:53 PDT 2024


Author: Vitaly Buka
Date: 2024-10-10T14:52:50-07:00
New Revision: 5deadc6eaede3d32ccdd68529f371092d4d218da

URL: https://github.com/llvm/llvm-project/commit/5deadc6eaede3d32ccdd68529f371092d4d218da
DIFF: https://github.com/llvm/llvm-project/commit/5deadc6eaede3d32ccdd68529f371092d4d218da.diff

LOG: [NFC][sanitizer] Extract `LoadStatus` (#111909)

For #111901

Added: 
    

Modified: 
    compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
    compiler-rt/lib/sanitizer_common/sanitizer_linux.h

Removed: 
    


################################################################################
diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
index e5d6d0a6e71649..a4e58133c79f08 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp
@@ -1042,8 +1042,6 @@ ThreadLister::Result ThreadLister::ListThreads(
 
   Result result = Ok;
   for (bool first_read = true;; first_read = false) {
-    // Resize to max capacity if it was downsized by IsAlive.
-    buffer_.resize(buffer_.capacity());
     CHECK_GE(buffer_.size(), 4096);
     uptr read = internal_getdents(
         descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
@@ -1088,14 +1086,25 @@ ThreadLister::Result ThreadLister::ListThreads(
   }
 }
 
+const char *ThreadLister::LoadStatus(int tid) {
+  auto cleanup = at_scope_exit([&] {
+    // Resize back to capacity if it is downsized by `ReadFileToVector`.
+    buffer_.resize(buffer_.capacity());
+  });
+  if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
+    return nullptr;
+  buffer_.push_back('\0');
+  return buffer_.data();
+}
+
 bool ThreadLister::IsAlive(int tid) {
   // /proc/%d/task/%d/status uses same call to detect alive threads as
   // proc_task_readdir. See task_state implementation in Linux.
-  if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
-    return false;
-  buffer_.push_back(0);
   static const char kPrefix[] = "\nPPid:";
-  const char *field = internal_strstr(buffer_.data(), kPrefix);
+  const char *status = LoadStatus(tid);
+  if (!status)
+    return false;
+  const char *field = internal_strstr(status, kPrefix);
   if (!field)
     return false;
   field += internal_strlen(kPrefix);

diff  --git a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
index 96c617822b5b27..07d9528813b3fe 100644
--- a/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
+++ b/compiler-rt/lib/sanitizer_common/sanitizer_linux.h
@@ -103,6 +103,7 @@ class ThreadLister {
     Ok,
   };
   Result ListThreads(InternalMmapVector<tid_t> *threads);
+  const char *LoadStatus(int tid);
 
  private:
   bool IsAlive(int tid);


        


More information about the llvm-commits mailing list