Skip to content

Commit 6cba93f

Browse files
committed
[RISCV] Add partial validation of S and X extension names to RISCVISAInfo::parseNormalizedArchString.
Extensions starting with 's' or 'x' should always be followed by an alphabetical character. I don't know of any crashes from this currently, but it seemed better to be defensive.
1 parent 62bed56 commit 6cba93f

File tree

3 files changed

+23
-5
lines changed

3 files changed

+23
-5
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,11 @@ RISCVISAInfo::parseNormalizedArchString(StringRef Arch) {
486486
return createStringError(errc::invalid_argument,
487487
"failed to parse major version number");
488488

489-
if (ExtName[0] == 'z' && (ExtName.size() == 1 || isDigit(ExtName[1])))
489+
if ((ExtName[0] == 'z' || ExtName[0] == 's' || ExtName[0] == 'x') &&
490+
(ExtName.size() == 1 || isDigit(ExtName[1])))
490491
return createStringError(errc::invalid_argument,
491-
"'z' must be followed by a letter");
492+
"'" + Twine(ExtName[0]) +
493+
"' must be followed by a letter");
492494

493495
ISAInfo->addExtension(ExtName, {MajorVersion, MinorVersion});
494496
}

llvm/test/tools/llvm-objdump/ELF/RISCV/unknown-arch-attr.test

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## The expected behavior is to ignore the unrecognized arch feature and
44
## continue to process the following arch features.
55
##
6-
## The object file has the "rv32i2p0_m2p0_x1p0" arch feature. "x1p0" is an
6+
## The object file has the "rv32i2p0_m2p0_y1p0" arch feature. "y1p0" is an
77
## unrecognized architecture extension. llvm-objdump will ignore it and decode
88
## "mul" instruction correctly according to "m2p0" in the arch feature.
99
##
@@ -34,5 +34,5 @@ Sections:
3434
Content: 3385C502
3535
- Name: .riscv.attributes
3636
Type: SHT_RISCV_ATTRIBUTES
37-
## The content is the encoding of the arch feature "rv32i2p0_m2p0_x1p0"
38-
Content: 412300000072697363760001190000000572763332693270305F6D3270305F7831703000
37+
## The content is the encoding of the arch feature "rv32i2p0_m2p0_y1p0"
38+
Content: 412300000072697363760001190000000572763332693270305F6D3270305F7931703000

llvm/unittests/TargetParser/RISCVISAInfoTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,22 @@ TEST(ParseNormalizedArchString, RejectsBadZ) {
6262
}
6363
}
6464

65+
TEST(ParseNormalizedArchString, RejectsBadS) {
66+
for (StringRef Input : {"rv64i2p0_s1p0", "rv32i2p0_s2a1p0"}) {
67+
EXPECT_EQ(
68+
toString(RISCVISAInfo::parseNormalizedArchString(Input).takeError()),
69+
"'s' must be followed by a letter");
70+
}
71+
}
72+
73+
TEST(ParseNormalizedArchString, RejectsBadX) {
74+
for (StringRef Input : {"rv64i2p0_x1p0", "rv32i2p0_x2a1p0"}) {
75+
EXPECT_EQ(
76+
toString(RISCVISAInfo::parseNormalizedArchString(Input).takeError()),
77+
"'x' must be followed by a letter");
78+
}
79+
}
80+
6581
TEST(ParseNormalizedArchString, AcceptsValidBaseISAsAndSetsXLen) {
6682
auto MaybeRV32I = RISCVISAInfo::parseNormalizedArchString("rv32i2p0");
6783
ASSERT_THAT_EXPECTED(MaybeRV32I, Succeeded());

0 commit comments

Comments
 (0)