From 4041bd39925149aafa8eebaf22319da4acbbdd40 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 3 Jul 2024 13:10:33 -0700 Subject: [PATCH 01/19] Move dxil.dll to projects/dxildll. --- projects/CMakeLists.txt | 5 +- projects/dxildll/CMakeLists.txt | 28 + projects/dxildll/include/DxilHash.h | 29 + projects/dxildll/lib/CMakeLists.txt | 1 + projects/dxildll/lib/DxilHash/CMakeLists.txt | 16 + projects/dxildll/lib/DxilHash/DxilHash.cpp | 528 ++++++++++++++++++ projects/dxildll/tools/CMakeLists.txt | 1 + projects/dxildll/tools/dxildll/CMakeLists.txt | 85 +++ .../dxildll/DxcSigningContainerBuilder.cpp | 120 ++++ .../dxildll/tools/dxildll/dxcvalidatorp.cpp | 270 +++++++++ projects/dxildll/tools/dxildll/dxildll.cpp | 160 ++++++ projects/dxildll/tools/dxildll/dxildll.def | 5 + projects/dxildll/tools/dxildll/dxildll.rc | 8 + 13 files changed, 1254 insertions(+), 2 deletions(-) create mode 100644 projects/dxildll/CMakeLists.txt create mode 100644 projects/dxildll/include/DxilHash.h create mode 100644 projects/dxildll/lib/CMakeLists.txt create mode 100644 projects/dxildll/lib/DxilHash/CMakeLists.txt create mode 100644 projects/dxildll/lib/DxilHash/DxilHash.cpp create mode 100644 projects/dxildll/tools/CMakeLists.txt create mode 100644 projects/dxildll/tools/dxildll/CMakeLists.txt create mode 100644 projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp create mode 100644 projects/dxildll/tools/dxildll/dxcvalidatorp.cpp create mode 100644 projects/dxildll/tools/dxildll/dxildll.cpp create mode 100644 projects/dxildll/tools/dxildll/dxildll.def create mode 100644 projects/dxildll/tools/dxildll/dxildll.rc diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index d3093985af..ec85c77af8 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -1,7 +1,8 @@ set(DXC_PROJECTS_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) set(DXC_PROJECTS_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) -if(WIN32 AND HLSL_BUILD_DXILCONV) +#if(WIN32 AND HLSL_BUILD_DXILCONV) add_subdirectory(include/Tracing) add_subdirectory(dxilconv) -endif (WIN32 AND HLSL_BUILD_DXILCONV) + add_subdirectory(dxildll) +#endif (WIN32 AND HLSL_BUILD_DXILCONV) diff --git a/projects/dxildll/CMakeLists.txt b/projects/dxildll/CMakeLists.txt new file mode 100644 index 0000000000..15e34380fe --- /dev/null +++ b/projects/dxildll/CMakeLists.txt @@ -0,0 +1,28 @@ +set(DXILDLL_PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) +set(DXILDLL_PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) + +macro(add_dxildll_project_library name) + add_llvm_library(${name} ${ARGN}) + set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR}) + set_target_properties(${name} PROPERTIES FOLDER "Dxildll libraries") +endmacro(add_dxildll_project_library) + +macro(add_dxildll_project_executable name) + add_llvm_library(${name} SHARED ${ARGN}) + set_target_properties(${name} PROPERTIES FOLDER "Dxildll executables") +endmacro(add_dxildll_project_executable) + +macro(add_dxildll_project_test_library name) + add_dxildll_project_library(${name} ${ARGN}) + set_target_properties(${name} PROPERTIES FOLDER "Dxildll tests") +endmacro(add_dxildll_project_test_library) + + +#if(WIN32) + add_subdirectory(lib) + add_subdirectory(tools) + #if (HLSL_INCLUDE_TESTS) + # add_subdirectory(unittests) + # add_subdirectory(test) + #endif (HLSL_INCLUDE_TESTS) +#endif() \ No newline at end of file diff --git a/projects/dxildll/include/DxilHash.h b/projects/dxildll/include/DxilHash.h new file mode 100644 index 0000000000..5de5cb8a1f --- /dev/null +++ b/projects/dxildll/include/DxilHash.h @@ -0,0 +1,29 @@ +/////////////////////////////////////////////////////////////////////////////// +// // +// Hash.h // +// Copyright (C) Microsoft Corporation. All rights reserved. // +// // +// DXBC/DXIL container hashing functions // +// // +/////////////////////////////////////////////////////////////////////////////// + +#pragma once + +#define DXIL_CONTAINER_HASH_SIZE 16 + +// Prototype for hash computing function +// pOutHash must always return a DXIL_CONTAINER_HASH_SIZE byte hash result. +typedef void HASH_FUNCTION_PROTO(const BYTE* pData, uint32_t byteCount, BYTE* pOutHash); + +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** +// Derived from: RSA Data Security, Inc. M +// D +// 5 Message-Digest Algorithm +// Computes a 128-bit hash of pData (size byteCount), returning 16 BYTE output +void ComputeHashRetail(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); +void ComputeHashDebug(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** diff --git a/projects/dxildll/lib/CMakeLists.txt b/projects/dxildll/lib/CMakeLists.txt new file mode 100644 index 0000000000..ef056afffe --- /dev/null +++ b/projects/dxildll/lib/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(DxilHash) diff --git a/projects/dxildll/lib/DxilHash/CMakeLists.txt b/projects/dxildll/lib/DxilHash/CMakeLists.txt new file mode 100644 index 0000000000..c992252e25 --- /dev/null +++ b/projects/dxildll/lib/DxilHash/CMakeLists.txt @@ -0,0 +1,16 @@ +if (WIN32) +add_dxildll_project_library(DxilHash + DxilHash.cpp +) +else() +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} + ${WINTOOLS_SOURCE_ROOT}/include + ${DXC_PROJECTS_SOURCE_DIR}/include + ${DXC_PROJECTS_BINARY_DIR}/include + ${DXILDLL_PROJECT_SOURCE_DIR}/include + ${DXILDLL_PROJECT_BINARY_DIR}/include +) +add_llvm_library(DxilHash + DxilHash.cpp) +endif() \ No newline at end of file diff --git a/projects/dxildll/lib/DxilHash/DxilHash.cpp b/projects/dxildll/lib/DxilHash/DxilHash.cpp new file mode 100644 index 0000000000..9f876fb338 --- /dev/null +++ b/projects/dxildll/lib/DxilHash/DxilHash.cpp @@ -0,0 +1,528 @@ +/////////////////////////////////////////////////////////////////////////////// +// // +// Hash.cpp // +// Copyright (C) Microsoft Corporation. All rights reserved. // +// // +// DXBC/DXIL container hashing functions // +// // +/////////////////////////////////////////////////////////////////////////////// + +#include "assert.h" + +#ifdef _WIN32 +#include +#else +#include "dxc/WinAdapter.h" +typedef unsigned char UINT8; +#endif + +// RSA Data Security, Inc. M +// D +// 5 Message-Digest Algorithm + +#define S11 7 +#define S12 12 +#define S13 17 +#define S14 22 +#define S21 5 +#define S22 9 +#define S23 14 +#define S24 20 +#define S31 4 +#define S32 11 +#define S33 16 +#define S34 23 +#define S41 6 +#define S42 10 +#define S43 15 +#define S44 21 + +const BYTE padding[64] = +{ + 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 +}; + +void FF( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) +{ + a += ((b & c) | (~b & d)) + x + ac; + a = ((a << s) | (a >> (32-s))) + b; +} + +void GG( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) +{ + a += ((b & d) | (c & ~d)) + x + ac; + a = ((a << s) | (a >> (32-s))) + b; +} + +void HH( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) +{ + a += (b ^ c ^ d) + x + ac; + a = ((a << s) | (a >> (32-s))) + b; +} + +void II( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) +{ + a += (c ^ (b | ~d)) + x + ac; + a = ((a << s) | (a >> (32-s))) + b; +} + +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** +void ComputeM_D_5Hash( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) +{ + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if( leftOver < 56 ) + { + padAmount = 56 - leftOver; + } + else + { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; + const BYTE* pCurrData = pData; + for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) + { + assert(byteCount - offset <= byteCount); // prefast doesn't understand this - no underflow will happen + assert(byteCount < 64*i+65); // prefast doesn't understand this - no overflows will happen in any memcpy below + assert(byteCount < leftOver+64*i+9); + assert(byteCount < leftOver+64*i+1); + UINT x[16]; + const UINT* pX; + if( i == NextEndState ) + { + if( !bTwoRowsPadding && i == N-1 ) + { + UINT remainder = byteCount - offset; + memcpy(x,pCurrData, remainder); // could copy nothing + memcpy((BYTE*)x + remainder, padding, padAmount); + x[14] = byteCount << 3; // sizepad lo + x[15] = 0; // sizepad hi + } + else if( bTwoRowsPadding ) + { + if( i == N-2 ) + { + UINT remainder = byteCount - offset; + memcpy(x,pCurrData, remainder); + memcpy((BYTE*)x + remainder, padding, padAmount-56); + NextEndState = N-1; + } + else if( i == N-1 ) + { + memcpy(x, padding + padAmount-56, 56); + x[14] = byteCount << 3; // sizepad lo + x[15] = 0; // sizepad hi + } + } + pX = x; + } + else + { + pX = (const UINT*)pCurrData; + } + + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ + FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ + FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ + FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ + FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ + FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ + FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ + FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ + FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ + FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ + FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ + FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ + FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ + FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ + FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ + FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ + + /* Round 2 */ + GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ + GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ + GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ + GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ + GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ + GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ + GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ + GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ + GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ + GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ + GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ + GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ + GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ + GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ + GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ + GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ + + /* Round 3 */ + HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ + HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ + HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ + HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ + HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ + HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ + HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ + HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ + HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ + HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ + HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ + HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ + HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ + HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ + HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ + HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ + + /* Round 4 */ + II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ + II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ + II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ + II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ + II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ + II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ + II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ + II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ + II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ + II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ + II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ + II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ + II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ + II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ + II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ + II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash,state,16); +} + +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** +void ComputeHashRetail( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) +{ + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if( leftOver < 56 ) + { + padAmount = 56 - leftOver; + } + else + { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; + const BYTE* pCurrData = pData; + for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) + { + UINT x[16]; + const UINT* pX; + if( i == NextEndState ) + { + if( !bTwoRowsPadding && i == N-1 ) + { + UINT remainder = byteCount - offset; + x[0] = byteCount << 3; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy((BYTE*)x+4,pCurrData, remainder); // could copy nothing + memcpy((BYTE*)x+4 + remainder, padding, padAmount); + x[15] = 1 | (byteCount << 1); + } + else if( bTwoRowsPadding ) + { + if( i == N-2 ) + { + UINT remainder = byteCount - offset; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy(x,pCurrData, remainder); + memcpy((BYTE*)x + remainder, padding, padAmount-56); + NextEndState = N-1; + } + else if( i == N-1 ) + { + x[0] = byteCount << 3; + memcpy((BYTE*)x+4, padding + padAmount-56, 56); + x[15] = 1 | (byteCount << 1); + } + } + pX = x; + } + else + { + assert(pCurrData + 64 <= pData + byteCount); + pX = (const UINT*)pCurrData; + } + + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ + FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ + FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ + FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ + FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ + FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ + FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ + FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ + FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ + FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ + FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ + FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ + FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ + FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ + FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ + FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ + + /* Round 2 */ + GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ + GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ + GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ + GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ + GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ + GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ + GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ + GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ + GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ + GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ + GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ + GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ + GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ + GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ + GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ + GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ + + /* Round 3 */ + HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ + HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ + HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ + HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ + HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ + HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ + HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ + HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ + HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ + HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ + HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ + HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ + HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ + HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ + HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ + HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ + + /* Round 4 */ + II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ + II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ + II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ + II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ + II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ + II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ + II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ + II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ + II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ + II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ + II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ + II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ + II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ + II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ + II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ + II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash,state,16); +} + +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** +void ComputeHashDebug( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) +{ + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if( leftOver < 56 ) + { + padAmount = 56 - leftOver; + } + else + { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; + const BYTE* pCurrData = pData; + for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) + { + UINT x[16]; + const UINT* pX; + if( i == NextEndState ) + { + if( !bTwoRowsPadding && i == N-1 ) + { + UINT remainder = byteCount - offset; + x[0] = byteCount << 4 | 0xf; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy((BYTE*)x+4,pCurrData, remainder); // could copy nothing + memcpy((BYTE*)x+4 + remainder, padding, padAmount); + x[15] = (byteCount << 2) | 0x10000000; + } + else if( bTwoRowsPadding ) + { + if( i == N-2 ) + { + UINT remainder = byteCount - offset; + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy(x,pCurrData, remainder); + memcpy((BYTE*)x + remainder, padding, padAmount-56); + NextEndState = N-1; + } + else if( i == N-1 ) + { + x[0] = byteCount << 4 | 0xf; + memcpy((BYTE*)x+4, padding + padAmount-56, 56); + x[15] = (byteCount << 2) | 0x10000000; + } + } + pX = x; + } + else + { + assert(pCurrData + 64 <= pData + byteCount); + pX = (const UINT*)pCurrData; + } + + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ + FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ + FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ + FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ + FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ + FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ + FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ + FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ + FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ + FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ + FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ + FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ + FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ + FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ + FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ + FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ + + /* Round 2 */ + GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ + GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ + GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ + GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ + GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ + GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ + GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ + GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ + GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ + GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ + GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ + GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ + GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ + GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ + GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ + GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ + + /* Round 3 */ + HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ + HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ + HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ + HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ + HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ + HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ + HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ + HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ + HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ + HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ + HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ + HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ + HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ + HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ + HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ + HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ + + /* Round 4 */ + II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ + II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ + II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ + II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ + II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ + II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ + II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ + II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ + II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ + II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ + II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ + II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ + II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ + II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ + II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ + II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash,state,16); +} +// ************************************************************************************** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// ************************************************************************************** diff --git a/projects/dxildll/tools/CMakeLists.txt b/projects/dxildll/tools/CMakeLists.txt new file mode 100644 index 0000000000..1c3004b318 --- /dev/null +++ b/projects/dxildll/tools/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(dxildll) diff --git a/projects/dxildll/tools/dxildll/CMakeLists.txt b/projects/dxildll/tools/dxildll/CMakeLists.txt new file mode 100644 index 0000000000..46cec06b86 --- /dev/null +++ b/projects/dxildll/tools/dxildll/CMakeLists.txt @@ -0,0 +1,85 @@ +# Builds dxil.dll for Windows + +set(SHARED_LIBRARY TRUE) + +set(DXCComponents + LLVMBitWriter + LLVMDxcSupport + LLVMDxilContainer + LLVMDxilRootSignature + LLVMHLSL + LLVMMSSupport + LLVMSupport + LLVMTransformUtils + LLVMAnalysis + LLVMDxcBindingTable + LLVMipa +) + +foreach(C ${DXCComponents}) +if (WIN32) + list(APPEND DXCLibs ${C}) + list(APPEND DXCDependencies ${C}) +else() + list(APPEND DXCLibs ${C}) + list(APPEND DXCDependencies ${C}) +endif() +endforeach(C ${DXCComponents}) + +include_directories( + ${CMAKE_CURRENT_SOURCE_DIR} + ${WINTOOLS_SOURCE_ROOT}/include + ${DXC_PROJECTS_SOURCE_DIR}/include + ${DXC_PROJECTS_BINARY_DIR}/include + ${DXILDLL_PROJECT_SOURCE_DIR}/include + ${DXILDLL_PROJECT_BINARY_DIR}/include +) + +set(sources + dxildll.cpp + dxildll.def + DxcSigningContainerBuilder.cpp + dxcvalidatorp.cpp +) + +if (WIN32) +add_dxildll_project_executable(dxildll ${sources}) +list(APPEND DXCLibs + kernel32.lib + atls.lib + advapi32.lib + ole32.lib) +else() +add_llvm_library(dxildll SHARED ${sources}) +endif() + +target_link_libraries(dxildll PRIVATE + ${DXCLibs} + DxilHash +) + +if (WIN32) +add_dependencies(dxildll + ${DXCDependencies} + DxilHash + DxcEtw + DxcRuntimeEtw +) +else() +add_dependencies(dxildll + ${DXCDependencies} + DxilHash +) +endif() + +if (WIN32) +#wintools_set_resource_file(dxildll dxildll.rc) +else() +set_target_properties(dxildll PROPERTIES + LINK_FLAGS "-z defs") +endif() + +set_target_properties(dxildll + PROPERTIES + OUTPUT_NAME "dxil" +) diff --git a/projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp b/projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp new file mode 100644 index 0000000000..e2a4646130 --- /dev/null +++ b/projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp @@ -0,0 +1,120 @@ +/////////////////////////////////////////////////////////////////////////////// +// // +// dxcontainerbuilder.cpp // +// Copyright (C) Microsoft Corporation. All rights reserved. // +// This file is distributed under the University of Illinois Open Source // +// License. See LICENSE.TXT for details. // +// // +// Implements the Dxil Container Builder // +// // +/////////////////////////////////////////////////////////////////////////////// + +#include "dxc/Support/WinIncludes.h" +#include "dxc/dxcapi.h" +#include "dxc/DxilContainer/DxilContainer.h" +#include "dxc/DxilContainer/DxcContainerBuilder.h" +#include "dxc/Support/Global.h" +#include "dxc/Support/ErrorCodes.h" +#include "dxc/Support/FileIOHelper.h" +#include "dxc/Support/microcom.h" +#include "dxc/Support/dxcapi.impl.h" +#include "DxilHash.h" + +#include + +using namespace hlsl; + +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv); + +class DxcSigningContainerBuilder : public DxcContainerBuilder { +public: + DxcSigningContainerBuilder(IMalloc *pMalloc) : DxcContainerBuilder(pMalloc) {} + + DXC_MICROCOM_TM_ALLOC(DxcSigningContainerBuilder); + + HRESULT STDMETHODCALLTYPE Load(_In_ IDxcBlob *pDxilContainerHeader) override; // Loads DxilContainer to the builder + HRESULT STDMETHODCALLTYPE SerializeContainer(_Out_ IDxcOperationResult **ppResult) override; // Builds a container of the given container builder state + +private: + // Function to compute hash when valid dxil container is built + // This is nullptr if loaded container has invalid hash + HASH_FUNCTION_PROTO *m_pHashFunction; + + void FindHashFunctionFromSource(const DxilContainerHeader *pContainerHeader); + void HashAndUpdate(DxilContainerHeader *pContainerHeader); +}; + +HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::Load(_In_ IDxcBlob *pSource) { + HRESULT hr = DxcContainerBuilder::Load(pSource); + if (SUCCEEDED(hr)) { + const DxilContainerHeader *pHeader = (DxilContainerHeader *)pSource->GetBufferPointer(); + FindHashFunctionFromSource(pHeader); + } + return hr; +} + +HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::SerializeContainer(_Out_ IDxcOperationResult **ppResult) { + HRESULT hr_saved = DxcContainerBuilder::SerializeContainer(ppResult); + if (!SUCCEEDED(hr_saved)) { + return hr_saved; + } + if (ppResult != nullptr && *ppResult != nullptr) { + HRESULT hr; + (*ppResult)->GetStatus(&hr); + if (SUCCEEDED(hr)) { + CComPtr pObject; + hr = (*ppResult)->GetResult(&pObject); + if (SUCCEEDED(hr)) { + LPVOID ptr = pObject->GetBufferPointer(); + if (IsDxilContainerLike(ptr, pObject->GetBufferSize())) { + HashAndUpdate((DxilContainerHeader *)ptr); + } + } + } + } + return hr_saved; +} + +void DxcSigningContainerBuilder::FindHashFunctionFromSource(const DxilContainerHeader *pContainerHeader) { + DXASSERT(pContainerHeader != nullptr && + IsDxilContainerLike(pContainerHeader, + pContainerHeader->ContainerSizeInBytes), + "otherwise load function should have returned an error."); + static const UINT32 HashStartOffset = + offsetof(struct DxilContainerHeader, Version); + const BYTE *pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; + UINT AmountToHash = pContainerHeader->ContainerSizeInBytes - HashStartOffset; + BYTE result[DxilContainerHashSize]; + ComputeHashRetail(pDataToHash, AmountToHash, result); + if (0 == memcmp(result, pContainerHeader->Hash.Digest, sizeof(result))) { + m_pHashFunction = ComputeHashRetail; + } else { + ComputeHashDebug(pDataToHash, AmountToHash, result); + if (0 == memcmp(result, pContainerHeader->Hash.Digest, sizeof(result))) { + m_pHashFunction = ComputeHashDebug; + } else { + m_pHashFunction = nullptr; + } + } +} + +// For Internal hash function. +void DxcSigningContainerBuilder::HashAndUpdate(DxilContainerHeader *pContainerHeader) { + if (m_pHashFunction != nullptr) { + DXASSERT(pContainerHeader != nullptr, "Otherwise serialization should have failed."); + static const UINT32 HashStartOffset = offsetof(struct DxilContainerHeader, Version); + const BYTE* pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; + UINT AmountToHash = pContainerHeader->ContainerSizeInBytes - HashStartOffset; + m_pHashFunction(pDataToHash, AmountToHash, pContainerHeader->Hash.Digest); + } +} + + +HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv) { + // Call dxil.dll's containerbuilder + *ppv = nullptr; + CComPtr Result(DxcSigningContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); + IFROOM(Result.p); + Result->Init(); + return Result->QueryInterface(riid, ppv); +} \ No newline at end of file diff --git a/projects/dxildll/tools/dxildll/dxcvalidatorp.cpp b/projects/dxildll/tools/dxildll/dxcvalidatorp.cpp new file mode 100644 index 0000000000..57f04ff804 --- /dev/null +++ b/projects/dxildll/tools/dxildll/dxcvalidatorp.cpp @@ -0,0 +1,270 @@ +/////////////////////////////////////////////////////////////////////////////// +// // +// dxcvalidatorp.cpp // +// Copyright (C) Microsoft Corporation. All rights reserved. // +// // +// Implements the DirectX Validator object, including signing support. // +// // +/////////////////////////////////////////////////////////////////////////////// + +#include "llvm/Bitcode/ReaderWriter.h" +#include "llvm/IR/LLVMContext.h" +#include "llvm/IR/DiagnosticPrinter.h" + +#include "dxc/Support/WinIncludes.h" +#include "dxc/Support/microcom.h" +#include "dxc/DxilContainer/DxilContainer.h" +#include "dxc/HLSL/DxilValidation.h" + +#include "dxc/Support/Global.h" +#include "llvm/Support/FileSystem.h" +#include "llvm/Support/MSFileSystem.h" +#include "dxc/Support/microcom.h" +#include "dxc/Support/FileIOHelper.h" +#include "dxc/Support/dxcapi.impl.h" +#include "dxc/DxilRootSignature/DxilRootSignature.h" +#ifdef _WIN32 +#include "dxc/Tracing/dxcetw.h" +#endif +#include "DxilHash.h" + +using namespace llvm; +using namespace hlsl; + +class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { +private: + DXC_MICROCOM_TM_REF_FIELDS() + + HRESULT RunValidation(_In_ IDxcBlob *pShader, + _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, + _In_opt_ DxcBuffer *pOptDebugBitcode, + _COM_Outptr_ IDxcBlob **pSigned); + + HRESULT RunRootSignatureValidation( + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, + _COM_Outptr_ IDxcBlob **pSigned); + + +public: + DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() + DXC_MICROCOM_TM_CTOR(DxcValidator) + + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) override { + + return DoBasicQueryInterface(this, iid, ppvObject); + } + + HRESULT STDMETHODCALLTYPE Validate( + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors + ) override; + + // IDxcValidator2 + HRESULT STDMETHODCALLTYPE ValidateWithDebug( + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _In_ DxcBuffer *pDebugModule, // Debug module bitcode to provide line numbers + _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors + ) override; + + // IDxcVersionInfo + HRESULT STDMETHODCALLTYPE GetVersion(_Out_ UINT32 *pMajor, _Out_ UINT32 *pMinor) override; + HRESULT STDMETHODCALLTYPE GetFlags(_Out_ UINT32 *pFlags) override; +}; + +HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(_Out_ UINT32 *pMajor, _Out_ UINT32 *pMinor) { + DxcThreadMalloc TM(m_pMalloc); + if (pMajor == nullptr || pMinor == nullptr) + return E_INVALIDARG; + GetValidationVersion(pMajor, pMinor); + return S_OK; +} + +HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(_Out_ UINT32 *pFlags) { + DxcThreadMalloc TM(m_pMalloc); + if (pFlags == nullptr) + return E_INVALIDARG; + *pFlags = DxcVersionInfoFlags_None; +#ifdef _DEBUG + *pFlags |= DxcVersionInfoFlags_Debug; +#endif + return S_OK; +} + +static void HashAndUpdate(DxilContainerHeader *pContainer) { + // Compute hash and update stored hash. + // Hash the container from this offset to the end. + static const UINT32 DXBCHashStartOffset = offsetof(struct DxilContainerHeader,Version); + const BYTE* pDataToHash = (const BYTE*)pContainer + DXBCHashStartOffset; + UINT AmountToHash = pContainer->ContainerSizeInBytes - DXBCHashStartOffset; + ComputeHashRetail(pDataToHash, AmountToHash, pContainer->Hash.Digest); +} + +static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, IDxcBlob **pSigned) { + if (Flags & DxcValidatorFlags_InPlaceEdit) { + HashAndUpdate((DxilContainerHeader *)pShader->GetBufferPointer()); + *pSigned = pShader; + pShader->AddRef(); + } + else { + // Possible gotcha: the blob allocated here is tied to this .dll, so the + // DLL shouldn't be unloaded before the blob is released. + CComPtr pSignedBlobStream; + IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &pSignedBlobStream)); + ULONG cb; + IFT(pSignedBlobStream->Write(pShader->GetBufferPointer(), pShader->GetBufferSize(), &cb)); + HashAndUpdate((DxilContainerHeader *)pSignedBlobStream->GetPtr()); + IFT(pSignedBlobStream.QueryInterface(pSigned)); + } +} + +HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _In_opt_ DxcBuffer *pOptDebugBitcode, // Debug module bitcode to provide line numbers + _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors +) +{ + DxcThreadMalloc TM(m_pMalloc); + if (pShader == nullptr || ppResult == nullptr || Flags & ~DxcValidatorFlags_ValidMask) + return E_INVALIDARG; + if (Flags & DxcValidatorFlags_ModuleOnly) + return E_INVALIDARG; + if (pOptDebugBitcode && (pOptDebugBitcode->Ptr == nullptr || pOptDebugBitcode->Size == 0 || + pOptDebugBitcode->Size >= UINT32_MAX)) + return E_INVALIDARG; + + *ppResult = nullptr; + HRESULT hr = S_OK; + HRESULT validationStatus = S_OK; + DxcEtw_DxcValidation_Start(); + try { + CComPtr pDiagStream; + CComPtr pSignedBlob; + IFT(CreateMemoryStream(m_pMalloc, &pDiagStream)); + + // Run validation may throw, but that indicates an inability to validate, + // not that the validation failed (eg out of memory). + if (Flags & DxcValidatorFlags_RootSignatureOnly) { + validationStatus = RunRootSignatureValidation(pShader, pDiagStream, Flags, &pSignedBlob); + } + else { + validationStatus = RunValidation(pShader, pDiagStream, Flags, pOptDebugBitcode, &pSignedBlob); + } + if (FAILED(validationStatus)) { + std::string msg("Validation failed.\n"); + ULONG cbWritten; + pDiagStream->Write(msg.c_str(), msg.size(), &cbWritten); + } + // Assemble the result object. + CComPtr pDiagBlob; + CComPtr pDiagBlobEnconding; + hr = pDiagStream.QueryInterface(&pDiagBlob); + DXASSERT_NOMSG(SUCCEEDED(hr)); + IFT(DxcCreateBlobWithEncodingSet(pDiagBlob, CP_UTF8, &pDiagBlobEnconding)); + IFT(DxcResult::Create(validationStatus, DXC_OUT_OBJECT, { + DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pSignedBlob), + DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding) + }, ppResult)); + } + CATCH_CPP_ASSIGN_HRESULT(); + + DxcEtw_DxcValidation_Stop(SUCCEEDED(hr) ? validationStatus : hr); + return hr; +} + +HRESULT STDMETHODCALLTYPE DxcValidator::Validate( + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors +) { + return ValidateWithDebug(pShader, Flags, nullptr, ppResult); +} + +HRESULT DxcValidator::RunValidation(_In_ IDxcBlob *pShader, + _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, + _In_ DxcBuffer *pDebugBitcode, + _COM_Outptr_ IDxcBlob **pSigned) { + + // Run validation may throw, but that indicates an inability to validate, + // not that the validation failed (eg out of memory). That is indicated + // by a failing HRESULT, and possibly error messages in the diagnostics stream. + + *pSigned = nullptr; + raw_stream_ostream DiagStream(pDiagStream); + + if (IsDxilContainerLike(pShader->GetBufferPointer(), pShader->GetBufferSize())) { + IFR(ValidateDxilContainer(pShader->GetBufferPointer(), pShader->GetBufferSize(), + pDebugBitcode ? pDebugBitcode->Ptr : nullptr, + pDebugBitcode ? (uint32_t)pDebugBitcode->Size : 0, + DiagStream)); + } + else { + IFR(DXC_E_CONTAINER_INVALID); + } + + HashAndUpdateOrCopy(Flags, pShader, pSigned); + + return S_OK; +} + +HRESULT DxcValidator::RunRootSignatureValidation( + _In_ IDxcBlob *pShader, + _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, + _COM_Outptr_ IDxcBlob **pSigned) { + + const DxilContainerHeader *pDxilContainer = IsDxilContainerLike( + pShader->GetBufferPointer(), pShader->GetBufferSize()); + if (!pDxilContainer) { + return DXC_E_IR_VERIFICATION_FAILED; + } + + const DxilProgramHeader *pProgramHeader = GetDxilProgramHeader(pDxilContainer, DFCC_DXIL); + const DxilPartHeader *pPSVPart = GetDxilPartByType(pDxilContainer, DFCC_PipelineStateValidation); + const DxilPartHeader *pRSPart = GetDxilPartByType(pDxilContainer, DFCC_RootSignature); + IFRBOOL(pRSPart, DXC_E_MISSING_PART); + if (pProgramHeader) { + // Container has shader part, make sure we have PSV. + IFRBOOL(pPSVPart, DXC_E_MISSING_PART); + } + try { + RootSignatureHandle RSH; + RSH.LoadSerialized((const uint8_t*)GetDxilPartData(pRSPart), pRSPart->PartSize); + RSH.Deserialize(); + raw_stream_ostream DiagStream(pDiagStream); + if (pProgramHeader) { + IFRBOOL(VerifyRootSignatureWithShaderPSV(RSH.GetDesc(), + GetVersionShaderType(pProgramHeader->ProgramVersion), + GetDxilPartData(pPSVPart), + pPSVPart->PartSize, + DiagStream), + DXC_E_INCORRECT_ROOT_SIGNATURE); + // Do not sign here; shaders must go through full shader validation for signing. + } else { + IFRBOOL(VerifyRootSignature(RSH.GetDesc(), DiagStream, false), + DXC_E_INCORRECT_ROOT_SIGNATURE); + HashAndUpdateOrCopy(Flags, pShader, pSigned); + } + } + catch (...) { + return DXC_E_IR_VERIFICATION_FAILED; + } + + return S_OK; +} + + +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv) { + try { + CComPtr result(DxcValidator::Alloc(DxcGetThreadMallocNoRef())); + IFROOM(result.p); + return result.p->QueryInterface(riid, ppv); + } + CATCH_CPP_RETURN_HRESULT(); +} diff --git a/projects/dxildll/tools/dxildll/dxildll.cpp b/projects/dxildll/tools/dxildll/dxildll.cpp new file mode 100644 index 0000000000..479306d9b1 --- /dev/null +++ b/projects/dxildll/tools/dxildll/dxildll.cpp @@ -0,0 +1,160 @@ +/////////////////////////////////////////////////////////////////////////////// +// // +// dxildll.cpp // +// Copyright (C) Microsoft. All rights reserved. // +// // +// Implements the DxcCreateInstanceand DllMain functions // +// for the dxil.dll module. // +// // +/////////////////////////////////////////////////////////////////////////////// + +#include +#include "llvm/Support/Debug.h" +#include "llvm/Support/ManagedStatic.h" +#include "llvm/Support/FileSystem.h" +#include "dxc/Support/WinIncludes.h" +#include "dxc/Support/Global.h" +#ifdef _WIN32 +#include "dxc/Tracing/dxcetw.h" +#include "Tracing/DxcRuntimeEtw.h" +#endif + +#ifdef _WIN32 +#define DXC_API_IMPORT +#else +#define DXC_API_IMPORT __attribute__ ((visibility ("default"))) +#endif + +#include "dxc/dxcisense.h" +#include "dxc/dxctools.h" + +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv); +HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv); + +// C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +static HRESULT InitMaybeFail() throw() { + HRESULT hr; + bool memSetup = false; + IFC(DxcInitThreadMalloc()); + DxcSetThreadMallocToDefault(); + memSetup = true; + if (::llvm::sys::fs::SetupPerThreadFileSystem()) { + hr = E_FAIL; + goto Cleanup; + } +Cleanup: + if (FAILED(hr)) { + if (memSetup) { + DxcClearThreadMalloc(); + DxcCleanupThreadMalloc(); + } + } + else { + DxcClearThreadMalloc(); + } + return hr; +} + +#if defined(LLVM_ON_UNIX) +HRESULT __attribute__ ((constructor)) DllMain() { + return InitMaybeFail(); +} + +void __attribute__ ((destructor)) DllShutdown() { + DxcSetThreadMallocToDefault(); + ::llvm::sys::fs::CleanupPerThreadFileSystem(); + ::llvm::llvm_shutdown(); + DxcClearThreadMalloc(); + DxcCleanupThreadMalloc(); +} +#else + +#pragma warning( disable : 4290 ) +BOOL WINAPI +DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) +{ + if (Reason == DLL_PROCESS_ATTACH) + { + EventRegisterMicrosoft_Windows_DxcRuntime_API(); + DxcRuntimeEtw_DxcRuntimeInitialization_Start(); + HRESULT hr = InitMaybeFail(); + DxcRuntimeEtw_DxcRuntimeInitialization_Stop(hr); + if (FAILED(hr)) { + EventUnregisterMicrosoft_Windows_DxcRuntime_API(); + return hr; + } + } + else if (Reason == DLL_PROCESS_DETACH) + { + DxcRuntimeEtw_DxcRuntimeShutdown_Start(); + DxcSetThreadMallocToDefault(); + ::llvm::sys::fs::CleanupPerThreadFileSystem(); + ::llvm::llvm_shutdown(); + DxcClearThreadMalloc(); + DxcCleanupThreadMalloc(); + DxcRuntimeEtw_DxcRuntimeShutdown_Stop(S_OK); + EventUnregisterMicrosoft_Windows_DxcRuntime_API(); + } + + return TRUE; +} + +void *__CRTDECL operator new(std::size_t size) noexcept(false) { + void *ptr = DxcNew(size); + if (ptr == nullptr) + throw std::bad_alloc(); + return ptr; +} +void * __CRTDECL operator new(std::size_t size, + const std::nothrow_t ¬hrow_value) throw() { + return DxcNew(size); +} +void __CRTDECL operator delete (void* ptr) throw() { + DxcDelete(ptr); +} +void __CRTDECL operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw() { + DxcDelete(ptr); +} +#endif + +static HRESULT ThreadMallocDxcCreateInstance( + _In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + *ppv = nullptr; + if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) { + return CreateDxcValidator(riid, ppv); + } + if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) { + return CreateDxcSigningContainerBuilder(riid, ppv); + } + return REGDB_E_CLASSNOTREG; +} + +DXC_API_IMPORT HRESULT __stdcall +DxcCreateInstance(_In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + HRESULT hr = S_OK; + DxcEtw_DXCompilerCreateInstance_Start(); + DxcThreadMalloc TM(nullptr); + hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); + DxcEtw_DXCompilerCreateInstance_Stop(hr); + return hr; +} + +DXC_API_IMPORT HRESULT __stdcall +DxcCreateInstance2(_In_ IMalloc *pMalloc, + _In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + if (ppv == nullptr) { + return E_POINTER; + } + HRESULT hr = S_OK; + DxcEtw_DXCompilerCreateInstance_Start(); + DxcThreadMalloc TM(pMalloc); + hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); + DxcEtw_DXCompilerCreateInstance_Stop(hr); + return hr; +} diff --git a/projects/dxildll/tools/dxildll/dxildll.def b/projects/dxildll/tools/dxildll/dxildll.def new file mode 100644 index 0000000000..4a333d2c48 --- /dev/null +++ b/projects/dxildll/tools/dxildll/dxildll.def @@ -0,0 +1,5 @@ +LIBRARY dxil + +EXPORTS + DxcCreateInstance + DxcCreateInstance2 diff --git a/projects/dxildll/tools/dxildll/dxildll.rc b/projects/dxildll/tools/dxildll/dxildll.rc new file mode 100644 index 0000000000..371a40cd61 --- /dev/null +++ b/projects/dxildll/tools/dxildll/dxildll.rc @@ -0,0 +1,8 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// + +#define RC_FILE_DESCRIPTION "DirectX IL for Redistribution" +#define RC_INTERNAL_NAME "DirectX IL for Redistribution" +#define RC_ORIGINAL_FILENAME "dxil.dll" + +#include "resources\windows_version_resource.rc" \ No newline at end of file From 26f9becfa2f35f9b2e5a63530b1b6bc2689dc5a5 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 3 Jul 2024 13:11:07 -0700 Subject: [PATCH 02/19] Move to tools/clang/tools/dxildll --- projects/CMakeLists.txt | 1 - projects/dxildll/CMakeLists.txt | 28 ------------------- projects/dxildll/lib/CMakeLists.txt | 1 - projects/dxildll/tools/CMakeLists.txt | 1 - tools/clang/tools/CMakeLists.txt | 2 ++ .../clang/tools}/DxilHash/CMakeLists.txt | 7 ++--- .../clang/tools}/DxilHash/DxilHash.cpp | 0 .../clang/tools/DxilHash}/DxilHash.h | 0 .../clang}/tools/dxildll/CMakeLists.txt | 8 +++--- .../dxildll/DxcSigningContainerBuilder.cpp | 0 .../clang}/tools/dxildll/dxcvalidatorp.cpp | 0 .../clang}/tools/dxildll/dxildll.cpp | 0 .../clang}/tools/dxildll/dxildll.def | 0 .../clang}/tools/dxildll/dxildll.rc | 0 14 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 projects/dxildll/CMakeLists.txt delete mode 100644 projects/dxildll/lib/CMakeLists.txt delete mode 100644 projects/dxildll/tools/CMakeLists.txt rename {projects/dxildll/lib => tools/clang/tools}/DxilHash/CMakeLists.txt (77%) rename {projects/dxildll/lib => tools/clang/tools}/DxilHash/DxilHash.cpp (100%) rename {projects/dxildll/include => tools/clang/tools/DxilHash}/DxilHash.h (100%) rename {projects/dxildll => tools/clang}/tools/dxildll/CMakeLists.txt (88%) rename {projects/dxildll => tools/clang}/tools/dxildll/DxcSigningContainerBuilder.cpp (100%) rename {projects/dxildll => tools/clang}/tools/dxildll/dxcvalidatorp.cpp (100%) rename {projects/dxildll => tools/clang}/tools/dxildll/dxildll.cpp (100%) rename {projects/dxildll => tools/clang}/tools/dxildll/dxildll.def (100%) rename {projects/dxildll => tools/clang}/tools/dxildll/dxildll.rc (100%) diff --git a/projects/CMakeLists.txt b/projects/CMakeLists.txt index ec85c77af8..bba494ca6b 100644 --- a/projects/CMakeLists.txt +++ b/projects/CMakeLists.txt @@ -4,5 +4,4 @@ set(DXC_PROJECTS_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) #if(WIN32 AND HLSL_BUILD_DXILCONV) add_subdirectory(include/Tracing) add_subdirectory(dxilconv) - add_subdirectory(dxildll) #endif (WIN32 AND HLSL_BUILD_DXILCONV) diff --git a/projects/dxildll/CMakeLists.txt b/projects/dxildll/CMakeLists.txt deleted file mode 100644 index 15e34380fe..0000000000 --- a/projects/dxildll/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -set(DXILDLL_PROJECT_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR}) -set(DXILDLL_PROJECT_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR}) - -macro(add_dxildll_project_library name) - add_llvm_library(${name} ${ARGN}) - set_output_directory(${name} ${LLVM_RUNTIME_OUTPUT_INTDIR} ${LLVM_LIBRARY_OUTPUT_INTDIR}) - set_target_properties(${name} PROPERTIES FOLDER "Dxildll libraries") -endmacro(add_dxildll_project_library) - -macro(add_dxildll_project_executable name) - add_llvm_library(${name} SHARED ${ARGN}) - set_target_properties(${name} PROPERTIES FOLDER "Dxildll executables") -endmacro(add_dxildll_project_executable) - -macro(add_dxildll_project_test_library name) - add_dxildll_project_library(${name} ${ARGN}) - set_target_properties(${name} PROPERTIES FOLDER "Dxildll tests") -endmacro(add_dxildll_project_test_library) - - -#if(WIN32) - add_subdirectory(lib) - add_subdirectory(tools) - #if (HLSL_INCLUDE_TESTS) - # add_subdirectory(unittests) - # add_subdirectory(test) - #endif (HLSL_INCLUDE_TESTS) -#endif() \ No newline at end of file diff --git a/projects/dxildll/lib/CMakeLists.txt b/projects/dxildll/lib/CMakeLists.txt deleted file mode 100644 index ef056afffe..0000000000 --- a/projects/dxildll/lib/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(DxilHash) diff --git a/projects/dxildll/tools/CMakeLists.txt b/projects/dxildll/tools/CMakeLists.txt deleted file mode 100644 index 1c3004b318..0000000000 --- a/projects/dxildll/tools/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -add_subdirectory(dxildll) diff --git a/tools/clang/tools/CMakeLists.txt b/tools/clang/tools/CMakeLists.txt index 595687c23d..559c9aa5e1 100644 --- a/tools/clang/tools/CMakeLists.txt +++ b/tools/clang/tools/CMakeLists.txt @@ -32,6 +32,8 @@ add_subdirectory(dxopt) add_subdirectory(dxl) add_subdirectory(dxr) add_subdirectory(dxv) +add_subdirectory(DxilHash) +add_subdirectory(dxildll) # These targets can currently only be built on Windows. if (MSVC) diff --git a/projects/dxildll/lib/DxilHash/CMakeLists.txt b/tools/clang/tools/DxilHash/CMakeLists.txt similarity index 77% rename from projects/dxildll/lib/DxilHash/CMakeLists.txt rename to tools/clang/tools/DxilHash/CMakeLists.txt index c992252e25..2d86a4b42f 100644 --- a/projects/dxildll/lib/DxilHash/CMakeLists.txt +++ b/tools/clang/tools/DxilHash/CMakeLists.txt @@ -1,7 +1,6 @@ if (WIN32) -add_dxildll_project_library(DxilHash - DxilHash.cpp -) +add_clang_library(DxilHash + DxilHash.cpp) else() include_directories( ${CMAKE_CURRENT_SOURCE_DIR} @@ -11,6 +10,6 @@ include_directories( ${DXILDLL_PROJECT_SOURCE_DIR}/include ${DXILDLL_PROJECT_BINARY_DIR}/include ) -add_llvm_library(DxilHash +add_clang_library(DxilHash DxilHash.cpp) endif() \ No newline at end of file diff --git a/projects/dxildll/lib/DxilHash/DxilHash.cpp b/tools/clang/tools/DxilHash/DxilHash.cpp similarity index 100% rename from projects/dxildll/lib/DxilHash/DxilHash.cpp rename to tools/clang/tools/DxilHash/DxilHash.cpp diff --git a/projects/dxildll/include/DxilHash.h b/tools/clang/tools/DxilHash/DxilHash.h similarity index 100% rename from projects/dxildll/include/DxilHash.h rename to tools/clang/tools/DxilHash/DxilHash.h diff --git a/projects/dxildll/tools/dxildll/CMakeLists.txt b/tools/clang/tools/dxildll/CMakeLists.txt similarity index 88% rename from projects/dxildll/tools/dxildll/CMakeLists.txt rename to tools/clang/tools/dxildll/CMakeLists.txt index 46cec06b86..4c61c2ce19 100644 --- a/projects/dxildll/tools/dxildll/CMakeLists.txt +++ b/tools/clang/tools/dxildll/CMakeLists.txt @@ -31,8 +31,8 @@ include_directories( ${WINTOOLS_SOURCE_ROOT}/include ${DXC_PROJECTS_SOURCE_DIR}/include ${DXC_PROJECTS_BINARY_DIR}/include - ${DXILDLL_PROJECT_SOURCE_DIR}/include - ${DXILDLL_PROJECT_BINARY_DIR}/include + ${LLVM_BINARY_DIR}/projects/include + ${LLVM_SOURCE_DIR}/tools/clang/tools/DxilHash ) set(sources @@ -43,14 +43,14 @@ set(sources ) if (WIN32) -add_dxildll_project_executable(dxildll ${sources}) +add_clang_library(dxildll SHARED ${sources}) list(APPEND DXCLibs kernel32.lib atls.lib advapi32.lib ole32.lib) else() -add_llvm_library(dxildll SHARED ${sources}) +add_clang_library(dxildll SHARED ${sources}) endif() target_link_libraries(dxildll PRIVATE diff --git a/projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp similarity index 100% rename from projects/dxildll/tools/dxildll/DxcSigningContainerBuilder.cpp rename to tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp diff --git a/projects/dxildll/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp similarity index 100% rename from projects/dxildll/tools/dxildll/dxcvalidatorp.cpp rename to tools/clang/tools/dxildll/dxcvalidatorp.cpp diff --git a/projects/dxildll/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp similarity index 100% rename from projects/dxildll/tools/dxildll/dxildll.cpp rename to tools/clang/tools/dxildll/dxildll.cpp diff --git a/projects/dxildll/tools/dxildll/dxildll.def b/tools/clang/tools/dxildll/dxildll.def similarity index 100% rename from projects/dxildll/tools/dxildll/dxildll.def rename to tools/clang/tools/dxildll/dxildll.def diff --git a/projects/dxildll/tools/dxildll/dxildll.rc b/tools/clang/tools/dxildll/dxildll.rc similarity index 100% rename from projects/dxildll/tools/dxildll/dxildll.rc rename to tools/clang/tools/dxildll/dxildll.rc From 2ac8a70bcbec17ceca51a991858b1fe56f51bccb Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 9 Jul 2024 13:04:39 -0700 Subject: [PATCH 03/19] Fix file header. Enable dxildll.rc. --- tools/clang/tools/DxilHash/DxilHash.cpp | 4 +++- tools/clang/tools/DxilHash/DxilHash.h | 5 ++++- tools/clang/tools/dxildll/CMakeLists.txt | 9 ++++++++- tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp | 2 +- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 2 ++ tools/clang/tools/dxildll/dxildll.cpp | 2 ++ 6 files changed, 20 insertions(+), 4 deletions(-) diff --git a/tools/clang/tools/DxilHash/DxilHash.cpp b/tools/clang/tools/DxilHash/DxilHash.cpp index 9f876fb338..65e306295d 100644 --- a/tools/clang/tools/DxilHash/DxilHash.cpp +++ b/tools/clang/tools/DxilHash/DxilHash.cpp @@ -1,7 +1,9 @@ /////////////////////////////////////////////////////////////////////////////// // // -// Hash.cpp // +// DxilHash.cpp // // Copyright (C) Microsoft Corporation. All rights reserved. // +// This file is distributed under the University of Illinois Open Source // +// License. See LICENSE.TXT for details. // // // // DXBC/DXIL container hashing functions // // // diff --git a/tools/clang/tools/DxilHash/DxilHash.h b/tools/clang/tools/DxilHash/DxilHash.h index 5de5cb8a1f..f470d2285a 100644 --- a/tools/clang/tools/DxilHash/DxilHash.h +++ b/tools/clang/tools/DxilHash/DxilHash.h @@ -1,7 +1,10 @@ /////////////////////////////////////////////////////////////////////////////// // // -// Hash.h // +// DxilHash.h // // Copyright (C) Microsoft Corporation. All rights reserved. // +// This file is distributed under the University of Illinois Open Source // +// License. See LICENSE.TXT for details. // +// // // // // DXBC/DXIL container hashing functions // // // diff --git a/tools/clang/tools/dxildll/CMakeLists.txt b/tools/clang/tools/dxildll/CMakeLists.txt index 4c61c2ce19..1f81ab62be 100644 --- a/tools/clang/tools/dxildll/CMakeLists.txt +++ b/tools/clang/tools/dxildll/CMakeLists.txt @@ -73,7 +73,14 @@ add_dependencies(dxildll endif() if (WIN32) -#wintools_set_resource_file(dxildll dxildll.rc) + get_target_property(sources dxildll SOURCES) + + list(APPEND sources dxildll.rc) + set_target_properties(${target_name} PROPERTIES SOURCES "${sources}") + + set_property(SOURCE dxildll.rc PROPERTY COMPILE_DEFINITIONS "INCLUDE_HLSL_VERSION_FILE=1") + set_property(SOURCE dxildll.rc PROPERTY COMPILE_OPTIONS "/I" "${HLSL_VERSION_LOCATION}" "/I" "${LLVM_MAIN_SRC_DIR}") + else() set_target_properties(dxildll PROPERTIES LINK_FLAGS "-z defs") diff --git a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp index e2a4646130..7e4e2e413e 100644 --- a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// // // -// dxcontainerbuilder.cpp // +// DxcSigningContainerBuilder.cpp // // Copyright (C) Microsoft Corporation. All rights reserved. // // This file is distributed under the University of Illinois Open Source // // License. See LICENSE.TXT for details. // diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index 57f04ff804..fabbc935ae 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -2,6 +2,8 @@ // // // dxcvalidatorp.cpp // // Copyright (C) Microsoft Corporation. All rights reserved. // +// This file is distributed under the University of Illinois Open Source // +// License. See LICENSE.TXT for details. // // // // Implements the DirectX Validator object, including signing support. // // // diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index 479306d9b1..f9594d741d 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -2,6 +2,8 @@ // // // dxildll.cpp // // Copyright (C) Microsoft. All rights reserved. // +// This file is distributed under the University of Illinois Open Source // +// License. See LICENSE.TXT for details. // // // // Implements the DxcCreateInstanceand DllMain functions // // for the dxil.dll module. // From b810ae7aea34be82ad7c9c87694fe27b3c7b74c4 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 10 Jul 2024 09:14:28 -0700 Subject: [PATCH 04/19] Add unit test for DxilHash. --- tools/clang/tools/DxilHash/DxilHash.h | 2 +- tools/clang/unittests/CMakeLists.txt | 3 +- tools/clang/unittests/DxilHash/CMakeLists.txt | 13 ++ .../clang/unittests/DxilHash/DxilHashTest.cpp | 125 ++++++++++++++++++ 4 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 tools/clang/unittests/DxilHash/CMakeLists.txt create mode 100644 tools/clang/unittests/DxilHash/DxilHashTest.cpp diff --git a/tools/clang/tools/DxilHash/DxilHash.h b/tools/clang/tools/DxilHash/DxilHash.h index f470d2285a..b35fd1b330 100644 --- a/tools/clang/tools/DxilHash/DxilHash.h +++ b/tools/clang/tools/DxilHash/DxilHash.h @@ -16,7 +16,7 @@ // Prototype for hash computing function // pOutHash must always return a DXIL_CONTAINER_HASH_SIZE byte hash result. -typedef void HASH_FUNCTION_PROTO(const BYTE* pData, uint32_t byteCount, BYTE* pOutHash); +typedef void HASH_FUNCTION_PROTO(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); // ************************************************************************************** // **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** diff --git a/tools/clang/unittests/CMakeLists.txt b/tools/clang/unittests/CMakeLists.txt index de7d45b897..dbb2898387 100644 --- a/tools/clang/unittests/CMakeLists.txt +++ b/tools/clang/unittests/CMakeLists.txt @@ -42,7 +42,8 @@ endif (CLANG_INCLUDE_TESTS) # HLSL Change # HLSL Change Starts -if (HLSL_INCLUDE_TESTS) +if (HLSL_INCLUDE_TESTS) + add_subdirectory(DxilHash) add_subdirectory(HLSL) add_subdirectory(HLSLTestLib) if (WIN32) # These tests require MS specific TAEF and DIA SDK diff --git a/tools/clang/unittests/DxilHash/CMakeLists.txt b/tools/clang/unittests/DxilHash/CMakeLists.txt new file mode 100644 index 0000000000..f4d35cac55 --- /dev/null +++ b/tools/clang/unittests/DxilHash/CMakeLists.txt @@ -0,0 +1,13 @@ +set(LLVM_LINK_COMPONENTS + Support + ) + +add_clang_unittest(DxilHashTests + DxilHashTest.cpp + ) + +include_directories(${LLVM_SOURCE_DIR}/tools/clang/tools/DxilHash) + +target_link_libraries(DxilHashTests + DxilHash + ) diff --git a/tools/clang/unittests/DxilHash/DxilHashTest.cpp b/tools/clang/unittests/DxilHash/DxilHashTest.cpp new file mode 100644 index 0000000000..816b6f1516 --- /dev/null +++ b/tools/clang/unittests/DxilHash/DxilHashTest.cpp @@ -0,0 +1,125 @@ +//===- unittests/DxilHash/DxilHashTest.cpp ---- Run DxilHash tests --------===// +// +// The LLVM Compiler Infrastructure +// +// This file is distributed under the University of Illinois Open Source +// License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// DxilHashing.h unit tests. +// +//===----------------------------------------------------------------------===// + +using BYTE = unsigned char; +using UINT32 = unsigned int; + +#include "DxilHash.h" +#include "gtest/gtest.h" + +namespace { + +struct OutputHash { + BYTE Data[DXIL_CONTAINER_HASH_SIZE]; + bool equal(UINT32 A, UINT32 B, UINT32 C, UINT32 D) { + UINT32 *DataPtr = (UINT32 *)Data; + return DataPtr[0] == A && DataPtr[1] == B && DataPtr[2] == C && + DataPtr[3] == D; + } +}; + +bool operator==(const OutputHash &A, const OutputHash &B) { + return memcmp(&A, &B, sizeof(OutputHash)) == 0; +} +bool operator!=(const OutputHash &A, const OutputHash &B) { + return memcmp(&A, &B, sizeof(OutputHash)) != 0; +} + +template OutputHash hash_value(T Input) { + OutputHash O; + ComputeHashRetail((const BYTE *)&Input, sizeof(T), (BYTE *)&O); + return O; +} + +template <> OutputHash hash_value(std::string S) { + OutputHash O; + ComputeHashRetail((const BYTE *)S.data(), S.size(), (BYTE *)&O); + return O; +} + +enum TestEnumeration { TE_Foo = 42, TE_Bar = 43 }; + +TEST(DxilHashTest, HashValueBasicTest) { + int x = 42, y = 43, c = 'x'; + void *p = nullptr; + uint64_t i = 71; + const unsigned ci = 71; + volatile int vi = 71; + const volatile int cvi = 71; + uintptr_t addr = reinterpret_cast(&y); + EXPECT_EQ(hash_value(42), hash_value(x)); + EXPECT_EQ(hash_value(42), hash_value(TE_Foo)); + EXPECT_NE(hash_value(42), hash_value(y)); + EXPECT_NE(hash_value(42), hash_value(TE_Bar)); + EXPECT_NE(hash_value(42), hash_value(p)); + EXPECT_EQ(hash_value(71), hash_value(ci)); + EXPECT_EQ(hash_value(71), hash_value(vi)); + EXPECT_EQ(hash_value(71), hash_value(cvi)); + EXPECT_EQ(hash_value(addr), hash_value(&y)); + // Miss match for type mismatch. + EXPECT_NE(hash_value(71), hash_value(i)); + EXPECT_NE(hash_value(c), hash_value('x')); + EXPECT_NE(hash_value('4'), hash_value('0' + 4)); + + std::string strA = "42"; + std::string strB = ""; + std::string strC = "42"; + + EXPECT_NE(hash_value(strA), hash_value(strB)); + EXPECT_EQ(hash_value(strA), hash_value(strC)); +} + +TEST(DxilHashTest, FixHashValueTest) { + std::string Data = ""; + EXPECT_EQ( + hash_value(Data).equal(0xf6600d14, 0xbae275b7, 0xd4be4a4e, 0xa1e9b201), + true); + + std::string Data2 = "abcdefghijklmnopqrstuvwxyzabcdef" + "abcdefghijklmnopqrstuvwxyzghijkl" + "abcdefghijklmnopqrstuvwxyzmnopqr" + "abcdefghijklmnopqrstuvwxyzstuvwx" + "abcdefghijklmnopqrstuvwxyzyzabcd"; + EXPECT_EQ( + hash_value(Data2).equal(0x00830fe6, 0xd8d8a035, 0xe6d62794, 0x016629df), + true); + + std::string Data3 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + EXPECT_EQ( + hash_value(Data3).equal(0xa5e5a0bb, 0xd96dd9f8, 0x0b4b7191, 0xd63aa54a), + true); + + std::string Data4 = "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz" + "zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"; + EXPECT_EQ( + hash_value(Data4).equal(0x58ed1b7a, 0x90ede58b, 0xf9ad857c, 0x5440e613), + true); + + std::string Data5 = "abababababababababababababababab" + "abababababababababababababababab" + "abababababababababababababababab" + "abababababababababababababababab" + "abababababababababababababababab"; + EXPECT_EQ( + hash_value(Data5).equal(0xf4fc06fc, 0x0bbd9ef7, 0x765ae0f7, 0x52a55925), + true); +} + +} // namespace From 3b9ce6e33b41f26b215d0e0657bbcf17636eed2d Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 11 Jul 2024 10:19:24 -0700 Subject: [PATCH 05/19] Fix the header clang-format. --- .../tools/dxildll/DxcSigningContainerBuilder.cpp | 11 ++++++----- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 14 +++++++------- tools/clang/tools/dxildll/dxildll.cpp | 12 ++++++------ 3 files changed, 19 insertions(+), 18 deletions(-) diff --git a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp index 7e4e2e413e..04c467d381 100644 --- a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp @@ -10,15 +10,16 @@ /////////////////////////////////////////////////////////////////////////////// #include "dxc/Support/WinIncludes.h" -#include "dxc/dxcapi.h" -#include "dxc/DxilContainer/DxilContainer.h" + +#include "DxilHash.h" #include "dxc/DxilContainer/DxcContainerBuilder.h" -#include "dxc/Support/Global.h" +#include "dxc/DxilContainer/DxilContainer.h" #include "dxc/Support/ErrorCodes.h" #include "dxc/Support/FileIOHelper.h" -#include "dxc/Support/microcom.h" +#include "dxc/Support/Global.h" #include "dxc/Support/dxcapi.impl.h" -#include "DxilHash.h" +#include "dxc/Support/microcom.h" +#include "dxc/dxcapi.h" #include diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index fabbc935ae..790654c47a 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -10,21 +10,21 @@ /////////////////////////////////////////////////////////////////////////////// #include "llvm/Bitcode/ReaderWriter.h" -#include "llvm/IR/LLVMContext.h" #include "llvm/IR/DiagnosticPrinter.h" +#include "llvm/IR/LLVMContext.h" -#include "dxc/Support/WinIncludes.h" -#include "dxc/Support/microcom.h" #include "dxc/DxilContainer/DxilContainer.h" #include "dxc/HLSL/DxilValidation.h" +#include "dxc/Support/WinIncludes.h" +#include "dxc/Support/microcom.h" +#include "dxc/DxilRootSignature/DxilRootSignature.h" +#include "dxc/Support/FileIOHelper.h" #include "dxc/Support/Global.h" +#include "dxc/Support/dxcapi.impl.h" +#include "dxc/Support/microcom.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/MSFileSystem.h" -#include "dxc/Support/microcom.h" -#include "dxc/Support/FileIOHelper.h" -#include "dxc/Support/dxcapi.impl.h" -#include "dxc/DxilRootSignature/DxilRootSignature.h" #ifdef _WIN32 #include "dxc/Tracing/dxcetw.h" #endif diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index f9594d741d..3ab3467dca 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -10,21 +10,21 @@ // // /////////////////////////////////////////////////////////////////////////////// -#include +#include "dxc/Support/Global.h" +#include "dxc/Support/WinIncludes.h" #include "llvm/Support/Debug.h" -#include "llvm/Support/ManagedStatic.h" #include "llvm/Support/FileSystem.h" -#include "dxc/Support/WinIncludes.h" -#include "dxc/Support/Global.h" +#include "llvm/Support/ManagedStatic.h" +#include #ifdef _WIN32 -#include "dxc/Tracing/dxcetw.h" #include "Tracing/DxcRuntimeEtw.h" +#include "dxc/Tracing/dxcetw.h" #endif #ifdef _WIN32 #define DXC_API_IMPORT #else -#define DXC_API_IMPORT __attribute__ ((visibility ("default"))) +#define DXC_API_IMPORT __attribute__((visibility("default"))) #endif #include "dxc/dxcisense.h" From 9ff5161b48835bf586a1ac1c2a79a4945815fede Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Thu, 11 Jul 2024 17:22:09 +0000 Subject: [PATCH 06/19] chore: autopublish 2024-07-11T17:22:09Z --- tools/clang/tools/DxilHash/DxilHash.cpp | 876 +++++++++--------- tools/clang/tools/DxilHash/DxilHash.h | 15 +- .../dxildll/DxcSigningContainerBuilder.cpp | 43 +- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 165 ++-- tools/clang/tools/dxildll/dxildll.cpp | 185 ++-- 5 files changed, 630 insertions(+), 654 deletions(-) diff --git a/tools/clang/tools/DxilHash/DxilHash.cpp b/tools/clang/tools/DxilHash/DxilHash.cpp index 65e306295d..24a44e842e 100644 --- a/tools/clang/tools/DxilHash/DxilHash.cpp +++ b/tools/clang/tools/DxilHash/DxilHash.cpp @@ -39,492 +39,450 @@ typedef unsigned char UINT8; #define S43 15 #define S44 21 -const BYTE padding[64] = -{ - 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 -}; - -void FF( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) -{ - a += ((b & c) | (~b & d)) + x + ac; - a = ((a << s) | (a >> (32-s))) + b; +const BYTE padding[64] = {0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; + +void FF(UINT &a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac) { + a += ((b & c) | (~b & d)) + x + ac; + a = ((a << s) | (a >> (32 - s))) + b; } -void GG( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) -{ - a += ((b & d) | (c & ~d)) + x + ac; - a = ((a << s) | (a >> (32-s))) + b; +void GG(UINT &a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac) { + a += ((b & d) | (c & ~d)) + x + ac; + a = ((a << s) | (a >> (32 - s))) + b; } -void HH( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) -{ - a += (b ^ c ^ d) + x + ac; - a = ((a << s) | (a >> (32-s))) + b; +void HH(UINT &a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac) { + a += (b ^ c ^ d) + x + ac; + a = ((a << s) | (a >> (32 - s))) + b; } -void II( UINT& a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac ) -{ - a += (c ^ (b | ~d)) + x + ac; - a = ((a << s) | (a >> (32-s))) + b; +void II(UINT &a, UINT b, UINT c, UINT d, UINT x, UINT8 s, UINT ac) { + a += (c ^ (b | ~d)) + x + ac; + a = ((a << s) | (a >> (32 - s))) + b; } // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** -void ComputeM_D_5Hash( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) -{ - UINT leftOver = byteCount & 0x3f; - UINT padAmount; - bool bTwoRowsPadding = false; - if( leftOver < 56 ) - { - padAmount = 56 - leftOver; - } - else - { - padAmount = 120 - leftOver; - bTwoRowsPadding = true; - } - UINT padAmountPlusSize = padAmount + 8; - UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; - UINT N = (byteCount + padAmountPlusSize) >> 6; - UINT offset = 0; - UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; - const BYTE* pCurrData = pData; - for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) - { - assert(byteCount - offset <= byteCount); // prefast doesn't understand this - no underflow will happen - assert(byteCount < 64*i+65); // prefast doesn't understand this - no overflows will happen in any memcpy below - assert(byteCount < leftOver+64*i+9); - assert(byteCount < leftOver+64*i+1); - UINT x[16]; - const UINT* pX; - if( i == NextEndState ) - { - if( !bTwoRowsPadding && i == N-1 ) - { - UINT remainder = byteCount - offset; - memcpy(x,pCurrData, remainder); // could copy nothing - memcpy((BYTE*)x + remainder, padding, padAmount); - x[14] = byteCount << 3; // sizepad lo - x[15] = 0; // sizepad hi - } - else if( bTwoRowsPadding ) - { - if( i == N-2 ) - { - UINT remainder = byteCount - offset; - memcpy(x,pCurrData, remainder); - memcpy((BYTE*)x + remainder, padding, padAmount-56); - NextEndState = N-1; - } - else if( i == N-1 ) - { - memcpy(x, padding + padAmount-56, 56); - x[14] = byteCount << 3; // sizepad lo - x[15] = 0; // sizepad hi - } - } - pX = x; - } - else - { - pX = (const UINT*)pCurrData; +void ComputeM_D_5Hash(const BYTE *pData, UINT byteCount, BYTE *pOutHash) { + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if (leftOver < 56) { + padAmount = 56 - leftOver; + } else { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N - 2 : N - 1; + const BYTE *pCurrData = pData; + for (UINT i = 0; i < N; i++, offset += 64, pCurrData += 64) { + assert(byteCount - offset <= byteCount); // prefast doesn't understand this + // - no underflow will happen + assert(byteCount < + 64 * i + 65); // prefast doesn't understand this - no overflows will + // happen in any memcpy below + assert(byteCount < leftOver + 64 * i + 9); + assert(byteCount < leftOver + 64 * i + 1); + UINT x[16]; + const UINT *pX; + if (i == NextEndState) { + if (!bTwoRowsPadding && i == N - 1) { + UINT remainder = byteCount - offset; + memcpy(x, pCurrData, remainder); // could copy nothing + memcpy((BYTE *)x + remainder, padding, padAmount); + x[14] = byteCount << 3; // sizepad lo + x[15] = 0; // sizepad hi + } else if (bTwoRowsPadding) { + if (i == N - 2) { + UINT remainder = byteCount - offset; + memcpy(x, pCurrData, remainder); + memcpy((BYTE *)x + remainder, padding, padAmount - 56); + NextEndState = N - 1; + } else if (i == N - 1) { + memcpy(x, padding + padAmount - 56, 56); + x[14] = byteCount << 3; // sizepad lo + x[15] = 0; // sizepad hi } - - UINT a = state[0]; - UINT b = state[1]; - UINT c = state[2]; - UINT d = state[3]; - - /* Round 1 */ - FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ - FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ - FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ - FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ - FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ - FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ - FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ - FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ - FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ - FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ - FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ - FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ - FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ - FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ - FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ - FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ - - /* Round 2 */ - GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ - GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ - GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ - GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ - GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ - GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ - GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ - GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ - GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ - GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ - GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ - GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ - GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ - GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ - GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ - GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ - - /* Round 3 */ - HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ - HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ - HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ - HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ - HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ - HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ - HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ - HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ - HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ - HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ - HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ - HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ - HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ - HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ - HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ - HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ - - /* Round 4 */ - II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ - II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ - II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ - II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ - II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ - II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ - II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ - II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ - II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ - II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ - II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ - II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ - II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ - II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ - II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ - II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; + } + pX = x; + } else { + pX = (const UINT *)pCurrData; } - memcpy(pOutHash,state,16); + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF(a, b, c, d, pX[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, pX[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, pX[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, pX[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, pX[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, pX[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, pX[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, pX[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, pX[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, pX[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, pX[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, pX[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, pX[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, pX[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, pX[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, pX[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG(a, b, c, d, pX[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, pX[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, pX[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, pX[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, pX[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, pX[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, pX[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, pX[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, pX[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, pX[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, pX[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, pX[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, pX[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, pX[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, pX[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, pX[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH(a, b, c, d, pX[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, pX[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, pX[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, pX[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, pX[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, pX[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, pX[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, pX[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, pX[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, pX[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, pX[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, pX[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, pX[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, pX[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, pX[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, pX[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II(a, b, c, d, pX[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, pX[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, pX[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, pX[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, pX[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, pX[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, pX[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, pX[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, pX[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, pX[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, pX[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, pX[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, pX[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, pX[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, pX[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, pX[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash, state, 16); } // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** -void ComputeHashRetail( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) -{ - UINT leftOver = byteCount & 0x3f; - UINT padAmount; - bool bTwoRowsPadding = false; - if( leftOver < 56 ) - { - padAmount = 56 - leftOver; - } - else - { - padAmount = 120 - leftOver; - bTwoRowsPadding = true; - } - UINT padAmountPlusSize = padAmount + 8; - UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; - UINT N = (byteCount + padAmountPlusSize) >> 6; - UINT offset = 0; - UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; - const BYTE* pCurrData = pData; - for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) - { - UINT x[16]; - const UINT* pX; - if( i == NextEndState ) - { - if( !bTwoRowsPadding && i == N-1 ) - { - UINT remainder = byteCount - offset; - x[0] = byteCount << 3; - - assert(byteCount - offset <= byteCount); // check for underflow - assert(pCurrData + remainder == pData + byteCount); - memcpy((BYTE*)x+4,pCurrData, remainder); // could copy nothing - memcpy((BYTE*)x+4 + remainder, padding, padAmount); - x[15] = 1 | (byteCount << 1); - } - else if( bTwoRowsPadding ) - { - if( i == N-2 ) - { - UINT remainder = byteCount - offset; - - assert(byteCount - offset <= byteCount); // check for underflow - assert(pCurrData + remainder == pData + byteCount); - memcpy(x,pCurrData, remainder); - memcpy((BYTE*)x + remainder, padding, padAmount-56); - NextEndState = N-1; - } - else if( i == N-1 ) - { - x[0] = byteCount << 3; - memcpy((BYTE*)x+4, padding + padAmount-56, 56); - x[15] = 1 | (byteCount << 1); - } - } - pX = x; +void ComputeHashRetail(const BYTE *pData, UINT byteCount, BYTE *pOutHash) { + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if (leftOver < 56) { + padAmount = 56 - leftOver; + } else { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N - 2 : N - 1; + const BYTE *pCurrData = pData; + for (UINT i = 0; i < N; i++, offset += 64, pCurrData += 64) { + UINT x[16]; + const UINT *pX; + if (i == NextEndState) { + if (!bTwoRowsPadding && i == N - 1) { + UINT remainder = byteCount - offset; + x[0] = byteCount << 3; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy((BYTE *)x + 4, pCurrData, remainder); // could copy nothing + memcpy((BYTE *)x + 4 + remainder, padding, padAmount); + x[15] = 1 | (byteCount << 1); + } else if (bTwoRowsPadding) { + if (i == N - 2) { + UINT remainder = byteCount - offset; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy(x, pCurrData, remainder); + memcpy((BYTE *)x + remainder, padding, padAmount - 56); + NextEndState = N - 1; + } else if (i == N - 1) { + x[0] = byteCount << 3; + memcpy((BYTE *)x + 4, padding + padAmount - 56, 56); + x[15] = 1 | (byteCount << 1); } - else - { - assert(pCurrData + 64 <= pData + byteCount); - pX = (const UINT*)pCurrData; - } - - UINT a = state[0]; - UINT b = state[1]; - UINT c = state[2]; - UINT d = state[3]; - - /* Round 1 */ - FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ - FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ - FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ - FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ - FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ - FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ - FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ - FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ - FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ - FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ - FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ - FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ - FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ - FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ - FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ - FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ - - /* Round 2 */ - GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ - GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ - GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ - GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ - GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ - GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ - GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ - GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ - GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ - GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ - GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ - GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ - GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ - GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ - GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ - GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ - - /* Round 3 */ - HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ - HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ - HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ - HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ - HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ - HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ - HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ - HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ - HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ - HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ - HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ - HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ - HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ - HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ - HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ - HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ - - /* Round 4 */ - II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ - II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ - II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ - II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ - II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ - II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ - II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ - II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ - II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ - II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ - II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ - II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ - II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ - II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ - II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ - II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; + } + pX = x; + } else { + assert(pCurrData + 64 <= pData + byteCount); + pX = (const UINT *)pCurrData; } - memcpy(pOutHash,state,16); + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF(a, b, c, d, pX[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, pX[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, pX[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, pX[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, pX[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, pX[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, pX[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, pX[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, pX[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, pX[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, pX[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, pX[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, pX[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, pX[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, pX[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, pX[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG(a, b, c, d, pX[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, pX[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, pX[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, pX[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, pX[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, pX[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, pX[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, pX[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, pX[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, pX[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, pX[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, pX[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, pX[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, pX[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, pX[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, pX[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH(a, b, c, d, pX[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, pX[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, pX[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, pX[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, pX[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, pX[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, pX[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, pX[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, pX[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, pX[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, pX[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, pX[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, pX[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, pX[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, pX[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, pX[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II(a, b, c, d, pX[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, pX[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, pX[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, pX[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, pX[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, pX[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, pX[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, pX[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, pX[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, pX[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, pX[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, pX[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, pX[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, pX[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, pX[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, pX[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash, state, 16); } // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** -void ComputeHashDebug( const BYTE* pData, UINT byteCount, BYTE* pOutHash ) -{ - UINT leftOver = byteCount & 0x3f; - UINT padAmount; - bool bTwoRowsPadding = false; - if( leftOver < 56 ) - { - padAmount = 56 - leftOver; - } - else - { - padAmount = 120 - leftOver; - bTwoRowsPadding = true; - } - UINT padAmountPlusSize = padAmount + 8; - UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; - UINT N = (byteCount + padAmountPlusSize) >> 6; - UINT offset = 0; - UINT NextEndState = bTwoRowsPadding ? N-2 : N-1; - const BYTE* pCurrData = pData; - for(UINT i = 0; i < N; i++, offset+=64, pCurrData+=64) - { - UINT x[16]; - const UINT* pX; - if( i == NextEndState ) - { - if( !bTwoRowsPadding && i == N-1 ) - { - UINT remainder = byteCount - offset; - x[0] = byteCount << 4 | 0xf; - - assert(byteCount - offset <= byteCount); // check for underflow - assert(pCurrData + remainder == pData + byteCount); - memcpy((BYTE*)x+4,pCurrData, remainder); // could copy nothing - memcpy((BYTE*)x+4 + remainder, padding, padAmount); - x[15] = (byteCount << 2) | 0x10000000; - } - else if( bTwoRowsPadding ) - { - if( i == N-2 ) - { - UINT remainder = byteCount - offset; - assert(byteCount - offset <= byteCount); // check for underflow - assert(pCurrData + remainder == pData + byteCount); - memcpy(x,pCurrData, remainder); - memcpy((BYTE*)x + remainder, padding, padAmount-56); - NextEndState = N-1; - } - else if( i == N-1 ) - { - x[0] = byteCount << 4 | 0xf; - memcpy((BYTE*)x+4, padding + padAmount-56, 56); - x[15] = (byteCount << 2) | 0x10000000; - } - } - pX = x; - } - else - { - assert(pCurrData + 64 <= pData + byteCount); - pX = (const UINT*)pCurrData; +void ComputeHashDebug(const BYTE *pData, UINT byteCount, BYTE *pOutHash) { + UINT leftOver = byteCount & 0x3f; + UINT padAmount; + bool bTwoRowsPadding = false; + if (leftOver < 56) { + padAmount = 56 - leftOver; + } else { + padAmount = 120 - leftOver; + bTwoRowsPadding = true; + } + UINT padAmountPlusSize = padAmount + 8; + UINT state[4] = {0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476}; + UINT N = (byteCount + padAmountPlusSize) >> 6; + UINT offset = 0; + UINT NextEndState = bTwoRowsPadding ? N - 2 : N - 1; + const BYTE *pCurrData = pData; + for (UINT i = 0; i < N; i++, offset += 64, pCurrData += 64) { + UINT x[16]; + const UINT *pX; + if (i == NextEndState) { + if (!bTwoRowsPadding && i == N - 1) { + UINT remainder = byteCount - offset; + x[0] = byteCount << 4 | 0xf; + + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy((BYTE *)x + 4, pCurrData, remainder); // could copy nothing + memcpy((BYTE *)x + 4 + remainder, padding, padAmount); + x[15] = (byteCount << 2) | 0x10000000; + } else if (bTwoRowsPadding) { + if (i == N - 2) { + UINT remainder = byteCount - offset; + assert(byteCount - offset <= byteCount); // check for underflow + assert(pCurrData + remainder == pData + byteCount); + memcpy(x, pCurrData, remainder); + memcpy((BYTE *)x + remainder, padding, padAmount - 56); + NextEndState = N - 1; + } else if (i == N - 1) { + x[0] = byteCount << 4 | 0xf; + memcpy((BYTE *)x + 4, padding + padAmount - 56, 56); + x[15] = (byteCount << 2) | 0x10000000; } - - UINT a = state[0]; - UINT b = state[1]; - UINT c = state[2]; - UINT d = state[3]; - - /* Round 1 */ - FF( a, b, c, d, pX[ 0], S11, 0xd76aa478 ); /* 1 */ - FF( d, a, b, c, pX[ 1], S12, 0xe8c7b756 ); /* 2 */ - FF( c, d, a, b, pX[ 2], S13, 0x242070db ); /* 3 */ - FF( b, c, d, a, pX[ 3], S14, 0xc1bdceee ); /* 4 */ - FF( a, b, c, d, pX[ 4], S11, 0xf57c0faf ); /* 5 */ - FF( d, a, b, c, pX[ 5], S12, 0x4787c62a ); /* 6 */ - FF( c, d, a, b, pX[ 6], S13, 0xa8304613 ); /* 7 */ - FF( b, c, d, a, pX[ 7], S14, 0xfd469501 ); /* 8 */ - FF( a, b, c, d, pX[ 8], S11, 0x698098d8 ); /* 9 */ - FF( d, a, b, c, pX[ 9], S12, 0x8b44f7af ); /* 10 */ - FF( c, d, a, b, pX[10], S13, 0xffff5bb1 ); /* 11 */ - FF( b, c, d, a, pX[11], S14, 0x895cd7be ); /* 12 */ - FF( a, b, c, d, pX[12], S11, 0x6b901122 ); /* 13 */ - FF( d, a, b, c, pX[13], S12, 0xfd987193 ); /* 14 */ - FF( c, d, a, b, pX[14], S13, 0xa679438e ); /* 15 */ - FF( b, c, d, a, pX[15], S14, 0x49b40821 ); /* 16 */ - - /* Round 2 */ - GG( a, b, c, d, pX[ 1], S21, 0xf61e2562 ); /* 17 */ - GG( d, a, b, c, pX[ 6], S22, 0xc040b340 ); /* 18 */ - GG( c, d, a, b, pX[11], S23, 0x265e5a51 ); /* 19 */ - GG( b, c, d, a, pX[ 0], S24, 0xe9b6c7aa ); /* 20 */ - GG( a, b, c, d, pX[ 5], S21, 0xd62f105d ); /* 21 */ - GG( d, a, b, c, pX[10], S22, 0x2441453 ); /* 22 */ - GG( c, d, a, b, pX[15], S23, 0xd8a1e681 ); /* 23 */ - GG( b, c, d, a, pX[ 4], S24, 0xe7d3fbc8 ); /* 24 */ - GG( a, b, c, d, pX[ 9], S21, 0x21e1cde6 ); /* 25 */ - GG( d, a, b, c, pX[14], S22, 0xc33707d6 ); /* 26 */ - GG( c, d, a, b, pX[ 3], S23, 0xf4d50d87 ); /* 27 */ - GG( b, c, d, a, pX[ 8], S24, 0x455a14ed ); /* 28 */ - GG( a, b, c, d, pX[13], S21, 0xa9e3e905 ); /* 29 */ - GG( d, a, b, c, pX[ 2], S22, 0xfcefa3f8 ); /* 30 */ - GG( c, d, a, b, pX[ 7], S23, 0x676f02d9 ); /* 31 */ - GG( b, c, d, a, pX[12], S24, 0x8d2a4c8a ); /* 32 */ - - /* Round 3 */ - HH( a, b, c, d, pX[ 5], S31, 0xfffa3942 ); /* 33 */ - HH( d, a, b, c, pX[ 8], S32, 0x8771f681 ); /* 34 */ - HH( c, d, a, b, pX[11], S33, 0x6d9d6122 ); /* 35 */ - HH( b, c, d, a, pX[14], S34, 0xfde5380c ); /* 36 */ - HH( a, b, c, d, pX[ 1], S31, 0xa4beea44 ); /* 37 */ - HH( d, a, b, c, pX[ 4], S32, 0x4bdecfa9 ); /* 38 */ - HH( c, d, a, b, pX[ 7], S33, 0xf6bb4b60 ); /* 39 */ - HH( b, c, d, a, pX[10], S34, 0xbebfbc70 ); /* 40 */ - HH( a, b, c, d, pX[13], S31, 0x289b7ec6 ); /* 41 */ - HH( d, a, b, c, pX[ 0], S32, 0xeaa127fa ); /* 42 */ - HH( c, d, a, b, pX[ 3], S33, 0xd4ef3085 ); /* 43 */ - HH( b, c, d, a, pX[ 6], S34, 0x4881d05 ); /* 44 */ - HH( a, b, c, d, pX[ 9], S31, 0xd9d4d039 ); /* 45 */ - HH( d, a, b, c, pX[12], S32, 0xe6db99e5 ); /* 46 */ - HH( c, d, a, b, pX[15], S33, 0x1fa27cf8 ); /* 47 */ - HH( b, c, d, a, pX[ 2], S34, 0xc4ac5665 ); /* 48 */ - - /* Round 4 */ - II( a, b, c, d, pX[ 0], S41, 0xf4292244 ); /* 49 */ - II( d, a, b, c, pX[ 7], S42, 0x432aff97 ); /* 50 */ - II( c, d, a, b, pX[14], S43, 0xab9423a7 ); /* 51 */ - II( b, c, d, a, pX[ 5], S44, 0xfc93a039 ); /* 52 */ - II( a, b, c, d, pX[12], S41, 0x655b59c3 ); /* 53 */ - II( d, a, b, c, pX[ 3], S42, 0x8f0ccc92 ); /* 54 */ - II( c, d, a, b, pX[10], S43, 0xffeff47d ); /* 55 */ - II( b, c, d, a, pX[ 1], S44, 0x85845dd1 ); /* 56 */ - II( a, b, c, d, pX[ 8], S41, 0x6fa87e4f ); /* 57 */ - II( d, a, b, c, pX[15], S42, 0xfe2ce6e0 ); /* 58 */ - II( c, d, a, b, pX[ 6], S43, 0xa3014314 ); /* 59 */ - II( b, c, d, a, pX[13], S44, 0x4e0811a1 ); /* 60 */ - II( a, b, c, d, pX[ 4], S41, 0xf7537e82 ); /* 61 */ - II( d, a, b, c, pX[11], S42, 0xbd3af235 ); /* 62 */ - II( c, d, a, b, pX[ 2], S43, 0x2ad7d2bb ); /* 63 */ - II( b, c, d, a, pX[ 9], S44, 0xeb86d391 ); /* 64 */ - - state[0] += a; - state[1] += b; - state[2] += c; - state[3] += d; + } + pX = x; + } else { + assert(pCurrData + 64 <= pData + byteCount); + pX = (const UINT *)pCurrData; } - memcpy(pOutHash,state,16); + UINT a = state[0]; + UINT b = state[1]; + UINT c = state[2]; + UINT d = state[3]; + + /* Round 1 */ + FF(a, b, c, d, pX[0], S11, 0xd76aa478); /* 1 */ + FF(d, a, b, c, pX[1], S12, 0xe8c7b756); /* 2 */ + FF(c, d, a, b, pX[2], S13, 0x242070db); /* 3 */ + FF(b, c, d, a, pX[3], S14, 0xc1bdceee); /* 4 */ + FF(a, b, c, d, pX[4], S11, 0xf57c0faf); /* 5 */ + FF(d, a, b, c, pX[5], S12, 0x4787c62a); /* 6 */ + FF(c, d, a, b, pX[6], S13, 0xa8304613); /* 7 */ + FF(b, c, d, a, pX[7], S14, 0xfd469501); /* 8 */ + FF(a, b, c, d, pX[8], S11, 0x698098d8); /* 9 */ + FF(d, a, b, c, pX[9], S12, 0x8b44f7af); /* 10 */ + FF(c, d, a, b, pX[10], S13, 0xffff5bb1); /* 11 */ + FF(b, c, d, a, pX[11], S14, 0x895cd7be); /* 12 */ + FF(a, b, c, d, pX[12], S11, 0x6b901122); /* 13 */ + FF(d, a, b, c, pX[13], S12, 0xfd987193); /* 14 */ + FF(c, d, a, b, pX[14], S13, 0xa679438e); /* 15 */ + FF(b, c, d, a, pX[15], S14, 0x49b40821); /* 16 */ + + /* Round 2 */ + GG(a, b, c, d, pX[1], S21, 0xf61e2562); /* 17 */ + GG(d, a, b, c, pX[6], S22, 0xc040b340); /* 18 */ + GG(c, d, a, b, pX[11], S23, 0x265e5a51); /* 19 */ + GG(b, c, d, a, pX[0], S24, 0xe9b6c7aa); /* 20 */ + GG(a, b, c, d, pX[5], S21, 0xd62f105d); /* 21 */ + GG(d, a, b, c, pX[10], S22, 0x2441453); /* 22 */ + GG(c, d, a, b, pX[15], S23, 0xd8a1e681); /* 23 */ + GG(b, c, d, a, pX[4], S24, 0xe7d3fbc8); /* 24 */ + GG(a, b, c, d, pX[9], S21, 0x21e1cde6); /* 25 */ + GG(d, a, b, c, pX[14], S22, 0xc33707d6); /* 26 */ + GG(c, d, a, b, pX[3], S23, 0xf4d50d87); /* 27 */ + GG(b, c, d, a, pX[8], S24, 0x455a14ed); /* 28 */ + GG(a, b, c, d, pX[13], S21, 0xa9e3e905); /* 29 */ + GG(d, a, b, c, pX[2], S22, 0xfcefa3f8); /* 30 */ + GG(c, d, a, b, pX[7], S23, 0x676f02d9); /* 31 */ + GG(b, c, d, a, pX[12], S24, 0x8d2a4c8a); /* 32 */ + + /* Round 3 */ + HH(a, b, c, d, pX[5], S31, 0xfffa3942); /* 33 */ + HH(d, a, b, c, pX[8], S32, 0x8771f681); /* 34 */ + HH(c, d, a, b, pX[11], S33, 0x6d9d6122); /* 35 */ + HH(b, c, d, a, pX[14], S34, 0xfde5380c); /* 36 */ + HH(a, b, c, d, pX[1], S31, 0xa4beea44); /* 37 */ + HH(d, a, b, c, pX[4], S32, 0x4bdecfa9); /* 38 */ + HH(c, d, a, b, pX[7], S33, 0xf6bb4b60); /* 39 */ + HH(b, c, d, a, pX[10], S34, 0xbebfbc70); /* 40 */ + HH(a, b, c, d, pX[13], S31, 0x289b7ec6); /* 41 */ + HH(d, a, b, c, pX[0], S32, 0xeaa127fa); /* 42 */ + HH(c, d, a, b, pX[3], S33, 0xd4ef3085); /* 43 */ + HH(b, c, d, a, pX[6], S34, 0x4881d05); /* 44 */ + HH(a, b, c, d, pX[9], S31, 0xd9d4d039); /* 45 */ + HH(d, a, b, c, pX[12], S32, 0xe6db99e5); /* 46 */ + HH(c, d, a, b, pX[15], S33, 0x1fa27cf8); /* 47 */ + HH(b, c, d, a, pX[2], S34, 0xc4ac5665); /* 48 */ + + /* Round 4 */ + II(a, b, c, d, pX[0], S41, 0xf4292244); /* 49 */ + II(d, a, b, c, pX[7], S42, 0x432aff97); /* 50 */ + II(c, d, a, b, pX[14], S43, 0xab9423a7); /* 51 */ + II(b, c, d, a, pX[5], S44, 0xfc93a039); /* 52 */ + II(a, b, c, d, pX[12], S41, 0x655b59c3); /* 53 */ + II(d, a, b, c, pX[3], S42, 0x8f0ccc92); /* 54 */ + II(c, d, a, b, pX[10], S43, 0xffeff47d); /* 55 */ + II(b, c, d, a, pX[1], S44, 0x85845dd1); /* 56 */ + II(a, b, c, d, pX[8], S41, 0x6fa87e4f); /* 57 */ + II(d, a, b, c, pX[15], S42, 0xfe2ce6e0); /* 58 */ + II(c, d, a, b, pX[6], S43, 0xa3014314); /* 59 */ + II(b, c, d, a, pX[13], S44, 0x4e0811a1); /* 60 */ + II(a, b, c, d, pX[4], S41, 0xf7537e82); /* 61 */ + II(d, a, b, c, pX[11], S42, 0xbd3af235); /* 62 */ + II(c, d, a, b, pX[2], S43, 0x2ad7d2bb); /* 63 */ + II(b, c, d, a, pX[9], S44, 0xeb86d391); /* 64 */ + + state[0] += a; + state[1] += b; + state[2] += c; + state[3] += d; + } + + memcpy(pOutHash, state, 16); } // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** diff --git a/tools/clang/tools/DxilHash/DxilHash.h b/tools/clang/tools/DxilHash/DxilHash.h index b35fd1b330..a483e5eedd 100644 --- a/tools/clang/tools/DxilHash/DxilHash.h +++ b/tools/clang/tools/DxilHash/DxilHash.h @@ -14,19 +14,22 @@ #define DXIL_CONTAINER_HASH_SIZE 16 -// Prototype for hash computing function +// Prototype for hash computing function // pOutHash must always return a DXIL_CONTAINER_HASH_SIZE byte hash result. -typedef void HASH_FUNCTION_PROTO(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); +typedef void HASH_FUNCTION_PROTO(const BYTE *pData, UINT32 byteCount, + BYTE *pOutHash); // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** // Derived from: RSA Data Security, Inc. M // D // 5 Message-Digest Algorithm // Computes a 128-bit hash of pData (size byteCount), returning 16 BYTE output -void ComputeHashRetail(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); -void ComputeHashDebug(const BYTE* pData, UINT32 byteCount, BYTE* pOutHash); +void ComputeHashRetail(const BYTE *pData, UINT32 byteCount, BYTE *pOutHash); +void ComputeHashDebug(const BYTE *pData, UINT32 byteCount, BYTE *pOutHash); // ************************************************************************************** -// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE SECURE!!! *** +// **** DO NOT USE THESE ROUTINES TO PROVIDE FUNCTIONALITY THAT NEEDS TO BE +// SECURE!!! *** // ************************************************************************************** diff --git a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp index 04c467d381..7574db26e8 100644 --- a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp @@ -25,7 +25,7 @@ using namespace hlsl; -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv); +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv); class DxcSigningContainerBuilder : public DxcContainerBuilder { public: @@ -33,28 +33,34 @@ class DxcSigningContainerBuilder : public DxcContainerBuilder { DXC_MICROCOM_TM_ALLOC(DxcSigningContainerBuilder); - HRESULT STDMETHODCALLTYPE Load(_In_ IDxcBlob *pDxilContainerHeader) override; // Loads DxilContainer to the builder - HRESULT STDMETHODCALLTYPE SerializeContainer(_Out_ IDxcOperationResult **ppResult) override; // Builds a container of the given container builder state + HRESULT STDMETHODCALLTYPE Load(_In_ IDxcBlob *pDxilContainerHeader) + override; // Loads DxilContainer to the builder + HRESULT STDMETHODCALLTYPE + SerializeContainer(_Out_ IDxcOperationResult **ppResult) + override; // Builds a container of the given container builder state private: // Function to compute hash when valid dxil container is built // This is nullptr if loaded container has invalid hash - HASH_FUNCTION_PROTO *m_pHashFunction; - + HASH_FUNCTION_PROTO *m_pHashFunction; + void FindHashFunctionFromSource(const DxilContainerHeader *pContainerHeader); void HashAndUpdate(DxilContainerHeader *pContainerHeader); }; -HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::Load(_In_ IDxcBlob *pSource) { +HRESULT STDMETHODCALLTYPE +DxcSigningContainerBuilder::Load(_In_ IDxcBlob *pSource) { HRESULT hr = DxcContainerBuilder::Load(pSource); if (SUCCEEDED(hr)) { - const DxilContainerHeader *pHeader = (DxilContainerHeader *)pSource->GetBufferPointer(); + const DxilContainerHeader *pHeader = + (DxilContainerHeader *)pSource->GetBufferPointer(); FindHashFunctionFromSource(pHeader); } return hr; } -HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::SerializeContainer(_Out_ IDxcOperationResult **ppResult) { +HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::SerializeContainer( + _Out_ IDxcOperationResult **ppResult) { HRESULT hr_saved = DxcContainerBuilder::SerializeContainer(ppResult); if (!SUCCEEDED(hr_saved)) { return hr_saved; @@ -76,7 +82,8 @@ HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::SerializeContainer(_Out_ I return hr_saved; } -void DxcSigningContainerBuilder::FindHashFunctionFromSource(const DxilContainerHeader *pContainerHeader) { +void DxcSigningContainerBuilder::FindHashFunctionFromSource( + const DxilContainerHeader *pContainerHeader) { DXASSERT(pContainerHeader != nullptr && IsDxilContainerLike(pContainerHeader, pContainerHeader->ContainerSizeInBytes), @@ -100,21 +107,25 @@ void DxcSigningContainerBuilder::FindHashFunctionFromSource(const DxilContainerH } // For Internal hash function. -void DxcSigningContainerBuilder::HashAndUpdate(DxilContainerHeader *pContainerHeader) { +void DxcSigningContainerBuilder::HashAndUpdate( + DxilContainerHeader *pContainerHeader) { if (m_pHashFunction != nullptr) { - DXASSERT(pContainerHeader != nullptr, "Otherwise serialization should have failed."); - static const UINT32 HashStartOffset = offsetof(struct DxilContainerHeader, Version); - const BYTE* pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; - UINT AmountToHash = pContainerHeader->ContainerSizeInBytes - HashStartOffset; + DXASSERT(pContainerHeader != nullptr, + "Otherwise serialization should have failed."); + static const UINT32 HashStartOffset = + offsetof(struct DxilContainerHeader, Version); + const BYTE *pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; + UINT AmountToHash = + pContainerHeader->ContainerSizeInBytes - HashStartOffset; m_pHashFunction(pDataToHash, AmountToHash, pContainerHeader->Hash.Digest); } } - HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv) { // Call dxil.dll's containerbuilder *ppv = nullptr; - CComPtr Result(DxcSigningContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); + CComPtr Result( + DxcSigningContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); IFROOM(Result.p); Result->Init(); return Result->QueryInterface(riid, ppv); diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index 790654c47a..f93f03852b 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -39,46 +39,51 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { HRESULT RunValidation(_In_ IDxcBlob *pShader, _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, - _In_opt_ DxcBuffer *pOptDebugBitcode, + _In_ UINT32 Flags, _In_opt_ DxcBuffer *pOptDebugBitcode, _COM_Outptr_ IDxcBlob **pSigned); - HRESULT RunRootSignatureValidation( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, - _COM_Outptr_ IDxcBlob **pSigned); - + HRESULT + RunRootSignatureValidation(_In_ IDxcBlob *pShader, // Shader to validate. + _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, + _COM_Outptr_ IDxcBlob **pSigned); public: DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() DXC_MICROCOM_TM_CTOR(DxcValidator) - HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, void **ppvObject) override { + HRESULT STDMETHODCALLTYPE QueryInterface(REFIID iid, + void **ppvObject) override { - return DoBasicQueryInterface(this, iid, ppvObject); + return DoBasicQueryInterface(this, iid, ppvObject); } HRESULT STDMETHODCALLTYPE Validate( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors - ) override; + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _COM_Outptr_ IDxcOperationResult * + *ppResult // Validation output status, buffer, and errors + ) override; // IDxcValidator2 HRESULT STDMETHODCALLTYPE ValidateWithDebug( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _In_ DxcBuffer *pDebugModule, // Debug module bitcode to provide line numbers - _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors - ) override; + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _In_ DxcBuffer + *pDebugModule, // Debug module bitcode to provide line numbers + _COM_Outptr_ IDxcOperationResult * + *ppResult // Validation output status, buffer, and errors + ) override; // IDxcVersionInfo - HRESULT STDMETHODCALLTYPE GetVersion(_Out_ UINT32 *pMajor, _Out_ UINT32 *pMinor) override; + HRESULT STDMETHODCALLTYPE GetVersion(_Out_ UINT32 *pMajor, + _Out_ UINT32 *pMinor) override; HRESULT STDMETHODCALLTYPE GetFlags(_Out_ UINT32 *pFlags) override; }; -HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(_Out_ UINT32 *pMajor, _Out_ UINT32 *pMinor) { +HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(_Out_ UINT32 *pMajor, + _Out_ UINT32 *pMinor) { DxcThreadMalloc TM(m_pMalloc); if (pMajor == nullptr || pMinor == nullptr) return E_INVALIDARG; @@ -100,44 +105,49 @@ HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(_Out_ UINT32 *pFlags) { static void HashAndUpdate(DxilContainerHeader *pContainer) { // Compute hash and update stored hash. // Hash the container from this offset to the end. - static const UINT32 DXBCHashStartOffset = offsetof(struct DxilContainerHeader,Version); - const BYTE* pDataToHash = (const BYTE*)pContainer + DXBCHashStartOffset; + static const UINT32 DXBCHashStartOffset = + offsetof(struct DxilContainerHeader, Version); + const BYTE *pDataToHash = (const BYTE *)pContainer + DXBCHashStartOffset; UINT AmountToHash = pContainer->ContainerSizeInBytes - DXBCHashStartOffset; ComputeHashRetail(pDataToHash, AmountToHash, pContainer->Hash.Digest); } -static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, IDxcBlob **pSigned) { +static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, + IDxcBlob **pSigned) { if (Flags & DxcValidatorFlags_InPlaceEdit) { HashAndUpdate((DxilContainerHeader *)pShader->GetBufferPointer()); *pSigned = pShader; pShader->AddRef(); - } - else { + } else { // Possible gotcha: the blob allocated here is tied to this .dll, so the // DLL shouldn't be unloaded before the blob is released. CComPtr pSignedBlobStream; IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &pSignedBlobStream)); ULONG cb; - IFT(pSignedBlobStream->Write(pShader->GetBufferPointer(), pShader->GetBufferSize(), &cb)); + IFT(pSignedBlobStream->Write(pShader->GetBufferPointer(), + pShader->GetBufferSize(), &cb)); HashAndUpdate((DxilContainerHeader *)pSignedBlobStream->GetPtr()); IFT(pSignedBlobStream.QueryInterface(pSigned)); } } HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _In_opt_ DxcBuffer *pOptDebugBitcode, // Debug module bitcode to provide line numbers - _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors -) -{ + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _In_opt_ DxcBuffer + *pOptDebugBitcode, // Debug module bitcode to provide line numbers + _COM_Outptr_ IDxcOperationResult * + *ppResult // Validation output status, buffer, and errors +) { DxcThreadMalloc TM(m_pMalloc); - if (pShader == nullptr || ppResult == nullptr || Flags & ~DxcValidatorFlags_ValidMask) + if (pShader == nullptr || ppResult == nullptr || + Flags & ~DxcValidatorFlags_ValidMask) return E_INVALIDARG; if (Flags & DxcValidatorFlags_ModuleOnly) return E_INVALIDARG; - if (pOptDebugBitcode && (pOptDebugBitcode->Ptr == nullptr || pOptDebugBitcode->Size == 0 || - pOptDebugBitcode->Size >= UINT32_MAX)) + if (pOptDebugBitcode && + (pOptDebugBitcode->Ptr == nullptr || pOptDebugBitcode->Size == 0 || + pOptDebugBitcode->Size >= UINT32_MAX)) return E_INVALIDARG; *ppResult = nullptr; @@ -152,10 +162,11 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). if (Flags & DxcValidatorFlags_RootSignatureOnly) { - validationStatus = RunRootSignatureValidation(pShader, pDiagStream, Flags, &pSignedBlob); - } - else { - validationStatus = RunValidation(pShader, pDiagStream, Flags, pOptDebugBitcode, &pSignedBlob); + validationStatus = + RunRootSignatureValidation(pShader, pDiagStream, Flags, &pSignedBlob); + } else { + validationStatus = RunValidation(pShader, pDiagStream, Flags, + pOptDebugBitcode, &pSignedBlob); } if (FAILED(validationStatus)) { std::string msg("Validation failed.\n"); @@ -168,10 +179,11 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( hr = pDiagStream.QueryInterface(&pDiagBlob); DXASSERT_NOMSG(SUCCEEDED(hr)); IFT(DxcCreateBlobWithEncodingSet(pDiagBlob, CP_UTF8, &pDiagBlobEnconding)); - IFT(DxcResult::Create(validationStatus, DXC_OUT_OBJECT, { - DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pSignedBlob), - DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding) - }, ppResult)); + IFT(DxcResult::Create( + validationStatus, DXC_OUT_OBJECT, + {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pSignedBlob), + DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding)}, + ppResult)); } CATCH_CPP_ASSIGN_HRESULT(); @@ -180,9 +192,10 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( } HRESULT STDMETHODCALLTYPE DxcValidator::Validate( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _COM_Outptr_ IDxcOperationResult **ppResult // Validation output status, buffer, and errors + _In_ IDxcBlob *pShader, // Shader to validate. + _In_ UINT32 Flags, // Validation flags. + _COM_Outptr_ IDxcOperationResult * + *ppResult // Validation output status, buffer, and errors ) { return ValidateWithDebug(pShader, Flags, nullptr, ppResult); } @@ -195,18 +208,19 @@ HRESULT DxcValidator::RunValidation(_In_ IDxcBlob *pShader, // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). That is indicated - // by a failing HRESULT, and possibly error messages in the diagnostics stream. + // by a failing HRESULT, and possibly error messages in the diagnostics + // stream. *pSigned = nullptr; raw_stream_ostream DiagStream(pDiagStream); - if (IsDxilContainerLike(pShader->GetBufferPointer(), pShader->GetBufferSize())) { - IFR(ValidateDxilContainer(pShader->GetBufferPointer(), pShader->GetBufferSize(), - pDebugBitcode ? pDebugBitcode->Ptr : nullptr, - pDebugBitcode ? (uint32_t)pDebugBitcode->Size : 0, - DiagStream)); - } - else { + if (IsDxilContainerLike(pShader->GetBufferPointer(), + pShader->GetBufferSize())) { + IFR(ValidateDxilContainer( + pShader->GetBufferPointer(), pShader->GetBufferSize(), + pDebugBitcode ? pDebugBitcode->Ptr : nullptr, + pDebugBitcode ? (uint32_t)pDebugBitcode->Size : 0, DiagStream)); + } else { IFR(DXC_E_CONTAINER_INVALID); } @@ -216,20 +230,21 @@ HRESULT DxcValidator::RunValidation(_In_ IDxcBlob *pShader, } HRESULT DxcValidator::RunRootSignatureValidation( - _In_ IDxcBlob *pShader, - _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, - _COM_Outptr_ IDxcBlob **pSigned) { + _In_ IDxcBlob *pShader, _In_ AbstractMemoryStream *pDiagStream, + _In_ UINT32 Flags, _COM_Outptr_ IDxcBlob **pSigned) { const DxilContainerHeader *pDxilContainer = IsDxilContainerLike( - pShader->GetBufferPointer(), pShader->GetBufferSize()); + pShader->GetBufferPointer(), pShader->GetBufferSize()); if (!pDxilContainer) { return DXC_E_IR_VERIFICATION_FAILED; } - const DxilProgramHeader *pProgramHeader = GetDxilProgramHeader(pDxilContainer, DFCC_DXIL); - const DxilPartHeader *pPSVPart = GetDxilPartByType(pDxilContainer, DFCC_PipelineStateValidation); - const DxilPartHeader *pRSPart = GetDxilPartByType(pDxilContainer, DFCC_RootSignature); + const DxilProgramHeader *pProgramHeader = + GetDxilProgramHeader(pDxilContainer, DFCC_DXIL); + const DxilPartHeader *pPSVPart = + GetDxilPartByType(pDxilContainer, DFCC_PipelineStateValidation); + const DxilPartHeader *pRSPart = + GetDxilPartByType(pDxilContainer, DFCC_RootSignature); IFRBOOL(pRSPart, DXC_E_MISSING_PART); if (pProgramHeader) { // Container has shader part, make sure we have PSV. @@ -237,34 +252,34 @@ HRESULT DxcValidator::RunRootSignatureValidation( } try { RootSignatureHandle RSH; - RSH.LoadSerialized((const uint8_t*)GetDxilPartData(pRSPart), pRSPart->PartSize); + RSH.LoadSerialized((const uint8_t *)GetDxilPartData(pRSPart), + pRSPart->PartSize); RSH.Deserialize(); raw_stream_ostream DiagStream(pDiagStream); if (pProgramHeader) { - IFRBOOL(VerifyRootSignatureWithShaderPSV(RSH.GetDesc(), - GetVersionShaderType(pProgramHeader->ProgramVersion), - GetDxilPartData(pPSVPart), - pPSVPart->PartSize, - DiagStream), + IFRBOOL(VerifyRootSignatureWithShaderPSV( + RSH.GetDesc(), + GetVersionShaderType(pProgramHeader->ProgramVersion), + GetDxilPartData(pPSVPart), pPSVPart->PartSize, DiagStream), DXC_E_INCORRECT_ROOT_SIGNATURE); - // Do not sign here; shaders must go through full shader validation for signing. + // Do not sign here; shaders must go through full shader validation for + // signing. } else { IFRBOOL(VerifyRootSignature(RSH.GetDesc(), DiagStream, false), DXC_E_INCORRECT_ROOT_SIGNATURE); HashAndUpdateOrCopy(Flags, pShader, pSigned); } - } - catch (...) { + } catch (...) { return DXC_E_IR_VERIFICATION_FAILED; } return S_OK; } - -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv) { +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv) { try { - CComPtr result(DxcValidator::Alloc(DxcGetThreadMallocNoRef())); + CComPtr result( + DxcValidator::Alloc(DxcGetThreadMallocNoRef())); IFROOM(result.p); return result.p->QueryInterface(riid, ppv); } diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index 3ab3467dca..7d616be513 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -30,39 +30,37 @@ #include "dxc/dxcisense.h" #include "dxc/dxctools.h" -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID* ppv); +HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv); HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv); -// C++ exception specification ignored except to indicate a function is not __declspec(nothrow) +// C++ exception specification ignored except to indicate a function is not +// __declspec(nothrow) static HRESULT InitMaybeFail() throw() { - HRESULT hr; - bool memSetup = false; - IFC(DxcInitThreadMalloc()); - DxcSetThreadMallocToDefault(); - memSetup = true; - if (::llvm::sys::fs::SetupPerThreadFileSystem()) { - hr = E_FAIL; - goto Cleanup; - } + HRESULT hr; + bool memSetup = false; + IFC(DxcInitThreadMalloc()); + DxcSetThreadMallocToDefault(); + memSetup = true; + if (::llvm::sys::fs::SetupPerThreadFileSystem()) { + hr = E_FAIL; + goto Cleanup; + } Cleanup: - if (FAILED(hr)) { - if (memSetup) { - DxcClearThreadMalloc(); - DxcCleanupThreadMalloc(); - } + if (FAILED(hr)) { + if (memSetup) { + DxcClearThreadMalloc(); + DxcCleanupThreadMalloc(); } - else { - DxcClearThreadMalloc(); - } - return hr; + } else { + DxcClearThreadMalloc(); + } + return hr; } #if defined(LLVM_ON_UNIX) -HRESULT __attribute__ ((constructor)) DllMain() { - return InitMaybeFail(); -} +HRESULT __attribute__((constructor)) DllMain() { return InitMaybeFail(); } -void __attribute__ ((destructor)) DllShutdown() { +void __attribute__((destructor)) DllShutdown() { DxcSetThreadMallocToDefault(); ::llvm::sys::fs::CleanupPerThreadFileSystem(); ::llvm::llvm_shutdown(); @@ -71,92 +69,83 @@ void __attribute__ ((destructor)) DllShutdown() { } #else -#pragma warning( disable : 4290 ) -BOOL WINAPI -DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) -{ - if (Reason == DLL_PROCESS_ATTACH) - { - EventRegisterMicrosoft_Windows_DxcRuntime_API(); - DxcRuntimeEtw_DxcRuntimeInitialization_Start(); - HRESULT hr = InitMaybeFail(); - DxcRuntimeEtw_DxcRuntimeInitialization_Stop(hr); - if (FAILED(hr)) { - EventUnregisterMicrosoft_Windows_DxcRuntime_API(); - return hr; - } - } - else if (Reason == DLL_PROCESS_DETACH) - { - DxcRuntimeEtw_DxcRuntimeShutdown_Start(); - DxcSetThreadMallocToDefault(); - ::llvm::sys::fs::CleanupPerThreadFileSystem(); - ::llvm::llvm_shutdown(); - DxcClearThreadMalloc(); - DxcCleanupThreadMalloc(); - DxcRuntimeEtw_DxcRuntimeShutdown_Stop(S_OK); - EventUnregisterMicrosoft_Windows_DxcRuntime_API(); +#pragma warning(disable : 4290) +BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) { + if (Reason == DLL_PROCESS_ATTACH) { + EventRegisterMicrosoft_Windows_DxcRuntime_API(); + DxcRuntimeEtw_DxcRuntimeInitialization_Start(); + HRESULT hr = InitMaybeFail(); + DxcRuntimeEtw_DxcRuntimeInitialization_Stop(hr); + if (FAILED(hr)) { + EventUnregisterMicrosoft_Windows_DxcRuntime_API(); + return hr; } + } else if (Reason == DLL_PROCESS_DETACH) { + DxcRuntimeEtw_DxcRuntimeShutdown_Start(); + DxcSetThreadMallocToDefault(); + ::llvm::sys::fs::CleanupPerThreadFileSystem(); + ::llvm::llvm_shutdown(); + DxcClearThreadMalloc(); + DxcCleanupThreadMalloc(); + DxcRuntimeEtw_DxcRuntimeShutdown_Stop(S_OK); + EventUnregisterMicrosoft_Windows_DxcRuntime_API(); + } - return TRUE; + return TRUE; } void *__CRTDECL operator new(std::size_t size) noexcept(false) { - void *ptr = DxcNew(size); - if (ptr == nullptr) - throw std::bad_alloc(); - return ptr; -} -void * __CRTDECL operator new(std::size_t size, - const std::nothrow_t ¬hrow_value) throw() { - return DxcNew(size); + void *ptr = DxcNew(size); + if (ptr == nullptr) + throw std::bad_alloc(); + return ptr; } -void __CRTDECL operator delete (void* ptr) throw() { - DxcDelete(ptr); +void *__CRTDECL operator new(std::size_t size, + const std::nothrow_t ¬hrow_value) throw() { + return DxcNew(size); } -void __CRTDECL operator delete (void* ptr, const std::nothrow_t& nothrow_constant) throw() { - DxcDelete(ptr); +void __CRTDECL operator delete(void *ptr) throw() { DxcDelete(ptr); } +void __CRTDECL operator delete(void *ptr, + const std::nothrow_t ¬hrow_constant) throw() { + DxcDelete(ptr); } #endif -static HRESULT ThreadMallocDxcCreateInstance( - _In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { - *ppv = nullptr; - if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) { - return CreateDxcValidator(riid, ppv); - } - if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) { - return CreateDxcSigningContainerBuilder(riid, ppv); - } - return REGDB_E_CLASSNOTREG; +static HRESULT ThreadMallocDxcCreateInstance(_In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + *ppv = nullptr; + if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) { + return CreateDxcValidator(riid, ppv); + } + if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) { + return CreateDxcSigningContainerBuilder(riid, ppv); + } + return REGDB_E_CLASSNOTREG; } -DXC_API_IMPORT HRESULT __stdcall -DxcCreateInstance(_In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { - HRESULT hr = S_OK; - DxcEtw_DXCompilerCreateInstance_Start(); - DxcThreadMalloc TM(nullptr); - hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); - DxcEtw_DXCompilerCreateInstance_Stop(hr); - return hr; +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(_In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + HRESULT hr = S_OK; + DxcEtw_DXCompilerCreateInstance_Start(); + DxcThreadMalloc TM(nullptr); + hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); + DxcEtw_DXCompilerCreateInstance_Stop(hr); + return hr; } -DXC_API_IMPORT HRESULT __stdcall -DxcCreateInstance2(_In_ IMalloc *pMalloc, - _In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { - if (ppv == nullptr) { - return E_POINTER; - } - HRESULT hr = S_OK; - DxcEtw_DXCompilerCreateInstance_Start(); - DxcThreadMalloc TM(pMalloc); - hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); - DxcEtw_DXCompilerCreateInstance_Stop(hr); - return hr; +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(_In_ IMalloc *pMalloc, + _In_ REFCLSID rclsid, + _In_ REFIID riid, + _Out_ LPVOID *ppv) { + if (ppv == nullptr) { + return E_POINTER; + } + HRESULT hr = S_OK; + DxcEtw_DXCompilerCreateInstance_Start(); + DxcThreadMalloc TM(pMalloc); + hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); + DxcEtw_DXCompilerCreateInstance_Stop(hr); + return hr; } From 5f4fc679dd5714aba432b58fbb9f2c592f9d7dc0 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 11 Jul 2024 11:40:18 -0700 Subject: [PATCH 07/19] Remove unused header. --- tools/clang/tools/dxildll/dxildll.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index 7d616be513..ee43a9264f 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -12,7 +12,6 @@ #include "dxc/Support/Global.h" #include "dxc/Support/WinIncludes.h" -#include "llvm/Support/Debug.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/ManagedStatic.h" #include From c9dcca3815c4315af2aee74b5511f15b8f67a43f Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 12 Jul 2024 14:11:37 -0700 Subject: [PATCH 08/19] Remove SAL annotations. --- tools/clang/tools/dxildll/CMakeLists.txt | 2 +- ...der.cpp => DxcHashingContainerBuilder.cpp} | 18 ++--- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 75 +++++++++---------- tools/clang/tools/dxildll/dxildll.cpp | 21 +++--- 4 files changed, 53 insertions(+), 63 deletions(-) rename tools/clang/tools/dxildll/{DxcSigningContainerBuilder.cpp => DxcHashingContainerBuilder.cpp} (89%) diff --git a/tools/clang/tools/dxildll/CMakeLists.txt b/tools/clang/tools/dxildll/CMakeLists.txt index 1f81ab62be..f0df0921d4 100644 --- a/tools/clang/tools/dxildll/CMakeLists.txt +++ b/tools/clang/tools/dxildll/CMakeLists.txt @@ -38,7 +38,7 @@ include_directories( set(sources dxildll.cpp dxildll.def - DxcSigningContainerBuilder.cpp + DxcHashingContainerBuilder.cpp dxcvalidatorp.cpp ) diff --git a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp similarity index 89% rename from tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp rename to tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp index 7574db26e8..2af79c7fea 100644 --- a/tools/clang/tools/dxildll/DxcSigningContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp @@ -1,6 +1,6 @@ /////////////////////////////////////////////////////////////////////////////// // // -// DxcSigningContainerBuilder.cpp // +// DxcHashingContainerBuilder.cpp // // Copyright (C) Microsoft Corporation. All rights reserved. // // This file is distributed under the University of Illinois Open Source // // License. See LICENSE.TXT for details. // @@ -25,7 +25,7 @@ using namespace hlsl; -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv); +HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); class DxcSigningContainerBuilder : public DxcContainerBuilder { public: @@ -33,10 +33,9 @@ class DxcSigningContainerBuilder : public DxcContainerBuilder { DXC_MICROCOM_TM_ALLOC(DxcSigningContainerBuilder); - HRESULT STDMETHODCALLTYPE Load(_In_ IDxcBlob *pDxilContainerHeader) + HRESULT STDMETHODCALLTYPE Load(IDxcBlob *pDxilContainerHeader) override; // Loads DxilContainer to the builder - HRESULT STDMETHODCALLTYPE - SerializeContainer(_Out_ IDxcOperationResult **ppResult) + HRESULT STDMETHODCALLTYPE SerializeContainer(IDxcOperationResult **ppResult) override; // Builds a container of the given container builder state private: @@ -48,8 +47,7 @@ class DxcSigningContainerBuilder : public DxcContainerBuilder { void HashAndUpdate(DxilContainerHeader *pContainerHeader); }; -HRESULT STDMETHODCALLTYPE -DxcSigningContainerBuilder::Load(_In_ IDxcBlob *pSource) { +HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::Load(IDxcBlob *pSource) { HRESULT hr = DxcContainerBuilder::Load(pSource); if (SUCCEEDED(hr)) { const DxilContainerHeader *pHeader = @@ -59,8 +57,8 @@ DxcSigningContainerBuilder::Load(_In_ IDxcBlob *pSource) { return hr; } -HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::SerializeContainer( - _Out_ IDxcOperationResult **ppResult) { +HRESULT STDMETHODCALLTYPE +DxcSigningContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) { HRESULT hr_saved = DxcContainerBuilder::SerializeContainer(ppResult); if (!SUCCEEDED(hr_saved)) { return hr_saved; @@ -121,7 +119,7 @@ void DxcSigningContainerBuilder::HashAndUpdate( } } -HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv) { +HRESULT CreateDxcSigningContainerBuilder(REFIID riid, LPVOID *ppv) { // Call dxil.dll's containerbuilder *ppv = nullptr; CComPtr Result( diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index f93f03852b..fee001c9e1 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -5,7 +5,7 @@ // This file is distributed under the University of Illinois Open Source // // License. See LICENSE.TXT for details. // // // -// Implements the DirectX Validator object, including signing support. // +// Implements the DirectX Validator object, including hashing support. // // // /////////////////////////////////////////////////////////////////////////////// @@ -37,16 +37,14 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { private: DXC_MICROCOM_TM_REF_FIELDS() - HRESULT RunValidation(_In_ IDxcBlob *pShader, - _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, _In_opt_ DxcBuffer *pOptDebugBitcode, - _COM_Outptr_ IDxcBlob **pSigned); + HRESULT RunValidation(IDxcBlob *pShader, AbstractMemoryStream *pDiagStream, + UINT32 Flags, DxcBuffer *pOptDebugBitcode, + IDxcBlob **pSigned); HRESULT - RunRootSignatureValidation(_In_ IDxcBlob *pShader, // Shader to validate. - _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, - _COM_Outptr_ IDxcBlob **pSigned); + RunRootSignatureValidation(IDxcBlob *pShader, // Shader to validate. + AbstractMemoryStream *pDiagStream, UINT32 Flags, + IDxcBlob **pSigned); public: DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() @@ -60,30 +58,28 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { } HRESULT STDMETHODCALLTYPE Validate( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _COM_Outptr_ IDxcOperationResult * + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. + IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) override; // IDxcValidator2 HRESULT STDMETHODCALLTYPE ValidateWithDebug( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _In_ DxcBuffer - *pDebugModule, // Debug module bitcode to provide line numbers - _COM_Outptr_ IDxcOperationResult * + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. + DxcBuffer *pDebugModule, // Debug module bitcode to provide line numbers + IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) override; // IDxcVersionInfo - HRESULT STDMETHODCALLTYPE GetVersion(_Out_ UINT32 *pMajor, - _Out_ UINT32 *pMinor) override; - HRESULT STDMETHODCALLTYPE GetFlags(_Out_ UINT32 *pFlags) override; + HRESULT STDMETHODCALLTYPE GetVersion(UINT32 *pMajor, UINT32 *pMinor) override; + HRESULT STDMETHODCALLTYPE GetFlags(UINT32 *pFlags) override; }; -HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(_Out_ UINT32 *pMajor, - _Out_ UINT32 *pMinor) { +HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(UINT32 *pMajor, + UINT32 *pMinor) { DxcThreadMalloc TM(m_pMalloc); if (pMajor == nullptr || pMinor == nullptr) return E_INVALIDARG; @@ -91,7 +87,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(_Out_ UINT32 *pMajor, return S_OK; } -HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(_Out_ UINT32 *pFlags) { +HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(UINT32 *pFlags) { DxcThreadMalloc TM(m_pMalloc); if (pFlags == nullptr) return E_INVALIDARG; @@ -132,11 +128,10 @@ static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, } HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _In_opt_ DxcBuffer - *pOptDebugBitcode, // Debug module bitcode to provide line numbers - _COM_Outptr_ IDxcOperationResult * + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. + DxcBuffer *pOptDebugBitcode, // Debug module bitcode to provide line numbers + IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { DxcThreadMalloc TM(m_pMalloc); @@ -192,19 +187,18 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( } HRESULT STDMETHODCALLTYPE DxcValidator::Validate( - _In_ IDxcBlob *pShader, // Shader to validate. - _In_ UINT32 Flags, // Validation flags. - _COM_Outptr_ IDxcOperationResult * + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. + IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { return ValidateWithDebug(pShader, Flags, nullptr, ppResult); } -HRESULT DxcValidator::RunValidation(_In_ IDxcBlob *pShader, - _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, - _In_ DxcBuffer *pDebugBitcode, - _COM_Outptr_ IDxcBlob **pSigned) { +HRESULT DxcValidator::RunValidation(IDxcBlob *pShader, + AbstractMemoryStream *pDiagStream, + UINT32 Flags, DxcBuffer *pDebugBitcode, + IDxcBlob **pSigned) { // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). That is indicated @@ -229,9 +223,10 @@ HRESULT DxcValidator::RunValidation(_In_ IDxcBlob *pShader, return S_OK; } -HRESULT DxcValidator::RunRootSignatureValidation( - _In_ IDxcBlob *pShader, _In_ AbstractMemoryStream *pDiagStream, - _In_ UINT32 Flags, _COM_Outptr_ IDxcBlob **pSigned) { +HRESULT +DxcValidator::RunRootSignatureValidation(IDxcBlob *pShader, + AbstractMemoryStream *pDiagStream, + UINT32 Flags, IDxcBlob **pSigned) { const DxilContainerHeader *pDxilContainer = IsDxilContainerLike( pShader->GetBufferPointer(), pShader->GetBufferSize()); @@ -276,7 +271,7 @@ HRESULT DxcValidator::RunRootSignatureValidation( return S_OK; } -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv) { +HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv) { try { CComPtr result( DxcValidator::Alloc(DxcGetThreadMallocNoRef())); diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index ee43a9264f..08864ec025 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -29,8 +29,8 @@ #include "dxc/dxcisense.h" #include "dxc/dxctools.h" -HRESULT CreateDxcValidator(_In_ REFIID riid, _Out_ LPVOID *ppv); -HRESULT CreateDxcSigningContainerBuilder(_In_ REFIID riid, _Out_ LPVOID *ppv); +HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); +HRESULT CreateDxcSigningContainerBuilder(REFIID riid, LPVOID *ppv); // C++ exception specification ignored except to indicate a function is not // __declspec(nothrow) @@ -110,9 +110,8 @@ void __CRTDECL operator delete(void *ptr, } #endif -static HRESULT ThreadMallocDxcCreateInstance(_In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { +static HRESULT ThreadMallocDxcCreateInstance(REFCLSID rclsid, REFIID riid, + LPVOID *ppv) { *ppv = nullptr; if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) { return CreateDxcValidator(riid, ppv); @@ -123,9 +122,8 @@ static HRESULT ThreadMallocDxcCreateInstance(_In_ REFCLSID rclsid, return REGDB_E_CLASSNOTREG; } -DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(_In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(REFCLSID rclsid, REFIID riid, + LPVOID *ppv) { HRESULT hr = S_OK; DxcEtw_DXCompilerCreateInstance_Start(); DxcThreadMalloc TM(nullptr); @@ -134,10 +132,9 @@ DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(_In_ REFCLSID rclsid, return hr; } -DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(_In_ IMalloc *pMalloc, - _In_ REFCLSID rclsid, - _In_ REFIID riid, - _Out_ LPVOID *ppv) { +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(IMalloc *pMalloc, + REFCLSID rclsid, + REFIID riid, LPVOID *ppv) { if (ppv == nullptr) { return E_POINTER; } From f5552bd53489bd5d5fa7e86d172512dc2ac2073a Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Fri, 12 Jul 2024 19:14:58 -0700 Subject: [PATCH 09/19] Rename signing to hashing. --- .../dxildll/DxcHashingContainerBuilder.cpp | 20 +++++++++---------- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 2 +- tools/clang/tools/dxildll/dxildll.cpp | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp index 2af79c7fea..1d05bc2f85 100644 --- a/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp @@ -27,11 +27,11 @@ using namespace hlsl; HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); -class DxcSigningContainerBuilder : public DxcContainerBuilder { +class DxcHashingContainerBuilder : public DxcContainerBuilder { public: - DxcSigningContainerBuilder(IMalloc *pMalloc) : DxcContainerBuilder(pMalloc) {} + DxcHashingContainerBuilder(IMalloc *pMalloc) : DxcContainerBuilder(pMalloc) {} - DXC_MICROCOM_TM_ALLOC(DxcSigningContainerBuilder); + DXC_MICROCOM_TM_ALLOC(DxcHashingContainerBuilder); HRESULT STDMETHODCALLTYPE Load(IDxcBlob *pDxilContainerHeader) override; // Loads DxilContainer to the builder @@ -47,7 +47,7 @@ class DxcSigningContainerBuilder : public DxcContainerBuilder { void HashAndUpdate(DxilContainerHeader *pContainerHeader); }; -HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::Load(IDxcBlob *pSource) { +HRESULT STDMETHODCALLTYPE DxcHashingContainerBuilder::Load(IDxcBlob *pSource) { HRESULT hr = DxcContainerBuilder::Load(pSource); if (SUCCEEDED(hr)) { const DxilContainerHeader *pHeader = @@ -58,7 +58,7 @@ HRESULT STDMETHODCALLTYPE DxcSigningContainerBuilder::Load(IDxcBlob *pSource) { } HRESULT STDMETHODCALLTYPE -DxcSigningContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) { +DxcHashingContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) { HRESULT hr_saved = DxcContainerBuilder::SerializeContainer(ppResult); if (!SUCCEEDED(hr_saved)) { return hr_saved; @@ -80,7 +80,7 @@ DxcSigningContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) { return hr_saved; } -void DxcSigningContainerBuilder::FindHashFunctionFromSource( +void DxcHashingContainerBuilder::FindHashFunctionFromSource( const DxilContainerHeader *pContainerHeader) { DXASSERT(pContainerHeader != nullptr && IsDxilContainerLike(pContainerHeader, @@ -105,7 +105,7 @@ void DxcSigningContainerBuilder::FindHashFunctionFromSource( } // For Internal hash function. -void DxcSigningContainerBuilder::HashAndUpdate( +void DxcHashingContainerBuilder::HashAndUpdate( DxilContainerHeader *pContainerHeader) { if (m_pHashFunction != nullptr) { DXASSERT(pContainerHeader != nullptr, @@ -119,11 +119,11 @@ void DxcSigningContainerBuilder::HashAndUpdate( } } -HRESULT CreateDxcSigningContainerBuilder(REFIID riid, LPVOID *ppv) { +HRESULT CreateDxcHashingContainerBuilder(REFIID riid, LPVOID *ppv) { // Call dxil.dll's containerbuilder *ppv = nullptr; - CComPtr Result( - DxcSigningContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); + CComPtr Result( + DxcHashingContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); IFROOM(Result.p); Result->Init(); return Result->QueryInterface(riid, ppv); diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index fee001c9e1..587a519bc0 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -258,7 +258,7 @@ DxcValidator::RunRootSignatureValidation(IDxcBlob *pShader, GetDxilPartData(pPSVPart), pPSVPart->PartSize, DiagStream), DXC_E_INCORRECT_ROOT_SIGNATURE); // Do not sign here; shaders must go through full shader validation for - // signing. + // hashing. } else { IFRBOOL(VerifyRootSignature(RSH.GetDesc(), DiagStream, false), DXC_E_INCORRECT_ROOT_SIGNATURE); diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index 08864ec025..15e7e9cca8 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -30,7 +30,7 @@ #include "dxc/dxctools.h" HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); -HRESULT CreateDxcSigningContainerBuilder(REFIID riid, LPVOID *ppv); +HRESULT CreateDxcHashingContainerBuilder(REFIID riid, LPVOID *ppv); // C++ exception specification ignored except to indicate a function is not // __declspec(nothrow) @@ -117,7 +117,7 @@ static HRESULT ThreadMallocDxcCreateInstance(REFCLSID rclsid, REFIID riid, return CreateDxcValidator(riid, ppv); } if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) { - return CreateDxcSigningContainerBuilder(riid, ppv); + return CreateDxcHashingContainerBuilder(riid, ppv); } return REGDB_E_CLASSNOTREG; } From da3a5bfaa1b0ff4a0630efcf7399b91c8484d7e4 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 16 Jul 2024 13:43:12 -0700 Subject: [PATCH 10/19] Rename Signed to Hashed. --- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 38 ++++++++++----------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index 587a519bc0..437d6b3820 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -39,12 +39,12 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { HRESULT RunValidation(IDxcBlob *pShader, AbstractMemoryStream *pDiagStream, UINT32 Flags, DxcBuffer *pOptDebugBitcode, - IDxcBlob **pSigned); + IDxcBlob **Hashed); HRESULT RunRootSignatureValidation(IDxcBlob *pShader, // Shader to validate. AbstractMemoryStream *pDiagStream, UINT32 Flags, - IDxcBlob **pSigned); + IDxcBlob **Hashed); public: DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() @@ -109,21 +109,21 @@ static void HashAndUpdate(DxilContainerHeader *pContainer) { } static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, - IDxcBlob **pSigned) { + IDxcBlob **Hashed) { if (Flags & DxcValidatorFlags_InPlaceEdit) { HashAndUpdate((DxilContainerHeader *)pShader->GetBufferPointer()); - *pSigned = pShader; + *Hashed = pShader; pShader->AddRef(); } else { // Possible gotcha: the blob allocated here is tied to this .dll, so the // DLL shouldn't be unloaded before the blob is released. - CComPtr pSignedBlobStream; - IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &pSignedBlobStream)); + CComPtr HashedBlobStream; + IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream)); ULONG cb; - IFT(pSignedBlobStream->Write(pShader->GetBufferPointer(), + IFT(HashedBlobStream->Write(pShader->GetBufferPointer(), pShader->GetBufferSize(), &cb)); - HashAndUpdate((DxilContainerHeader *)pSignedBlobStream->GetPtr()); - IFT(pSignedBlobStream.QueryInterface(pSigned)); + HashAndUpdate((DxilContainerHeader *)HashedBlobStream->GetPtr()); + IFT(HashedBlobStream.QueryInterface(Hashed)); } } @@ -151,17 +151,17 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( DxcEtw_DxcValidation_Start(); try { CComPtr pDiagStream; - CComPtr pSignedBlob; + CComPtr HashedBlob; IFT(CreateMemoryStream(m_pMalloc, &pDiagStream)); // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). if (Flags & DxcValidatorFlags_RootSignatureOnly) { validationStatus = - RunRootSignatureValidation(pShader, pDiagStream, Flags, &pSignedBlob); + RunRootSignatureValidation(pShader, pDiagStream, Flags, &HashedBlob); } else { validationStatus = RunValidation(pShader, pDiagStream, Flags, - pOptDebugBitcode, &pSignedBlob); + pOptDebugBitcode, &HashedBlob); } if (FAILED(validationStatus)) { std::string msg("Validation failed.\n"); @@ -176,7 +176,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( IFT(DxcCreateBlobWithEncodingSet(pDiagBlob, CP_UTF8, &pDiagBlobEnconding)); IFT(DxcResult::Create( validationStatus, DXC_OUT_OBJECT, - {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, pSignedBlob), + {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, HashedBlob), DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding)}, ppResult)); } @@ -198,14 +198,14 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( HRESULT DxcValidator::RunValidation(IDxcBlob *pShader, AbstractMemoryStream *pDiagStream, UINT32 Flags, DxcBuffer *pDebugBitcode, - IDxcBlob **pSigned) { + IDxcBlob **Hashed) { // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). That is indicated // by a failing HRESULT, and possibly error messages in the diagnostics // stream. - *pSigned = nullptr; + *Hashed = nullptr; raw_stream_ostream DiagStream(pDiagStream); if (IsDxilContainerLike(pShader->GetBufferPointer(), @@ -218,7 +218,7 @@ HRESULT DxcValidator::RunValidation(IDxcBlob *pShader, IFR(DXC_E_CONTAINER_INVALID); } - HashAndUpdateOrCopy(Flags, pShader, pSigned); + HashAndUpdateOrCopy(Flags, pShader, Hashed); return S_OK; } @@ -226,7 +226,7 @@ HRESULT DxcValidator::RunValidation(IDxcBlob *pShader, HRESULT DxcValidator::RunRootSignatureValidation(IDxcBlob *pShader, AbstractMemoryStream *pDiagStream, - UINT32 Flags, IDxcBlob **pSigned) { + UINT32 Flags, IDxcBlob **Hashed) { const DxilContainerHeader *pDxilContainer = IsDxilContainerLike( pShader->GetBufferPointer(), pShader->GetBufferSize()); @@ -257,12 +257,12 @@ DxcValidator::RunRootSignatureValidation(IDxcBlob *pShader, GetVersionShaderType(pProgramHeader->ProgramVersion), GetDxilPartData(pPSVPart), pPSVPart->PartSize, DiagStream), DXC_E_INCORRECT_ROOT_SIGNATURE); - // Do not sign here; shaders must go through full shader validation for + // Do not hash here; shaders must go through full shader validation for // hashing. } else { IFRBOOL(VerifyRootSignature(RSH.GetDesc(), DiagStream, false), DXC_E_INCORRECT_ROOT_SIGNATURE); - HashAndUpdateOrCopy(Flags, pShader, pSigned); + HashAndUpdateOrCopy(Flags, pShader, Hashed); } } catch (...) { return DXC_E_IR_VERIFICATION_FAILED; From 72cfe5b82c5a676440545fe6230679316da6bc25 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Tue, 16 Jul 2024 13:46:15 -0700 Subject: [PATCH 11/19] Fix clang format. --- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index 437d6b3820..8e4c4b3248 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -121,7 +121,7 @@ static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream)); ULONG cb; IFT(HashedBlobStream->Write(pShader->GetBufferPointer(), - pShader->GetBufferSize(), &cb)); + pShader->GetBufferSize(), &cb)); HashAndUpdate((DxilContainerHeader *)HashedBlobStream->GetPtr()); IFT(HashedBlobStream.QueryInterface(Hashed)); } From 31c754caf943c7c282638b8b2dc384cbcbe81969 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 28 Jul 2024 07:29:01 -0700 Subject: [PATCH 12/19] Fix styles. --- tools/clang/tools/dxcvalidator/CMakeLists.txt | 10 +- .../clang/tools/dxcvalidator/dxcvalidator.cpp | 247 ++++++++++++------ tools/clang/tools/dxildll/CMakeLists.txt | 8 +- .../dxildll/DxcHashingContainerBuilder.cpp | 115 ++++---- tools/clang/tools/dxildll/dxcvalidatorp.cpp | 229 ++-------------- tools/clang/tools/dxildll/dxildll.cpp | 98 ++++--- 6 files changed, 314 insertions(+), 393 deletions(-) diff --git a/tools/clang/tools/dxcvalidator/CMakeLists.txt b/tools/clang/tools/dxcvalidator/CMakeLists.txt index ae58e13883..3b8a58d0cf 100644 --- a/tools/clang/tools/dxcvalidator/CMakeLists.txt +++ b/tools/clang/tools/dxcvalidator/CMakeLists.txt @@ -16,10 +16,18 @@ add_clang_library(dxcvalidator dxcvalidator.cpp ) +include_directories( + ${LLVM_SOURCE_DIR}/tools/clang/tools/DxilHash +) + + if (MINGW) target_link_options(dxcvalidator PUBLIC -mconsole -municode) - target_link_libraries(dxcvalidator PRIVATE version) + target_link_libraries(dxcvalidator PRIVATE version DxilHash) +else () + target_link_libraries(dxcvalidator PRIVATE DxilHash) endif() + target_compile_definitions(dxcvalidator PRIVATE VERSION_STRING_SUFFIX=" for ${CMAKE_SYSTEM_NAME}") diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index 93c69bea6f..eb79f6ddba 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -23,11 +23,12 @@ #include "dxc/Support/FileIOHelper.h" #include "dxc/Support/Global.h" #include "dxc/Support/dxcapi.impl.h" - #ifdef _WIN32 #include "dxcetw.h" #endif +#include "DxilHash.h" + using namespace llvm; using namespace hlsl; @@ -47,12 +48,50 @@ struct DiagRestore { ~DiagRestore() { Ctx.setDiagnosticHandler(OrigHandler, OrigDiagContext); } }; +static void HashAndUpdate(DxilContainerHeader *Container) { + // Compute hash and update stored hash. + // Hash the container from this offset to the end. + static const uint32_t DXBCHashStartOffset = + offsetof(struct DxilContainerHeader, Version); + const unsigned char *DataToHash = + (const unsigned char *)Container + DXBCHashStartOffset; + unsigned AmountToHash = Container->ContainerSizeInBytes - DXBCHashStartOffset; + ComputeHashRetail(DataToHash, AmountToHash, Container->Hash.Digest); +} + +static void HashAndUpdateOrCopy(uint32_t Flags, IDxcBlob *Shader, + IDxcBlob **Hashed) { + if (Flags & DxcValidatorFlags_InPlaceEdit) { + HashAndUpdate((DxilContainerHeader *)Shader->GetBufferPointer()); + *Hashed = Shader; + Shader->AddRef(); + } else { + // Possible gotcha: the blob allocated here is tied to this .dll, so the + // DLL shouldn't be unloaded before the blob is released. + CComPtr HashedBlobStream; + uint32_t HR = + CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream); + if (FAILED(HR)) + throw hlsl::Exception(HR); + + unsigned long CB; + HR = HashedBlobStream->Write(Shader->GetBufferPointer(), + Shader->GetBufferSize(), &CB); + if (FAILED(HR)) + throw hlsl::Exception(HR); + HashAndUpdate((DxilContainerHeader *)HashedBlobStream->GetPtr()); + HR = HashedBlobStream.QueryInterface(Hashed); + if (FAILED(HR)) + throw hlsl::Exception(HR); + } +} + static uint32_t runValidation( IDxcBlob *Shader, uint32_t Flags, // Validation flags. llvm::Module *Module, // Module to validate, if available. llvm::Module *DebugModule, // Debug module to validate, if available - AbstractMemoryStream *DiagMemStream) { + AbstractMemoryStream *DiagMemStream, IDxcBlob **Hashed) { // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). That is indicated @@ -73,42 +112,75 @@ static uint32_t runValidation( if (!Module) { DXASSERT_NOMSG(DebugModule == nullptr); - if (Flags & DxcValidatorFlags_ModuleOnly) { - return ValidateDxilBitcode((const char *)Shader->GetBufferPointer(), - (uint32_t)Shader->GetBufferSize(), DiagStream); - } else { - return ValidateDxilContainer(Shader->GetBufferPointer(), - Shader->GetBufferSize(), DiagStream); - } + HRESULT HR = S_OK; + if (Flags & DxcValidatorFlags_ModuleOnly) + HR = ValidateDxilBitcode((const char *)Shader->GetBufferPointer(), + (uint32_t)Shader->GetBufferSize(), DiagStream); + else + HR = ValidateDxilContainer(Shader->GetBufferPointer(), + Shader->GetBufferSize(), DiagStream); + if (HR == S_OK) + HashAndUpdateOrCopy(Flags, Shader, Hashed); + return HR; } llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); PrintDiagnosticContext DiagContext(DiagPrinter); DiagRestore DR(Module->getContext(), &DiagContext); - HRESULT hr = hlsl::ValidateDxilModule(Module, DebugModule); - if (FAILED(hr)) - return hr; + HRESULT HR = hlsl::ValidateDxilModule(Module, DebugModule); + if (FAILED(HR)) + return HR; if (!(Flags & DxcValidatorFlags_ModuleOnly)) { - hr = ValidateDxilContainerParts( + HR = ValidateDxilContainerParts( Module, DebugModule, IsDxilContainerLike(Shader->GetBufferPointer(), Shader->GetBufferSize()), (uint32_t)Shader->GetBufferSize()); - if (FAILED(hr)) - return hr; + if (FAILED(HR)) + return HR; } - if (DiagContext.HasErrors() || DiagContext.HasWarnings()) { + if (DiagContext.HasErrors() || DiagContext.HasWarnings()) return DXC_E_IR_VERIFICATION_FAILED; - } + + HashAndUpdateOrCopy(Flags, Shader, Hashed); + + return S_OK; +} + +static uint32_t runValidation(IDxcBlob *Shader, + AbstractMemoryStream *DiagMemStream, + uint32_t Flags, DxcBuffer *DebugBitcode, + IDxcBlob **Hashed) { + + // Run validation may throw, but that indicates an inability to validate, + // not that the validation failed (eg out of memory). That is indicated + // by a failing HRESULT, and possibly error messages in the diagnostics + // stream. + + *Hashed = nullptr; + raw_stream_ostream DiagStream(DiagMemStream); + + if (IsDxilContainerLike(Shader->GetBufferPointer(), + Shader->GetBufferSize())) { + uint32_t HR = ValidateDxilContainer( + Shader->GetBufferPointer(), Shader->GetBufferSize(), + DebugBitcode ? DebugBitcode->Ptr : nullptr, + DebugBitcode ? (uint32_t)DebugBitcode->Size : 0, DiagStream); + if (DXC_FAILED(HR)) + return HR; + } else + return DXC_E_CONTAINER_INVALID; + + HashAndUpdateOrCopy(Flags, Shader, Hashed); return S_OK; } -static uint32_t -runRootSignatureValidation(IDxcBlob *Shader, - AbstractMemoryStream *DiagMemStream) { +static uint32_t runRootSignatureValidation(IDxcBlob *Shader, + AbstractMemoryStream *DiagMemStream, + uint32_t Flags, IDxcBlob **Hashed) { const DxilContainerHeader *DxilContainer = IsDxilContainerLike(Shader->GetBufferPointer(), Shader->GetBufferSize()); @@ -143,6 +215,7 @@ runRootSignatureValidation(IDxcBlob *Shader, } else { if (!VerifyRootSignature(RSH.GetDesc(), DiagStream, false)) return DXC_E_INCORRECT_ROOT_SIGNATURE; + HashAndUpdateOrCopy(Flags, Shader, Hashed); } } catch (...) { return DXC_E_IR_VERIFICATION_FAILED; @@ -157,17 +230,7 @@ uint32_t hlsl::validate( uint32_t Flags, // Validation flags. IDxcOperationResult **Result // Validation output status, buffer, and errors ) { - DxcThreadMalloc TM(DxcGetThreadMallocNoRef()); - if (Result == nullptr) - return false; - *Result = nullptr; - if (Shader == nullptr || Flags & ~DxcValidatorFlags_ValidMask) - return false; - if ((Flags & DxcValidatorFlags_ModuleOnly) && - (Flags & - (DxcValidatorFlags_InPlaceEdit | DxcValidatorFlags_RootSignatureOnly))) - return false; - return validateWithOptModules(Shader, Flags, nullptr, nullptr, Result); + return validateWithDebug(Shader, Flags, nullptr, Result); } uint32_t hlsl::validateWithDebug( @@ -191,32 +254,54 @@ uint32_t hlsl::validateWithDebug( OptDebugBitcode->Size >= UINT32_MAX)) return E_INVALIDARG; - HRESULT hr = S_OK; + HRESULT HR = S_OK; + HRESULT ValidationStatus = S_OK; DxcThreadMalloc TM(DxcGetThreadMallocNoRef()); try { - LLVMContext Ctx; CComPtr DiagMemStream; - hr = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagMemStream); - if (FAILED(hr)) - throw hlsl::Exception(hr); - raw_stream_ostream DiagStream(DiagMemStream); - llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); - PrintDiagnosticContext DiagContext(DiagPrinter); - Ctx.setDiagnosticHandler(PrintDiagnosticContext::PrintDiagnosticHandler, - &DiagContext, true); - std::unique_ptr DebugModule; - if (OptDebugBitcode) { - hr = ValidateLoadModule((const char *)OptDebugBitcode->Ptr, - (uint32_t)OptDebugBitcode->Size, DebugModule, Ctx, - DiagStream, /*bLazyLoad*/ false); - if (FAILED(hr)) - throw hlsl::Exception(hr); + CComPtr HashedBlob; + HR = + CreateMemoryStream(TM.GetInstalledAllocator(), &DiagMemStream); + if (FAILED(HR)) + throw hlsl::Exception(HR); + + // Run validation may throw, but that indicates an inability to validate, + // not that the validation failed (eg out of memory). + if (Flags & DxcValidatorFlags_RootSignatureOnly) + ValidationStatus = + runRootSignatureValidation(Shader, DiagMemStream, Flags, &HashedBlob); + else + ValidationStatus = runValidation(Shader, DiagMemStream, Flags, + OptDebugBitcode, &HashedBlob); + + if (FAILED(ValidationStatus)) { + std::string msg("Validation failed.\n"); + ULONG cbWritten; + DiagMemStream->Write(msg.c_str(), msg.size(), &cbWritten); } - return validateWithOptModules(Shader, Flags, nullptr, DebugModule.get(), - Result); + // Assemble the result object. + CComPtr DiagBlob; + CComPtr DiagBlobEnconding; + HR = DiagMemStream.QueryInterface(&DiagBlob); + DXASSERT_NOMSG(SUCCEEDED(HR)); + HR = DxcCreateBlobWithEncodingSet(DiagBlob, CP_UTF8, &DiagBlobEnconding); + if (FAILED(HR)) + throw hlsl::Exception(HR); + HR = DxcResult::Create( + ValidationStatus, DXC_OUT_OBJECT, + {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, HashedBlob), + DxcOutputObject::DataOutput(DXC_OUT_ERRORS, DiagBlobEnconding)}, + Result); + if (FAILED(HR)) + throw hlsl::Exception(HR); + } catch (std::bad_alloc &) { + HR = E_OUTOFMEMORY; + } catch (hlsl::Exception &_hlsl_exception_) { + HR = _hlsl_exception_.hr; + } catch (...) { + HR = E_FAIL; } - CATCH_CPP_ASSIGN_HRESULT(); - return hr; + return HR; } uint32_t hlsl::validateWithOptModules( @@ -227,45 +312,55 @@ uint32_t hlsl::validateWithOptModules( IDxcOperationResult **Result // Validation output status, buffer, and errors ) { *Result = nullptr; - HRESULT hr = S_OK; - HRESULT validationStatus = S_OK; + HRESULT HR = S_OK; + HRESULT ValidationStatus = S_OK; DxcEtw_DxcValidation_Start(); DxcThreadMalloc TM(DxcGetThreadMallocNoRef()); try { CComPtr DiagStream; - hr = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagStream); - if (FAILED(hr)) - throw hlsl::Exception(hr); + HR = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagStream); + if (FAILED(HR)) + throw hlsl::Exception(HR); + CComPtr HashedBlob; // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). - if (Flags & DxcValidatorFlags_RootSignatureOnly) { - validationStatus = runRootSignatureValidation(Shader, DiagStream); - } else { - validationStatus = - runValidation(Shader, Flags, Module, DebugModule, DiagStream); - } - if (FAILED(validationStatus)) { + if (Flags & DxcValidatorFlags_RootSignatureOnly) + ValidationStatus = + runRootSignatureValidation(Shader, DiagStream, Flags, &HashedBlob); + else + ValidationStatus = runValidation(Shader, Flags, Module, DebugModule, + DiagStream, &HashedBlob); + if (FAILED(ValidationStatus)) { std::string msg("Validation failed.\n"); ULONG cbWritten; DiagStream->Write(msg.c_str(), msg.size(), &cbWritten); } // Assemble the result object. CComPtr pDiagBlob; - hr = DiagStream.QueryInterface(&pDiagBlob); - DXASSERT_NOMSG(SUCCEEDED(hr)); - hr = DxcResult::Create( - validationStatus, DXC_OUT_NONE, - {DxcOutputObject::ErrorOutput( - CP_UTF8, // TODO Support DefaultTextCodePage - (LPCSTR)pDiagBlob->GetBufferPointer(), pDiagBlob->GetBufferSize())}, + HR = DiagStream.QueryInterface(&pDiagBlob); + DXASSERT_NOMSG(SUCCEEDED(HR)); + CComPtr pDiagBlobEnconding; + HR = DxcCreateBlobWithEncodingSet(pDiagBlob, CP_UTF8, &pDiagBlobEnconding); + if (FAILED(HR)) + throw hlsl::Exception(HR); + + HR = DxcResult::Create( + ValidationStatus, DXC_OUT_OBJECT, + {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, HashedBlob), + DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding)}, Result); - if (FAILED(hr)) - throw hlsl::Exception(hr); + if (FAILED(HR)) + throw hlsl::Exception(HR); + } catch (std::bad_alloc &) { + HR = E_OUTOFMEMORY; + } catch (hlsl::Exception &_hlsl_exception_) { + HR = _hlsl_exception_.hr; + } catch (...) { + HR = E_FAIL; } - CATCH_CPP_ASSIGN_HRESULT(); - DxcEtw_DxcValidation_Stop(SUCCEEDED(hr) ? validationStatus : hr); - return hr; + DxcEtw_DxcValidation_Stop(SUCCEEDED(HR) ? ValidationStatus : HR); + return HR; } uint32_t hlsl::getValidationVersion(unsigned *Major, unsigned *Minor) { diff --git a/tools/clang/tools/dxildll/CMakeLists.txt b/tools/clang/tools/dxildll/CMakeLists.txt index f0df0921d4..021b5977eb 100644 --- a/tools/clang/tools/dxildll/CMakeLists.txt +++ b/tools/clang/tools/dxildll/CMakeLists.txt @@ -27,12 +27,9 @@ endif() endforeach(C ${DXCComponents}) include_directories( - ${CMAKE_CURRENT_SOURCE_DIR} - ${WINTOOLS_SOURCE_ROOT}/include - ${DXC_PROJECTS_SOURCE_DIR}/include - ${DXC_PROJECTS_BINARY_DIR}/include - ${LLVM_BINARY_DIR}/projects/include + ${LLVM_SOURCE_DIR}/tools/clang/tools/dxcvalidator ${LLVM_SOURCE_DIR}/tools/clang/tools/DxilHash + ${LLVM_BINARY_DIR}/projects/include ) set(sources @@ -56,6 +53,7 @@ endif() target_link_libraries(dxildll PRIVATE ${DXCLibs} DxilHash + dxcvalidator ) if (WIN32) diff --git a/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp index 1d05bc2f85..a6392d27f2 100644 --- a/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp +++ b/tools/clang/tools/dxildll/DxcHashingContainerBuilder.cpp @@ -25,106 +25,105 @@ using namespace hlsl; -HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); +HRESULT CreateDxcValidator(REFIID RIID, LPVOID *V); class DxcHashingContainerBuilder : public DxcContainerBuilder { public: - DxcHashingContainerBuilder(IMalloc *pMalloc) : DxcContainerBuilder(pMalloc) {} + DxcHashingContainerBuilder(IMalloc *Malloc) : DxcContainerBuilder(Malloc) {} DXC_MICROCOM_TM_ALLOC(DxcHashingContainerBuilder); - HRESULT STDMETHODCALLTYPE Load(IDxcBlob *pDxilContainerHeader) + HRESULT STDMETHODCALLTYPE Load(IDxcBlob *DxilContainerHeader) override; // Loads DxilContainer to the builder - HRESULT STDMETHODCALLTYPE SerializeContainer(IDxcOperationResult **ppResult) + HRESULT STDMETHODCALLTYPE SerializeContainer(IDxcOperationResult **Result) override; // Builds a container of the given container builder state private: // Function to compute hash when valid dxil container is built // This is nullptr if loaded container has invalid hash - HASH_FUNCTION_PROTO *m_pHashFunction; + HASH_FUNCTION_PROTO *m_HashFunction; - void FindHashFunctionFromSource(const DxilContainerHeader *pContainerHeader); - void HashAndUpdate(DxilContainerHeader *pContainerHeader); + void FindHashFunctionFromSource(const DxilContainerHeader *ContainerHeader); + void HashAndUpdate(DxilContainerHeader *ContainerHeader); }; -HRESULT STDMETHODCALLTYPE DxcHashingContainerBuilder::Load(IDxcBlob *pSource) { - HRESULT hr = DxcContainerBuilder::Load(pSource); - if (SUCCEEDED(hr)) { - const DxilContainerHeader *pHeader = - (DxilContainerHeader *)pSource->GetBufferPointer(); - FindHashFunctionFromSource(pHeader); +HRESULT STDMETHODCALLTYPE DxcHashingContainerBuilder::Load(IDxcBlob *Source) { + HRESULT HR = DxcContainerBuilder::Load(Source); + if (SUCCEEDED(HR)) { + const DxilContainerHeader *Header = + (DxilContainerHeader *)Source->GetBufferPointer(); + FindHashFunctionFromSource(Header); } - return hr; + return HR; } HRESULT STDMETHODCALLTYPE -DxcHashingContainerBuilder::SerializeContainer(IDxcOperationResult **ppResult) { - HRESULT hr_saved = DxcContainerBuilder::SerializeContainer(ppResult); - if (!SUCCEEDED(hr_saved)) { - return hr_saved; - } - if (ppResult != nullptr && *ppResult != nullptr) { - HRESULT hr; - (*ppResult)->GetStatus(&hr); - if (SUCCEEDED(hr)) { +DxcHashingContainerBuilder::SerializeContainer(IDxcOperationResult **Result) { + HRESULT HR = DxcContainerBuilder::SerializeContainer(Result); + if (!SUCCEEDED(HR)) + return HR; + + if (Result != nullptr && *Result != nullptr) { + HRESULT HR; + (*Result)->GetStatus(&HR); + if (SUCCEEDED(HR)) { CComPtr pObject; - hr = (*ppResult)->GetResult(&pObject); - if (SUCCEEDED(hr)) { - LPVOID ptr = pObject->GetBufferPointer(); - if (IsDxilContainerLike(ptr, pObject->GetBufferSize())) { - HashAndUpdate((DxilContainerHeader *)ptr); - } + HR = (*Result)->GetResult(&pObject); + if (SUCCEEDED(HR)) { + LPVOID PTR = pObject->GetBufferPointer(); + if (IsDxilContainerLike(PTR, pObject->GetBufferSize())) + HashAndUpdate((DxilContainerHeader *)PTR); } } } - return hr_saved; + return HR; } void DxcHashingContainerBuilder::FindHashFunctionFromSource( - const DxilContainerHeader *pContainerHeader) { - DXASSERT(pContainerHeader != nullptr && - IsDxilContainerLike(pContainerHeader, - pContainerHeader->ContainerSizeInBytes), + const DxilContainerHeader *ContainerHeader) { + DXASSERT(ContainerHeader != nullptr && + IsDxilContainerLike(ContainerHeader, + ContainerHeader->ContainerSizeInBytes), "otherwise load function should have returned an error."); - static const UINT32 HashStartOffset = + static const uint32_t HashStartOffset = offsetof(struct DxilContainerHeader, Version); - const BYTE *pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; - UINT AmountToHash = pContainerHeader->ContainerSizeInBytes - HashStartOffset; - BYTE result[DxilContainerHashSize]; - ComputeHashRetail(pDataToHash, AmountToHash, result); - if (0 == memcmp(result, pContainerHeader->Hash.Digest, sizeof(result))) { - m_pHashFunction = ComputeHashRetail; + const BYTE *DataToHash = (const BYTE *)ContainerHeader + HashStartOffset; + UINT AmountToHash = ContainerHeader->ContainerSizeInBytes - HashStartOffset; + BYTE Result[DxilContainerHashSize]; + ComputeHashRetail(DataToHash, AmountToHash, Result); + if (0 == memcmp(Result, ContainerHeader->Hash.Digest, sizeof(Result))) { + m_HashFunction = ComputeHashRetail; } else { - ComputeHashDebug(pDataToHash, AmountToHash, result); - if (0 == memcmp(result, pContainerHeader->Hash.Digest, sizeof(result))) { - m_pHashFunction = ComputeHashDebug; - } else { - m_pHashFunction = nullptr; - } + ComputeHashDebug(DataToHash, AmountToHash, Result); + if (0 == memcmp(Result, ContainerHeader->Hash.Digest, sizeof(Result))) + m_HashFunction = ComputeHashDebug; + else + m_HashFunction = nullptr; } } // For Internal hash function. void DxcHashingContainerBuilder::HashAndUpdate( - DxilContainerHeader *pContainerHeader) { - if (m_pHashFunction != nullptr) { - DXASSERT(pContainerHeader != nullptr, + DxilContainerHeader *ContainerHeader) { + if (m_HashFunction != nullptr) { + DXASSERT(ContainerHeader != nullptr, "Otherwise serialization should have failed."); static const UINT32 HashStartOffset = offsetof(struct DxilContainerHeader, Version); - const BYTE *pDataToHash = (const BYTE *)pContainerHeader + HashStartOffset; - UINT AmountToHash = - pContainerHeader->ContainerSizeInBytes - HashStartOffset; - m_pHashFunction(pDataToHash, AmountToHash, pContainerHeader->Hash.Digest); + const BYTE *DataToHash = (const BYTE *)ContainerHeader + HashStartOffset; + UINT AmountToHash = ContainerHeader->ContainerSizeInBytes - HashStartOffset; + m_HashFunction(DataToHash, AmountToHash, ContainerHeader->Hash.Digest); } } -HRESULT CreateDxcHashingContainerBuilder(REFIID riid, LPVOID *ppv) { +HRESULT CreateDxcHashingContainerBuilder(REFIID RRID, LPVOID *V) { // Call dxil.dll's containerbuilder - *ppv = nullptr; + *V = nullptr; CComPtr Result( DxcHashingContainerBuilder::Alloc(DxcGetThreadMallocNoRef())); - IFROOM(Result.p); + if (nullptr == Result.p) + return E_OUTOFMEMORY; + Result->Init(); - return Result->QueryInterface(riid, ppv); + return Result->QueryInterface(RRID, V); } \ No newline at end of file diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index 8e4c4b3248..bac1de37d0 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -9,25 +9,14 @@ // // /////////////////////////////////////////////////////////////////////////////// -#include "llvm/Bitcode/ReaderWriter.h" -#include "llvm/IR/DiagnosticPrinter.h" -#include "llvm/IR/LLVMContext.h" +#include "dxcvalidator.h" -#include "dxc/DxilContainer/DxilContainer.h" -#include "dxc/HLSL/DxilValidation.h" #include "dxc/Support/WinIncludes.h" -#include "dxc/Support/microcom.h" -#include "dxc/DxilRootSignature/DxilRootSignature.h" -#include "dxc/Support/FileIOHelper.h" #include "dxc/Support/Global.h" -#include "dxc/Support/dxcapi.impl.h" #include "dxc/Support/microcom.h" -#include "llvm/Support/FileSystem.h" -#include "llvm/Support/MSFileSystem.h" -#ifdef _WIN32 -#include "dxc/Tracing/dxcetw.h" -#endif +#include "dxc/dxcapi.h" + #include "DxilHash.h" using namespace llvm; @@ -37,15 +26,6 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { private: DXC_MICROCOM_TM_REF_FIELDS() - HRESULT RunValidation(IDxcBlob *pShader, AbstractMemoryStream *pDiagStream, - UINT32 Flags, DxcBuffer *pOptDebugBitcode, - IDxcBlob **Hashed); - - HRESULT - RunRootSignatureValidation(IDxcBlob *pShader, // Shader to validate. - AbstractMemoryStream *pDiagStream, UINT32 Flags, - IDxcBlob **Hashed); - public: DXC_MICROCOM_TM_ADDREF_RELEASE_IMPL() DXC_MICROCOM_TM_CTOR(DxcValidator) @@ -78,196 +58,40 @@ class DxcValidator : public IDxcValidator2, public IDxcVersionInfo { HRESULT STDMETHODCALLTYPE GetFlags(UINT32 *pFlags) override; }; -HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(UINT32 *pMajor, - UINT32 *pMinor) { - DxcThreadMalloc TM(m_pMalloc); - if (pMajor == nullptr || pMinor == nullptr) - return E_INVALIDARG; - GetValidationVersion(pMajor, pMinor); - return S_OK; -} - -HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(UINT32 *pFlags) { - DxcThreadMalloc TM(m_pMalloc); - if (pFlags == nullptr) - return E_INVALIDARG; - *pFlags = DxcVersionInfoFlags_None; -#ifdef _DEBUG - *pFlags |= DxcVersionInfoFlags_Debug; -#endif - return S_OK; -} - -static void HashAndUpdate(DxilContainerHeader *pContainer) { - // Compute hash and update stored hash. - // Hash the container from this offset to the end. - static const UINT32 DXBCHashStartOffset = - offsetof(struct DxilContainerHeader, Version); - const BYTE *pDataToHash = (const BYTE *)pContainer + DXBCHashStartOffset; - UINT AmountToHash = pContainer->ContainerSizeInBytes - DXBCHashStartOffset; - ComputeHashRetail(pDataToHash, AmountToHash, pContainer->Hash.Digest); -} - -static void HashAndUpdateOrCopy(UINT32 Flags, IDxcBlob *pShader, - IDxcBlob **Hashed) { - if (Flags & DxcValidatorFlags_InPlaceEdit) { - HashAndUpdate((DxilContainerHeader *)pShader->GetBufferPointer()); - *Hashed = pShader; - pShader->AddRef(); - } else { - // Possible gotcha: the blob allocated here is tied to this .dll, so the - // DLL shouldn't be unloaded before the blob is released. - CComPtr HashedBlobStream; - IFT(CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream)); - ULONG cb; - IFT(HashedBlobStream->Write(pShader->GetBufferPointer(), - pShader->GetBufferSize(), &cb)); - HashAndUpdate((DxilContainerHeader *)HashedBlobStream->GetPtr()); - IFT(HashedBlobStream.QueryInterface(Hashed)); - } -} - -HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( - IDxcBlob *pShader, // Shader to validate. - UINT32 Flags, // Validation flags. - DxcBuffer *pOptDebugBitcode, // Debug module bitcode to provide line numbers +// Compile a single entry point to the target shader model +HRESULT STDMETHODCALLTYPE DxcValidator::Validate( + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - DxcThreadMalloc TM(m_pMalloc); - if (pShader == nullptr || ppResult == nullptr || - Flags & ~DxcValidatorFlags_ValidMask) - return E_INVALIDARG; - if (Flags & DxcValidatorFlags_ModuleOnly) - return E_INVALIDARG; - if (pOptDebugBitcode && - (pOptDebugBitcode->Ptr == nullptr || pOptDebugBitcode->Size == 0 || - pOptDebugBitcode->Size >= UINT32_MAX)) - return E_INVALIDARG; - - *ppResult = nullptr; - HRESULT hr = S_OK; - HRESULT validationStatus = S_OK; - DxcEtw_DxcValidation_Start(); - try { - CComPtr pDiagStream; - CComPtr HashedBlob; - IFT(CreateMemoryStream(m_pMalloc, &pDiagStream)); - - // Run validation may throw, but that indicates an inability to validate, - // not that the validation failed (eg out of memory). - if (Flags & DxcValidatorFlags_RootSignatureOnly) { - validationStatus = - RunRootSignatureValidation(pShader, pDiagStream, Flags, &HashedBlob); - } else { - validationStatus = RunValidation(pShader, pDiagStream, Flags, - pOptDebugBitcode, &HashedBlob); - } - if (FAILED(validationStatus)) { - std::string msg("Validation failed.\n"); - ULONG cbWritten; - pDiagStream->Write(msg.c_str(), msg.size(), &cbWritten); - } - // Assemble the result object. - CComPtr pDiagBlob; - CComPtr pDiagBlobEnconding; - hr = pDiagStream.QueryInterface(&pDiagBlob); - DXASSERT_NOMSG(SUCCEEDED(hr)); - IFT(DxcCreateBlobWithEncodingSet(pDiagBlob, CP_UTF8, &pDiagBlobEnconding)); - IFT(DxcResult::Create( - validationStatus, DXC_OUT_OBJECT, - {DxcOutputObject::DataOutput(DXC_OUT_OBJECT, HashedBlob), - DxcOutputObject::DataOutput(DXC_OUT_ERRORS, pDiagBlobEnconding)}, - ppResult)); - } - CATCH_CPP_ASSIGN_HRESULT(); - - DxcEtw_DxcValidation_Stop(SUCCEEDED(hr) ? validationStatus : hr); - return hr; + return hlsl::validate(pShader, Flags, ppResult); } -HRESULT STDMETHODCALLTYPE DxcValidator::Validate( - IDxcBlob *pShader, // Shader to validate. - UINT32 Flags, // Validation flags. +HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( + IDxcBlob *pShader, // Shader to validate. + UINT32 Flags, // Validation flags. + DxcBuffer *pOptDebugBitcode, // Optional debug module bitcode to provide + // line numbers IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return ValidateWithDebug(pShader, Flags, nullptr, ppResult); + return hlsl::validateWithDebug(pShader, Flags, pOptDebugBitcode, ppResult); } -HRESULT DxcValidator::RunValidation(IDxcBlob *pShader, - AbstractMemoryStream *pDiagStream, - UINT32 Flags, DxcBuffer *pDebugBitcode, - IDxcBlob **Hashed) { - - // Run validation may throw, but that indicates an inability to validate, - // not that the validation failed (eg out of memory). That is indicated - // by a failing HRESULT, and possibly error messages in the diagnostics - // stream. - - *Hashed = nullptr; - raw_stream_ostream DiagStream(pDiagStream); - - if (IsDxilContainerLike(pShader->GetBufferPointer(), - pShader->GetBufferSize())) { - IFR(ValidateDxilContainer( - pShader->GetBufferPointer(), pShader->GetBufferSize(), - pDebugBitcode ? pDebugBitcode->Ptr : nullptr, - pDebugBitcode ? (uint32_t)pDebugBitcode->Size : 0, DiagStream)); - } else { - IFR(DXC_E_CONTAINER_INVALID); - } - - HashAndUpdateOrCopy(Flags, pShader, Hashed); - - return S_OK; +HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(UINT32 *pMajor, + UINT32 *pMinor) { + return hlsl::getValidationVersion(pMajor, pMinor); } -HRESULT -DxcValidator::RunRootSignatureValidation(IDxcBlob *pShader, - AbstractMemoryStream *pDiagStream, - UINT32 Flags, IDxcBlob **Hashed) { - - const DxilContainerHeader *pDxilContainer = IsDxilContainerLike( - pShader->GetBufferPointer(), pShader->GetBufferSize()); - if (!pDxilContainer) { - return DXC_E_IR_VERIFICATION_FAILED; - } - - const DxilProgramHeader *pProgramHeader = - GetDxilProgramHeader(pDxilContainer, DFCC_DXIL); - const DxilPartHeader *pPSVPart = - GetDxilPartByType(pDxilContainer, DFCC_PipelineStateValidation); - const DxilPartHeader *pRSPart = - GetDxilPartByType(pDxilContainer, DFCC_RootSignature); - IFRBOOL(pRSPart, DXC_E_MISSING_PART); - if (pProgramHeader) { - // Container has shader part, make sure we have PSV. - IFRBOOL(pPSVPart, DXC_E_MISSING_PART); - } - try { - RootSignatureHandle RSH; - RSH.LoadSerialized((const uint8_t *)GetDxilPartData(pRSPart), - pRSPart->PartSize); - RSH.Deserialize(); - raw_stream_ostream DiagStream(pDiagStream); - if (pProgramHeader) { - IFRBOOL(VerifyRootSignatureWithShaderPSV( - RSH.GetDesc(), - GetVersionShaderType(pProgramHeader->ProgramVersion), - GetDxilPartData(pPSVPart), pPSVPart->PartSize, DiagStream), - DXC_E_INCORRECT_ROOT_SIGNATURE); - // Do not hash here; shaders must go through full shader validation for - // hashing. - } else { - IFRBOOL(VerifyRootSignature(RSH.GetDesc(), DiagStream, false), - DXC_E_INCORRECT_ROOT_SIGNATURE); - HashAndUpdateOrCopy(Flags, pShader, Hashed); - } - } catch (...) { - return DXC_E_IR_VERIFICATION_FAILED; - } - +HRESULT STDMETHODCALLTYPE DxcValidator::GetFlags(UINT32 *pFlags) { + DxcThreadMalloc TM(m_pMalloc); + if (pFlags == nullptr) + return E_INVALIDARG; + *pFlags = DxcVersionInfoFlags_None; +#ifdef _DEBUG + *pFlags |= DxcVersionInfoFlags_Debug; +#endif return S_OK; } @@ -275,7 +99,8 @@ HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv) { try { CComPtr result( DxcValidator::Alloc(DxcGetThreadMallocNoRef())); - IFROOM(result.p); + if (result.p == nullptr) + return E_OUTOFMEMORY; return result.p->QueryInterface(riid, ppv); } CATCH_CPP_RETURN_HRESULT(); diff --git a/tools/clang/tools/dxildll/dxildll.cpp b/tools/clang/tools/dxildll/dxildll.cpp index 15e7e9cca8..3fb0dcd045 100644 --- a/tools/clang/tools/dxildll/dxildll.cpp +++ b/tools/clang/tools/dxildll/dxildll.cpp @@ -29,31 +29,31 @@ #include "dxc/dxcisense.h" #include "dxc/dxctools.h" -HRESULT CreateDxcValidator(REFIID riid, LPVOID *ppv); -HRESULT CreateDxcHashingContainerBuilder(REFIID riid, LPVOID *ppv); +HRESULT CreateDxcValidator(REFIID, LPVOID *); +HRESULT CreateDxcHashingContainerBuilder(REFIID, LPVOID *); // C++ exception specification ignored except to indicate a function is not // __declspec(nothrow) static HRESULT InitMaybeFail() throw() { - HRESULT hr; - bool memSetup = false; - IFC(DxcInitThreadMalloc()); + bool MemSetup = false; + HRESULT HR = (DxcInitThreadMalloc()); + if (DXC_FAILED(HR)) + goto Cleanup; DxcSetThreadMallocToDefault(); - memSetup = true; + MemSetup = true; if (::llvm::sys::fs::SetupPerThreadFileSystem()) { - hr = E_FAIL; + HR = E_FAIL; goto Cleanup; } Cleanup: - if (FAILED(hr)) { - if (memSetup) { + if (FAILED(HR)) { + if (MemSetup) { DxcClearThreadMalloc(); DxcCleanupThreadMalloc(); } - } else { + } else DxcClearThreadMalloc(); - } - return hr; + return HR; } #if defined(LLVM_ON_UNIX) @@ -73,11 +73,11 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) { if (Reason == DLL_PROCESS_ATTACH) { EventRegisterMicrosoft_Windows_DxcRuntime_API(); DxcRuntimeEtw_DxcRuntimeInitialization_Start(); - HRESULT hr = InitMaybeFail(); - DxcRuntimeEtw_DxcRuntimeInitialization_Stop(hr); - if (FAILED(hr)) { + HRESULT HR = InitMaybeFail(); + DxcRuntimeEtw_DxcRuntimeInitialization_Stop(HR); + if (FAILED(HR)) { EventUnregisterMicrosoft_Windows_DxcRuntime_API(); - return hr; + return HR; } } else if (Reason == DLL_PROCESS_DETACH) { DxcRuntimeEtw_DxcRuntimeShutdown_Start(); @@ -93,55 +93,51 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) { return TRUE; } -void *__CRTDECL operator new(std::size_t size) noexcept(false) { - void *ptr = DxcNew(size); - if (ptr == nullptr) +void *__CRTDECL operator new(std::size_t Size) noexcept(false) { + void *PTR = DxcNew(Size); + if (PTR == nullptr) throw std::bad_alloc(); - return ptr; + return PTR; } -void *__CRTDECL operator new(std::size_t size, - const std::nothrow_t ¬hrow_value) throw() { - return DxcNew(size); +void *__CRTDECL operator new(std::size_t Size, const std::nothrow_t &) throw() { + return DxcNew(Size); } -void __CRTDECL operator delete(void *ptr) throw() { DxcDelete(ptr); } -void __CRTDECL operator delete(void *ptr, +void __CRTDECL operator delete(void *PTR) throw() { DxcDelete(PTR); } +void __CRTDECL operator delete(void *PTR, const std::nothrow_t ¬hrow_constant) throw() { - DxcDelete(ptr); + DxcDelete(PTR); } #endif -static HRESULT ThreadMallocDxcCreateInstance(REFCLSID rclsid, REFIID riid, - LPVOID *ppv) { - *ppv = nullptr; - if (IsEqualCLSID(rclsid, CLSID_DxcValidator)) { - return CreateDxcValidator(riid, ppv); - } - if (IsEqualCLSID(rclsid, CLSID_DxcContainerBuilder)) { - return CreateDxcHashingContainerBuilder(riid, ppv); - } +static HRESULT ThreadMallocDxcCreateInstance(REFCLSID RCLSID, REFIID RIID, + LPVOID *V) { + *V = nullptr; + if (IsEqualCLSID(RCLSID, CLSID_DxcValidator)) + return CreateDxcValidator(RIID, V); + if (IsEqualCLSID(RCLSID, CLSID_DxcContainerBuilder)) + return CreateDxcHashingContainerBuilder(RIID, V); return REGDB_E_CLASSNOTREG; } -DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(REFCLSID rclsid, REFIID riid, - LPVOID *ppv) { - HRESULT hr = S_OK; +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance(REFCLSID RCLSID, REFIID RIID, + LPVOID *V) { + HRESULT HR = S_OK; DxcEtw_DXCompilerCreateInstance_Start(); DxcThreadMalloc TM(nullptr); - hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); - DxcEtw_DXCompilerCreateInstance_Stop(hr); - return hr; + HR = ThreadMallocDxcCreateInstance(RCLSID, RIID, V); + DxcEtw_DXCompilerCreateInstance_Stop(HR); + return HR; } -DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(IMalloc *pMalloc, - REFCLSID rclsid, - REFIID riid, LPVOID *ppv) { - if (ppv == nullptr) { +DXC_API_IMPORT HRESULT __stdcall DxcCreateInstance2(IMalloc *Malloc, + REFCLSID RCLSID, + REFIID RIID, LPVOID *V) { + if (V == nullptr) return E_POINTER; - } - HRESULT hr = S_OK; + HRESULT HR = S_OK; DxcEtw_DXCompilerCreateInstance_Start(); - DxcThreadMalloc TM(pMalloc); - hr = ThreadMallocDxcCreateInstance(rclsid, riid, ppv); - DxcEtw_DXCompilerCreateInstance_Stop(hr); - return hr; + DxcThreadMalloc TM(Malloc); + HR = ThreadMallocDxcCreateInstance(RCLSID, RIID, V); + DxcEtw_DXCompilerCreateInstance_Stop(HR); + return HR; } From 8361702772384b037cc89fbdb8828a4441a75031 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 28 Jul 2024 18:21:25 -0400 Subject: [PATCH 13/19] Recover internal validator bahavior. --- tools/clang/tools/dxcompiler/dxcvalidator.cpp | 5 ++-- .../clang/tools/dxcvalidator/dxcvalidator.cpp | 24 +++++++++---------- tools/clang/tools/dxcvalidator/dxcvalidator.h | 2 ++ tools/clang/tools/dxildll/dxcvalidatorp.cpp | 5 ++-- .../dxrfallbackcompiler/dxcvalidator.cpp | 2 +- 5 files changed, 20 insertions(+), 18 deletions(-) diff --git a/tools/clang/tools/dxcompiler/dxcvalidator.cpp b/tools/clang/tools/dxcompiler/dxcvalidator.cpp index d49620bbda..97d04016cb 100644 --- a/tools/clang/tools/dxcompiler/dxcvalidator.cpp +++ b/tools/clang/tools/dxcompiler/dxcvalidator.cpp @@ -90,7 +90,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validate(pShader, Flags, ppResult); + return hlsl::validate(pShader, Flags, true, ppResult); } HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( @@ -101,7 +101,8 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validateWithDebug(pShader, Flags, pOptDebugBitcode, ppResult); + return hlsl::validateWithDebug(pShader, Flags, true, pOptDebugBitcode, + ppResult); } HRESULT DxcValidator::ValidateWithOptModules( diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index eb79f6ddba..a66a6e8903 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -112,16 +112,12 @@ static uint32_t runValidation( if (!Module) { DXASSERT_NOMSG(DebugModule == nullptr); - HRESULT HR = S_OK; if (Flags & DxcValidatorFlags_ModuleOnly) - HR = ValidateDxilBitcode((const char *)Shader->GetBufferPointer(), - (uint32_t)Shader->GetBufferSize(), DiagStream); + return ValidateDxilBitcode((const char *)Shader->GetBufferPointer(), + (uint32_t)Shader->GetBufferSize(), DiagStream); else - HR = ValidateDxilContainer(Shader->GetBufferPointer(), - Shader->GetBufferSize(), DiagStream); - if (HR == S_OK) - HashAndUpdateOrCopy(Flags, Shader, Hashed); - return HR; + return ValidateDxilContainer(Shader->GetBufferPointer(), + Shader->GetBufferSize(), DiagStream); } llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); @@ -144,8 +140,6 @@ static uint32_t runValidation( if (DiagContext.HasErrors() || DiagContext.HasWarnings()) return DXC_E_IR_VERIFICATION_FAILED; - HashAndUpdateOrCopy(Flags, Shader, Hashed); - return S_OK; } @@ -228,14 +222,16 @@ static uint32_t runRootSignatureValidation(IDxcBlob *Shader, uint32_t hlsl::validate( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. + bool IsInternalValidator, // Run internal validator. IDxcOperationResult **Result // Validation output status, buffer, and errors ) { - return validateWithDebug(Shader, Flags, nullptr, Result); + return validateWithDebug(Shader, Flags, IsInternalValidator, nullptr, Result); } uint32_t hlsl::validateWithDebug( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. + bool IsInternalValidator, // Run internal validator. DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide // line numbers IDxcOperationResult **Result // Validation output status, buffer, and errors @@ -260,8 +256,7 @@ uint32_t hlsl::validateWithDebug( try { CComPtr DiagMemStream; CComPtr HashedBlob; - HR = - CreateMemoryStream(TM.GetInstalledAllocator(), &DiagMemStream); + HR = CreateMemoryStream(TM.GetInstalledAllocator(), &DiagMemStream); if (FAILED(HR)) throw hlsl::Exception(HR); @@ -270,6 +265,9 @@ uint32_t hlsl::validateWithDebug( if (Flags & DxcValidatorFlags_RootSignatureOnly) ValidationStatus = runRootSignatureValidation(Shader, DiagMemStream, Flags, &HashedBlob); + else if ((Flags & DxcValidatorFlags_ModuleOnly) && IsInternalValidator) + ValidationStatus = runValidation(Shader, Flags, nullptr, nullptr, + DiagMemStream, &HashedBlob); else ValidationStatus = runValidation(Shader, DiagMemStream, Flags, OptDebugBitcode, &HashedBlob); diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.h b/tools/clang/tools/dxcvalidator/dxcvalidator.h index 9edfe1e868..46b869c46a 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.h +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.h @@ -37,6 +37,7 @@ uint32_t validateWithOptModules( uint32_t validate( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. + bool IsInternalValidator, // Run internal validator. IDxcOperationResult **Result // Validation output status, buffer, and errors ); @@ -44,6 +45,7 @@ uint32_t validate( uint32_t validateWithDebug( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. + bool IsInternalValidator, // Run internal validator. DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide // line numbers IDxcOperationResult **Result // Validation output status, buffer, and errors diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidatorp.cpp index bac1de37d0..45f0857ada 100644 --- a/tools/clang/tools/dxildll/dxcvalidatorp.cpp +++ b/tools/clang/tools/dxildll/dxcvalidatorp.cpp @@ -65,7 +65,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validate(pShader, Flags, ppResult); + return hlsl::validate(pShader, Flags, false, ppResult); } HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( @@ -76,7 +76,8 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validateWithDebug(pShader, Flags, pOptDebugBitcode, ppResult); + return hlsl::validateWithDebug(pShader, Flags, false, pOptDebugBitcode, + ppResult); } HRESULT STDMETHODCALLTYPE DxcValidator::GetVersion(UINT32 *pMajor, diff --git a/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp b/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp index ec01414ef5..3221516586 100644 --- a/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp +++ b/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp @@ -64,7 +64,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validate(pShader, Flags, ppResult); + return hlsl::validate(pShader, Flags, true, ppResult); } HRESULT DxcValidator::ValidateWithOptModules( From acd422642049d50f7f1ca80c7db0924a07562396 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Sun, 28 Jul 2024 18:25:39 -0400 Subject: [PATCH 14/19] Fix clang format. --- tools/clang/tools/dxcvalidator/dxcvalidator.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.h b/tools/clang/tools/dxcvalidator/dxcvalidator.h index 46b869c46a..0e5138e289 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.h +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.h @@ -37,7 +37,7 @@ uint32_t validateWithOptModules( uint32_t validate( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. - bool IsInternalValidator, // Run internal validator. + bool IsInternalValidator, // Run internal validator. IDxcOperationResult **Result // Validation output status, buffer, and errors ); From e2ca0380d48e6ca3be5566b574185ebd383b339a Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2024 23:18:14 -0400 Subject: [PATCH 15/19] Remove comment in wrong place. --- tools/clang/tools/dxcvalidator/dxcvalidator.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index a66a6e8903..8b25e4ea9a 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -66,8 +66,6 @@ static void HashAndUpdateOrCopy(uint32_t Flags, IDxcBlob *Shader, *Hashed = Shader; Shader->AddRef(); } else { - // Possible gotcha: the blob allocated here is tied to this .dll, so the - // DLL shouldn't be unloaded before the blob is released. CComPtr HashedBlobStream; uint32_t HR = CreateMemoryStream(DxcGetThreadMallocNoRef(), &HashedBlobStream); From a2801cd69773bea6bac841b06de680625774f85c Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2024 23:35:42 -0400 Subject: [PATCH 16/19] Recover DebugModule. --- .../clang/tools/dxcvalidator/dxcvalidator.cpp | 28 ++++++++++++++----- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index 8b25e4ea9a..c23e4d12fa 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -89,7 +89,7 @@ static uint32_t runValidation( uint32_t Flags, // Validation flags. llvm::Module *Module, // Module to validate, if available. llvm::Module *DebugModule, // Debug module to validate, if available - AbstractMemoryStream *DiagMemStream, IDxcBlob **Hashed) { + AbstractMemoryStream *DiagMemStream) { // Run validation may throw, but that indicates an inability to validate, // not that the validation failed (eg out of memory). That is indicated @@ -263,13 +263,27 @@ uint32_t hlsl::validateWithDebug( if (Flags & DxcValidatorFlags_RootSignatureOnly) ValidationStatus = runRootSignatureValidation(Shader, DiagMemStream, Flags, &HashedBlob); - else if ((Flags & DxcValidatorFlags_ModuleOnly) && IsInternalValidator) - ValidationStatus = runValidation(Shader, Flags, nullptr, nullptr, - DiagMemStream, &HashedBlob); - else + else if ((Flags & DxcValidatorFlags_ModuleOnly) && IsInternalValidator) { + LLVMContext Ctx; + raw_stream_ostream DiagStream(DiagMemStream); + llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); + PrintDiagnosticContext DiagContext(DiagPrinter); + Ctx.setDiagnosticHandler(PrintDiagnosticContext::PrintDiagnosticHandler, + &DiagContext, true); + std::unique_ptr DebugModule; + if (OptDebugBitcode) { + HR = ValidateLoadModule((const char *)OptDebugBitcode->Ptr, + (uint32_t)OptDebugBitcode->Size, DebugModule, + Ctx, DiagStream, /*bLazyLoad*/ false); + if (FAILED(HR)) + throw hlsl::Exception(HR); + } + ValidationStatus = runValidation(Shader, Flags, nullptr, + DebugModule.get(), DiagMemStream); + } else { ValidationStatus = runValidation(Shader, DiagMemStream, Flags, OptDebugBitcode, &HashedBlob); - + } if (FAILED(ValidationStatus)) { std::string msg("Validation failed.\n"); ULONG cbWritten; @@ -325,7 +339,7 @@ uint32_t hlsl::validateWithOptModules( runRootSignatureValidation(Shader, DiagStream, Flags, &HashedBlob); else ValidationStatus = runValidation(Shader, Flags, Module, DebugModule, - DiagStream, &HashedBlob); + DiagStream); if (FAILED(ValidationStatus)) { std::string msg("Validation failed.\n"); ULONG cbWritten; From d2c42edfb1ffe948ab1a21cd6da7891afc9457a1 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2024 23:49:56 -0400 Subject: [PATCH 17/19] Fix clang format. --- tools/clang/tools/dxcvalidator/dxcvalidator.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index c23e4d12fa..2446879832 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -338,8 +338,8 @@ uint32_t hlsl::validateWithOptModules( ValidationStatus = runRootSignatureValidation(Shader, DiagStream, Flags, &HashedBlob); else - ValidationStatus = runValidation(Shader, Flags, Module, DebugModule, - DiagStream); + ValidationStatus = + runValidation(Shader, Flags, Module, DebugModule, DiagStream); if (FAILED(ValidationStatus)) { std::string msg("Validation failed.\n"); ULONG cbWritten; From f4366ceb71f286421ba66f2516e5bc229298d614 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Wed, 31 Jul 2024 23:56:34 -0400 Subject: [PATCH 18/19] Rename file. --- tools/clang/tools/dxildll/CMakeLists.txt | 2 +- .../clang/tools/dxildll/{dxcvalidatorp.cpp => dxcvalidator.cpp} | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename tools/clang/tools/dxildll/{dxcvalidatorp.cpp => dxcvalidator.cpp} (100%) diff --git a/tools/clang/tools/dxildll/CMakeLists.txt b/tools/clang/tools/dxildll/CMakeLists.txt index 021b5977eb..50e3c3dedb 100644 --- a/tools/clang/tools/dxildll/CMakeLists.txt +++ b/tools/clang/tools/dxildll/CMakeLists.txt @@ -36,7 +36,7 @@ set(sources dxildll.cpp dxildll.def DxcHashingContainerBuilder.cpp - dxcvalidatorp.cpp + dxcvalidator.cpp ) if (WIN32) diff --git a/tools/clang/tools/dxildll/dxcvalidatorp.cpp b/tools/clang/tools/dxildll/dxcvalidator.cpp similarity index 100% rename from tools/clang/tools/dxildll/dxcvalidatorp.cpp rename to tools/clang/tools/dxildll/dxcvalidator.cpp From 7844b648c73793d5641753e72e48ce68b897de21 Mon Sep 17 00:00:00 2001 From: Xiang Li Date: Thu, 1 Aug 2024 18:12:00 -0400 Subject: [PATCH 19/19] Support DxcValidatorFlags_ModuleOnly for both internal and external validator. --- tools/clang/tools/dxcompiler/dxcvalidator.cpp | 5 +- .../clang/tools/dxcvalidator/dxcvalidator.cpp | 69 +++++++++++++------ tools/clang/tools/dxcvalidator/dxcvalidator.h | 2 - .../dxrfallbackcompiler/dxcvalidator.cpp | 2 +- 4 files changed, 50 insertions(+), 28 deletions(-) diff --git a/tools/clang/tools/dxcompiler/dxcvalidator.cpp b/tools/clang/tools/dxcompiler/dxcvalidator.cpp index 97d04016cb..d49620bbda 100644 --- a/tools/clang/tools/dxcompiler/dxcvalidator.cpp +++ b/tools/clang/tools/dxcompiler/dxcvalidator.cpp @@ -90,7 +90,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validate(pShader, Flags, true, ppResult); + return hlsl::validate(pShader, Flags, ppResult); } HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( @@ -101,8 +101,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::ValidateWithDebug( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validateWithDebug(pShader, Flags, true, pOptDebugBitcode, - ppResult); + return hlsl::validateWithDebug(pShader, Flags, pOptDebugBitcode, ppResult); } HRESULT DxcValidator::ValidateWithOptModules( diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp index 2446879832..973923194b 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.cpp +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.cpp @@ -216,20 +216,59 @@ static uint32_t runRootSignatureValidation(IDxcBlob *Shader, return S_OK; } +static uint32_t runDxilModuleValidation( + IDxcBlob *Shader, // Shader to validate. + DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide + // line numbers + AbstractMemoryStream *DiagMemStream) { + if (IsDxilContainerLike(Shader->GetBufferPointer(), Shader->GetBufferSize())) + return E_INVALIDARG; + + LLVMContext Ctx; + raw_stream_ostream DiagStream(DiagMemStream); + llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); + PrintDiagnosticContext DiagContext(DiagPrinter); + Ctx.setDiagnosticHandler(PrintDiagnosticContext::PrintDiagnosticHandler, + &DiagContext, true); + std::unique_ptr DebugModule; + if (OptDebugBitcode) { + uint32_t HR = ValidateLoadModule( + (const char *)OptDebugBitcode->Ptr, (uint32_t)OptDebugBitcode->Size, + DebugModule, Ctx, DiagStream, /*bLazyLoad*/ false); + if (FAILED(HR)) + return HR; + } + + std::unique_ptr Module; + uint32_t HR = ValidateLoadModule((const char *)Shader->GetBufferPointer(), + (uint32_t)Shader->GetBufferSize(), Module, + Ctx, DiagStream, /*bLazyLoad*/ false); + if (FAILED(HR)) + return HR; + + DiagRestore DR(Module->getContext(), &DiagContext); + + HR = hlsl::ValidateDxilModule(Module.get(), DebugModule.get()); + if (FAILED(HR)) + return HR; + if (DiagContext.HasErrors() || DiagContext.HasWarnings()) + return DXC_E_IR_VERIFICATION_FAILED; + + return S_OK; +} + // Compile a single entry point to the target shader model uint32_t hlsl::validate( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. - bool IsInternalValidator, // Run internal validator. IDxcOperationResult **Result // Validation output status, buffer, and errors ) { - return validateWithDebug(Shader, Flags, IsInternalValidator, nullptr, Result); + return validateWithDebug(Shader, Flags, nullptr, Result); } uint32_t hlsl::validateWithDebug( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. - bool IsInternalValidator, // Run internal validator. DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide // line numbers IDxcOperationResult **Result // Validation output status, buffer, and errors @@ -263,27 +302,13 @@ uint32_t hlsl::validateWithDebug( if (Flags & DxcValidatorFlags_RootSignatureOnly) ValidationStatus = runRootSignatureValidation(Shader, DiagMemStream, Flags, &HashedBlob); - else if ((Flags & DxcValidatorFlags_ModuleOnly) && IsInternalValidator) { - LLVMContext Ctx; - raw_stream_ostream DiagStream(DiagMemStream); - llvm::DiagnosticPrinterRawOStream DiagPrinter(DiagStream); - PrintDiagnosticContext DiagContext(DiagPrinter); - Ctx.setDiagnosticHandler(PrintDiagnosticContext::PrintDiagnosticHandler, - &DiagContext, true); - std::unique_ptr DebugModule; - if (OptDebugBitcode) { - HR = ValidateLoadModule((const char *)OptDebugBitcode->Ptr, - (uint32_t)OptDebugBitcode->Size, DebugModule, - Ctx, DiagStream, /*bLazyLoad*/ false); - if (FAILED(HR)) - throw hlsl::Exception(HR); - } - ValidationStatus = runValidation(Shader, Flags, nullptr, - DebugModule.get(), DiagMemStream); - } else { + else if (Flags & DxcValidatorFlags_ModuleOnly) + ValidationStatus = + runDxilModuleValidation(Shader, OptDebugBitcode, DiagMemStream); + else ValidationStatus = runValidation(Shader, DiagMemStream, Flags, OptDebugBitcode, &HashedBlob); - } + if (FAILED(ValidationStatus)) { std::string msg("Validation failed.\n"); ULONG cbWritten; diff --git a/tools/clang/tools/dxcvalidator/dxcvalidator.h b/tools/clang/tools/dxcvalidator/dxcvalidator.h index 0e5138e289..9edfe1e868 100644 --- a/tools/clang/tools/dxcvalidator/dxcvalidator.h +++ b/tools/clang/tools/dxcvalidator/dxcvalidator.h @@ -37,7 +37,6 @@ uint32_t validateWithOptModules( uint32_t validate( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. - bool IsInternalValidator, // Run internal validator. IDxcOperationResult **Result // Validation output status, buffer, and errors ); @@ -45,7 +44,6 @@ uint32_t validate( uint32_t validateWithDebug( IDxcBlob *Shader, // Shader to validate. uint32_t Flags, // Validation flags. - bool IsInternalValidator, // Run internal validator. DxcBuffer *OptDebugBitcode, // Optional debug module bitcode to provide // line numbers IDxcOperationResult **Result // Validation output status, buffer, and errors diff --git a/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp b/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp index 3221516586..ec01414ef5 100644 --- a/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp +++ b/tools/clang/tools/dxrfallbackcompiler/dxcvalidator.cpp @@ -64,7 +64,7 @@ HRESULT STDMETHODCALLTYPE DxcValidator::Validate( IDxcOperationResult * *ppResult // Validation output status, buffer, and errors ) { - return hlsl::validate(pShader, Flags, true, ppResult); + return hlsl::validate(pShader, Flags, ppResult); } HRESULT DxcValidator::ValidateWithOptModules(