Skip to content

Commit c3e0585

Browse files
Ban untyped pointers in OpSizeOf
1 parent 9633b76 commit c3e0585

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

source/val/validate_misc.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ spv_result_t ValidateSizeOf(ValidationState_t& _, const Instruction* inst) {
9393
if (!_.IsConcreteType(pointer_id)) {
9494
return _.diag(SPV_ERROR_INVALID_DATA, inst)
9595
<< "OpSizeOf Pointer operand is not concrete.";
96+
} else if (_.FindDef(pointer_id)->opcode() ==
97+
spv::Op::OpTypeUntypedPointerKHR) {
98+
return _.diag(SPV_ERROR_INVALID_DATA, inst)
99+
<< "OpSizeOf Pointer operand is to an untyped pointer, which size "
100+
"is not well defined.";
96101
}
97102

98103
return SPV_SUCCESS;

test/val/val_misc_test.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,33 @@ TEST_F(ValidateMisc, SizeOfStructWithAbstract) {
155155
HasSubstr("OpSizeOf Pointer operand is not concrete"));
156156
}
157157

158+
TEST_F(ValidateMisc, SizeOfUntyped) {
159+
const std::string spirv = R"(
160+
OpCapability Addresses
161+
OpCapability UntypedPointersKHR
162+
OpCapability Kernel
163+
OpExtension "SPV_KHR_untyped_pointers"
164+
OpMemoryModel Physical64 OpenCL
165+
OpEntryPoint Kernel %f "f"
166+
%void = OpTypeVoid
167+
%i32 = OpTypeInt 32 0
168+
%fnTy = OpTypeFunction %void
169+
%untyped_ptr = OpTypeUntypedPointerKHR CrossWorkgroup
170+
%f = OpFunction %void None %fnTy
171+
%entry = OpLabel
172+
%s = OpSizeOf %i32 %untyped_ptr
173+
OpReturn
174+
OpFunctionEnd
175+
)";
176+
177+
CompileSuccessfully(spirv, SPV_ENV_UNIVERSAL_1_1);
178+
EXPECT_EQ(SPV_ERROR_INVALID_DATA,
179+
ValidateInstructions(SPV_ENV_UNIVERSAL_1_1));
180+
EXPECT_THAT(getDiagnosticString(),
181+
HasSubstr("OpSizeOf Pointer operand is to an untyped pointer, "
182+
"which size is not well defined"));
183+
}
184+
158185
TEST_F(ValidateMisc, SizeOfFloat) {
159186
const std::string spirv = R"(
160187
OpCapability Addresses

0 commit comments

Comments
 (0)