v8/test/unittests/compiler/opcodes-unittest.cc
Jakob Kummerow 828342dd7f [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/1436221
Reviewed-by: Jaroslav Sevcik <jarin@chromium.org>
Commit-Queue: Jakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#59102}
2019-01-25 20:55:25 +00:00

134 lines
2.8 KiB
C++

// Copyright 2015 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/compiler/opcodes.h"
#include "testing/gtest-support.h"
namespace v8 {
namespace internal {
namespace compiler {
namespace {
bool IsCommonOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
COMMON_OP_LIST(OPCODE)
CONTROL_OP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
bool IsControlOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
CONTROL_OP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
bool IsJsOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
JS_OP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
bool IsConstantOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
CONSTANT_OP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
bool IsComparisonOpcode(IrOpcode::Value opcode) {
switch (opcode) {
#define OPCODE(Opcode) \
case IrOpcode::k##Opcode: \
return true;
JS_COMPARE_BINOP_LIST(OPCODE)
SIMPLIFIED_COMPARE_BINOP_LIST(OPCODE)
MACHINE_COMPARE_BINOP_LIST(OPCODE)
#undef OPCODE
default:
return false;
}
}
char const* const kMnemonics[] = {
#define OPCODE(Opcode) #Opcode,
ALL_OP_LIST(OPCODE)
#undef OPCODE
};
const IrOpcode::Value kOpcodes[] = {
#define OPCODE(Opcode) IrOpcode::k##Opcode,
ALL_OP_LIST(OPCODE)
#undef OPCODE
};
} // namespace
TEST(IrOpcodeTest, IsCommonOpcode) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsCommonOpcode(opcode), IrOpcode::IsCommonOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsControlOpcode) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsControlOpcode(opcode), IrOpcode::IsControlOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsJsOpcode) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsJsOpcode(opcode), IrOpcode::IsJsOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsConstantOpcode) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsConstantOpcode(opcode), IrOpcode::IsConstantOpcode(opcode));
}
}
TEST(IrOpcodeTest, IsComparisonOpcode) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_EQ(IsComparisonOpcode(opcode), IrOpcode::IsComparisonOpcode(opcode));
}
}
TEST(IrOpcodeTest, Mnemonic) {
TRACED_FOREACH(IrOpcode::Value, opcode, kOpcodes) {
EXPECT_STREQ(kMnemonics[opcode], IrOpcode::Mnemonic(opcode));
}
}
} // namespace compiler
} // namespace internal
} // namespace v8