Skip to content

Commit 21605d2

Browse files
authored
[val] Disallow duplicate targets for OpGroupDecorate (KhronosGroup#6720)
Fix https://crbug.com/oss-fuzz/497811679 * Check for duplicate targets in OpGroupDecorate
1 parent 744fe88 commit 21605d2

2 files changed

Lines changed: 27 additions & 0 deletions

File tree

source/val/validate_annotation.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
// See the License for the specific language governing permissions and
1515
// limitations under the License.
1616

17+
#include <unordered_set>
18+
1719
#include "source/opcode.h"
1820
#include "source/spirv_target_env.h"
1921
#include "source/val/instruction.h"
@@ -548,6 +550,7 @@ spv_result_t ValidateGroupDecorate(ValidationState_t& _,
548550
<< "OpGroupDecorate Decoration group <id> "
549551
<< _.getIdName(decoration_group_id) << " is not a decoration group.";
550552
}
553+
std::unordered_set<uint32_t> seen;
551554
for (unsigned i = 1; i < inst->operands().size(); ++i) {
552555
auto target_id = inst->GetOperandAs<uint32_t>(i);
553556
auto target = _.FindDef(target_id);
@@ -556,6 +559,10 @@ spv_result_t ValidateGroupDecorate(ValidationState_t& _,
556559
<< "OpGroupDecorate may not target OpDecorationGroup <id> "
557560
<< _.getIdName(target_id);
558561
}
562+
if (!seen.insert(target_id).second) {
563+
return _.diag(SPV_ERROR_INVALID_ID, inst)
564+
<< "Targets contains duplicate id " << _.getIdName(target_id);
565+
}
559566
}
560567
return SPV_SUCCESS;
561568
}

test/val/val_annotation_test.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,26 @@ OpDecorate %ptr ArrayStride 4
273273
EXPECT_EQ(SPV_SUCCESS, ValidateInstructions());
274274
}
275275

276+
TEST_F(DecorationTest, DecorationGroupDuplicateTarget) {
277+
const std::string text = R"(
278+
OpCapability ClipDistance
279+
OpCapability Linkage
280+
OpMemoryModel Logical Simple
281+
%4 = OpDecorationGroup
282+
OpGroupDecorate %4 %2 %2
283+
%void = OpTypeVoid
284+
%19 = OpTypeFunction %void
285+
%2 = OpFunction %void None %19
286+
%553590816 = OpLabel
287+
OpUnreachable
288+
OpFunctionEnd
289+
)";
290+
291+
CompileSuccessfully(text, SPV_ENV_UNIVERSAL_1_0);
292+
EXPECT_EQ(SPV_ERROR_INVALID_ID, ValidateInstructions());
293+
EXPECT_THAT(getDiagnosticString(),
294+
HasSubstr("Targets contains duplicate id '2[%2]'"));
295+
}
276296
using MemberOnlyDecorations = spvtest::ValidateBase<std::string>;
277297

278298
TEST_P(MemberOnlyDecorations, MemberDecoration) {

0 commit comments

Comments
 (0)