Skip to content

Commit 7ba72f1

Browse files
authored
spirv-val: Disallow stores according to VUID 06924 (#5368)
Ensure that the validator rejects stores to objects of types `OpTypeImage`, `OpTypeSampler`, `OpTypeSampledImage`, `OpTypeAccelerationStructureKHR`, and arrays of these types, according to `VUID-StandaloneSpirv-OpTypeImage-06924`. Guard the check behind the before_hlsl_legalization option, as sometimes we may have temporaries or local variables that are expected to get optimized away. Fixes #4796 Change-Id: Ie035c01c5f94e7bdfc16b5c6c85705f302b7bda3 Signed-off-by: Sven van Haastregt <[email protected]>
1 parent 01c8438 commit 7ba72f1

File tree

3 files changed

+171
-0
lines changed

3 files changed

+171
-0
lines changed

source/val/validate_memory.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1165,6 +1165,23 @@ spv_result_t ValidateStore(ValidationState_t& _, const Instruction* inst) {
11651165
}
11661166
}
11671167

1168+
if (spvIsVulkanEnv(_.context()->target_env) &&
1169+
!_.options()->before_hlsl_legalization) {
1170+
const auto isForbiddenType = [](const Instruction* type_inst) {
1171+
auto opcode = type_inst->opcode();
1172+
return opcode == spv::Op::OpTypeImage ||
1173+
opcode == spv::Op::OpTypeSampler ||
1174+
opcode == spv::Op::OpTypeSampledImage ||
1175+
opcode == spv::Op::OpTypeAccelerationStructureKHR;
1176+
};
1177+
if (_.ContainsType(object_type->id(), isForbiddenType)) {
1178+
return _.diag(SPV_ERROR_INVALID_ID, inst)
1179+
<< _.VkErrorID(6924)
1180+
<< "Cannot store to OpTypeImage, OpTypeSampler, "
1181+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR objects";
1182+
}
1183+
}
1184+
11681185
return SPV_SUCCESS;
11691186
}
11701187

source/val/validation_state.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2368,6 +2368,8 @@ std::string ValidationState_t::VkErrorID(uint32_t id,
23682368
return VUID_WRAP(VUID-StandaloneSpirv-Uniform-06807);
23692369
case 6808:
23702370
return VUID_WRAP(VUID-StandaloneSpirv-PushConstant-06808);
2371+
case 6924:
2372+
return VUID_WRAP(VUID-StandaloneSpirv-OpTypeImage-06924);
23712373
case 6925:
23722374
return VUID_WRAP(VUID-StandaloneSpirv-Uniform-06925);
23732375
case 7041:

test/val/val_memory_test.cpp

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3934,6 +3934,158 @@ OpMemoryModel Logical GLSL450
39343934
HasSubstr("Initializer type must match the data type"));
39353935
}
39363936

3937+
TEST_F(ValidateMemory, StoreToImage) {
3938+
const std::string spirv = R"(
3939+
OpCapability Shader
3940+
OpMemoryModel Logical GLSL450
3941+
OpEntryPoint GLCompute %main "main"
3942+
OpExecutionMode %main LocalSize 1 1 1
3943+
%void = OpTypeVoid
3944+
%int = OpTypeInt 32 0
3945+
%img = OpTypeImage %int 2D 2 0 0 2 R32i
3946+
%ptr_img = OpTypePointer Function %img
3947+
%void_fn = OpTypeFunction %void
3948+
%main = OpFunction %void None %void_fn
3949+
%entry = OpLabel
3950+
%var = OpVariable %ptr_img Function
3951+
%value = OpLoad %img %var
3952+
OpStore %var %value
3953+
OpReturn
3954+
OpFunctionEnd
3955+
)";
3956+
CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3957+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3958+
EXPECT_THAT(getDiagnosticString(),
3959+
AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
3960+
EXPECT_THAT(
3961+
getDiagnosticString(),
3962+
HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
3963+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
3964+
}
3965+
3966+
TEST_F(ValidateMemory, StoreToImageArray) {
3967+
const std::string spirv = R"(
3968+
OpCapability Shader
3969+
OpMemoryModel Logical GLSL450
3970+
OpEntryPoint GLCompute %main "main"
3971+
OpExecutionMode %main LocalSize 1 1 1
3972+
%void = OpTypeVoid
3973+
%int = OpTypeInt 32 0
3974+
%img = OpTypeImage %int 2D 2 0 0 2 R32i
3975+
%arr_size = OpConstant %int 5
3976+
%i = OpConstant %int 2
3977+
%arr_img = OpTypeArray %img %arr_size
3978+
%ptr_img = OpTypePointer Function %img
3979+
%ptr_arr_img = OpTypePointer Function %arr_img
3980+
%void_fn = OpTypeFunction %void
3981+
%main = OpFunction %void None %void_fn
3982+
%entry = OpLabel
3983+
%var = OpVariable %ptr_arr_img Function
3984+
%value = OpLoad %arr_img %var
3985+
OpStore %var %value
3986+
OpReturn
3987+
OpFunctionEnd
3988+
)";
3989+
CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
3990+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
3991+
EXPECT_THAT(getDiagnosticString(),
3992+
AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
3993+
EXPECT_THAT(
3994+
getDiagnosticString(),
3995+
HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
3996+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
3997+
}
3998+
3999+
TEST_F(ValidateMemory, StoreToSampler) {
4000+
const std::string spirv = R"(
4001+
OpCapability Shader
4002+
OpMemoryModel Logical GLSL450
4003+
OpEntryPoint GLCompute %main "main"
4004+
OpExecutionMode %main LocalSize 1 1 1
4005+
%void = OpTypeVoid
4006+
%smp = OpTypeSampler
4007+
%ptr_smp = OpTypePointer Function %smp
4008+
%void_fn = OpTypeFunction %void
4009+
%main = OpFunction %void None %void_fn
4010+
%entry = OpLabel
4011+
%var = OpVariable %ptr_smp Function
4012+
%value = OpLoad %smp %var
4013+
OpStore %var %value
4014+
OpReturn
4015+
OpFunctionEnd
4016+
)";
4017+
CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
4018+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4019+
EXPECT_THAT(getDiagnosticString(),
4020+
AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
4021+
EXPECT_THAT(
4022+
getDiagnosticString(),
4023+
HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
4024+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
4025+
}
4026+
4027+
TEST_F(ValidateMemory, StoreToSampledImage) {
4028+
const std::string spirv = R"(
4029+
OpCapability Shader
4030+
OpMemoryModel Logical GLSL450
4031+
OpEntryPoint GLCompute %main "main"
4032+
OpExecutionMode %main LocalSize 1 1 1
4033+
%void = OpTypeVoid
4034+
%int = OpTypeInt 32 0
4035+
%img = OpTypeImage %int 2D 2 0 0 1 R32i
4036+
%samp_img = OpTypeSampledImage %img
4037+
%ptr_samp_img = OpTypePointer Function %samp_img
4038+
%void_fn = OpTypeFunction %void
4039+
%main = OpFunction %void None %void_fn
4040+
%entry = OpLabel
4041+
%var = OpVariable %ptr_samp_img Function
4042+
%value = OpLoad %samp_img %var
4043+
OpStore %var %value
4044+
OpReturn
4045+
OpFunctionEnd
4046+
)";
4047+
4048+
CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
4049+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4050+
EXPECT_THAT(getDiagnosticString(),
4051+
AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
4052+
EXPECT_THAT(
4053+
getDiagnosticString(),
4054+
HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
4055+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
4056+
}
4057+
4058+
TEST_F(ValidateMemory, StoreToAccelarationStructureKHR) {
4059+
const std::string spirv = R"(
4060+
OpCapability Shader
4061+
OpCapability RayQueryKHR
4062+
OpExtension "SPV_KHR_ray_query"
4063+
OpMemoryModel Logical GLSL450
4064+
OpEntryPoint GLCompute %main "main"
4065+
OpExecutionMode %main LocalSize 1 1 1
4066+
%void = OpTypeVoid
4067+
%as = OpTypeAccelerationStructureKHR
4068+
%ptr_as = OpTypePointer Function %as
4069+
%void_fn = OpTypeFunction %void
4070+
%main = OpFunction %void None %void_fn
4071+
%entry = OpLabel
4072+
%var = OpVariable %ptr_as Function
4073+
%value = OpLoad %as %var
4074+
OpStore %var %value
4075+
OpReturn
4076+
OpFunctionEnd
4077+
)";
4078+
4079+
CompileSuccessfully(spirv, SPV_ENV_VULKAN_1_1);
4080+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_VULKAN_1_1));
4081+
EXPECT_THAT(getDiagnosticString(),
4082+
AnyVUID("VUID-StandaloneSpirv-OpTypeImage-06924"));
4083+
EXPECT_THAT(
4084+
getDiagnosticString(),
4085+
HasSubstr("Cannot store to OpTypeImage, OpTypeSampler, "
4086+
"OpTypeSampledImage, or OpTypeAccelerationStructureKHR"));
4087+
}
4088+
39374089
TEST_F(ValidateMemory, StoreToUniformBlock) {
39384090
const std::string spirv = R"(
39394091
OpCapability Shader

0 commit comments

Comments
 (0)