Skip to content

Commit ecc65ed

Browse files
author
Ana Pazos
committedAug 24, 2018
[RISCV] Fixed Assertion`Kind == Immediate && "Invalid type access!"' failed.
Summary: Missing check for isImm() in some Immediate classes. This bug was uncovered by a LLVM MC Assembler Protocol Buffer Fuzzer for the RISC-V assembly language. Reviewers: hiraditya, asb Reviewed By: hiraditya, asb Subscribers: llvm-commits, hiraditya, kito-cheng, shiva0217, rkruppe, asb, rbar, johnrusso, simoncook, sabuasal, niosHD, zzheng, edward-jones, mgrang, rogfer01, MartinMosbeck, brucehoult, the_o, PkmX, jocewei Differential Revision: https://reviews.llvm.org/D50797 llvm-svn: 340674
1 parent 4ddd059 commit ecc65ed

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
 

‎llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

+18
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
322322
}
323323

324324
bool isSImm6() const {
325+
if (!isImm())
326+
return false;
325327
RISCVMCExpr::VariantKind VK;
326328
int64_t Imm;
327329
bool IsValid;
@@ -335,6 +337,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
335337
}
336338

337339
bool isSImm6NonZero() const {
340+
if (!isImm())
341+
return false;
338342
RISCVMCExpr::VariantKind VK;
339343
int64_t Imm;
340344
bool IsValid;
@@ -348,6 +352,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
348352
}
349353

350354
bool isCLUIImm() const {
355+
if (!isImm())
356+
return false;
351357
int64_t Imm;
352358
RISCVMCExpr::VariantKind VK;
353359
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -357,6 +363,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
357363
}
358364

359365
bool isUImm7Lsb00() const {
366+
if (!isImm())
367+
return false;
360368
int64_t Imm;
361369
RISCVMCExpr::VariantKind VK;
362370
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -365,6 +373,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
365373
}
366374

367375
bool isUImm8Lsb00() const {
376+
if (!isImm())
377+
return false;
368378
int64_t Imm;
369379
RISCVMCExpr::VariantKind VK;
370380
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -373,6 +383,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
373383
}
374384

375385
bool isUImm8Lsb000() const {
386+
if (!isImm())
387+
return false;
376388
int64_t Imm;
377389
RISCVMCExpr::VariantKind VK;
378390
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -383,6 +395,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
383395
bool isSImm9Lsb0() const { return isBareSimmNLsb0<9>(); }
384396

385397
bool isUImm9Lsb000() const {
398+
if (!isImm())
399+
return false;
386400
int64_t Imm;
387401
RISCVMCExpr::VariantKind VK;
388402
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -391,6 +405,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
391405
}
392406

393407
bool isUImm10Lsb00NonZero() const {
408+
if (!isImm())
409+
return false;
394410
int64_t Imm;
395411
RISCVMCExpr::VariantKind VK;
396412
bool IsConstantImm = evaluateConstantImm(Imm, VK);
@@ -428,6 +444,8 @@ struct RISCVOperand : public MCParsedAsmOperand {
428444
bool isSImm13Lsb0() const { return isBareSimmNLsb0<13>(); }
429445

430446
bool isSImm10Lsb0000NonZero() const {
447+
if (!isImm())
448+
return false;
431449
int64_t Imm;
432450
RISCVMCExpr::VariantKind VK;
433451
bool IsConstantImm = evaluateConstantImm(Imm, VK);
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# REQUIRES: asserts
2+
# RUN: not llvm-mc -triple riscv32 -mattr=+c,+f,+d < %s 2>&1 | FileCheck %s
3+
#
4+
# Fuzzed test cases produced by a LLVM MC Assembler
5+
# Protocol Buffer Fuzzer for the RISC-V assembly language.
6+
#
7+
8+
c.addi x13,f30,0 # CHECK: error: immediate must be non-zero in the range [-32, 31]
9+
c.swsp x0,(f14) # CHECK: error: immediate must be a multiple of 4 bytes in the range [0, 252]
10+
c.lui x4,x0 # CHECK: error: immediate must be in [0xfffe0, 0xfffff] or [1, 31]
11+
c.li x6,x6,x0,x0 # CHECK: error: immediate must be an integer in the range [-32, 31]
12+
c.addi16sp 2,(x0) # CHECK: error: invalid operand for instruction
13+
c.fsdsp f9,x0,0 # CHECK: error: immediate must be a multiple of 8 bytes in the range [0, 504]
14+
c.flw f15,x14,x0 # CHECK: error: immediate must be a multiple of 4 bytes in the range [0, 124]
15+
c.fld f8,f30,x17 # CHECK: error: immediate must be a multiple of 8 bytes in the range [0, 248]
16+
c.addi4spn x8,x2,x8 # CHECK: error: immediate must be a multiple of 4 bytes in the range [4, 1020]
17+

0 commit comments

Comments
 (0)