InstrProf: Change magic number to have non-text characters
[oota-llvm.git] / lib / ProfileData / InstrProfReader.cpp
1 //=-- InstrProfReader.cpp - Instrumented profiling reader -------------------=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains support for reading profiling data for clang's
11 // instrumentation based PGO and coverage.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/ProfileData/InstrProfReader.h"
16 #include "llvm/ProfileData/InstrProf.h"
17
18 #include <cassert>
19
20 using namespace llvm;
21
22 error_code InstrProfReader::create(std::string Path,
23                                    std::unique_ptr<InstrProfReader> &Result) {
24   std::unique_ptr<MemoryBuffer> Buffer;
25   if (error_code EC = MemoryBuffer::getFileOrSTDIN(Path, Buffer))
26     return EC;
27
28   // Sanity check the file.
29   if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
30     return instrprof_error::too_large;
31
32   // Create the reader.
33   if (RawInstrProfReader::hasFormat(*Buffer))
34     Result.reset(new RawInstrProfReader(std::move(Buffer)));
35   else
36     Result.reset(new TextInstrProfReader(std::move(Buffer)));
37
38   // Read the header and return the result.
39   return Result->readHeader();
40 }
41
42 void InstrProfIterator::Increment() {
43   if (Reader->readNextRecord(Record))
44     *this = InstrProfIterator();
45 }
46
47 error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
48   // Skip empty lines.
49   while (!Line.is_at_end() && Line->empty())
50     ++Line;
51   // If we hit EOF while looking for a name, we're done.
52   if (Line.is_at_end())
53     return error(instrprof_error::eof);
54
55   // Read the function name.
56   Record.Name = *Line++;
57
58   // Read the function hash.
59   if (Line.is_at_end())
60     return error(instrprof_error::truncated);
61   if ((Line++)->getAsInteger(10, Record.Hash))
62     return error(instrprof_error::malformed);
63
64   // Read the number of counters.
65   uint64_t NumCounters;
66   if (Line.is_at_end())
67     return error(instrprof_error::truncated);
68   if ((Line++)->getAsInteger(10, NumCounters))
69     return error(instrprof_error::malformed);
70
71   // Read each counter and fill our internal storage with the values.
72   Counts.clear();
73   Counts.reserve(NumCounters);
74   for (uint64_t I = 0; I < NumCounters; ++I) {
75     if (Line.is_at_end())
76       return error(instrprof_error::truncated);
77     uint64_t Count;
78     if ((Line++)->getAsInteger(10, Count))
79       return error(instrprof_error::malformed);
80     Counts.push_back(Count);
81   }
82   // Give the record a reference to our internal counter storage.
83   Record.Counts = Counts;
84
85   return success();
86 }
87
88 RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> DataBuffer)
89     : DataBuffer(std::move(DataBuffer)) { }
90
91 static uint64_t getRawMagic() {
92   return
93     uint64_t(255) << 56 |
94     uint64_t('l') << 48 |
95     uint64_t('p') << 40 |
96     uint64_t('r') << 32 |
97     uint64_t('o') << 24 |
98     uint64_t('f') << 16 |
99     uint64_t('r') <<  8 |
100     uint64_t(129);
101 }
102
103 bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
104   if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
105     return false;
106   const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
107   return getRawMagic() == Header->Magic ||
108     sys::SwapByteOrder(getRawMagic()) == Header->Magic;
109 }
110
111 error_code RawInstrProfReader::readHeader() {
112   if (!hasFormat(*DataBuffer))
113     return error(instrprof_error::bad_magic);
114   if (DataBuffer->getBufferSize() < sizeof(RawHeader))
115     return error(instrprof_error::bad_header);
116   const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
117   ShouldSwapBytes = Header->Magic != getRawMagic();
118   return readHeader(*Header);
119 }
120
121 static uint64_t getRawVersion() {
122   return 1;
123 }
124
125 error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
126   if (swap(Header.Version) != getRawVersion())
127     return error(instrprof_error::unsupported_version);
128
129   CountersDelta = swap(Header.CountersDelta);
130   NamesDelta = swap(Header.NamesDelta);
131   auto DataSize = swap(Header.DataSize);
132   auto CountersSize = swap(Header.CountersSize);
133   auto NamesSize = swap(Header.NamesSize);
134
135   ptrdiff_t DataOffset = sizeof(RawHeader);
136   ptrdiff_t CountersOffset = DataOffset + sizeof(ProfileData) * DataSize;
137   ptrdiff_t NamesOffset = CountersOffset + sizeof(uint64_t) * CountersSize;
138   size_t FileSize = NamesOffset + sizeof(char) * NamesSize;
139
140   if (FileSize != DataBuffer->getBufferSize())
141     return error(instrprof_error::bad_header);
142
143   Data = (ProfileData *)(DataBuffer->getBufferStart() + DataOffset);
144   DataEnd = Data + DataSize;
145   CountersStart = (uint64_t *)(DataBuffer->getBufferStart() + CountersOffset);
146   NamesStart = DataBuffer->getBufferStart() + NamesOffset;
147
148   return success();
149 }
150
151 error_code RawInstrProfReader::readNextRecord(InstrProfRecord &Record) {
152   if (Data == DataEnd)
153     return error(instrprof_error::eof);
154
155   // Get the raw data.
156   StringRef RawName(getName(Data->NamePtr), swap(Data->NameSize));
157   auto RawCounts = makeArrayRef(getCounter(Data->CounterPtr),
158                                 swap(Data->NumCounters));
159
160   // Check bounds.
161   if (RawName.data() < NamesStart ||
162       RawName.data() + RawName.size() > DataBuffer->getBufferEnd() ||
163       RawCounts.data() < CountersStart ||
164       RawCounts.data() + RawCounts.size() > (uint64_t *)NamesStart)
165     return error(instrprof_error::malformed);
166
167   // Store the data in Record, byte-swapping as necessary.
168   Record.Hash = swap(Data->FuncHash);
169   Record.Name = RawName;
170   if (ShouldSwapBytes) {
171     Counts.clear();
172     Counts.reserve(RawCounts.size());
173     for (uint64_t Count : RawCounts)
174       Counts.push_back(swap(Count));
175     Record.Counts = Counts;
176   } else
177     Record.Counts = RawCounts;
178
179   // Iterate.
180   ++Data;
181   return success();
182 }