r341572 - Re-land r334417 "[MS] Use mangled names and comdats for string merging with ASan"

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Thu Sep 6 11:25:39 PDT 2018


Author: rnk
Date: Thu Sep  6 11:25:39 2018
New Revision: 341572

URL: http://llvm.org/viewvc/llvm-project?rev=341572&view=rev
Log:
Re-land r334417 "[MS] Use mangled names and comdats for string merging with ASan"

The issue with -fprofile-generate was fixed and the dependent CL
relanded in r340232.

Added:
    cfe/trunk/test/CodeGen/asan-strings.c
Modified:
    cfe/trunk/lib/CodeGen/CodeGenModule.cpp

Modified: cfe/trunk/lib/CodeGen/CodeGenModule.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/CodeGen/CodeGenModule.cpp?rev=341572&r1=341571&r2=341572&view=diff
==============================================================================
--- cfe/trunk/lib/CodeGen/CodeGenModule.cpp (original)
+++ cfe/trunk/lib/CodeGen/CodeGenModule.cpp Thu Sep  6 11:25:39 2018
@@ -4305,15 +4305,13 @@ CodeGenModule::GetAddrOfConstantStringFr
   StringRef GlobalVariableName;
   llvm::GlobalValue::LinkageTypes LT;
 
-  // Mangle the string literal if the ABI allows for it.  However, we cannot
-  // do this if  we are compiling with ASan or -fwritable-strings because they
-  // rely on strings having normal linkage.
-  if (!LangOpts.WritableStrings &&
-      !LangOpts.Sanitize.has(SanitizerKind::Address) &&
-      getCXXABI().getMangleContext().shouldMangleStringLiteral(S)) {
+  // Mangle the string literal if that's how the ABI merges duplicate strings.
+  // Don't do it if they are writable, since we don't want writes in one TU to
+  // affect strings in another.
+  if (getCXXABI().getMangleContext().shouldMangleStringLiteral(S) &&
+      !LangOpts.WritableStrings) {
     llvm::raw_svector_ostream Out(MangledNameBuffer);
     getCXXABI().getMangleContext().mangleStringLiteral(S, Out);
-
     LT = llvm::GlobalValue::LinkOnceODRLinkage;
     GlobalVariableName = MangledNameBuffer;
   } else {

Added: cfe/trunk/test/CodeGen/asan-strings.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/asan-strings.c?rev=341572&view=auto
==============================================================================
--- cfe/trunk/test/CodeGen/asan-strings.c (added)
+++ cfe/trunk/test/CodeGen/asan-strings.c Thu Sep  6 11:25:39 2018
@@ -0,0 +1,17 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -fsanitize=address -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=LINUX
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fsanitize=address -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=WINDOWS
+// RUN: %clang_cc1 -triple x86_64-windows-msvc -fsanitize=address -fwritable-strings -disable-llvm-passes -emit-llvm -o - %s | FileCheck %s --check-prefix=WINWRITE
+
+// On Linux (and basically every non-MS target) string literals are emitted with
+// private linkage, which means ASan can freely instrument them. On Windows,
+// they are emitted with comdats. ASan's global instrumentation code for COFF
+// knows how to make the metadata comdat associative, so the string literal
+// global is only registered if the instrumented global prevails during linking.
+
+const char *foo() { return "asdf"; }
+
+// LINUX: @.str = private unnamed_addr constant [5 x i8] c"asdf\00", align 1
+
+// WINDOWS: @"??_C at _04JIHMPGLA@asdf?$AA@" = linkonce_odr dso_local unnamed_addr constant [5 x i8] c"asdf\00", comdat, align 1
+
+// WINWRITE: @.str = private unnamed_addr global [5 x i8] c"asdf\00", align 1




More information about the cfe-commits mailing list