Skip to content

Commit 942fefe

Browse files
committed
[NFC][sanitizer] Reopen '/proc/%d/task' instead of seek
NFC because I am not aware of any particular issue from seek, but reopen looks less error prone. Pull Request: #111899
1 parent f0ed31c commit 942fefe

File tree

2 files changed

+13
-22
lines changed

2 files changed

+13
-22
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,21 +1025,19 @@ bool internal_sigismember(__sanitizer_sigset_t *set, int signum) {
10251025

10261026
# if !SANITIZER_NETBSD
10271027
// ThreadLister implementation.
1028-
ThreadLister::ThreadLister(pid_t pid) : pid_(pid), buffer_(4096) {
1029-
char task_directory_path[80];
1030-
internal_snprintf(task_directory_path, sizeof(task_directory_path),
1031-
"/proc/%d/task/", pid);
1032-
descriptor_ = internal_open(task_directory_path, O_RDONLY | O_DIRECTORY);
1033-
if (internal_iserror(descriptor_)) {
1034-
Report("Can't open /proc/%d/task for reading.\n", pid);
1035-
}
1028+
ThreadLister::ThreadLister(pid_t pid) : buffer_(4096) {
1029+
task_path_.AppendF("/proc/%d/task", pid);
1030+
status_path_.AppendF("%s/status", task_path_.data());
10361031
}
10371032

10381033
ThreadLister::Result ThreadLister::ListThreads(
10391034
InternalMmapVector<tid_t> *threads) {
1040-
if (internal_iserror(descriptor_))
1035+
int descriptor = internal_open(task_path_.data(), O_RDONLY | O_DIRECTORY);
1036+
if (internal_iserror(descriptor)) {
1037+
Report("Can't open %s for reading.\n", task_path_.data());
10411038
return Error;
1042-
internal_lseek(descriptor_, 0, SEEK_SET);
1039+
}
1040+
auto acts_cleanup = at_scope_exit([&] { internal_close(descriptor); });
10431041
threads->clear();
10441042

10451043
Result result = Ok;
@@ -1048,11 +1046,11 @@ ThreadLister::Result ThreadLister::ListThreads(
10481046
buffer_.resize(buffer_.capacity());
10491047
CHECK_GE(buffer_.size(), 4096);
10501048
uptr read = internal_getdents(
1051-
descriptor_, (struct linux_dirent *)buffer_.data(), buffer_.size());
1049+
descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
10521050
if (!read)
10531051
return result;
10541052
if (internal_iserror(read)) {
1055-
Report("Can't read directory entries from /proc/%d/task.\n", pid_);
1053+
Report("Can't read directory entries from %s.\n", task_path_.data());
10561054
return Error;
10571055
}
10581056

@@ -1093,9 +1091,7 @@ ThreadLister::Result ThreadLister::ListThreads(
10931091
bool ThreadLister::IsAlive(int tid) {
10941092
// /proc/%d/task/%d/status uses same call to detect alive threads as
10951093
// proc_task_readdir. See task_state implementation in Linux.
1096-
char path[80];
1097-
internal_snprintf(path, sizeof(path), "/proc/%d/task/%d/status", pid_, tid);
1098-
if (!ReadFileToVector(path, &buffer_) || buffer_.empty())
1094+
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
10991095
return false;
11001096
buffer_.push_back(0);
11011097
static const char kPrefix[] = "\nPPid:";
@@ -1106,10 +1102,6 @@ bool ThreadLister::IsAlive(int tid) {
11061102
return (int)internal_atoll(field) != 0;
11071103
}
11081104

1109-
ThreadLister::~ThreadLister() {
1110-
if (!internal_iserror(descriptor_))
1111-
internal_close(descriptor_);
1112-
}
11131105
# endif
11141106

11151107
# if SANITIZER_WORDSIZE == 32

compiler-rt/lib/sanitizer_common/sanitizer_linux.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,6 @@ uptr internal_clone(int (*fn)(void *), void *child_stack, int flags, void *arg);
9797
class ThreadLister {
9898
public:
9999
explicit ThreadLister(pid_t pid);
100-
~ThreadLister();
101100
enum Result {
102101
Error,
103102
Incomplete,
@@ -108,8 +107,8 @@ class ThreadLister {
108107
private:
109108
bool IsAlive(int tid);
110109

111-
pid_t pid_;
112-
int descriptor_ = -1;
110+
InternalScopedString task_path_;
111+
InternalScopedString status_path_;
113112
InternalMmapVector<char> buffer_;
114113
};
115114

0 commit comments

Comments
 (0)