Skip to content

Commit

Permalink
Validate storage class of target pointer for OpStore
Browse files Browse the repository at this point in the history
  • Loading branch information
nsubtil authored and dneto0 committed Nov 2, 2017
1 parent 9d6cc26 commit 2dddb81
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
17 changes: 17 additions & 0 deletions source/validate_id.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1223,6 +1223,23 @@ bool idUsage::isValid<SpvOpStore>(const spv_instruction_t* inst,
return false;
}

// validate storage class
{
uint32_t dataType;
uint32_t storageClass;
if (!module_.GetPointerTypeInfo(pointerType->id(), &dataType, &storageClass)) {
DIAG(pointerIndex) << "OpStore Pointer <id> '"<< inst->words[pointerIndex] << "' is not pointer type";
return false;
}

if (storageClass == SpvStorageClassUniformConstant ||
storageClass == SpvStorageClassInput ||
storageClass == SpvStorageClassPushConstant) {
DIAG(pointerIndex) << "OpStore Pointer <id> '" << inst->words[pointerIndex] << "' storage class is read-only";
return false;
}
}

auto objectIndex = 2;
auto object = module_.FindDef(inst->words[objectIndex]);
if (!object || !object->type_id()) {
Expand Down
49 changes: 44 additions & 5 deletions test/val/val_id_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2092,7 +2092,7 @@ TEST_F(ValidateIdWithMessage, OpStoreGood) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
%3 = OpTypePointer UniformConstant %2
%3 = OpTypePointer Uniform %2
%4 = OpTypeFunction %1
%5 = OpConstant %2 42
%6 = OpVariable %3 UniformConstant
Expand Down Expand Up @@ -2187,7 +2187,7 @@ TEST_F(ValidateIdWithMessage, OpStoreObjectGood) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
%3 = OpTypePointer UniformConstant %2
%3 = OpTypePointer Uniform %2
%4 = OpTypeFunction %1
%5 = OpConstant %2 42
%6 = OpVariable %3 UniformConstant
Expand All @@ -2206,7 +2206,7 @@ TEST_F(ValidateIdWithMessage, OpStoreTypeBad) {
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
%9 = OpTypeFloat 32
%3 = OpTypePointer UniformConstant %2
%3 = OpTypePointer Uniform %2
%4 = OpTypeFunction %1
%5 = OpConstant %9 3.14
%6 = OpVariable %3 UniformConstant
Expand Down Expand Up @@ -2430,7 +2430,7 @@ TEST_F(ValidateIdWithMessage, OpStoreVoid) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
%3 = OpTypePointer UniformConstant %2
%3 = OpTypePointer Uniform %2
%4 = OpTypeFunction %1
%6 = OpVariable %3 UniformConstant
%7 = OpFunction %1 None %4
Expand All @@ -2449,7 +2449,7 @@ TEST_F(ValidateIdWithMessage, OpStoreLabel) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
%2 = OpTypeInt 32 0
%3 = OpTypePointer UniformConstant %2
%3 = OpTypePointer Uniform %2
%4 = OpTypeFunction %1
%6 = OpVariable %3 UniformConstant
%7 = OpFunction %1 None %4
Expand Down Expand Up @@ -2481,6 +2481,45 @@ TEST_F(ValidateIdWithMessage, DISABLED_OpStoreFunction) {
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
}

TEST_F(ValidateIdWithMessage, OpStoreBuiltin) {
string spirv = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main" %gl_GlobalInvocationID
OpExecutionMode %main LocalSize 1 1 1
OpSource GLSL 450
OpName %main "main"
OpName %gl_GlobalInvocationID "gl_GlobalInvocationID"
OpDecorate %gl_GlobalInvocationID BuiltIn GlobalInvocationId
%int = OpTypeInt 32 1
%uint = OpTypeInt 32 0
%v3uint = OpTypeVector %uint 3
%_ptr_Input_v3uint = OpTypePointer Input %v3uint
%gl_GlobalInvocationID = OpVariable %_ptr_Input_v3uint Input
%zero = OpConstant %uint 0
%v3uint_000 = OpConstantComposite %v3uint %zero %zero %zero
%void = OpTypeVoid
%voidfunc = OpTypeFunction %void
%main = OpFunction %void None %voidfunc
%lmain = OpLabel
OpStore %gl_GlobalInvocationID %v3uint_000
OpReturn
OpFunctionEnd
)";

CompileSuccessfully(spirv.c_str());
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
EXPECT_THAT(getDiagnosticString(),
HasSubstr("storage class is read-only"));
}

TEST_F(ValidateIdWithMessage, OpCopyMemoryGood) {
string spirv = kGLSL450MemoryModel + R"(
%1 = OpTypeVoid
Expand Down

0 comments on commit 2dddb81

Please sign in to comment.