Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(52)

Unified Diff: lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp

Issue 807643002: Don't allow instructions/globals to use alignment > 2**29. (Closed) Base URL: https://chromium.googlesource.com/native_client/pnacl-llvm.git@master
Patch Set: Fix issues in patch set 4. Created 6 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
diff --git a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
index 2d0be943df7b9f0ab0fc2c1ce26d06b2f2000ff3..6f38255d7cf8888f7135d120f399189a4c26c1a5 100644
--- a/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
+++ b/lib/Bitcode/NaCl/Reader/NaClBitcodeReader.cpp
@@ -177,13 +177,38 @@ Type *NaClBitcodeReader::getTypeByID(unsigned ID) {
//===----------------------------------------------------------------------===//
+namespace {
+
+static const unsigned MaxAlignmentExponent = 29;
+static_assert(
+ (1u << MaxAlignmentExponent) == Value::MaximumAlignment,
+ "Inconsistency between Value.MaxAlignment and PNaCl alignment limit");
+}
+
std::error_code NaClBitcodeReader::Error(ErrorType E,
const std::string &Message) const {
- if (Verbose)
- *Verbose << "Error: " << Message << "\n";
+ if (Verbose) {
+ uint64_t Bit = Stream.GetCurrentBitNo();
+ *Verbose << "Error: (" << (Bit / CHAR_BIT) << ":"
+ << static_cast<unsigned>(Bit % CHAR_BIT)
+ << ") " << Message << "\n";
+ }
return Error(E);
}
+std::error_code NaClBitcodeReader::getAlignmentValue(
+ uint64_t Exponent, unsigned &Alignment) {
+ if (Exponent > MaxAlignmentExponent + 1) {
+ std::string Buffer;
+ raw_string_ostream StrBuf(Buffer);
+ StrBuf << "Alignment can't be greater than 2**" << MaxAlignmentExponent
+ << ". Found: 2**" << (Exponent - 1);
+ return Error(InvalidValue, StrBuf.str());
+ }
+ Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
+ return std::error_code();
+}
+
std::error_code NaClBitcodeReader::ParseTypeTable() {
DEBUG(dbgs() << "-> ParseTypeTable\n");
if (Stream.EnterSubBlock(naclbitc::TYPE_BLOCK_ID_NEW))
@@ -402,7 +427,9 @@ public:
return Reader.Error(NaClBitcodeReader::InvalidRecord,
"Bad GLOBALVAR_VAR record");
ProcessingGlobal = true;
- VarAlignment = (1 << Record[0]) >> 1;
+ if (std::error_code EC =
+ Reader.getAlignmentValue(Record[0], VarAlignment))
+ return EC;
VarIsConstant = Record[1] != 0;
// Assume (by default) there is a single initializer.
VarInitializersNeeded = 1;
@@ -1480,8 +1507,10 @@ std::error_code NaClBitcodeReader::ParseFunctionBody(Function *F) {
unsigned OpNum = 0;
if (popValue(Record, &OpNum, NextValueNo, &Size))
return Error(InvalidRecord, "Invalid ALLOCA record");
- unsigned Align = Record[1];
- I = new AllocaInst(Type::getInt8Ty(Context), Size, (1 << Align) >> 1);
+ unsigned Alignment;
+ if (std::error_code EC = getAlignmentValue(Record[1], Alignment))
+ return EC;
+ I = new AllocaInst(Type::getInt8Ty(Context), Size, Alignment);
break;
}
case naclbitc::FUNC_CODE_INST_LOAD: {
@@ -1499,7 +1528,10 @@ std::error_code NaClBitcodeReader::ParseFunctionBody(Function *F) {
Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo);
if (Op == nullptr)
return Error(InvalidTypeForValue, "Can't convert cast to type");
- I = new LoadInst(Op, "", false, (1 << Record[OpNum]) >> 1);
+ unsigned Alignment;
+ if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment))
+ return EC;
+ I = new LoadInst(Op, "", false, Alignment);
break;
}
case naclbitc::FUNC_CODE_INST_STORE: {
@@ -1514,7 +1546,10 @@ std::error_code NaClBitcodeReader::ParseFunctionBody(Function *F) {
Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo);
if (Ptr == nullptr)
return Error(InvalidTypeForValue, "Can't convert cast to type");
- I = new StoreInst(Val, Ptr, false, (1 << Record[OpNum]) >> 1);
+ unsigned Alignment;
+ if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment))
+ return EC;
+ I = new StoreInst(Val, Ptr, false, Alignment);
break;
}
case naclbitc::FUNC_CODE_INST_CALL:

Powered by Google App Engine
This is Rietveld 408576698