llvm.org GIT mirror llvm / 1404e4c
[FuzzMutate] NFC. Move parseModule and writeModule from llvm-isel-fuzzer into FuzzMutate. This is to be able to reuse them in the llvm-opt-fuzzer. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@318407 91177308-0d34-0410-b5e6-96231b3b80d8 Igor Laevsky 1 year, 9 months ago
4 changed file(s) with 54 addition(s) and 63 deletion(s). Raw diff Collapse all Expand all
1515 #define LLVM_FUZZMUTATE_FUZZER_CLI_H
1616
1717 #include "llvm/ADT/StringRef.h"
18 #include "llvm/IR/LLVMContext.h"
1819 #include "llvm/Support/DataTypes.h"
1920
2021 namespace llvm {
4950 int runFuzzerOnInputs(int ArgC, char *ArgV[], FuzzerTestFun TestOne,
5051 FuzzerInitFun Init = [](int *, char ***) { return 0; });
5152
53 /// Fuzzer friendly interface for the llvm bitcode parser.
54 ///
55 /// \param Data Bitcode we are going to parse
56 /// \param Size Size of the 'Data' in bytes
57 /// \return New module or nullptr in case of error
58 std::unique_ptr parseModule(const uint8_t *Data, size_t Size,
59 LLVMContext &Context);
60
61 /// Fuzzer friendly interface for the llvm bitcode printer.
62 ///
63 /// \param M Module to print
64 /// \param Dest Location to store serialized module
65 /// \param MaxSize Size of the destination buffer
66 /// \return Number of bytes that were written. When module size exceeds MaxSize
67 /// returns 0 and leaves Dest unchanged.
68 size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize);
69
5270 } // end llvm namespace
5371
5472 #endif // LLVM_FUZZMUTATE_FUZZER_CLI_H
88
99 #include "llvm/FuzzMutate/FuzzerCLI.h"
1010 #include "llvm/ADT/Triple.h"
11 #include "llvm/Bitcode/BitcodeReader.h"
12 #include "llvm/Bitcode/BitcodeWriter.h"
13 #include "llvm/IR/LLVMContext.h"
1114 #include "llvm/Support/CommandLine.h"
1215 #include "llvm/Support/Compiler.h"
1316 #include "llvm/Support/Error.h"
1417 #include "llvm/Support/MemoryBuffer.h"
18 #include "llvm/Support/SourceMgr.h"
1519 #include "llvm/Support/raw_ostream.h"
1620
1721 using namespace llvm;
130134 }
131135 return 0;
132136 }
137
138 std::unique_ptr llvm::parseModule(
139 const uint8_t *Data, size_t Size, LLVMContext &Context) {
140
141 if (Size <= 1)
142 // We get bogus data given an empty corpus - just create a new module.
143 return llvm::make_unique("M", Context);
144
145 auto Buffer = MemoryBuffer::getMemBuffer(
146 StringRef(reinterpret_cast(Data), Size), "Fuzzer input",
147 /*RequiresNullTerminator=*/false);
148
149 SMDiagnostic Err;
150 auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
151 if (Error E = M.takeError()) {
152 errs() << toString(std::move(E)) << "\n";
153 return nullptr;
154 }
155 return std::move(M.get());
156 }
157
158 size_t llvm::writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
159 std::string Buf;
160 {
161 raw_string_ostream OS(Buf);
162 WriteBitcodeToFile(&M, OS);
163 }
164 if (Buf.size() > MaxSize)
165 return 0;
166 memcpy(Dest, Buf.data(), Buf.size());
167 return Buf.size();
168 }
5050
5151 static std::unique_ptr TM;
5252 static std::unique_ptr Mutator;
53
54 static std::unique_ptr parseModule(const uint8_t *Data, size_t Size,
55 LLVMContext &Context) {
56 auto Buffer = MemoryBuffer::getMemBuffer(
57 StringRef(reinterpret_cast(Data), Size), "Fuzzer input",
58 /*RequiresNullTerminator=*/false);
59
60 SMDiagnostic Err;
61 auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
62 if (Error E = M.takeError()) {
63 errs() << toString(std::move(E)) << "\n";
64 return nullptr;
65 }
66 return std::move(M.get());
67 }
68
69 static size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
70 std::string Buf;
71 {
72 raw_string_ostream OS(Buf);
73 WriteBitcodeToFile(&M, OS);
74 }
75 if (Buf.size() > MaxSize)
76 return 0;
77 memcpy(Dest, Buf.data(), Buf.size());
78 return Buf.size();
79 }
8053
8154 std::unique_ptr createISelMutator() {
8255 std::vector Types{
3636 static std::unique_ptr Mutator;
3737 static std::unique_ptr TM;
3838
39 // This function is mostly copied from the llvm-isel-fuzzer.
40 // TODO: Move this into FuzzMutate library and reuse.
41 static std::unique_ptr parseModule(const uint8_t *Data, size_t Size,
42 LLVMContext &Context) {
43
44 if (Size <= 1)
45 // We get bogus data given an empty corpus - just create a new module.
46 return llvm::make_unique("M", Context);
47
48 auto Buffer = MemoryBuffer::getMemBuffer(
49 StringRef(reinterpret_cast(Data), Size), "Fuzzer input",
50 /*RequiresNullTerminator=*/false);
51
52 SMDiagnostic Err;
53 auto M = parseBitcodeFile(Buffer->getMemBufferRef(), Context);
54 if (Error E = M.takeError()) {
55 errs() << toString(std::move(E)) << "\n";
56 return nullptr;
57 }
58 return std::move(M.get());
59 }
60
61 // This function is copied from the llvm-isel-fuzzer.
62 // TODO: Move this into FuzzMutate library and reuse.
63 static size_t writeModule(const Module &M, uint8_t *Dest, size_t MaxSize) {
64 std::string Buf;
65 {
66 raw_string_ostream OS(Buf);
67 WriteBitcodeToFile(&M, OS);
68 }
69 if (Buf.size() > MaxSize)
70 return 0;
71 memcpy(Dest, Buf.data(), Buf.size());
72 return Buf.size();
73 }
74
7539 std::unique_ptr createOptMutator() {
7640 std::vector Types{
7741 Type::getInt1Ty, Type::getInt8Ty, Type::getInt16Ty, Type::getInt32Ty,