Commit 828342dd authored by Jakob Kummerow's avatar Jakob Kummerow Committed by Commit Bot

[ubsan][compiler] Avoid out-of-range casts to IrOpcode::Value

An enum-typed value should never have a value outside of that enum's
range.
This patch enforces that in Debug mode, while in Release mode keeping
the previous behavior of returning "UnknownOpcode" as the mnemonic for
illegal IrOpcode values to ease debugging.

Bug: v8:3770
Change-Id: I83a5a356f1fb7a266921940a4495f1d39a1823cd
Reviewed-on: https://chromium-review.googlesource.com/c/1436221Reviewed-by: 's avatarJaroslav Sevcik <jarin@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59102}
parent 1df72c6f
......@@ -26,6 +26,8 @@ char const* const kMnemonics[] = {
// static
char const* IrOpcode::Mnemonic(Value value) {
DCHECK_LE(0, static_cast<int>(value));
DCHECK_LE(static_cast<int>(value), IrOpcode::Value::kLast);
size_t const n = std::min<size_t>(value, arraysize(kMnemonics) - 1);
return kMnemonics[n];
}
......
......@@ -78,9 +78,6 @@ bool IsComparisonOpcode(IrOpcode::Value opcode) {
}
}
const IrOpcode::Value kInvalidOpcode = static_cast<IrOpcode::Value>(123456789);
char const* const kMnemonics[] = {
#define OPCODE(Opcode) #Opcode,
ALL_OP_LIST(OPCODE)
......@@ -96,42 +93,36 @@ const IrOpcode::Value kOpcodes[] = {
} // namespace
TEST(IrOpcodeTest, IsCommonOpcode) {
EXPECT_FALSE(IrOpcode::IsCommonOpcode(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsCommonOpcode(opcode), IrOpcode::IsCommonOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsControlOpcode) {
EXPECT_FALSE(IrOpcode::IsControlOpcode(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsControlOpcode(opcode), IrOpcode::IsControlOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsJsOpcode) {
EXPECT_FALSE(IrOpcode::IsJsOpcode(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsJsOpcode(opcode), IrOpcode::IsJsOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsConstantOpcode) {
EXPECT_FALSE(IrOpcode::IsConstantOpcode(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsConstantOpcode(opcode), IrOpcode::IsConstantOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsComparisonOpcode) {
EXPECT_FALSE(IrOpcode::IsComparisonOpcode(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsComparisonOpcode(opcode), IrOpcode::IsComparisonOpcode(opcode));
}
}
TEST(IrOpcodeTest, Mnemonic) {
EXPECT_STREQ("UnknownOpcode", IrOpcode::Mnemonic(kInvalidOpcode));
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_STREQ(kMnemonics[opcode], IrOpcode::Mnemonic(opcode));
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment