OLD | NEW |
1 //===- NaClBitcodeMungeUtils.h - Munge bitcode records --------*- C++ -*-===// | 1 //===- NaClBitcodeMungeUtils.h - Munge bitcode records --------*- C++ -*-===// |
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 // This file defines utility class NaClMungedBitcode to edit a base | 10 // This file defines utility class NaClMungedBitcode to edit a base |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 #ifndef LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H | 74 #ifndef LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H |
75 #define LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H | 75 #define LLVM_BITCODE_NACL_NACLBITCODEMUNGEUTILS_H |
76 | 76 |
77 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" | 77 #include "llvm/Bitcode/NaCl/NaClBitcodeParser.h" |
78 | 78 |
79 #include <list> | 79 #include <list> |
80 #include <map> | 80 #include <map> |
81 | 81 |
82 namespace llvm { | 82 namespace llvm { |
83 | 83 |
| 84 class MemoryBuffer; // Buffer to read bitcode from |
84 class NaClBitcodeAbbrevRecord; // bitcode record. | 85 class NaClBitcodeAbbrevRecord; // bitcode record. |
85 class NaClMungedBitcodeIter; // iterator over edited bitcode records. | 86 class NaClMungedBitcodeIter; // iterator over edited bitcode records. |
86 | 87 |
87 /// \brief Defines a list of bitcode records. | 88 /// \brief Defines a list of bitcode records. |
88 typedef std::vector<std::unique_ptr<NaClBitcodeAbbrevRecord>> | 89 typedef std::vector<std::unique_ptr<NaClBitcodeAbbrevRecord>> |
89 NaClBitcodeRecordList; | 90 NaClBitcodeRecordList; |
90 | 91 |
91 /// \brief Extracts out the records in Records, and puts them into RecordList. | 92 /// \brief Extracts out the records in Records, and puts them into RecordList. |
92 /// | 93 /// |
93 /// \brief RecordList[in/out] Record list to read into. | 94 /// \brief RecordList[in/out] Record list to read into. |
94 /// \brief Records Array containing data defining records. | 95 /// \brief Records Array containing data defining records. |
95 /// \brief RecordsSize The size of array Records. | 96 /// \brief RecordsSize The size of array Records. |
96 /// \brief RecordTerminator The value used to terminate records. | 97 /// \brief RecordTerminator The value used to terminate records. |
97 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, | 98 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, |
98 const uint64_t Records[], | 99 const uint64_t Records[], |
99 size_t RecordsSize, | 100 size_t RecordsSize, |
100 uint64_t RecordTerminator); | 101 uint64_t RecordTerminator); |
101 | 102 |
| 103 /// \brief Read in the list of records from bitcode in a memory buffer. |
| 104 void readNaClBitcodeRecordList(NaClBitcodeRecordList &RecordList, |
| 105 std::unique_ptr<MemoryBuffer> InputBuffer); |
| 106 |
102 /// \brief An edited (i.e. munged) list of bitcode records. Edits are | 107 /// \brief An edited (i.e. munged) list of bitcode records. Edits are |
103 /// always relative to the initial list of records. | 108 /// always relative to the initial list of records. |
104 class NaClMungedBitcode { | 109 class NaClMungedBitcode { |
105 friend class NaClMungedBitcodeIter; | 110 friend class NaClMungedBitcodeIter; |
106 NaClMungedBitcode() = delete; | 111 NaClMungedBitcode() = delete; |
107 NaClMungedBitcode(const NaClMungedBitcode &) = delete; | 112 NaClMungedBitcode(const NaClMungedBitcode &) = delete; |
108 NaClMungedBitcode &operator=(const NaClMungedBitcode &) = delete; | 113 NaClMungedBitcode &operator=(const NaClMungedBitcode &) = delete; |
109 | 114 |
110 public: | 115 public: |
111 /// \brief Iterator over edited records. | 116 /// \brief Iterator over edited records. |
112 typedef NaClMungedBitcodeIter iterator; | 117 typedef NaClMungedBitcodeIter iterator; |
113 | 118 |
| 119 /// \brief Read in initial list of records from bitcode in a memory buffer. |
| 120 explicit NaClMungedBitcode(std::unique_ptr<MemoryBuffer> InputBuffer); |
| 121 |
114 /// \brief Initialize the list of records to be edited. | 122 /// \brief Initialize the list of records to be edited. |
115 explicit NaClMungedBitcode(std::unique_ptr<NaClBitcodeRecordList> BaseRecords) | 123 explicit NaClMungedBitcode(std::unique_ptr<NaClBitcodeRecordList> BaseRecords) |
116 : BaseRecords(std::move(BaseRecords)) {} | 124 : BaseRecords(std::move(BaseRecords)) {} |
117 | 125 |
118 /// \brief Initialize the list of records to be edited using | 126 /// \brief Initialize the list of records to be edited using |
119 /// array specification. | 127 /// array specification. |
120 /// | 128 /// |
121 /// \brief Records Array containing data defining records. | 129 /// \brief Records Array containing data defining records. |
122 /// \brief RecordsSize The size of array Records. | 130 /// \brief RecordsSize The size of array Records. |
123 /// \brief RecordTerminator The value used to terminate records. | 131 /// \brief RecordTerminator The value used to terminate records. |
(...skipping 29 matching lines...) Expand all Loading... |
153 | 161 |
154 /// \brief Replace the record at RecordIndex with Record. Note that | 162 /// \brief Replace the record at RecordIndex with Record. Note that |
155 /// because the record index is based on the base records, and those | 163 /// because the record index is based on the base records, and those |
156 /// records are not modified, this editing action effectively undoes | 164 /// records are not modified, this editing action effectively undoes |
157 /// all previous remove/replace editing actions for this index. | 165 /// all previous remove/replace editing actions for this index. |
158 void replace(size_t RecordIndex, NaClBitcodeAbbrevRecord &Record); | 166 void replace(size_t RecordIndex, NaClBitcodeAbbrevRecord &Record); |
159 | 167 |
160 /// \brief Print out the resulting edited list of records. | 168 /// \brief Print out the resulting edited list of records. |
161 void print(raw_ostream &Out) const; | 169 void print(raw_ostream &Out) const; |
162 | 170 |
| 171 /// Defines set of possible write flags. |
| 172 struct WriteFlags { |
| 173 /// True if error recovery should be applied. |
| 174 bool getTryToRecover() const { return TryToRecover; } |
| 175 |
| 176 /// Define that error recovery should be applied when writing. |
| 177 void setTryToRecover(bool NewValue) { |
| 178 TryToRecover = NewValue; |
| 179 assert(!(TryToRecover && WriteBadAbbrevIndex)); |
| 180 } |
| 181 |
| 182 /// True if a bad abbreviation index should be written (rather than |
| 183 /// trying error recovery) so that bitcode readers can be tested for |
| 184 /// this condition. |
| 185 bool getWriteBadAbbrevIndex() const { return WriteBadAbbrevIndex; } |
| 186 |
| 187 /// Define that the first bad abbreviation index should be written, |
| 188 /// and corresponding minimal context added so that the bitcode can |
| 189 /// be used to test reading the erroneous written bitcode. |
| 190 void setWriteBadAbbrevIndex(bool NewValue) { |
| 191 WriteBadAbbrevIndex = NewValue; |
| 192 assert(!(TryToRecover && WriteBadAbbrevIndex)); |
| 193 } |
| 194 |
| 195 /// Get the stream to print errors while writing bitcode. |
| 196 raw_ostream &getErrStream() const { |
| 197 return ErrStream ? *ErrStream : errs(); |
| 198 } |
| 199 |
| 200 /// Set the stream to print errors to. |
| 201 void setErrStream(raw_ostream &NewValue) { |
| 202 ErrStream = &NewValue; |
| 203 } |
| 204 |
| 205 void reset() { |
| 206 TryToRecover = false; |
| 207 WriteBadAbbrevIndex = false; |
| 208 ErrStream = nullptr; |
| 209 } |
| 210 |
| 211 private: |
| 212 bool TryToRecover = false; |
| 213 bool WriteBadAbbrevIndex = false; |
| 214 raw_ostream *ErrStream = nullptr; |
| 215 }; |
| 216 |
| 217 /// Defines the results associated with writing bitcode. |
| 218 struct WriteResults { |
| 219 /// Number of errors generated. |
| 220 size_t NumErrors = 0; |
| 221 /// Number of repairs (via error recovery) that were applied. |
| 222 size_t NumRepairs = 0; |
| 223 /// True if a bad abbreviation index were written. |
| 224 bool WroteBadAbbrevIndex = false; |
| 225 }; |
| 226 |
| 227 /// \brief Write out the edited list of bitcode records using |
| 228 /// the given buffer. |
| 229 /// |
| 230 /// \param Buffer The buffer to write into. |
| 231 /// \param AddHeader Add header block when true. |
| 232 /// \param Flags Write flags to use. |
| 233 /// |
| 234 /// \return Returns the results of the write. |
| 235 WriteResults writeMaybeRepair( |
| 236 SmallVectorImpl<char> &Buffer, bool AddHeader, |
| 237 const WriteFlags &Flags) const; |
| 238 |
| 239 bool write(SmallVectorImpl<char> &Buffer, bool AddHeader, |
| 240 const WriteFlags &Flags) const { |
| 241 return writeMaybeRepair(Buffer, AddHeader, Flags).NumErrors == 0; |
| 242 } |
| 243 |
| 244 bool write(SmallVectorImpl<char> &Buffer, bool AddHeader) const { |
| 245 WriteFlags Flags; |
| 246 return write(Buffer, AddHeader, Flags); |
| 247 } |
| 248 |
163 /// \brief The types of editing actions that can be applied. | 249 /// \brief The types of editing actions that can be applied. |
164 enum EditAction { | 250 enum EditAction { |
165 AddBefore, // Insert new record before base record at index. | 251 AddBefore, // Insert new record before base record at index. |
166 AddAfter, // Insert new record after base record at index. | 252 AddAfter, // Insert new record after base record at index. |
167 Remove, // Remove record at index. | 253 Remove, // Remove record at index. |
168 Replace // Replace base record at index with new record. | 254 Replace // Replace base record at index with new record. |
169 }; | 255 }; |
170 | 256 |
171 /// \brief Apply a set of edits defined in the given array. | 257 /// \brief Apply a set of edits defined in the given array. |
172 /// | 258 /// |
(...skipping 10 matching lines...) Expand all Loading... |
183 /// | 269 /// |
184 /// \param Munges The array containing the edits to apply. | 270 /// \param Munges The array containing the edits to apply. |
185 /// \param MungesSize The size of Munges. | 271 /// \param MungesSize The size of Munges. |
186 /// \param Terminator The value used to terminate records in editing actions. | 272 /// \param Terminator The value used to terminate records in editing actions. |
187 void munge(const uint64_t Munges[], size_t MungesSize, uint64_t Terminator); | 273 void munge(const uint64_t Munges[], size_t MungesSize, uint64_t Terminator); |
188 | 274 |
189 /// \brief Removes all editing actions and resets back to the original | 275 /// \brief Removes all editing actions and resets back to the original |
190 /// set of base records. | 276 /// set of base records. |
191 void removeEdits(); | 277 void removeEdits(); |
192 | 278 |
| 279 /// Returns the unedited list of bitcode records. |
| 280 const NaClBitcodeRecordList &getBaseRecords() const { |
| 281 return *BaseRecords; |
| 282 } |
| 283 |
193 private: | 284 private: |
194 typedef std::list<NaClBitcodeAbbrevRecord *> RecordListType; | 285 typedef std::list<NaClBitcodeAbbrevRecord *> RecordListType; |
195 typedef std::map<size_t, RecordListType *> InsertionsMapType; | 286 typedef std::map<size_t, RecordListType *> InsertionsMapType; |
196 typedef std::map<size_t, NaClBitcodeAbbrevRecord *> ReplaceMapType; | 287 typedef std::map<size_t, NaClBitcodeAbbrevRecord *> ReplaceMapType; |
197 | 288 |
198 /// \brief The list of base records that will be edited. | 289 /// \brief The list of base records that will be edited. |
199 std::unique_ptr<NaClBitcodeRecordList> BaseRecords; | 290 std::unique_ptr<NaClBitcodeRecordList> BaseRecords; |
200 // Holds map from record index to list of records added before | 291 // Holds map from record index to list of records added before |
201 // the corresponding record in the list of base records. | 292 // the corresponding record in the list of base records. |
202 InsertionsMapType BeforeInsertionsMap; | 293 InsertionsMapType BeforeInsertionsMap; |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 public: | 325 public: |
235 /// \brief The abbreviation associated with the bitcode record. | 326 /// \brief The abbreviation associated with the bitcode record. |
236 unsigned Abbrev; | 327 unsigned Abbrev; |
237 | 328 |
238 /// \brief Creates a bitcode record. | 329 /// \brief Creates a bitcode record. |
239 /// | 330 /// |
240 /// \param Abbrev Abbreviation index associated with record. | 331 /// \param Abbrev Abbreviation index associated with record. |
241 /// \param Code The selector code of the record. | 332 /// \param Code The selector code of the record. |
242 /// \param Values The values associated with the selector code. | 333 /// \param Values The values associated with the selector code. |
243 NaClBitcodeAbbrevRecord(unsigned Abbrev, unsigned Code, | 334 NaClBitcodeAbbrevRecord(unsigned Abbrev, unsigned Code, |
244 NaClRecordVector &Values) | 335 const NaClRecordVector &Values) |
245 : NaClBitcodeRecordData(Code, Values), Abbrev(Abbrev) {} | 336 : NaClBitcodeRecordData(Code, Values), Abbrev(Abbrev) {} |
246 | 337 |
247 /// \brief Creates a copy of the given abbreviated bitcode record. | 338 /// \brief Creates a copy of the given abbreviated bitcode record. |
248 explicit NaClBitcodeAbbrevRecord(const NaClBitcodeAbbrevRecord &Rcd) | 339 explicit NaClBitcodeAbbrevRecord(const NaClBitcodeAbbrevRecord &Rcd) |
249 : NaClBitcodeRecordData(Rcd), Abbrev(Rcd.Abbrev) {} | 340 : NaClBitcodeRecordData(Rcd), Abbrev(Rcd.Abbrev) {} |
250 | 341 |
251 /// \brief Creates a default abbreviated bitcode record. | 342 /// \brief Creates a default abbreviated bitcode record. |
252 NaClBitcodeAbbrevRecord() : Abbrev(naclbitc::UNABBREV_RECORD) {} | 343 NaClBitcodeAbbrevRecord() : Abbrev(naclbitc::UNABBREV_RECORD) {} |
253 | 344 |
254 ~NaClBitcodeAbbrevRecord() {} | 345 ~NaClBitcodeAbbrevRecord() {} |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
361 } | 452 } |
362 | 453 |
363 // \brief Moves the iterator to the position of the next edited | 454 // \brief Moves the iterator to the position of the next edited |
364 // record. | 455 // record. |
365 void updatePosition(); | 456 void updatePosition(); |
366 }; | 457 }; |
367 | 458 |
368 } // end namespace llvm. | 459 } // end namespace llvm. |
369 | 460 |
370 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H | 461 #endif // LLVM_BITCODE_NACL_NACLBITCODEMUNGE_H |
OLD | NEW |