[RISCV] Fix uninitialized variable after call to evaluateConstantImm

For llvm/test/MC/RISCV/rv64i-aliases-invalid.s, UBSan reports:

lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp:371:9: runtime error:
load of value 3879186881, which is not a valid value for type
'RISCVMCExpr::VariantKind'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp:371:9 in

It turns out that evaluateConstantImm does not set `VK` and it remains
unitialized when doing comparisons in `isImmXLenLI()`.

Differential Revision: https://reviews.llvm.org/D65347

llvm-svn: 367230
This commit is contained in:
Francis Visoiu Mistrih 2019-07-29 15:52:13 +00:00
parent 89fb9e8ce1
commit d42289e291
1 changed files with 22 additions and 22 deletions

View File

@ -276,7 +276,7 @@ public:
// modifiers and isShiftedInt<N-1, 1>(Op). // modifiers and isShiftedInt<N-1, 1>(Op).
template <int N> bool isBareSimmNLsb0() const { template <int N> bool isBareSimmNLsb0() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
@ -292,7 +292,7 @@ public:
bool isBareSymbol() const { bool isBareSymbol() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
// Must be of 'immediate' type but not a constant. // Must be of 'immediate' type but not a constant.
if (!isImm() || evaluateConstantImm(getImm(), Imm, VK)) if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
return false; return false;
@ -302,7 +302,7 @@ public:
bool isCallSymbol() const { bool isCallSymbol() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
// Must be of 'immediate' type but not a constant. // Must be of 'immediate' type but not a constant.
if (!isImm() || evaluateConstantImm(getImm(), Imm, VK)) if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
return false; return false;
@ -313,7 +313,7 @@ public:
bool isTPRelAddSymbol() const { bool isTPRelAddSymbol() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
// Must be of 'immediate' type but not a constant. // Must be of 'immediate' type but not a constant.
if (!isImm() || evaluateConstantImm(getImm(), Imm, VK)) if (!isImm() || evaluateConstantImm(getImm(), Imm, VK))
return false; return false;
@ -364,7 +364,7 @@ public:
bool isImmXLenLI() const { bool isImmXLenLI() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
@ -378,7 +378,7 @@ public:
bool isUImmLog2XLen() const { bool isUImmLog2XLen() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
if (!evaluateConstantImm(getImm(), Imm, VK) || if (!evaluateConstantImm(getImm(), Imm, VK) ||
@ -389,7 +389,7 @@ public:
bool isUImmLog2XLenNonZero() const { bool isUImmLog2XLenNonZero() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
if (!evaluateConstantImm(getImm(), Imm, VK) || if (!evaluateConstantImm(getImm(), Imm, VK) ||
@ -402,7 +402,7 @@ public:
bool isUImm5() const { bool isUImm5() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
@ -411,7 +411,7 @@ public:
bool isUImm5NonZero() const { bool isUImm5NonZero() const {
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
if (!isImm()) if (!isImm())
return false; return false;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
@ -422,7 +422,7 @@ public:
bool isSImm6() const { bool isSImm6() const {
if (!isImm()) if (!isImm())
return false; return false;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
int64_t Imm; int64_t Imm;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isInt<6>(Imm) && return IsConstantImm && isInt<6>(Imm) &&
@ -432,7 +432,7 @@ public:
bool isSImm6NonZero() const { bool isSImm6NonZero() const {
if (!isImm()) if (!isImm())
return false; return false;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
int64_t Imm; int64_t Imm;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isInt<6>(Imm) && (Imm != 0) && return IsConstantImm && isInt<6>(Imm) && (Imm != 0) &&
@ -443,7 +443,7 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && (Imm != 0) && return IsConstantImm && (Imm != 0) &&
(isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) && (isUInt<5>(Imm) || (Imm >= 0xfffe0 && Imm <= 0xfffff)) &&
@ -454,7 +454,7 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isShiftedUInt<5, 2>(Imm) && return IsConstantImm && isShiftedUInt<5, 2>(Imm) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
@ -464,7 +464,7 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isShiftedUInt<6, 2>(Imm) && return IsConstantImm && isShiftedUInt<6, 2>(Imm) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
@ -474,7 +474,7 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isShiftedUInt<5, 3>(Imm) && return IsConstantImm && isShiftedUInt<5, 3>(Imm) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
@ -486,7 +486,7 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isShiftedUInt<6, 3>(Imm) && return IsConstantImm && isShiftedUInt<6, 3>(Imm) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
@ -496,14 +496,14 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) && return IsConstantImm && isShiftedUInt<8, 2>(Imm) && (Imm != 0) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
} }
bool isSImm12() const { bool isSImm12() const {
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
int64_t Imm; int64_t Imm;
bool IsValid; bool IsValid;
if (!isImm()) if (!isImm())
@ -527,14 +527,14 @@ public:
if (!isImm()) if (!isImm())
return false; return false;
int64_t Imm; int64_t Imm;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK); bool IsConstantImm = evaluateConstantImm(getImm(), Imm, VK);
return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) && return IsConstantImm && (Imm != 0) && isShiftedInt<6, 4>(Imm) &&
VK == RISCVMCExpr::VK_RISCV_None; VK == RISCVMCExpr::VK_RISCV_None;
} }
bool isUImm20LUI() const { bool isUImm20LUI() const {
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
int64_t Imm; int64_t Imm;
bool IsValid; bool IsValid;
if (!isImm()) if (!isImm())
@ -552,7 +552,7 @@ public:
} }
bool isUImm20AUIPC() const { bool isUImm20AUIPC() const {
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
int64_t Imm; int64_t Imm;
bool IsValid; bool IsValid;
if (!isImm()) if (!isImm())
@ -664,7 +664,7 @@ public:
void addExpr(MCInst &Inst, const MCExpr *Expr) const { void addExpr(MCInst &Inst, const MCExpr *Expr) const {
assert(Expr && "Expr shouldn't be null!"); assert(Expr && "Expr shouldn't be null!");
int64_t Imm = 0; int64_t Imm = 0;
RISCVMCExpr::VariantKind VK; RISCVMCExpr::VariantKind VK = RISCVMCExpr::VK_RISCV_None;
bool IsConstant = evaluateConstantImm(Expr, Imm, VK); bool IsConstant = evaluateConstantImm(Expr, Imm, VK);
if (IsConstant) if (IsConstant)