Skip to content

Commit f6aa6f6

Browse files
lszekerescopybara-github
authored andcommitted
Simplify some log statements with FUZZTEST_PCHECK.
PiperOrigin-RevId: 797455698
1 parent b742bb0 commit f6aa6f6

File tree

5 files changed

+45
-62
lines changed

5 files changed

+45
-62
lines changed

centipede/rusage_stats.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,9 +366,9 @@ RUsageTiming RUsageTiming::Snapshot( //
366366
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PID, scope.info().pid()};
367367
struct kinfo_proc info = {};
368368
size_t size = sizeof(info);
369-
FUZZTEST_CHECK(
369+
FUZZTEST_PCHECK(
370370
sysctl(mib, sizeof(mib) / sizeof(mib[0]), &info, &size, NULL, 0) == 0)
371-
<< "Error getting process information: " << strerror(errno);
371+
<< "Error getting process information";
372372
cpu_utilization = info.kp_proc.p_pctcpu;
373373
#else // __APPLE__
374374
// Get the CPU utilization in 1/1024th units of the maximum from

fuzztest/fuzztest_macros.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,8 +98,7 @@ std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
9898

9999
std::ifstream stream(entry.path());
100100
FUZZTEST_PRECONDITION(stream.good())
101-
<< "Cannot read input file: " << entry.path().string() << ": "
102-
<< strerror(errno);
101+
<< "Cannot read input file: " << entry.path().string();
103102

104103
std::stringstream buffer;
105104
buffer << stream.rdbuf();
@@ -151,7 +150,7 @@ std::vector<std::string> ReadDictionaryFromFile(
151150
const std::filesystem::path fs_path(dictionary_file);
152151
std::ifstream stream(fs_path);
153152
FUZZTEST_PRECONDITION(stream.good())
154-
<< "Error reading " << fs_path.string() << ": " << strerror(errno);
153+
<< "Error reading dictionary file: " << fs_path.string();
155154
std::stringstream buffer;
156155
buffer << stream.rdbuf();
157156
absl::StatusOr<std::vector<std::string>> parsed_entries =

fuzztest/internal/centipede_adaptor.cc

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -361,9 +361,8 @@ void InstallCentipedeTerminationHandler() {
361361
// needs to be properly handled.
362362
new_sigact.sa_flags = SA_ONSTACK;
363363

364-
FUZZTEST_CHECK(sigaction(signum, &new_sigact, nullptr) == 0)
365-
<< "Error installing signal handler: %s\n"
366-
<< strerror(errno);
364+
FUZZTEST_PCHECK(sigaction(signum, &new_sigact, nullptr) == 0)
365+
<< "Error installing signal handler";
367366
}
368367
return true;
369368
}();
@@ -581,26 +580,25 @@ class CentipedeAdaptorRunnerCallbacks
581580
const fuzztest::internal::ExecutionMetadata* metadata,
582581
TablesOfRecentCompares& cmp_tables) {
583582
if (metadata == nullptr) return;
584-
metadata->ForEachCmpEntry(
585-
[&cmp_tables](fuzztest::internal::ByteSpan a,
586-
fuzztest::internal::ByteSpan b) {
587-
FUZZTEST_CHECK(a.size() == b.size())
588-
<< "cmp operands must have the same size";
589-
const size_t size = a.size();
590-
if (size < kMinCmpEntrySize) return;
591-
if (size > kMaxCmpEntrySize) return;
592-
if (size == 2) {
593-
InsertCmpEntryIntoIntegerDictionary<uint16_t>(a.data(), b.data(),
594-
cmp_tables);
595-
} else if (size == 4) {
596-
InsertCmpEntryIntoIntegerDictionary<uint32_t>(a.data(), b.data(),
597-
cmp_tables);
598-
} else if (size == 8) {
599-
InsertCmpEntryIntoIntegerDictionary<uint64_t>(a.data(), b.data(),
600-
cmp_tables);
601-
}
602-
cmp_tables.GetMutable<0>().Insert(a.data(), b.data(), size);
603-
});
583+
metadata->ForEachCmpEntry([&cmp_tables](fuzztest::internal::ByteSpan a,
584+
fuzztest::internal::ByteSpan b) {
585+
FUZZTEST_CHECK(a.size() == b.size())
586+
<< "cmp operands must have the same size";
587+
const size_t size = a.size();
588+
if (size < kMinCmpEntrySize) return;
589+
if (size > kMaxCmpEntrySize) return;
590+
if (size == 2) {
591+
InsertCmpEntryIntoIntegerDictionary<uint16_t>(a.data(), b.data(),
592+
cmp_tables);
593+
} else if (size == 4) {
594+
InsertCmpEntryIntoIntegerDictionary<uint32_t>(a.data(), b.data(),
595+
cmp_tables);
596+
} else if (size == 8) {
597+
InsertCmpEntryIntoIntegerDictionary<uint64_t>(a.data(), b.data(),
598+
cmp_tables);
599+
}
600+
cmp_tables.GetMutable<0>().Insert(a.data(), b.data(), size);
601+
});
604602
}
605603

606604
// Size limits on the cmp entries to be used in mutation.

fuzztest/internal/logging.cc

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,9 @@ int GetStdoutFdDup() {
101101

102102
void Silence(int fd) {
103103
FILE* tmp = fopen("/dev/null", "w");
104-
FUZZTEST_CHECK(tmp) << "fopen() error: " << strerror(errno);
105-
FUZZTEST_CHECK(dup2(fileno(tmp), fd) != -1) << "dup2() error: ",
106-
strerror(errno);
107-
FUZZTEST_CHECK(fclose(tmp) == 0) << "fclose() error: " << strerror(errno);
104+
FUZZTEST_PCHECK(tmp) << "fopen() error";
105+
FUZZTEST_PCHECK(dup2(fileno(tmp), fd) != -1) << "dup2() error";
106+
FUZZTEST_PCHECK(fclose(tmp) == 0) << "fclose() error";
108107
}
109108

110109
// Only accepts 1 or 2 (stdout or stderr).
@@ -118,13 +117,11 @@ void DupAndSilence(int fd) {
118117
if (fd == STDOUT_FILENO) {
119118
FUZZTEST_CHECK(GetStdoutFdDup() != -1) << "GetStdoutFdDup() fails.";
120119
stdout_file_ = fdopen(GetStdoutFdDup(), "w");
121-
FUZZTEST_CHECK(stdout_file_)
122-
<< "fdopen(GetStdoutFdDup()) error:" << strerror(errno);
120+
FUZZTEST_PCHECK(stdout_file_) << "fdopen(GetStdoutFdDup()) error";
123121
} else {
124122
FUZZTEST_CHECK(GetStderrFdDup() != -1) << "GetStderrFdDup() fails.";
125123
auto file = fdopen(GetStderrFdDup(), "w");
126-
FUZZTEST_CHECK(file) << "fdopen(GetStderrFdDup()) error:"
127-
<< strerror(errno);
124+
FUZZTEST_PCHECK(file) << "fdopen(GetStderrFdDup()) error";
128125
absl::MutexLock lock(&stderr_file_guard_);
129126
stderr_file_ = file;
130127
}
@@ -147,20 +144,13 @@ void RestoreTargetStdoutAndStderr() {
147144
stderr_file_ = stderr;
148145
stderr_file_guard_.Unlock();
149146
FUZZTEST_CHECK(silenced_stderr != stderr)
150-
<< "Error, calling RestoreStderr without calling"
151-
"DupandSilenceStderr first.";
152-
FUZZTEST_CHECK(dup2(fileno(silenced_stderr), STDERR_FILENO) != -1)
153-
<< "dup2 error:" << strerror(errno);
154-
FUZZTEST_CHECK(fclose(silenced_stderr) == 0)
155-
<< "close() error:" << strerror(errno);
156-
147+
<< "RestoreStderr was called without calling DupandSilenceStderr first.";
148+
FUZZTEST_PCHECK(dup2(fileno(silenced_stderr), STDERR_FILENO) != -1);
149+
FUZZTEST_PCHECK(fclose(silenced_stderr) == 0);
157150
FUZZTEST_CHECK(stdout_file_ != stdout)
158-
<< "Error, calling RestoreStdout without calling"
159-
"DupandSilenceStdout first.";
160-
FUZZTEST_CHECK(dup2(fileno(stdout_file_), STDOUT_FILENO) != -1)
161-
<< "dup2() error:" << strerror(errno);
162-
FUZZTEST_CHECK(fclose(stdout_file_) == 0)
163-
<< "close() error:" << strerror(errno);
151+
<< "RestoreStdout was called without calling DupandSilenceStdout first.";
152+
FUZZTEST_PCHECK(dup2(fileno(stdout_file_), STDOUT_FILENO) != -1);
153+
FUZZTEST_PCHECK(fclose(stdout_file_) == 0);
164154
stdout_file_ = stdout;
165155
}
166156

fuzztest/internal/subprocess.cc

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,25 @@ class SubProcess {
108108
void SubProcess::CreatePipes() {
109109
for (int channel : {kStdOutIdx, kStdErrIdx}) {
110110
int pipe_fds[2];
111-
FUZZTEST_CHECK(pipe(pipe_fds) == 0) << "Cannot create pipe: ",
112-
strerror(errno);
111+
FUZZTEST_PCHECK(pipe(pipe_fds) == 0) << "Cannot create pipe";
113112

114113
parent_pipe_[channel] = pipe_fds[0];
115114
child_pipe_[channel] = pipe_fds[1];
116115

117-
FUZZTEST_CHECK(fcntl(parent_pipe_[channel], F_SETFL, O_NONBLOCK) != -1)
118-
<< "Cannot make pipe non-blocking: " << strerror(errno);
116+
FUZZTEST_PCHECK(fcntl(parent_pipe_[channel], F_SETFL, O_NONBLOCK) != -1)
117+
<< "Cannot make pipe non-blocking";
119118
}
120119
}
121120

122121
void SubProcess::CloseChildPipes() {
123122
for (int channel : {kStdOutIdx, kStdErrIdx}) {
124-
FUZZTEST_CHECK(close(child_pipe_[channel]) != -1)
125-
<< "Cannot close pipe: " << strerror(errno);
123+
FUZZTEST_PCHECK(close(child_pipe_[channel]) != -1) << "Cannot close pipe";
126124
}
127125
}
128126

129127
void SubProcess::CloseParentPipes() {
130128
for (int channel : {kStdOutIdx, kStdErrIdx}) {
131-
FUZZTEST_CHECK(close(parent_pipe_[channel]) != -1)
132-
<< "Cannot close pipe: " << strerror(errno);
129+
FUZZTEST_PCHECK(close(parent_pipe_[channel]) != -1) << "Cannot close pipe";
133130
}
134131
}
135132

@@ -228,9 +225,9 @@ void SubProcess::ReadChildOutput(
228225
while (fd_remain > 0) {
229226
int ret = poll(pfd, fd_count, -1);
230227
if ((ret == -1) && !ShouldRetry(errno)) {
231-
FUZZTEST_LOG(FATAL) << "Cannot poll(): " << strerror(errno);
228+
FUZZTEST_PLOG(FATAL) << "Cannot poll()";
232229
} else if (ret == 0) {
233-
FUZZTEST_LOG(FATAL) << "Impossible timeout: " << strerror(errno);
230+
FUZZTEST_PLOG(FATAL) << "Impossible timeout";
234231
} else if (ret > 0) {
235232
for (int channel : {kStdOutIdx, kStdErrIdx}) {
236233
// According to the poll() spec, use -1 for ignored entries.
@@ -267,7 +264,7 @@ int Wait(pid_t pid) {
267264
} else if (ret == pid && (WIFEXITED(status) || WIFSIGNALED(status))) {
268265
return status;
269266
} else {
270-
FUZZTEST_LOG(FATAL) << "wait() error: " << strerror(errno);
267+
FUZZTEST_PLOG(FATAL) << "wait() error";
271268
}
272269
}
273270
}
@@ -284,8 +281,7 @@ int WaitWithStopChecker(pid_t pid, absl::FunctionRef<bool()> should_stop) {
284281
continue;
285282
} else if (ret == 0) { // Still running.
286283
if (should_stop()) {
287-
FUZZTEST_CHECK(kill(pid, SIGTERM) == 0)
288-
<< "Cannot kill(): " << strerror(errno);
284+
FUZZTEST_PCHECK(kill(pid, SIGTERM) == 0) << "Cannot kill()";
289285
return Wait(pid);
290286
} else {
291287
absl::SleepFor(sleep_duration);

0 commit comments

Comments
 (0)