Skip to content

Commit 5deadc6

Browse files
authored
[NFC][sanitizer] Extract LoadStatus (#111909)
For #111901
1 parent c99b365 commit 5deadc6

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1042,8 +1042,6 @@ ThreadLister::Result ThreadLister::ListThreads(
10421042

10431043
Result result = Ok;
10441044
for (bool first_read = true;; first_read = false) {
1045-
// Resize to max capacity if it was downsized by IsAlive.
1046-
buffer_.resize(buffer_.capacity());
10471045
CHECK_GE(buffer_.size(), 4096);
10481046
uptr read = internal_getdents(
10491047
descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
@@ -1088,14 +1086,25 @@ ThreadLister::Result ThreadLister::ListThreads(
10881086
}
10891087
}
10901088

1089+
const char *ThreadLister::LoadStatus(int tid) {
1090+
auto cleanup = at_scope_exit([&] {
1091+
// Resize back to capacity if it is downsized by `ReadFileToVector`.
1092+
buffer_.resize(buffer_.capacity());
1093+
});
1094+
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
1095+
return nullptr;
1096+
buffer_.push_back('\0');
1097+
return buffer_.data();
1098+
}
1099+
10911100
bool ThreadLister::IsAlive(int tid) {
10921101
// /proc/%d/task/%d/status uses same call to detect alive threads as
10931102
// proc_task_readdir. See task_state implementation in Linux.
1094-
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
1095-
return false;
1096-
buffer_.push_back(0);
10971103
static const char kPrefix[] = "\nPPid:";
1098-
const char *field = internal_strstr(buffer_.data(), kPrefix);
1104+
const char *status = LoadStatus(tid);
1105+
if (!status)
1106+
return false;
1107+
const char *field = internal_strstr(status, kPrefix);
10991108
if (!field)
11001109
return false;
11011110
field += internal_strlen(kPrefix);

compiler-rt/lib/sanitizer_common/sanitizer_linux.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ThreadLister {
103103
Ok,
104104
};
105105
Result ListThreads(InternalMmapVector<tid_t> *threads);
106+
const char *LoadStatus(int tid);
106107

107108
private:
108109
bool IsAlive(int tid);

0 commit comments

Comments
 (0)