Skip to content

Commit 4caeb62

Browse files
author
Yuanfang Chen
committed
[Fuzzer] Rename ExecuteCommandWithPopen to ExecuteCommandNon-Fushsia target will keep using popen/pclose implementation. OnFuchsia, Two-args version of ExecuteCommand is a simple wrapper of theone-arg version. (Hopefully) Fix D73329 build on Fuchsia.
1 parent 8a56d64 commit 4caeb62

File tree

5 files changed

+41
-16
lines changed

5 files changed

+41
-16
lines changed

compiler-rt/lib/fuzzer/FuzzerDriver.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -316,20 +316,6 @@ static std::string GetDedupTokenFromCmdOutput(const std::string &S) {
316316
return S.substr(Beg, End - Beg);
317317
}
318318

319-
// Return true on success, false otherwise.
320-
static bool ExecuteCommandWithPopen(const Command &Cmd, std::string *CmdOutput) {
321-
FILE *Pipe = OpenProcessPipe(Cmd.toString().c_str(), "r");
322-
if (!Pipe)
323-
return false;
324-
325-
if (CmdOutput) {
326-
char TmpBuffer[128];
327-
while (fgets(TmpBuffer, sizeof(TmpBuffer), Pipe))
328-
CmdOutput->append(TmpBuffer);
329-
}
330-
return CloseProcessPipe(Pipe) == 0;
331-
}
332-
333319
int CleanseCrashInput(const Vector<std::string> &Args,
334320
const FuzzingOptions &Options) {
335321
if (Inputs->size() != 1 || !Flags.exact_artifact_path) {
@@ -417,7 +403,7 @@ int MinimizeCrashInput(const Vector<std::string> &Args,
417403

418404
Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
419405
std::string CmdOutput;
420-
bool Success = ExecuteCommandWithPopen(Cmd, &CmdOutput);
406+
bool Success = ExecuteCommand(Cmd, &CmdOutput);
421407
if (Success) {
422408
Printf("ERROR: the input %s did not crash\n", CurrentFilePath.c_str());
423409
exit(1);
@@ -437,7 +423,7 @@ int MinimizeCrashInput(const Vector<std::string> &Args,
437423
Cmd.addFlag("exact_artifact_path", ArtifactPath);
438424
Printf("CRASH_MIN: executing: %s\n", Cmd.toString().c_str());
439425
CmdOutput.clear();
440-
Success = ExecuteCommandWithPopen(Cmd, &CmdOutput);
426+
Success = ExecuteCommand(Cmd, &CmdOutput);
441427
Printf("%s", CmdOutput.c_str());
442428
if (Success) {
443429
if (Flags.exact_artifact_path) {

compiler-rt/lib/fuzzer/FuzzerUtil.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ unsigned long GetPid();
5757
size_t GetPeakRSSMb();
5858

5959
int ExecuteCommand(const Command &Cmd);
60+
bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput);
6061

62+
// Fuchsia does not have popen/pclose.
6163
FILE *OpenProcessPipe(const char *Command, const char *Mode);
6264
int CloseProcessPipe(FILE *F);
6365

compiler-rt/lib/fuzzer/FuzzerUtilFuchsia.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,16 @@ int ExecuteCommand(const Command &Cmd) {
533533
return Info.return_code;
534534
}
535535

536+
bool ExecuteCommand(const Command &BaseCmd, std::string *CmdOutput) {
537+
auto LogFilePath = TempPath("SimPopenOut", ".txt");
538+
Command Cmd(BaseCmd);
539+
Cmd.setOutputFile(LogFilePath);
540+
int Ret = ExecuteCommand(Cmd);
541+
*CmdOutput = FileToString(LogFilePath);
542+
RemoveFile(LogFilePath);
543+
return Ret == 0;
544+
}
545+
536546
const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt,
537547
size_t PattLen) {
538548
return memmem(Data, DataLen, Patt, PattLen);

compiler-rt/lib/fuzzer/FuzzerUtilPosix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ static void SetSigaction(int signum,
8686
}
8787
}
8888

89+
// Return true on success, false otherwise.
90+
bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
91+
FILE *Pipe = popen(Cmd.toString().c_str(), "r");
92+
if (!Pipe)
93+
return false;
94+
95+
if (CmdOutput) {
96+
char TmpBuffer[128];
97+
while (fgets(TmpBuffer, sizeof(TmpBuffer), Pipe))
98+
CmdOutput->append(TmpBuffer);
99+
}
100+
return pclose(Pipe) == 0;
101+
}
102+
89103
void SetTimer(int Seconds) {
90104
struct itimerval T {
91105
{Seconds, 0}, { Seconds, 0 }

compiler-rt/lib/fuzzer/FuzzerUtilWindows.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,19 @@ int ExecuteCommand(const Command &Cmd) {
161161
return system(CmdLine.c_str());
162162
}
163163

164+
bool ExecuteCommand(const Command &Cmd, std::string *CmdOutput) {
165+
FILE *Pipe = _popen(Cmd.toString().c_str(), "r");
166+
if (!Pipe)
167+
return false;
168+
169+
if (CmdOutput) {
170+
char TmpBuffer[128];
171+
while (fgets(TmpBuffer, sizeof(TmpBuffer), Pipe))
172+
CmdOutput->append(TmpBuffer);
173+
}
174+
return _pclose(Pipe) == 0;
175+
}
176+
164177
const void *SearchMemory(const void *Data, size_t DataLen, const void *Patt,
165178
size_t PattLen) {
166179
// TODO: make this implementation more efficient.

0 commit comments

Comments
 (0)