Validate sign of int types. (#2549)
Fixes https://crbug.com/959011.
diff --git a/source/val/validate_type.cpp b/source/val/validate_type.cpp
index ad72a37..edfede8 100644
--- a/source/val/validate_type.cpp
+++ b/source/val/validate_type.cpp
@@ -14,11 +14,10 @@
// Ensures type declarations are unique unless allowed by the specification.
-#include "source/val/validate.h"
-
#include "source/opcode.h"
#include "source/spirv_target_env.h"
#include "source/val/instruction.h"
+#include "source/val/validate.h"
#include "source/val/validation_state.h"
namespace spvtools {
@@ -67,6 +66,16 @@
return SPV_SUCCESS;
}
+spv_result_t ValidateTypeInt(ValidationState_t& _, const Instruction* inst) {
+ const auto signedness_index = 2;
+ const auto signedness = inst->GetOperandAs<uint32_t>(signedness_index);
+ if (signedness != 0 && signedness != 1) {
+ return _.diag(SPV_ERROR_INVALID_VALUE, inst)
+ << "OpTypeInt has invalid signedness:";
+ }
+ return SPV_SUCCESS;
+}
+
spv_result_t ValidateTypeVector(ValidationState_t& _, const Instruction* inst) {
const auto component_index = 1;
const auto component_id = inst->GetOperandAs<uint32_t>(component_index);
@@ -439,6 +448,9 @@
if (auto error = ValidateUniqueness(_, inst)) return error;
switch (inst->opcode()) {
+ case SpvOpTypeInt:
+ if (auto error = ValidateTypeInt(_, inst)) return error;
+ break;
case SpvOpTypeVector:
if (auto error = ValidateTypeVector(_, inst)) return error;
break;
diff --git a/test/val/val_literals_test.cpp b/test/val/val_literals_test.cpp
index 4ef6186..6eadf32 100644
--- a/test/val/val_literals_test.cpp
+++ b/test/val/val_literals_test.cpp
@@ -85,6 +85,16 @@
ASSERT_EQ(SPV_SUCCESS, ValidateInstructions());
}
+TEST_F(ValidateLiterals, InvalidInt) {
+ std::string str = GenerateShaderCode() + R"(
+%11 = OpTypeInt 32 90
+ )";
+ CompileSuccessfully(str);
+ EXPECT_EQ(SPV_ERROR_INVALID_VALUE, ValidateInstructions());
+ EXPECT_THAT(getDiagnosticString(),
+ HasSubstr("OpTypeInt has invalid signedness:"));
+}
+
TEST_P(ValidateLiteralsShader, LiteralsShaderBad) {
std::string str = GenerateShaderCode() + GetParam();
std::string inst_id = "11";