[PATCH] D20201: [llvm-mc-fuzzer] Use LLVMFuzzerInitialize() instead of a custom main() and FuzzerDriver()
Daniel Sanders via llvm-commits
llvm-commits at lists.llvm.org
Thu May 12 03:41:05 PDT 2016
dsanders created this revision.
dsanders added a reviewer: kcc.
dsanders added a subscriber: llvm-commits.
http://reviews.llvm.org/D20201
Files:
tools/llvm-mc-fuzzer/CMakeLists.txt
tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp
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<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 @@
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;
}
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()
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D20201.57015.patch
Type: text/x-patch
Size: 3240 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160512/f4820297/attachment.bin>
More information about the llvm-commits
mailing list