Skip to content

Commit 4277124

Browse files
committed
[TableGen] Validate the shift amount for !srl, !shl, and !sra operators.
The C operator has undefined behavior for out of bounds shifts so we should check this.
1 parent b858ba0 commit 4277124

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

llvm/lib/TableGen/Record.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1537,7 +1537,13 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
15371537
if (LHSi && RHSi) {
15381538
int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
15391539
int64_t Result;
1540-
switch (getOpcode()) {
1540+
1541+
unsigned Opc = getOpcode();
1542+
if ((Opc == SHL || Opc == SRA || Opc == SRL) && (RHSv < 0 || RHSv >= 64))
1543+
PrintFatalError(CurRec->getLoc(),
1544+
"Illegal operation: out of bounds shift");
1545+
1546+
switch (Opc) {
15411547
default: llvm_unreachable("Bad opcode!");
15421548
case ADD: Result = LHSv + RHSv; break;
15431549
case SUB: Result = LHSv - RHSv; break;
@@ -1556,7 +1562,7 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
15561562
case OR: Result = LHSv | RHSv; break;
15571563
case XOR: Result = LHSv ^ RHSv; break;
15581564
case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break;
1559-
case SRA: Result = LHSv >> RHSv; break;
1565+
case SRA: Result = LHSv >> (uint64_t)RHSv; break;
15601566
case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
15611567
}
15621568
return IntInit::get(getRecordKeeper(), Result);

0 commit comments

Comments
 (0)