mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2025-09-26 09:18:56 +00:00
InstrProf: Detect magic numbers in a more scalable way
No functionality change. <rdar://problem/15950346> git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204511 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -163,6 +163,7 @@ private:
|
|||||||
public:
|
public:
|
||||||
RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer);
|
RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer);
|
||||||
|
|
||||||
|
static bool hasFormat(const MemoryBuffer &DataBuffer);
|
||||||
error_code readHeader() override;
|
error_code readHeader() override;
|
||||||
error_code readNextRecord(InstrProfRecord &Record) override;
|
error_code readNextRecord(InstrProfRecord &Record) override;
|
||||||
|
|
||||||
|
@@ -19,18 +19,6 @@
|
|||||||
|
|
||||||
using namespace llvm;
|
using namespace llvm;
|
||||||
|
|
||||||
static uint64_t getRawMagic() {
|
|
||||||
return
|
|
||||||
uint64_t('l') << 56 |
|
|
||||||
uint64_t('p') << 48 |
|
|
||||||
uint64_t('r') << 40 |
|
|
||||||
uint64_t('o') << 32 |
|
|
||||||
uint64_t('f') << 24 |
|
|
||||||
uint64_t('r') << 16 |
|
|
||||||
uint64_t('a') << 8 |
|
|
||||||
uint64_t('w');
|
|
||||||
}
|
|
||||||
|
|
||||||
error_code InstrProfReader::create(std::string Path,
|
error_code InstrProfReader::create(std::string Path,
|
||||||
std::unique_ptr<InstrProfReader> &Result) {
|
std::unique_ptr<InstrProfReader> &Result) {
|
||||||
std::unique_ptr<MemoryBuffer> Buffer;
|
std::unique_ptr<MemoryBuffer> Buffer;
|
||||||
@@ -41,17 +29,13 @@ error_code InstrProfReader::create(std::string Path,
|
|||||||
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
|
if (Buffer->getBufferSize() > std::numeric_limits<unsigned>::max())
|
||||||
return instrprof_error::too_large;
|
return instrprof_error::too_large;
|
||||||
|
|
||||||
if (Buffer->getBufferSize() < sizeof(uint64_t)) {
|
// Create the reader.
|
||||||
Result.reset(new TextInstrProfReader(Buffer));
|
if (RawInstrProfReader::hasFormat(*Buffer))
|
||||||
return Result->readHeader();
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t Magic = *(uint64_t *)Buffer->getBufferStart();
|
|
||||||
uint64_t SwappedMagic = sys::SwapByteOrder(Magic);
|
|
||||||
if (Magic == getRawMagic() || SwappedMagic == getRawMagic())
|
|
||||||
Result.reset(new RawInstrProfReader(Buffer));
|
Result.reset(new RawInstrProfReader(Buffer));
|
||||||
else
|
else
|
||||||
Result.reset(new TextInstrProfReader(Buffer));
|
Result.reset(new TextInstrProfReader(Buffer));
|
||||||
|
|
||||||
|
// Read the header and return the result.
|
||||||
return Result->readHeader();
|
return Result->readHeader();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -101,29 +85,43 @@ error_code TextInstrProfReader::readNextRecord(InstrProfRecord &Record) {
|
|||||||
return success();
|
return success();
|
||||||
}
|
}
|
||||||
|
|
||||||
static uint64_t getRawVersion() {
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
namespace {
|
|
||||||
}
|
|
||||||
RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
|
RawInstrProfReader::RawInstrProfReader(std::unique_ptr<MemoryBuffer> &DataBuffer)
|
||||||
: DataBuffer(DataBuffer.release()) { }
|
: DataBuffer(DataBuffer.release()) { }
|
||||||
|
|
||||||
|
static uint64_t getRawMagic() {
|
||||||
|
return
|
||||||
|
uint64_t('l') << 56 |
|
||||||
|
uint64_t('p') << 48 |
|
||||||
|
uint64_t('r') << 40 |
|
||||||
|
uint64_t('o') << 32 |
|
||||||
|
uint64_t('f') << 24 |
|
||||||
|
uint64_t('r') << 16 |
|
||||||
|
uint64_t('a') << 8 |
|
||||||
|
uint64_t('w');
|
||||||
|
}
|
||||||
|
|
||||||
|
bool RawInstrProfReader::hasFormat(const MemoryBuffer &DataBuffer) {
|
||||||
|
if (DataBuffer.getBufferSize() < sizeof(getRawMagic()))
|
||||||
|
return false;
|
||||||
|
const RawHeader *Header = (const RawHeader *)DataBuffer.getBufferStart();
|
||||||
|
return getRawMagic() == Header->Magic ||
|
||||||
|
sys::SwapByteOrder(getRawMagic()) == Header->Magic;
|
||||||
|
}
|
||||||
|
|
||||||
error_code RawInstrProfReader::readHeader() {
|
error_code RawInstrProfReader::readHeader() {
|
||||||
|
if (!hasFormat(*DataBuffer))
|
||||||
|
return error(instrprof_error::bad_magic);
|
||||||
if (DataBuffer->getBufferSize() < sizeof(RawHeader))
|
if (DataBuffer->getBufferSize() < sizeof(RawHeader))
|
||||||
return error(instrprof_error::bad_header);
|
return error(instrprof_error::bad_header);
|
||||||
const RawHeader *Header = (RawHeader *)DataBuffer->getBufferStart();
|
const RawHeader *Header = (const RawHeader *)DataBuffer->getBufferStart();
|
||||||
if (Header->Magic == getRawMagic())
|
ShouldSwapBytes = Header->Magic != getRawMagic();
|
||||||
ShouldSwapBytes = false;
|
|
||||||
else {
|
|
||||||
if (sys::SwapByteOrder(Header->Magic) != getRawMagic())
|
|
||||||
return error(instrprof_error::bad_magic);
|
|
||||||
|
|
||||||
ShouldSwapBytes = true;
|
|
||||||
}
|
|
||||||
return readHeader(*Header);
|
return readHeader(*Header);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static uint64_t getRawVersion() {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
|
error_code RawInstrProfReader::readHeader(const RawHeader &Header) {
|
||||||
if (swap(Header.Version) != getRawVersion())
|
if (swap(Header.Version) != getRawVersion())
|
||||||
return error(instrprof_error::unsupported_version);
|
return error(instrprof_error::unsupported_version);
|
||||||
|
Reference in New Issue
Block a user