Skip to content

Commit b3be6f2

Browse files
authored
Correct metadata load for NodeRecordType for robustness (microsoft#6277)
Metadata loading for node record properties assumed fixed positions for values in a (key,value) property list, skipping and ignoring the keys. This was not correct, fragile, and not the intended implementation of the key,value property pattern. There is no functional change, but this makes the loader robust to additional properties instead of crashing or loading the wrong thing. Fixes microsoft#6276
1 parent 69a64d8 commit b3be6f2

File tree

3 files changed

+88
-14
lines changed

3 files changed

+88
-14
lines changed

include/dxc/DXIL/DxilMetadataHelper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,7 @@ class DxilMDHelper {
625625

626626
llvm::MDTuple *EmitDxilNodeIOState(const NodeIOProperties &Node);
627627
hlsl::NodeIOProperties LoadDxilNodeIOState(const llvm::MDOperand &MDO);
628+
hlsl::NodeRecordType LoadDxilNodeRecordType(const llvm::MDOperand &MDO);
628629

629630
void EmitDxilNodeState(std::vector<llvm::Metadata *> &MDVals,
630631
const DxilFunctionProps &props);

lib/DXIL/DxilMetadataHelper.cpp

Lines changed: 41 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2824,6 +2824,46 @@ DxilMDHelper::EmitDxilNodeIOState(const hlsl::NodeIOProperties &Node) {
28242824
return MDNode::get(m_Ctx, MDVals);
28252825
}
28262826

2827+
NodeRecordType
2828+
DxilMDHelper::LoadDxilNodeRecordType(const llvm::MDOperand &MDO) {
2829+
const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
2830+
IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
2831+
IFTBOOL((pTupleMD->getNumOperands() & 0x1) == 0,
2832+
DXC_E_INCORRECT_DXIL_METADATA);
2833+
2834+
NodeRecordType Record = {};
2835+
for (unsigned iNode = 0; iNode < pTupleMD->getNumOperands(); iNode += 2) {
2836+
unsigned Tag = DxilMDHelper::ConstMDToUint32(pTupleMD->getOperand(iNode));
2837+
const MDOperand &MDO = pTupleMD->getOperand(iNode + 1);
2838+
IFTBOOL(MDO.get() != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
2839+
2840+
switch (Tag) {
2841+
case DxilMDHelper::kDxilNodeRecordSizeTag: {
2842+
Record.size = ConstMDToUint32(MDO);
2843+
} break;
2844+
case DxilMDHelper::kDxilNodeSVDispatchGridTag: {
2845+
MDTuple *pSVDTupleMD = cast<MDTuple>(MDO.get());
2846+
// < 3 if fatal
2847+
IFTBOOL(pSVDTupleMD->getNumOperands() >= 3,
2848+
DXC_E_INCORRECT_DXIL_METADATA);
2849+
// > 3 is extra metadata, validator will fail.
2850+
if (pSVDTupleMD->getNumOperands() > 3)
2851+
m_bExtraMetadata = true;
2852+
Record.SV_DispatchGrid.ByteOffset =
2853+
ConstMDToUint32(pSVDTupleMD->getOperand(0));
2854+
Record.SV_DispatchGrid.ComponentType = static_cast<DXIL::ComponentType>(
2855+
ConstMDToUint32(pSVDTupleMD->getOperand(1)));
2856+
Record.SV_DispatchGrid.NumComponents =
2857+
ConstMDToUint32(pSVDTupleMD->getOperand(2));
2858+
} break;
2859+
default:
2860+
m_bExtraMetadata = true;
2861+
break;
2862+
}
2863+
}
2864+
return Record;
2865+
}
2866+
28272867
NodeIOProperties DxilMDHelper::LoadDxilNodeIOState(const llvm::MDOperand &MDO) {
28282868
const MDTuple *pTupleMD = dyn_cast<MDTuple>(MDO.get());
28292869
IFTBOOL(pTupleMD != nullptr, DXC_E_INCORRECT_DXIL_METADATA);
@@ -2841,20 +2881,7 @@ NodeIOProperties DxilMDHelper::LoadDxilNodeIOState(const llvm::MDOperand &MDO) {
28412881
Node.Flags = NodeFlags(ConstMDToUint32(MDO));
28422882
} break;
28432883
case DxilMDHelper::kDxilNodeRecordTypeTag: {
2844-
MDTuple *pTupleMD = cast<MDTuple>(MDO.get());
2845-
Node.RecordType.size = ConstMDToUint32(pTupleMD->getOperand(1));
2846-
if (pTupleMD->getNumOperands() > 2) {
2847-
DXASSERT(pTupleMD->getNumOperands() == 4,
2848-
"incorrect number of operands");
2849-
MDTuple *pSVDTupleMD = cast<MDTuple>(pTupleMD->getOperand(3));
2850-
Node.RecordType.SV_DispatchGrid.ByteOffset =
2851-
ConstMDToUint32(pSVDTupleMD->getOperand(0));
2852-
Node.RecordType.SV_DispatchGrid.ComponentType =
2853-
static_cast<DXIL::ComponentType>(
2854-
ConstMDToUint32(pSVDTupleMD->getOperand(1)));
2855-
Node.RecordType.SV_DispatchGrid.NumComponents =
2856-
ConstMDToUint32(pSVDTupleMD->getOperand(2));
2857-
}
2884+
Node.RecordType = LoadDxilNodeRecordType(MDO);
28582885
} break;
28592886
case DxilMDHelper::kDxilNodeOutputArraySizeTag: {
28602887
Node.OutputArraySize = ConstMDToUint32(MDO);
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
; RUN: %dxilver 1.8 | %D3DReflect %s | FileCheck %s -check-prefixes=RDAT
2+
3+
; Make sure NodeRecordType metdata loading is robust to tag,value list ordering.
4+
5+
; RDAT: FunctionTable[{{.*}}] = {
6+
; RDAT-LABEL: UnmangledName: "node01"
7+
; RDAT: Inputs: <11:RecordArrayRef<IONode>[1]> = {
8+
; RDAT: AttribKind: RecordSizeInBytes
9+
; RDAT-NEXT: RecordSizeInBytes: 12
10+
; RDAT: AttribKind: RecordDispatchGrid
11+
; RDAT-NEXT: RecordDispatchGrid: <RecordDispatchGrid>
12+
; RDAT-NEXT: ByteOffset: 0
13+
; RDAT-NEXT: ComponentNumAndType: 23
14+
; RDAT-LABEL: RecordTable
15+
16+
target datalayout = "e-m:e-p:32:32-i1:32-i8:32-i16:32-i32:32-i64:64-f16:32-f32:32-f64:64-n8:16:32:64"
17+
target triple = "dxil-ms-dx"
18+
19+
define void @node01() {
20+
ret void
21+
}
22+
23+
!llvm.ident = !{!0}
24+
!dx.version = !{!1}
25+
!dx.valver = !{!1}
26+
!dx.shaderModel = !{!2}
27+
!dx.typeAnnotations = !{!3}
28+
!dx.entryPoints = !{!7, !8}
29+
30+
!0 = !{!"dxc(private) 1.8.0.4454 (rdat-dump-flags, c997ea026-dirty)"}
31+
!1 = !{i32 1, i32 8}
32+
!2 = !{!"lib", i32 6, i32 8}
33+
!3 = !{i32 1, void ()* @node01, !4}
34+
!4 = !{!5}
35+
!5 = !{i32 0, !6, !6}
36+
!6 = !{}
37+
!7 = !{null, !"", null, null, null}
38+
!8 = !{void ()* @node01, !"node01", null, null, !9}
39+
!9 = !{i32 8, i32 15, i32 13, i32 1, i32 15, !10, i32 16, i32 -1, i32 22, !11, i32 20, !12, i32 4, !11, i32 5, !16}
40+
!10 = !{!"node01", i32 0}
41+
!11 = !{i32 4, i32 4, i32 4}
42+
!12 = !{!13}
43+
!13 = !{i32 1, i32 101, i32 2, !14}
44+
!14 = !{i32 1, !15, i32 0, i32 12} ; reordered tag,value list entries for NodeRecordType
45+
!15 = !{i32 0, i32 5, i32 3}
46+
!16 = !{i32 0}

0 commit comments

Comments
 (0)