浏览代码

Fixes #823 - Compile crashes if pSourceName if nullptr/empty (#1011)

Code had a (reasonable) assumption that sources have names, but the
entry point wasn't verifying that. For compat with prior compilers and
the annotation, we choose to support it. Unlikely other implementations,
the name is a simple hard-coded one, instead of varying, to allow for
easier deduping when the output is checked (name is in debug section).
Marcelo Lopez Ruiz 7 年之前
父节点
当前提交
528bcf354c
共有 2 个文件被更改,包括 38 次插入3 次删除
  1. 1 0
      tools/clang/tools/dxcompiler/dxcompilerobj.cpp
  2. 37 3
      tools/clang/unittests/HLSL/CompilerTest.cpp

+ 1 - 0
tools/clang/tools/dxcompiler/dxcompilerobj.cpp

@@ -309,6 +309,7 @@ public:
     CComPtr<AbstractMemoryStream> pOutputStream;
     CHeapPtr<wchar_t> DebugBlobName;
     DxcEtw_DXCompilerCompile_Start();
+    pSourceName = (pSourceName && *pSourceName) ? pSourceName : L"hlsl.hlsl"; // declared optional, so pick a default
     DxcThreadMalloc TM(m_pMalloc);
     IFC(hlsl::DxcGetBlobAsUtf8(pSource, &utf8Source));
 

+ 37 - 3
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -1927,16 +1927,50 @@ TEST_F(CompilerTest, CompileWhenEmptyThenFails) {
   CComPtr<IDxcCompiler> pCompiler;
   CComPtr<IDxcOperationResult> pResult;
   CComPtr<IDxcBlobEncoding> pSource;
+  CComPtr<IDxcBlobEncoding> pSourceBad;
+  LPCWSTR pProfile = L"ps_6_0";
+  LPCWSTR pEntryPoint = L"main";
 
   VERIFY_SUCCEEDED(CreateCompiler(&pCompiler));
   CreateBlobFromText("float4 main() : SV_Target { return 0; }", &pSource);
+  CreateBlobFromText("float4 main() : SV_Target { return undef; }", &pSourceBad);
+
+  // correct version
+  VERIFY_SUCCEEDED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
+                                      pProfile, nullptr, 0, nullptr, 0, nullptr,
+                                      &pResult));
+  pResult.Release();
+
+  // correct version with compilation errors
+  VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
+                                      pProfile, nullptr, 0, nullptr, 0, nullptr,
+                                      &pResult));
+  pResult.Release();
 
   // null source
-  VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", nullptr, nullptr,
+  VERIFY_FAILED(pCompiler->Compile(nullptr, L"source.hlsl", pEntryPoint, pProfile,
                                    nullptr, 0, nullptr, 0, nullptr, &pResult));
+
+  // null profile
+  VERIFY_FAILED(pCompiler->Compile(pSourceBad, L"source.hlsl", pEntryPoint,
+                                   nullptr, nullptr, 0, nullptr, 0, nullptr,
+                                   &pResult));
+
+  // null source name succeeds
+  VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, nullptr, pEntryPoint, pProfile,
+                                   nullptr, 0, nullptr, 0, nullptr, &pResult));
+  pResult.Release();
+
+  // empty source name (as opposed to null) also suceeds
+  VERIFY_SUCCEEDED(pCompiler->Compile(pSourceBad, L"", pEntryPoint, pProfile,
+                                      nullptr, 0, nullptr, 0, nullptr,
+                                      &pResult));
+  pResult.Release();
+
   // null result
-  VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", nullptr, nullptr,
-                                   nullptr, 0, nullptr, 0, nullptr, nullptr));
+  VERIFY_FAILED(pCompiler->Compile(pSource, L"source.hlsl", pEntryPoint,
+                                   pProfile, nullptr, 0, nullptr, 0, nullptr,
+                                   nullptr));
 }
 
 TEST_F(CompilerTest, CompileWhenIncorrectThenFails) {