[llvm] r269405 - [llvm-mc-fuzzer] Use LLVMFuzzerInitialize() instead of a custom main() and FuzzerDriver()
Daniel Sanders via llvm-commits
llvm-commits at lists.llvm.org
Fri May 13 03:23:04 PDT 2016
Author: dsanders
Date: Fri May 13 05:23:04 2016
New Revision: 269405
URL: http://llvm.org/viewvc/llvm-project?rev=269405&view=rev
Log:
[llvm-mc-fuzzer] Use LLVMFuzzerInitialize() instead of a custom main() and FuzzerDriver()
Reviewers: kcc
Subscribers: llvm-commits
Differential Revision: http://reviews.llvm.org/D20201
Modified:
llvm/trunk/tools/llvm-mc-fuzzer/CMakeLists.txt
llvm/trunk/tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp
Modified: llvm/trunk/tools/llvm-mc-fuzzer/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc-fuzzer/CMakeLists.txt?rev=269405&r1=269404&r2=269405&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc-fuzzer/CMakeLists.txt (original)
+++ llvm/trunk/tools/llvm-mc-fuzzer/CMakeLists.txt Fri May 13 05:23:04 2016
@@ -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()
Modified: llvm/trunk/tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp?rev=269405&r1=269404&r2=269405&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp (original)
+++ llvm/trunk/tools/llvm-mc-fuzzer/llvm-mc-fuzzer.cpp Fri May 13 05:23:04 2016
@@ -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 *D
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;
}
More information about the llvm-commits
mailing list