-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[TableGen] Validate the shift amount for !srl, !shl, and !sra operators. #132492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The C operator has undefined behavior for out of bounds shifts so we should check this.
|
@llvm/pr-subscribers-tablegen Author: Craig Topper (topperc) ChangesThe C operator has undefined behavior for out of bounds shifts so we should check this. Full diff: https://github.com/llvm/llvm-project/pull/132492.diff 1 Files Affected:
diff --git a/llvm/lib/TableGen/Record.cpp b/llvm/lib/TableGen/Record.cpp
index c5b9b670b6f42..655c4078697f3 100644
--- a/llvm/lib/TableGen/Record.cpp
+++ b/llvm/lib/TableGen/Record.cpp
@@ -1537,7 +1537,13 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
if (LHSi && RHSi) {
int64_t LHSv = LHSi->getValue(), RHSv = RHSi->getValue();
int64_t Result;
- switch (getOpcode()) {
+
+ unsigned Opc = getOpcode();
+ if ((Opc == SHL || Opc == SRA || Opc == SRL) && (RHSv < 0 || RHSv >= 64))
+ PrintFatalError(CurRec->getLoc(),
+ "Illegal operation: out of bounds shift");
+
+ switch (Opc) {
default: llvm_unreachable("Bad opcode!");
case ADD: Result = LHSv + RHSv; break;
case SUB: Result = LHSv - RHSv; break;
@@ -1556,7 +1562,7 @@ const Init *BinOpInit::Fold(const Record *CurRec) const {
case OR: Result = LHSv | RHSv; break;
case XOR: Result = LHSv ^ RHSv; break;
case SHL: Result = (uint64_t)LHSv << (uint64_t)RHSv; break;
- case SRA: Result = LHSv >> RHSv; break;
+ case SRA: Result = LHSv >> (uint64_t)RHSv; break;
case SRL: Result = (uint64_t)LHSv >> (uint64_t)RHSv; break;
}
return IntInit::get(getRecordKeeper(), Result);
|
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
|
Can you also add unit test? |
Done |
|
Ping |
s-barannikov
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The C operator has undefined behavior for out of bounds shifts so we should check this.