Commit 07af2ac7 by Karl Schimpf

Add check to verify alignment on global variables.

Moves the alignment check method from the function block parser, to the top-level parser, and then uses it to also check alignment on global variables. BUG= https://code.google.com/p/nativeclient/issues/detail?id=4329 R=stichnot@chromium.org Review URL: https://codereview.chromium.org/1378163002 .
parent db2fd9c0
...@@ -352,6 +352,26 @@ public: ...@@ -352,6 +352,26 @@ public:
return std::move(VariableDeclarations); return std::move(VariableDeclarations);
} }
// Upper limit of alignment power allowed by LLVM
static constexpr uint32_t AlignPowerLimit = 29;
// Extracts the corresponding Alignment to use, given the AlignPower (i.e.
// 2**(AlignPower-1), or 0 if AlignPower == 0). Parser defines the block
// context the alignment check appears in, and Prefix defines the context the
// alignment appears in.
uint32_t extractAlignment(NaClBitcodeParser *Parser, const char *Prefix,
uint32_t AlignPower) {
if (AlignPower <= AlignPowerLimit + 1)
return (1 << AlignPower) >> 1;
std::string Buffer;
raw_string_ostream StrBuf(Buffer);
StrBuf << Prefix << " alignment greater than 2**" << AlignPowerLimit
<< ". Found: 2**" << (AlignPower - 1);
Parser->Error(StrBuf.str());
// Error recover with value that is always acceptable.
return 1;
}
private: private:
// The translator associated with the parser. // The translator associated with the parser.
Ice::Translator &Translator; Ice::Translator &Translator;
...@@ -1061,10 +1081,12 @@ void GlobalsParser::ProcessRecord() { ...@@ -1061,10 +1081,12 @@ void GlobalsParser::ProcessRecord() {
// Always build the global variable, even if IR generation is turned off. // Always build the global variable, even if IR generation is turned off.
// This is needed because we need a placeholder in the top-level context // This is needed because we need a placeholder in the top-level context
// when no IR is generated. // when no IR is generated.
uint32_t Alignment =
Context->extractAlignment(this, "Global variable", Values[0]);
CurGlobalVar = getGlobalVarByID(NextGlobalID); CurGlobalVar = getGlobalVarByID(NextGlobalID);
if (!isIRGenerationDisabled()) { if (!isIRGenerationDisabled()) {
InitializersNeeded = 1; InitializersNeeded = 1;
CurGlobalVar->setAlignment((1 << Values[0]) >> 1); CurGlobalVar->setAlignment(Alignment);
CurGlobalVar->setIsConstant(Values[1] != 0); CurGlobalVar->setIsConstant(Values[1] != 0);
} }
++NextGlobalID; ++NextGlobalID;
...@@ -1381,26 +1403,6 @@ private: ...@@ -1381,26 +1403,6 @@ private:
NaClBcIndexSize_t NextLocalInstIndex; NaClBcIndexSize_t NextLocalInstIndex;
// True if the last processed instruction was a terminating instruction. // True if the last processed instruction was a terminating instruction.
bool InstIsTerminating = false; bool InstIsTerminating = false;
// Upper limit of alignment power allowed by LLVM
static const uint32_t AlignPowerLimit = 29;
// Extracts the corresponding Alignment to use, given the AlignPower (i.e.
// 2**(AlignPower-1), or 0 if AlignPower == 0). InstName is the name of the
// instruction the alignment appears in.
void extractAlignment(const char *InstName, uint32_t AlignPower,
uint32_t &Alignment) {
if (AlignPower <= AlignPowerLimit + 1) {
Alignment = (1 << AlignPower) >> 1;
return;
}
std::string Buffer;
raw_string_ostream StrBuf(Buffer);
StrBuf << InstName << " alignment greater than 2**" << AlignPowerLimit
<< ". Found: 2**" << (AlignPower - 1);
Error(StrBuf.str());
// Error recover with value that is always acceptable.
Alignment = 1;
}
bool ParseBlock(unsigned BlockID) override; bool ParseBlock(unsigned BlockID) override;
...@@ -2592,8 +2594,7 @@ void FunctionParser::ProcessRecord() { ...@@ -2592,8 +2594,7 @@ void FunctionParser::ProcessRecord() {
if (!isValidRecordSize(2, "alloca")) if (!isValidRecordSize(2, "alloca"))
return; return;
Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex); Ice::Operand *ByteCount = getRelativeOperand(Values[0], BaseIndex);
uint32_t Alignment; uint32_t Alignment = Context->extractAlignment(this, "Alloca", Values[1]);
extractAlignment("Alloca", Values[1], Alignment);
if (isIRGenerationDisabled()) { if (isIRGenerationDisabled()) {
assert(ByteCount == nullptr); assert(ByteCount == nullptr);
setNextLocalInstIndex(nullptr); setNextLocalInstIndex(nullptr);
...@@ -2618,8 +2619,7 @@ void FunctionParser::ProcessRecord() { ...@@ -2618,8 +2619,7 @@ void FunctionParser::ProcessRecord() {
return; return;
Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
Ice::Type Ty = Context->getSimpleTypeByID(Values[2]); Ice::Type Ty = Context->getSimpleTypeByID(Values[2]);
uint32_t Alignment; uint32_t Alignment = Context->extractAlignment(this, "Load", Values[1]);
extractAlignment("Load", Values[1], Alignment);
if (isIRGenerationDisabled()) { if (isIRGenerationDisabled()) {
assert(Address == nullptr); assert(Address == nullptr);
setNextLocalInstIndex(nullptr); setNextLocalInstIndex(nullptr);
...@@ -2643,8 +2643,7 @@ void FunctionParser::ProcessRecord() { ...@@ -2643,8 +2643,7 @@ void FunctionParser::ProcessRecord() {
return; return;
Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex); Ice::Operand *Address = getRelativeOperand(Values[0], BaseIndex);
Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex); Ice::Operand *Value = getRelativeOperand(Values[1], BaseIndex);
uint32_t Alignment; uint32_t Alignment = Context->extractAlignment(this, "Store", Values[2]);
extractAlignment("Store", Values[2], Alignment);
if (isIRGenerationDisabled()) { if (isIRGenerationDisabled()) {
assert(Address == nullptr && Value == nullptr); assert(Address == nullptr && Value == nullptr);
return; return;
......
65535,8,2;
1,1;
65535,19,2;
5,1;
0,31,0;
2,4;
65534;
65534;
; Test that we check that alignment on global variables can't be greater
; than 2**29.
; REQUIRES: no_minimal_build
; RUN: not %pnacl_sz -bitcode-as-text %p/Inputs/bad-global-alignment.tbc \
; RUN: -bitcode-format=pnacl -notranslate -no-ir-gen -build-on-read 2>&1 \
; RUN: | FileCheck %s
; CHECK: Global variable alignment greater than 2**29. Found: 2**30
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment