[libcxx-commits] [PATCH] D125922: [libcxx] [test] Don't use header defines for detecting linking against a DLL

Martin Storsjö via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 18 13:01:25 PDT 2022


mstorsjo created this revision.
mstorsjo added a reviewer: ldionne.
Herald added a subscriber: arichardson.
Herald added a project: All.
mstorsjo requested review of this revision.
Herald added a project: libc++.
Herald added a reviewer: libc++.

In clang-cl/MSVC environments, linking against a DLL C++ standard
library requires having dllimport attributes in the headers; this
has been used for detecting whether the tests link against a DLL,
by looking at the libc++ specific define
_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS.

In mingw environments, thanks to slightly different code generation
and a couple linker tricks, it's possible to link against a DLL C++
standard library without dllimport attributes. Therefore, don't
rely on the libc++ specific header define for the detection.

Replace the detection with a runtime test.

There are two potential alternatives for a test (I included both
in the patch, with one of them commented out); one simple, libc++
specific which just checks if the libc++ DLL exists loaded in the
process, and a more complex one which inspects the PE image and
checks whether the address of std::cout exists within the address
range of the EXE (which can detect whether linking against a DLL
version of other C++ standard libraries than libc++).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D125922

Files:
  libcxx/test/support/test_macros.h
  libcxx/utils/libcxx/test/features.py


Index: libcxx/utils/libcxx/test/features.py
===================================================================
--- libcxx/utils/libcxx/test/features.py
+++ libcxx/utils/libcxx/test/features.py
@@ -219,7 +219,31 @@
 DEFAULT_FEATURES += [
   Feature(name='darwin', when=lambda cfg: '__APPLE__' in compilerMacros(cfg)),
   Feature(name='windows', when=lambda cfg: '_WIN32' in compilerMacros(cfg)),
-  Feature(name='windows-dll', when=lambda cfg: '_WIN32' in compilerMacros(cfg) and not '_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS' in compilerMacros(cfg)),
+  Feature(name='windows-dll', when=lambda cfg: '_WIN32' in compilerMacros(cfg) and programSucceeds(cfg, """
+            #include <iostream>
+            #include <windows.h>
+            void *ptr;
+            int main(int, char**) {
+              ptr = &std::cout;
+              if (GetModuleHandle("libc++.dll") || GetModuleHandle("c++.dll"))
+                return 0;
+              return 1;
+            }
+          """), actions=[AddCompileFlag('-DTEST_WINDOWS_DLL')]),
+#            #include <iostream>
+#            #include <windows.h>
+#            #include <winnt.h>
+#            int main(int, char**) {
+#              void *ptr = &std::cout;
+#              void *exe = GetModuleHandle(NULL);
+#              PIMAGE_DOS_HEADER dosheader = (PIMAGE_DOS_HEADER)exe;
+#              PIMAGE_NT_HEADERS ntheader = (PIMAGE_NT_HEADERS)((BYTE *)dosheader + dosheader->e_lfanew);
+#              PIMAGE_OPTIONAL_HEADER peheader = &ntheader->OptionalHeader;
+#              void *exeend = (BYTE*)exe + peheader->SizeOfImage;
+#              if (ptr >= exe && ptr <= exeend)
+#                return 1;
+#              return 0;
+#            }
   Feature(name='linux', when=lambda cfg: '__linux__' in compilerMacros(cfg)),
   Feature(name='netbsd', when=lambda cfg: '__NetBSD__' in compilerMacros(cfg)),
   Feature(name='freebsd', when=lambda cfg: '__FreeBSD__' in compilerMacros(cfg))
Index: libcxx/test/support/test_macros.h
===================================================================
--- libcxx/test/support/test_macros.h
+++ libcxx/test/support/test_macros.h
@@ -306,8 +306,7 @@
 #define TEST_NOT_WIN32(...) __VA_ARGS__
 #endif
 
-#if (defined(_WIN32) && !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) ||   \
-    defined(__MVS__) || defined(_AIX)
+#if defined(TEST_WINDOWS_DLL) ||defined(__MVS__) || defined(_AIX)
 // Macros for waiving cases when we can't count allocations done within
 // the library implementation.
 //
@@ -325,8 +324,7 @@
 #define TEST_SUPPORTS_LIBRARY_INTERNAL_ALLOCATIONS 1
 #endif
 
-#if (defined(_WIN32) && !defined(_MSC_VER) &&                                  \
-     !defined(_LIBCPP_DISABLE_VISIBILITY_ANNOTATIONS)) ||                      \
+#if (defined(TEST_WINDOWS_DLL) && !defined(_MSC_VER)) ||                      \
     defined(__MVS__)
 // Normally, a replaced e.g. 'operator new' ends up used if the user code
 // does a call to e.g. 'operator new[]'; it's enough to replace the base


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D125922.430484.patch
Type: text/x-patch
Size: 2999 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220518/59ced5c0/attachment.bin>


More information about the libcxx-commits mailing list