Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate operand type before operating on it #5092

Merged
merged 1 commit into from
Jan 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Validate operand type before operating on it
Fixes https://crbug.com/oss-fuzz/52921

* Validate the data operand of OpBitCount before trying to get its
  dimension
  • Loading branch information
alan-baker committed Jan 31, 2023
commit 6ca6ae29598b6235b394f443f7c924070424b4f2
5 changes: 3 additions & 2 deletions source/val/validate_bitwise.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,14 @@ spv_result_t BitwisePass(ValidationState_t& _, const Instruction* inst) {
<< spvOpcodeString(opcode);

const uint32_t base_type = _.GetOperandTypeId(inst, 2);
const uint32_t base_dimension = _.GetDimension(base_type);
const uint32_t result_dimension = _.GetDimension(result_type);

if (spv_result_t error = ValidateBaseType(_, inst, base_type)) {
return error;
}

const uint32_t base_dimension = _.GetDimension(base_type);
const uint32_t result_dimension = _.GetDimension(result_type);

if (base_dimension != result_dimension)
return _.diag(SPV_ERROR_INVALID_DATA, inst)
<< "Expected Base dimension to be equal to Result Type "
Expand Down
26 changes: 26 additions & 0 deletions test/val/val_bitwise_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,32 @@ TEST_F(ValidateBitwise, OpBitCountNot32Vulkan) {
HasSubstr("Expected 32-bit int type for Base operand: BitCount"));
}

TEST_F(ValidateBitwise, OpBitCountPointer) {
const std::string body = R"(
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
%void = OpTypeVoid
%int = OpTypeInt 32 0
%ptr_int = OpTypePointer Function %int
%void_fn = OpTypeFunction %void
%main = OpFunction %void None %void_fn
%entry = OpLabel
%var = OpVariable %ptr_int Function
%count = OpBitCount %int %var
OpReturn
OpFunctionEnd
)";

CompileSuccessfully(body);
ASSERT_EQ(SPV_ERROR_INVALID_DATA, ValidateInstructions());
EXPECT_THAT(
getDiagnosticString(),
HasSubstr(
"Expected int scalar or vector type for Base operand: BitCount"));
}

} // namespace
} // namespace val
} // namespace spvtools