Skip to content

Commit

Permalink
[Error Handling] Fix DxcContainerBuilder error handling (#7056)
Browse files Browse the repository at this point in the history
Some error handling changes were incorrectly made to the container
assembler, as described in the issue below.
This is bad because the API protocol for error handling must remain
consistent across all functions. This PR
aims to restore the correct semantic meaning of returning bad HR values.
Fixes #7051

---------

Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
bob80905 and github-actions[bot] authored Jan 30, 2025
1 parent ef1472a commit 99726f9
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 16 deletions.
24 changes: 9 additions & 15 deletions lib/DxilContainer/DxcContainerBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,11 @@ HRESULT STDMETHODCALLTYPE DxcContainerBuilder::RemovePart(UINT32 fourCC) {

HRESULT STDMETHODCALLTYPE
DxcContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) {
if (ppResult == nullptr)
return E_INVALIDARG;

DxcThreadMalloc TM(m_pMalloc);

try {
// Allocate memory for new dxil container.
uint32_t ContainerSize = ComputeContainerSize();
Expand Down Expand Up @@ -161,6 +165,11 @@ DxcContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) {
errorHeap.Detach();
}

// Add Hash.
if (SUCCEEDED(valHR))
HashAndUpdate(IsDxilContainerLike(pResult->GetBufferPointer(),
pResult->GetBufferSize()));

IFT(DxcResult::Create(
valHR, DXC_OUT_OBJECT,
{DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pResult, DxcOutNoName),
Expand All @@ -169,21 +178,6 @@ DxcContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) {
}
CATCH_CPP_RETURN_HRESULT();

if (ppResult == nullptr || *ppResult == nullptr)
return S_OK;

HRESULT HR;
(*ppResult)->GetStatus(&HR);
if (FAILED(HR))
return HR;

CComPtr<IDxcBlob> pObject;
IFR((*ppResult)->GetResult(&pObject));

// Add Hash.
LPVOID PTR = pObject->GetBufferPointer();
if (IsDxilContainerLike(PTR, pObject->GetBufferSize()))
HashAndUpdate((DxilContainerHeader *)PTR);
return S_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion tools/clang/tools/dxclib/dxc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -644,7 +644,7 @@ int DxcContext::VerifyRootSignature() {
IFT(pContainerBuilder->AddPart(hlsl::DxilFourCC::DFCC_RootSignature,
pRootSignature));
CComPtr<IDxcOperationResult> pOperationResult;
pContainerBuilder->SerializeContainer(&pOperationResult);
IFT(pContainerBuilder->SerializeContainer(&pOperationResult));
HRESULT status = E_FAIL;
CComPtr<IDxcBlob> pResult;
IFT(pOperationResult->GetStatus(&status));
Expand Down
55 changes: 55 additions & 0 deletions tools/clang/unittests/HLSL/ValidationTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ class ValidationTest : public ::testing::Test {
TEST_METHOD(SimpleGs1Fail)
TEST_METHOD(UavBarrierFail)
TEST_METHOD(UndefValueFail)
TEST_METHOD(ValidationFailNoHash)
TEST_METHOD(UpdateCounterFail)
TEST_METHOD(LocalResCopy)
TEST_METHOD(ResCounter)
Expand Down Expand Up @@ -1189,6 +1190,60 @@ TEST_F(ValidationTest, UavBarrierFail) {
TEST_F(ValidationTest, UndefValueFail) {
TestCheck(L"..\\CodeGenHLSL\\UndefValue.hlsl");
}
// verify that containers that are not valid DXIL do not
// get assigned a hash.
TEST_F(ValidationTest, ValidationFailNoHash) {
if (m_ver.SkipDxilVersion(1, 8))
return;
CComPtr<IDxcBlob> pProgram;

// We need any shader that will pass compilation but fail validation.
// This shader reads from uninitialized 'float a', which works for now.
LPCSTR pSource = R"(
float main(snorm float b : B) : SV_DEPTH
{
float a;
return b + a;
}
)";

CComPtr<IDxcBlobEncoding> pSourceBlob;
Utf8ToBlob(m_dllSupport, pSource, &pSourceBlob);
std::vector<LPCWSTR> pArguments = {L"-Vd"};
LPCSTR pShaderModel = "ps_6_0";
bool result = CompileSource(pSourceBlob, pShaderModel, pArguments.data(), 1,
nullptr, 0, &pProgram);

VERIFY_IS_TRUE(result);

CComPtr<IDxcValidator> pValidator;
CComPtr<IDxcOperationResult> pResult;
unsigned Flags = 0;
VERIFY_SUCCEEDED(
m_dllSupport.CreateInstance(CLSID_DxcValidator, &pValidator));

VERIFY_SUCCEEDED(pValidator->Validate(pProgram, Flags, &pResult));
HRESULT status;
VERIFY_IS_NOT_NULL(pResult);
CComPtr<IDxcBlob> pValidationOutput;
pResult->GetStatus(&status);

// expect validation to fail
VERIFY_FAILED(status);
pResult->GetResult(&pValidationOutput);
// Make sure the validation output is not null even when validation fails
VERIFY_SUCCEEDED(pValidationOutput != nullptr);

hlsl::DxilContainerHeader *pHeader = IsDxilContainerLike(
pProgram->GetBufferPointer(), pProgram->GetBufferSize());
VERIFY_IS_NOT_NULL(pHeader);

BYTE ZeroHash[DxilContainerHashSize] = {0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0};

// Should be equal, this proves the hash isn't written when validation fails
VERIFY_ARE_EQUAL(memcmp(ZeroHash, pHeader->Hash.Digest, sizeof(ZeroHash)), 0);
}
TEST_F(ValidationTest, UpdateCounterFail) {
if (m_ver.SkipIRSensitiveTest())
return;
Expand Down

0 comments on commit 99726f9

Please sign in to comment.