[llvm-mc-fuzzer] Use LLVMFuzzerInitialize() instead of a custom main() and FuzzerDriver()

Reviewers: kcc

Subscribers: llvm-commits

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

llvm-svn: 269405
This commit is contained in:
Daniel Sanders 2016-05-13 10:23:04 +00:00
parent cdb7dab06c
commit 031da59423
2 changed files with 34 additions and 11 deletions

View File

@ -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()

View File

@ -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];
@ -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
@ -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
@ -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;
}