浏览代码

Fix problems with MSFileSystem used from independent locations

CompilerTest would hit appverif break due to using before other
code would call setup.
Adding setup leads to independent locations trying to setup/cleanup
the file system, which would trigger assert.

- Make [Setup/Cleanup]PerThreadFileSystem() support tracking/ignoring
nested calls.
- Added setup/cleanup to CompilerTest and DxilModuleTest
- Added some asserts
Tex Riddell 7 年之前
父节点
当前提交
fc2d6fe4da

+ 12 - 1
lib/Support/Windows/MSFileSystem.inc.cpp

@@ -53,9 +53,15 @@ namespace fs {
 
 
 static DWORD g_FileSystemTls;
 static DWORD g_FileSystemTls;
 
 
+// skip, but keep track of setup/cleanup nesting
+static unsigned g_FileSystemSetupNested = 0;
+
 error_code SetupPerThreadFileSystem() throw()
 error_code SetupPerThreadFileSystem() throw()
 {
 {
-  assert(g_FileSystemTls == 0 && "otherwise this has already been initialized");
+  if (g_FileSystemSetupNested++)
+    return error_code();
+
+  assert(g_FileSystemTls == 0 && "otherwise this has already been initialized, and nesting guard failed.");
   g_FileSystemTls = TlsAlloc();
   g_FileSystemTls = TlsAlloc();
   if (g_FileSystemTls == TLS_OUT_OF_INDEXES)
   if (g_FileSystemTls == TLS_OUT_OF_INDEXES)
   {
   {
@@ -67,12 +73,17 @@ error_code SetupPerThreadFileSystem() throw()
 
 
 void CleanupPerThreadFileSystem() throw()
 void CleanupPerThreadFileSystem() throw()
 {
 {
+  assert(g_FileSystemSetupNested && "otherwise, Cleanup called without matching Setup");
+  if (--g_FileSystemSetupNested)
+    return;
+  assert(g_FileSystemTls != 0 && "otherwise this has not been initialized");
   TlsFree(g_FileSystemTls);
   TlsFree(g_FileSystemTls);
   g_FileSystemTls = 0;
   g_FileSystemTls = 0;
 }
 }
 
 
 MSFileSystemRef GetCurrentThreadFileSystem() throw()
 MSFileSystemRef GetCurrentThreadFileSystem() throw()
 {
 {
+  assert(g_FileSystemTls != 0 && "otherwise this has not been initialized");
   return (MSFileSystemRef)TlsGetValue(g_FileSystemTls);
   return (MSFileSystemRef)TlsGetValue(g_FileSystemTls);
 }
 }
 
 

+ 9 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -389,6 +389,7 @@ public:
   END_TEST_CLASS()
   END_TEST_CLASS()
 
 
   TEST_CLASS_SETUP(InitSupport);
   TEST_CLASS_SETUP(InitSupport);
+  TEST_CLASS_CLEANUP(Cleanup);
 
 
   TEST_METHOD(CompileWhenDebugThenDIPresent)
   TEST_METHOD(CompileWhenDebugThenDIPresent)
   TEST_METHOD(CompileDebugLines)
   TEST_METHOD(CompileDebugLines)
@@ -1750,6 +1751,14 @@ bool CompilerTest::InitSupport() {
   if (!m_dllSupport.IsEnabled()) {
   if (!m_dllSupport.IsEnabled()) {
     VERIFY_SUCCEEDED(m_dllSupport.Initialize());
     VERIFY_SUCCEEDED(m_dllSupport.Initialize());
     m_ver.Initialize(m_dllSupport);
     m_ver.Initialize(m_dllSupport);
+    llvm::sys::fs::SetupPerThreadFileSystem();
+  }
+  return true;
+}
+bool CompilerTest::Cleanup() {
+  if (m_dllSupport.IsEnabled()) {
+    m_dllSupport.Cleanup();
+    llvm::sys::fs::CleanupPerThreadFileSystem();
   }
   }
   return true;
   return true;
 }
 }

+ 18 - 1
tools/clang/unittests/HLSL/DxilModuleTest.cpp

@@ -41,6 +41,9 @@ public:
     TEST_METHOD_PROPERTY(L"Priority", L"0")
     TEST_METHOD_PROPERTY(L"Priority", L"0")
   END_TEST_CLASS()
   END_TEST_CLASS()
 
 
+  TEST_CLASS_SETUP(InitSupport);
+  TEST_CLASS_CLEANUP(Cleanup);
+
   dxc::DxcDllSupport m_dllSupport;
   dxc::DxcDllSupport m_dllSupport;
 
 
   // Basic loading tests.
   // Basic loading tests.
@@ -58,6 +61,21 @@ public:
   TEST_METHOD(Precise7);
   TEST_METHOD(Precise7);
 };
 };
 
 
+bool DxilModuleTest::InitSupport() {
+  if (!m_dllSupport.IsEnabled()) {
+    VERIFY_SUCCEEDED(m_dllSupport.Initialize());
+  }
+  llvm::sys::fs::SetupPerThreadFileSystem();
+  return true;
+}
+bool DxilModuleTest::Cleanup() {
+  if (m_dllSupport.IsEnabled()) {
+    m_dllSupport.Cleanup();
+  }
+  llvm::sys::fs::CleanupPerThreadFileSystem();
+  return true;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 // Compilation and dxil module loading support.
 // Compilation and dxil module loading support.
 
 
@@ -69,7 +87,6 @@ public:
     , m_msf(CreateMSFileSystem())
     , m_msf(CreateMSFileSystem())
     , m_pts(m_msf.get())
     , m_pts(m_msf.get())
   {
   {
-    VERIFY_SUCCEEDED(m_dllSupport.Initialize());
     m_ver.Initialize(m_dllSupport);
     m_ver.Initialize(m_dllSupport);
     VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
     VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
   }
   }