OLD | NEW |
1 //===--- Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp - Bitcode Munger -----===// | 1 //===--- Bitcode/NaCl/TestUtils/NaClBitcodeMunge.cpp - Bitcode Munger -----===// |
2 // | 2 // |
3 // The LLVM Compiler Infrastructure | 3 // The LLVM Compiler Infrastructure |
4 // | 4 // |
5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 // | 9 // |
10 // Bitcode writer/munger implementation for testing. | 10 // Bitcode writer/munger implementation for testing. |
(...skipping 11 matching lines...) Expand all Loading... |
22 #include "llvm/Support/ErrorHandling.h" | 22 #include "llvm/Support/ErrorHandling.h" |
23 #include "llvm/Support/MemoryBuffer.h" | 23 #include "llvm/Support/MemoryBuffer.h" |
24 | 24 |
25 #include <memory> | 25 #include <memory> |
26 | 26 |
27 using namespace llvm; | 27 using namespace llvm; |
28 | 28 |
29 // For debugging. When true, shows each test being run. | 29 // For debugging. When true, shows each test being run. |
30 static bool TraceTestRuns = false; | 30 static bool TraceTestRuns = false; |
31 | 31 |
32 void NaClBitcodeMunger::setupTest( | 32 bool NaClBitcodeMunger::setupTest( |
33 const char *TestName, const uint64_t Munges[], size_t MungesSize, | 33 const char *TestName, const uint64_t Munges[], size_t MungesSize, |
34 bool AddHeader) { | 34 bool AddHeader) { |
35 assert(DumpStream == nullptr && "Test run with DumpStream already defined"); | 35 assert(DumpStream == nullptr && "Test run with DumpStream already defined"); |
36 assert(MungedInput.get() == nullptr | 36 assert(MungedInput.get() == nullptr |
37 && "Test run with MungedInput already defined"); | 37 && "Test run with MungedInput already defined"); |
38 FoundErrors = false; | 38 FoundErrors = false; |
39 DumpResults.clear(); // Throw away any previous results. | 39 DumpResults.clear(); // Throw away any previous results. |
40 std::string DumpBuffer; | 40 std::string DumpBuffer; |
41 DumpStream = new raw_string_ostream(DumpResults); | 41 DumpStream = new raw_string_ostream(DumpResults); |
42 MungedInputBuffer.clear(); | 42 MungedInputBuffer.clear(); |
43 | 43 |
44 if (TraceTestRuns) { | 44 if (TraceTestRuns) { |
45 errs() << "*** Run test: " << TestName << "\n"; | 45 errs() << "*** Run test: " << TestName << "\n"; |
46 } | 46 } |
47 | 47 |
48 MungedBitcode.munge(Munges, MungesSize, RecordTerminator); | 48 MungedBitcode.munge(Munges, MungesSize, RecordTerminator); |
49 WriteFlags.setErrStream(getDumpStream()); | 49 WriteFlags.setErrStream(getDumpStream()); |
50 NaClMungedBitcode::WriteResults Results = | 50 NaClMungedBitcode::WriteResults Results = |
51 MungedBitcode.writeMaybeRepair(MungedInputBuffer, AddHeader, WriteFlags); | 51 MungedBitcode.writeMaybeRepair(MungedInputBuffer, AddHeader, WriteFlags); |
52 if (Results.NumErrors != 0 | 52 if (Results.NumErrors != 0 |
53 && !(WriteFlags.getTryToRecover() | 53 && !(WriteFlags.getTryToRecover() |
54 && Results.NumRepairs == Results.NumErrors) | 54 && Results.NumRepairs == Results.NumErrors) |
55 && !(WriteFlags.getWriteBadAbbrevIndex() | 55 && !(WriteFlags.getWriteBadAbbrevIndex() |
56 && Results.WroteBadAbbrevIndex && Results.NumErrors == 1)) | 56 && Results.WroteBadAbbrevIndex && Results.NumErrors == 1)) { |
57 report_fatal_error("Unable to generate bitcode file due to write errors"); | 57 Error() << "Unable to generate bitcode file due to write errors\n"; |
| 58 return false; |
| 59 } |
58 | 60 |
59 // Add null terminator, so that we meet the requirements of the | 61 // Add null terminator, so that we meet the requirements of the |
60 // MemoryBuffer API. | 62 // MemoryBuffer API. |
61 MungedInputBuffer.push_back('\0'); | 63 MungedInputBuffer.push_back('\0'); |
62 | 64 |
63 MungedInput = MemoryBuffer::getMemBuffer( | 65 MungedInput = MemoryBuffer::getMemBuffer( |
64 StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1), | 66 StringRef(MungedInputBuffer.data(), MungedInputBuffer.size()-1), |
65 TestName); | 67 TestName); |
| 68 return true; |
66 } | 69 } |
67 | 70 |
68 void NaClBitcodeMunger::cleanupTest() { | 71 bool NaClBitcodeMunger::cleanupTest() { |
69 RunAsDeathTest = false; | 72 RunAsDeathTest = false; |
70 WriteFlags.reset(); | 73 WriteFlags.reset(); |
71 MungedBitcode.removeEdits(); | 74 MungedBitcode.removeEdits(); |
72 MungedInput.reset(); | 75 MungedInput.reset(); |
73 assert(DumpStream && "Dump stream removed before cleanup!"); | 76 assert(DumpStream && "Dump stream removed before cleanup!"); |
74 DumpStream->flush(); | 77 DumpStream->flush(); |
75 delete DumpStream; | 78 delete DumpStream; |
76 DumpStream = nullptr; | 79 DumpStream = nullptr; |
| 80 return !FoundErrors; |
77 } | 81 } |
78 | 82 |
79 // Return the next line of input (including eoln), starting from | 83 // Return the next line of input (including eoln), starting from |
80 // Pos. Then increment Pos past the end of that line. | 84 // Pos. Then increment Pos past the end of that line. |
81 static std::string getLine(const std::string &Input, size_t &Pos) { | 85 static std::string getLine(const std::string &Input, size_t &Pos) { |
82 std::string Line; | 86 std::string Line; |
83 if (Pos >= Input.size()) { | 87 if (Pos >= Input.size()) { |
84 Pos = std::string::npos; | 88 Pos = std::string::npos; |
85 return Line; | 89 return Line; |
86 } | 90 } |
(...skipping 16 matching lines...) Expand all Loading... |
103 std::string Line = getLine(DumpResults, LastLinePos); | 107 std::string Line = getLine(DumpResults, LastLinePos); |
104 if (LastLinePos == std::string::npos) break; | 108 if (LastLinePos == std::string::npos) break; |
105 size_t Pos = Line.find(Substring); | 109 size_t Pos = Line.find(Substring); |
106 if (Pos != std::string::npos && (!MustBePrefix || Pos == 0)) { | 110 if (Pos != std::string::npos && (!MustBePrefix || Pos == 0)) { |
107 Messages.append(Line); | 111 Messages.append(Line); |
108 } | 112 } |
109 } | 113 } |
110 return Messages; | 114 return Messages; |
111 } | 115 } |
112 | 116 |
| 117 bool NaClWriteMunger::runTest(const char* Name, const uint64_t Munges[], |
| 118 size_t MungesSize) { |
| 119 bool AddHeader = true; |
| 120 if (!setupTest(Name, Munges, MungesSize, AddHeader)) |
| 121 return cleanupTest(); |
| 122 MemoryBufferRef InputRef(MungedInput->getMemBufferRef()); |
| 123 NaClMungedBitcode WrittenBitcode(MemoryBuffer::getMemBuffer(InputRef)); |
| 124 WrittenBitcode.print(WriteFlags.getErrStream()); |
| 125 return cleanupTest(); |
| 126 } |
| 127 |
113 bool NaClObjDumpMunger::runTestWithFlags( | 128 bool NaClObjDumpMunger::runTestWithFlags( |
114 const char *Name, const uint64_t Munges[], size_t MungesSize, | 129 const char *Name, const uint64_t Munges[], size_t MungesSize, |
115 bool AddHeader, bool NoRecords, bool NoAssembly) { | 130 bool AddHeader, bool NoRecords, bool NoAssembly) { |
116 setupTest(Name, Munges, MungesSize, AddHeader); | 131 if (!setupTest(Name, Munges, MungesSize, AddHeader)) |
| 132 return cleanupTest(); |
117 | 133 |
118 if (NaClObjDump(MungedInput.get()->getMemBufferRef(), | 134 if (NaClObjDump(MungedInput.get()->getMemBufferRef(), |
119 getDumpStream(), NoRecords, NoAssembly)) | 135 getDumpStream(), NoRecords, NoAssembly)) |
120 FoundErrors = true; | 136 FoundErrors = true; |
121 cleanupTest(); | 137 return cleanupTest(); |
122 return !FoundErrors; | |
123 } | 138 } |
124 | 139 |
125 bool NaClParseBitcodeMunger::runTest( | 140 bool NaClParseBitcodeMunger::runTest( |
126 const char *Name, const uint64_t Munges[], size_t MungesSize, | 141 const char *Name, const uint64_t Munges[], size_t MungesSize, |
127 bool VerboseErrors) { | 142 bool VerboseErrors) { |
128 bool AddHeader = true; | 143 bool AddHeader = true; |
129 setupTest(Name, Munges, MungesSize, AddHeader); | 144 if (!setupTest(Name, Munges, MungesSize, AddHeader)) |
| 145 return cleanupTest(); |
| 146 |
130 LLVMContext &Context = getGlobalContext(); | 147 LLVMContext &Context = getGlobalContext(); |
| 148 |
131 raw_ostream *VerboseStrm = VerboseErrors ? &getDumpStream() : nullptr; | 149 raw_ostream *VerboseStrm = VerboseErrors ? &getDumpStream() : nullptr; |
132 ErrorOr<Module *> ModuleOrError = | 150 ErrorOr<Module *> ModuleOrError = |
133 NaClParseBitcodeFile(MungedInput->getMemBufferRef(), Context, | 151 NaClParseBitcodeFile(MungedInput->getMemBufferRef(), Context, |
134 VerboseStrm); | 152 VerboseStrm); |
135 if (ModuleOrError) { | 153 if (ModuleOrError) { |
136 if (VerboseErrors) | 154 if (VerboseErrors) |
137 getDumpStream() << "Successful parse!\n"; | 155 getDumpStream() << "Successful parse!\n"; |
138 delete ModuleOrError.get(); | 156 delete ModuleOrError.get(); |
139 } else { | 157 } else { |
140 Error() << ModuleOrError.getError().message() << "\n"; | 158 Error() << ModuleOrError.getError().message() << "\n"; |
141 } | 159 } |
142 cleanupTest(); | 160 return cleanupTest(); |
143 return !FoundErrors; | |
144 } | 161 } |
145 | 162 |
146 bool NaClCompressMunger::runTest(const char* Name, const uint64_t Munges[], | 163 bool NaClCompressMunger::runTest(const char* Name, const uint64_t Munges[], |
147 size_t MungesSize) { | 164 size_t MungesSize) { |
148 bool AddHeader = true; | 165 bool AddHeader = true; |
149 setupTest(Name, Munges, MungesSize, AddHeader); | 166 if (!setupTest(Name, Munges, MungesSize, AddHeader)) |
| 167 return cleanupTest(); |
| 168 |
150 NaClBitcodeCompressor Compressor; | 169 NaClBitcodeCompressor Compressor; |
151 bool Result = Compressor.compress(MungedInput.get(), getDumpStream()); | 170 if (!Compressor.compress(MungedInput.get(), getDumpStream())) |
152 cleanupTest(); | 171 Error() << "Unable to compress\n"; |
153 return Result; | 172 return cleanupTest(); |
154 } | 173 } |
OLD | NEW |