Index: tools/llvm-mc-fuzzer/CMakeLists.txt =================================================================== --- tools/llvm-mc-fuzzer/CMakeLists.txt +++ tools/llvm-mc-fuzzer/CMakeLists.txt @@ -13,6 +13,6 @@ add_llvm_tool(llvm-mc-fuzzer llvm-mc-fuzzer.cpp) target_link_libraries(llvm-mc-fuzzer - LLVMFuzzerNoMain + LLVMFuzzer ) endif() Index: tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp =================================================================== --- tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp +++ tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp @@ -57,9 +57,10 @@ std::string FeaturesStr; static cl::list - 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 ModifiedArgv; int DisassembleOneInput(const uint8_t *Data, size_t Size) { char AssemblyText[AssemblyTextBufSize]; @@ -87,7 +88,17 @@ 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 @@ // 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 @@ 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; }