Skip to content

Commit

Permalink
[llvm-mc-fuzzer] Use LLVMFuzzerInitialize() instead of a custom main(…
Browse files Browse the repository at this point in the history
…) and FuzzerDriver()

Reviewers: kcc

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D20201

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269405 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
dsandersllvm committed May 13, 2016
1 parent d772cfc commit acd1ec7
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tools/llvm-mc-fuzzer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ if( LLVM_USE_SANITIZE_COVERAGE )
add_llvm_tool(llvm-mc-fuzzer
llvm-mc-fuzzer.cpp)
target_link_libraries(llvm-mc-fuzzer
LLVMFuzzerNoMain
LLVMFuzzer
)
endif()
43 changes: 33 additions & 10 deletions tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,10 @@ static cl::list<std::string>
std::string FeaturesStr;

static cl::list<std::string>
FuzzerArgv("fuzzer-args", cl::Positional,
FuzzerArgs("fuzzer-args", cl::Positional,
cl::desc("Options to pass to the fuzzer"), cl::ZeroOrMore,
cl::PositionalEatsArgs);
static std::vector<char *> ModifiedArgv;

int DisassembleOneInput(const uint8_t *Data, size_t Size) {
char AssemblyText[AssemblyTextBufSize];
Expand Down Expand Up @@ -87,7 +88,17 @@ int DisassembleOneInput(const uint8_t *Data, size_t Size) {
return 0;
}

int main(int argc, char **argv) {
int LLVMFuzzerTestOneInput(const uint8_t *Data, size_t Size) {
if (Action == AC_Assemble)
errs() << "error: -assemble is not implemented\n";
else if (Action == AC_Disassemble)
return DisassembleOneInput(Data, Size);

llvm_unreachable("Unknown action");
return 0;
}

int LLVMFuzzerInitialize(int *argc, char ***argv) {
// The command line is unusual compared to other fuzzers due to the need to
// specify the target. Options like -triple, -mcpu, and -mattr work like
// their counterparts in llvm-mc, while -fuzzer-args collects options for the
Expand All @@ -111,11 +122,29 @@ int main(int argc, char **argv) {
// individual instructions that test unique paths. Without this constraint,
// there will be considerable redundancy in the corpus.

char **OriginalArgv = *argv;

LLVMInitializeAllTargetInfos();
LLVMInitializeAllTargetMCs();
LLVMInitializeAllDisassemblers();

cl::ParseCommandLineOptions(argc, argv);
cl::ParseCommandLineOptions(*argc, OriginalArgv);

// Rebuild the argv without the arguments llvm-mc-fuzzer consumed so that
// the driver can parse its arguments.
//
// FuzzerArgs cannot provide the non-const pointer that OriginalArgv needs.
// Re-use the strings from OriginalArgv instead of copying FuzzerArg to a
// non-const buffer to avoid the need to clean up when the fuzzer terminates.
ModifiedArgv.push_back(OriginalArgv[0]);
for (const auto &FuzzerArg : FuzzerArgs) {
for (int i = 1; i < *argc; ++i) {
if (FuzzerArg == OriginalArgv[i])
ModifiedArgv.push_back(OriginalArgv[i]);
}
}
*argc = ModifiedArgv.size();
*argv = ModifiedArgv.data();

// Package up features to be passed to target/subtarget
// We have to pass it via a global since the callback doesn't
Expand All @@ -127,11 +156,5 @@ int main(int argc, char **argv) {
FeaturesStr = Features.getString();
}

if (Action == AC_Assemble)
errs() << "error: -assemble is not implemented\n";
else if (Action == AC_Disassemble)
return fuzzer::FuzzerDriver(argc, argv, DisassembleOneInput);

llvm_unreachable("Unknown action");
return 1;
return 0;
}

0 comments on commit acd1ec7

Please sign in to comment.