Skip to content

Commit 877288b

Browse files
committed
[𝘀𝗽𝗿] initial version
Created using spr 1.3.4
1 parent 07892aa commit 877288b

File tree

2 files changed

+15
-22
lines changed

2 files changed

+15
-22
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,21 +1025,21 @@ 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([&] {
1041+
internal_close(descriptor);
1042+
});
10431043
threads->clear();
10441044

10451045
Result result = Ok;
@@ -1048,11 +1048,11 @@ ThreadLister::Result ThreadLister::ListThreads(
10481048
buffer_.resize(buffer_.capacity());
10491049
CHECK_GE(buffer_.size(), 4096);
10501050
uptr read = internal_getdents(
1051-
descriptor_, (struct linux_dirent *)buffer_.data(), buffer_.size());
1051+
descriptor, (struct linux_dirent *)buffer_.data(), buffer_.size());
10521052
if (!read)
10531053
return result;
10541054
if (internal_iserror(read)) {
1055-
Report("Can't read directory entries from /proc/%d/task.\n", pid_);
1055+
Report("Can't read directory entries from %s.\n", task_path_.data());
10561056
return Error;
10571057
}
10581058

@@ -1093,9 +1093,7 @@ ThreadLister::Result ThreadLister::ListThreads(
10931093
bool ThreadLister::IsAlive(int tid) {
10941094
// /proc/%d/task/%d/status uses same call to detect alive threads as
10951095
// 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())
1096+
if (!ReadFileToVector(status_path_.data(), &buffer_) || buffer_.empty())
10991097
return false;
11001098
buffer_.push_back(0);
11011099
static const char kPrefix[] = "\nPPid:";
@@ -1106,10 +1104,6 @@ bool ThreadLister::IsAlive(int tid) {
11061104
return (int)internal_atoll(field) != 0;
11071105
}
11081106

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

11151109
# 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)