Skip to content

Commit c62299a

Browse files
spirv-val: Fix crash with bad OpUntypedArrayLengthKHR (#6493)
working on the internal extension (and being new to Untyped Pointers still) I accidentally set the `OpUntypedArrayLengthKHR` Pointer ID to be `OpTypeUntypedPointerKHR` and it just crashed in `spirv-val`... so now it doesn't crash and provides hopefully a likely good answer for the next person like me ... the issue is `pointer` and `type` are really easy to mix up... `OpTypeUntyped` will always be my favorite thing to explain to those new to SPIR-V 😄
1 parent a1eace9 commit c62299a

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed

source/val/validate_memory.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2140,9 +2140,10 @@ spv_result_t ValidateArrayLength(ValidationState_t& state,
21402140
auto pointer_ty_id = state.GetOperandTypeId(inst, (untyped ? 3 : 2));
21412141
auto pointer_ty = state.FindDef(pointer_ty_id);
21422142
if (untyped) {
2143-
if (pointer_ty->opcode() != spv::Op::OpTypeUntypedPointerKHR) {
2143+
if (!pointer_ty ||
2144+
pointer_ty->opcode() != spv::Op::OpTypeUntypedPointerKHR) {
21442145
return state.diag(SPV_ERROR_INVALID_ID, inst)
2145-
<< "Pointer must be an untyped pointer";
2146+
<< "Pointer must be an untyped pointer object";
21462147
}
21472148
} else if (pointer_ty->opcode() != spv::Op::OpTypePointer) {
21482149
return state.diag(SPV_ERROR_INVALID_ID, inst)

test/val/val_memory_test.cpp

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8010,7 +8010,46 @@ OpFunctionEnd
80108010
CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
80118011
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
80128012
EXPECT_THAT(getDiagnosticString(),
8013-
HasSubstr("Pointer must be an untyped pointer"));
8013+
HasSubstr("Pointer must be an untyped pointer object"));
8014+
}
8015+
8016+
TEST_F(ValidateMemory, UntypedArrayLengthBadPointer2) {
8017+
const std::string spirv = R"(
8018+
OpCapability Shader
8019+
OpCapability UntypedPointersKHR
8020+
OpCapability Int64
8021+
OpExtension "SPV_KHR_untyped_pointers"
8022+
OpMemoryModel Logical GLSL450
8023+
OpEntryPoint GLCompute %main "main" %b
8024+
OpExecutionMode %main LocalSize 1 1 1
8025+
OpDecorate %_runtimearr_float ArrayStride 4
8026+
OpDecorate %B Block
8027+
OpMemberDecorate %B 0 NonWritable
8028+
OpMemberDecorate %B 0 Offset 0
8029+
OpDecorate %b NonWritable
8030+
OpDecorate %b Binding 0
8031+
OpDecorate %b DescriptorSet 0
8032+
%void = OpTypeVoid
8033+
%4 = OpTypeFunction %void
8034+
%float = OpTypeFloat 32
8035+
%_runtimearr_float = OpTypeRuntimeArray %float
8036+
%B = OpTypeStruct %_runtimearr_float
8037+
%ptr = OpTypeUntypedPointerKHR StorageBuffer
8038+
%b = OpUntypedVariableKHR %ptr StorageBuffer %B
8039+
%ulong = OpTypeInt 64 0
8040+
%long = OpTypeInt 64 1
8041+
%main = OpFunction %void None %4
8042+
%6 = OpLabel
8043+
%13 = OpUntypedArrayLengthKHR %ulong %b %ptr 0
8044+
%15 = OpBitcast %long %13
8045+
OpReturn
8046+
OpFunctionEnd
8047+
)";
8048+
8049+
CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_3);
8050+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions(SPV_ENV_UNIVERSAL_1_3));
8051+
EXPECT_THAT(getDiagnosticString(),
8052+
HasSubstr("Pointer must be an untyped pointer object"));
80148053
}
80158054

80168055
TEST_F(ValidateMemory, UntypedArrayLengtBadStruct) {

0 commit comments

Comments
 (0)