This is an archive of the discontinued LLVM Phabricator instance.

[LibFuzzer] Fix NumberOfCpuCores() for Mac OSX
ClosedPublic

Authored by delcypher on May 18 2016, 8:35 PM.

Details

Summary

[LibFuzzer] Fix `NumberOfCpuCores()` on Mac OSX.

The nprocs command does not exist under Mac OSX so use
sysctl instead on that platform.

Whilst I'm here

  • Use pclose() instead of fclose() which the popen() documentation says should be used.
  • Check for errors that were previously unhandled.

Diff Detail

Event Timeline

delcypher updated this revision to Diff 57730.May 18 2016, 8:35 PM
delcypher retitled this revision from to [LibFuzzer] Fix NumberOfCpuCores() for Mac OSX.
delcypher updated this object.
delcypher added reviewers: kcc, aizatsky, kubamracek.
kcc edited edge metadata.May 18 2016, 10:20 PM

Grrr, those pesky #ifdefs. This code could be as easily written in C++ instead of the C preprocessor. (well, almost)

#define in one place in FuzzerInternal.h things like LIBFUZZER_LINUX and LIBFUZZER_MAC (0 or 1).
Then do
if(LIBFUZZER_LINUX) .. else if (LIBFUZZER_MAC) ... else { assert(0 && "text")}

delcypher updated this revision to Diff 57865.May 19 2016, 3:17 PM
delcypher updated this object.
delcypher edited edge metadata.
  • Remove use of ifdefs
  • Add some more error checking.

@kcc I've added a bunch of error checking in this patch too and also replace fclose() with pclose(). If you don't like these changes I can remove this so this patch only adds Mac OSX support and nothing else.

delcypher updated this object.May 19 2016, 3:19 PM
delcypher updated this object.
kcc added inline comments.May 19 2016, 3:22 PM
lib/Fuzzer/FuzzerUtil.cpp
116

move this to where you initialize it.

128

fold this into the next warning and assume #CPUs == 1 instead of failing

133

In most of similar cases we do
Printf("WARNING:

139

This code uses different coding style (ReturnCode or PcloseRet)

delcypher updated this revision to Diff 57870.May 19 2016, 3:42 PM

Address @kcc 's comments.

@kcc Thanks for the fast review! I've tried to address your comments in the new version of the patch.

delcypher marked 4 inline comments as done.May 19 2016, 3:43 PM
kcc accepted this revision.May 19 2016, 3:55 PM
kcc edited edge metadata.

A few nits to make the code shorter.
Ok to commit with those fixes.

lib/Fuzzer/FuzzerUtil.cpp
116

Naming: CmdLIne

126

int N = 1;

135

if (pclose(F))

Printf("...")
140

with the default N = 1 you can just remove this.

This revision is now accepted and ready to land.May 19 2016, 3:55 PM
delcypher added inline comments.May 19 2016, 4:08 PM
lib/Fuzzer/FuzzerUtil.cpp
140

If the command returns the string "0" then N will still get set to 0 regardless of the default which means the assert is still needed.

delcypher updated this revision to Diff 57875.May 19 2016, 4:11 PM
delcypher edited edge metadata.

Address @kcc 's comments apart from assert before return.

delcypher marked 3 inline comments as done.May 19 2016, 4:11 PM
kcc requested changes to this revision.May 19 2016, 4:15 PM
kcc edited edge metadata.

LGTM

lib/Fuzzer/FuzzerUtil.cpp
140

But then you probably don't want the assert, just return 1.

This revision now requires changes to proceed.May 19 2016, 4:15 PM
delcypher updated this revision to Diff 57881.May 19 2016, 4:51 PM
delcypher edited edge metadata.
  • Replace assert with `if ()`
  • Remove stray comma in Printf() which lead to a crash
delcypher marked 3 inline comments as done.May 19 2016, 4:51 PM

@kcc is this change what you meant?

kcc accepted this revision.May 19 2016, 4:57 PM
kcc edited edge metadata.

LGTM, although this many warnings for this code might be an overkill

This revision is now accepted and ready to land.May 19 2016, 4:57 PM

@kcc

LGTM, although this many warnings for this code might be an overkill

Probably. We can always remove these warnings at a later date.

This revision was automatically updated to reflect the committed changes.