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

Side by Side 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 unified diff | Download patch
OLDNEW
1 //===- NaClBitcodeReader.cpp ----------------------------------------------===// 1 //===- NaClBitcodeReader.cpp ----------------------------------------------===//
2 // Internal NaClBitcodeReader implementation 2 // Internal NaClBitcodeReader implementation
3 // 3 //
4 // The LLVM Compiler Infrastructure 4 // The LLVM Compiler Infrastructure
5 // 5 //
6 // This file is distributed under the University of Illinois Open Source 6 // This file is distributed under the University of Illinois Open Source
7 // License. See LICENSE.TXT for details. 7 // License. See LICENSE.TXT for details.
8 // 8 //
9 //===----------------------------------------------------------------------===// 9 //===----------------------------------------------------------------------===//
10 10
(...skipping 159 matching lines...) Expand 10 before | Expand all | Expand 10 after
170 // named struct. Just create a placeholder for now. 170 // named struct. Just create a placeholder for now.
171 return TypeList[ID] = StructType::create(Context); 171 return TypeList[ID] = StructType::create(Context);
172 } 172 }
173 173
174 174
175 //===----------------------------------------------------------------------===// 175 //===----------------------------------------------------------------------===//
176 // Functions for parsing blocks from the bitcode file 176 // Functions for parsing blocks from the bitcode file
177 //===----------------------------------------------------------------------===// 177 //===----------------------------------------------------------------------===//
178 178
179 179
180 namespace {
181
182 static const unsigned MaxAlignmentExponent = 29;
183 static_assert(
184 (1u << MaxAlignmentExponent) == Value::MaximumAlignment,
185 "Inconsistency between Value.MaxAlignment and PNaCl alignment limit");
186 }
187
180 std::error_code NaClBitcodeReader::Error(ErrorType E, 188 std::error_code NaClBitcodeReader::Error(ErrorType E,
181 const std::string &Message) const { 189 const std::string &Message) const {
182 if (Verbose) 190 if (Verbose) {
183 *Verbose << "Error: " << Message << "\n"; 191 uint64_t Bit = Stream.GetCurrentBitNo();
192 *Verbose << "Error: (" << (Bit / CHAR_BIT) << ":"
193 << static_cast<unsigned>(Bit % CHAR_BIT)
194 << ") " << Message << "\n";
195 }
184 return Error(E); 196 return Error(E);
185 } 197 }
186 198
199 std::error_code NaClBitcodeReader::getAlignmentValue(
200 uint64_t Exponent, unsigned &Alignment) {
201 if (Exponent > MaxAlignmentExponent + 1) {
202 std::string Buffer;
203 raw_string_ostream StrBuf(Buffer);
204 StrBuf << "Alignment can't be greater than 2**" << MaxAlignmentExponent
205 << ". Found: 2**" << (Exponent - 1);
206 return Error(InvalidValue, StrBuf.str());
207 }
208 Alignment = (1 << static_cast<unsigned>(Exponent)) >> 1;
209 return std::error_code();
210 }
211
187 std::error_code NaClBitcodeReader::ParseTypeTable() { 212 std::error_code NaClBitcodeReader::ParseTypeTable() {
188 DEBUG(dbgs() << "-> ParseTypeTable\n"); 213 DEBUG(dbgs() << "-> ParseTypeTable\n");
189 if (Stream.EnterSubBlock(naclbitc::TYPE_BLOCK_ID_NEW)) 214 if (Stream.EnterSubBlock(naclbitc::TYPE_BLOCK_ID_NEW))
190 return Error(InvalidRecord, "Malformed block record"); 215 return Error(InvalidRecord, "Malformed block record");
191 216
192 std::error_code result = ParseTypeTableBody(); 217 std::error_code result = ParseTypeTableBody();
193 if (!result) 218 if (!result)
194 DEBUG(dbgs() << "<- ParseTypeTable\n"); 219 DEBUG(dbgs() << "<- ParseTypeTable\n");
195 return result; 220 return result;
196 } 221 }
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 switch (Bitcode) { 420 switch (Bitcode) {
396 default: 421 default:
397 return Reader.Error(NaClBitcodeReader::InvalidValue, 422 return Reader.Error(NaClBitcodeReader::InvalidValue,
398 "Unknown global variable entry"); 423 "Unknown global variable entry");
399 case naclbitc::GLOBALVAR_VAR: 424 case naclbitc::GLOBALVAR_VAR:
400 // Start the definition of a global variable. 425 // Start the definition of a global variable.
401 if (ProcessingGlobal || Record.size() != 2) 426 if (ProcessingGlobal || Record.size() != 2)
402 return Reader.Error(NaClBitcodeReader::InvalidRecord, 427 return Reader.Error(NaClBitcodeReader::InvalidRecord,
403 "Bad GLOBALVAR_VAR record"); 428 "Bad GLOBALVAR_VAR record");
404 ProcessingGlobal = true; 429 ProcessingGlobal = true;
405 VarAlignment = (1 << Record[0]) >> 1; 430 if (std::error_code EC =
431 Reader.getAlignmentValue(Record[0], VarAlignment))
432 return EC;
406 VarIsConstant = Record[1] != 0; 433 VarIsConstant = Record[1] != 0;
407 // Assume (by default) there is a single initializer. 434 // Assume (by default) there is a single initializer.
408 VarInitializersNeeded = 1; 435 VarInitializersNeeded = 1;
409 break; 436 break;
410 case naclbitc::GLOBALVAR_COMPOUND: 437 case naclbitc::GLOBALVAR_COMPOUND:
411 // Global variable has multiple initializers. Changes the 438 // Global variable has multiple initializers. Changes the
412 // default number of initializers to the given value in 439 // default number of initializers to the given value in
413 // Record[0]. 440 // Record[0].
414 if (!ProcessingGlobal || !VarType.empty() || 441 if (!ProcessingGlobal || !VarType.empty() ||
415 VarInitializersNeeded != 1 || Record.size() != 1) 442 VarInitializersNeeded != 1 || Record.size() != 1)
(...skipping 1057 matching lines...) Expand 10 before | Expand all | Expand 10 after
1473 break; 1500 break;
1474 } 1501 }
1475 1502
1476 case naclbitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [op, align] 1503 case naclbitc::FUNC_CODE_INST_ALLOCA: { // ALLOCA: [op, align]
1477 if (Record.size() != 2) 1504 if (Record.size() != 2)
1478 return Error(InvalidRecord, "Invalid ALLOCA record"); 1505 return Error(InvalidRecord, "Invalid ALLOCA record");
1479 Value *Size; 1506 Value *Size;
1480 unsigned OpNum = 0; 1507 unsigned OpNum = 0;
1481 if (popValue(Record, &OpNum, NextValueNo, &Size)) 1508 if (popValue(Record, &OpNum, NextValueNo, &Size))
1482 return Error(InvalidRecord, "Invalid ALLOCA record"); 1509 return Error(InvalidRecord, "Invalid ALLOCA record");
1483 unsigned Align = Record[1]; 1510 unsigned Alignment;
1484 I = new AllocaInst(Type::getInt8Ty(Context), Size, (1 << Align) >> 1); 1511 if (std::error_code EC = getAlignmentValue(Record[1], Alignment))
1512 return EC;
1513 I = new AllocaInst(Type::getInt8Ty(Context), Size, Alignment);
1485 break; 1514 break;
1486 } 1515 }
1487 case naclbitc::FUNC_CODE_INST_LOAD: { 1516 case naclbitc::FUNC_CODE_INST_LOAD: {
1488 // LOAD: [op, align, ty] 1517 // LOAD: [op, align, ty]
1489 unsigned OpNum = 0; 1518 unsigned OpNum = 0;
1490 Value *Op; 1519 Value *Op;
1491 if (popValue(Record, &OpNum, NextValueNo, &Op) || 1520 if (popValue(Record, &OpNum, NextValueNo, &Op) ||
1492 Record.size() != 3) 1521 Record.size() != 3)
1493 return Error(InvalidRecord, "Invalid LOAD record"); 1522 return Error(InvalidRecord, "Invalid LOAD record");
1494 1523
1495 // Add pointer cast to op. 1524 // Add pointer cast to op.
1496 Type *T = getTypeByID(Record[2]); 1525 Type *T = getTypeByID(Record[2]);
1497 if (T == nullptr) 1526 if (T == nullptr)
1498 return Error(InvalidType, "Invalid type for load instruction"); 1527 return Error(InvalidType, "Invalid type for load instruction");
1499 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo); 1528 Op = ConvertOpToType(Op, T->getPointerTo(), CurBBNo);
1500 if (Op == nullptr) 1529 if (Op == nullptr)
1501 return Error(InvalidTypeForValue, "Can't convert cast to type"); 1530 return Error(InvalidTypeForValue, "Can't convert cast to type");
1502 I = new LoadInst(Op, "", false, (1 << Record[OpNum]) >> 1); 1531 unsigned Alignment;
1532 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment))
1533 return EC;
1534 I = new LoadInst(Op, "", false, Alignment);
1503 break; 1535 break;
1504 } 1536 }
1505 case naclbitc::FUNC_CODE_INST_STORE: { 1537 case naclbitc::FUNC_CODE_INST_STORE: {
1506 // STORE: [ptr, val, align] 1538 // STORE: [ptr, val, align]
1507 unsigned OpNum = 0; 1539 unsigned OpNum = 0;
1508 Value *Val, *Ptr; 1540 Value *Val, *Ptr;
1509 if (popValue(Record, &OpNum, NextValueNo, &Ptr) || 1541 if (popValue(Record, &OpNum, NextValueNo, &Ptr) ||
1510 popValue(Record, &OpNum, NextValueNo, &Val) || 1542 popValue(Record, &OpNum, NextValueNo, &Val) ||
1511 OpNum+1 != Record.size()) 1543 OpNum+1 != Record.size())
1512 return Error(InvalidRecord, "Invalid STORE record"); 1544 return Error(InvalidRecord, "Invalid STORE record");
1513 Val = ConvertOpToScalar(Val, CurBBNo); 1545 Val = ConvertOpToScalar(Val, CurBBNo);
1514 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo); 1546 Ptr = ConvertOpToType(Ptr, Val->getType()->getPointerTo(), CurBBNo);
1515 if (Ptr == nullptr) 1547 if (Ptr == nullptr)
1516 return Error(InvalidTypeForValue, "Can't convert cast to type"); 1548 return Error(InvalidTypeForValue, "Can't convert cast to type");
1517 I = new StoreInst(Val, Ptr, false, (1 << Record[OpNum]) >> 1); 1549 unsigned Alignment;
1550 if (std::error_code EC = getAlignmentValue(Record[OpNum], Alignment))
1551 return EC;
1552 I = new StoreInst(Val, Ptr, false, Alignment);
1518 break; 1553 break;
1519 } 1554 }
1520 case naclbitc::FUNC_CODE_INST_CALL: 1555 case naclbitc::FUNC_CODE_INST_CALL:
1521 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: { 1556 case naclbitc::FUNC_CODE_INST_CALL_INDIRECT: {
1522 // CALL: [cc, fnid, arg0, arg1...] 1557 // CALL: [cc, fnid, arg0, arg1...]
1523 // CALL_INDIRECT: [cc, fnid, returnty, args...] 1558 // CALL_INDIRECT: [cc, fnid, returnty, args...]
1524 if ((Record.size() < 2) || 1559 if ((Record.size() < 2) ||
1525 (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT && 1560 (BitCode == naclbitc::FUNC_CODE_INST_CALL_INDIRECT &&
1526 Record.size() < 3)) 1561 Record.size() < 3))
1527 return Error(InvalidRecord, "Invalid CALL record"); 1562 return Error(InvalidRecord, "Invalid CALL record");
(...skipping 369 matching lines...) Expand 10 before | Expand all | Expand 10 after
1897 Materializer->releaseBuffer(); 1932 Materializer->releaseBuffer();
1898 delete M; 1933 delete M;
1899 return EC; 1934 return EC;
1900 } 1935 }
1901 1936
1902 // TODO: Restore the use-lists to the in-memory state when the bitcode was 1937 // TODO: Restore the use-lists to the in-memory state when the bitcode was
1903 // written. We must defer until the Module has been fully materialized. 1938 // written. We must defer until the Module has been fully materialized.
1904 1939
1905 return M; 1940 return M;
1906 } 1941 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698