<div dir="ltr">This patch seem to have two nearly-identical sections starting from a comment "<span class="gmail-il">Account</span> <span class="gmail-il">for</span> <span class="gmail-il">the</span> <span class="gmail-il">fact</span> <span class="gmail-il">that</span> <span class="gmail-il">on</span> <span class="gmail-il">windows</span>-<span class="gmail-il">msvc</span>"<div>Is it possible to account for any such fact just once? </div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Aug 30, 2018 at 8:55 AM Matt Morehouse via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: morehouse<br>
Date: Thu Aug 30 08:54:44 2018<br>
New Revision: 341082<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=341082&view=rev" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=341082&view=rev</a><br>
Log:<br>
[libFuzzer] Port to Windows<br>
<br>
Summary:<br>
Port libFuzzer to windows-msvc.<br>
This patch allows libFuzzer targets to be built and run on Windows, using -fsanitize=fuzzer and/or fsanitize=fuzzer-no-link. It allows these forms of coverage instrumentation to work on Windows as well.<br>
It does not fix all issues, such as those with -fsanitize-coverage=stack-depth, which is not usable on Windows as of this patch.<br>
It also does not fix any libFuzzer integration tests. Nearly all of them fail to compile, fixing them will come in a later patch, so libFuzzer tests are disabled on Windows until them.<br>
<br>
Patch By: metzman<br>
<br>
Reviewers: morehouse, rnk<br>
<br>
Reviewed By: morehouse, rnk<br>
<br>
Subscribers: #sanitizers, delcypher, morehouse, kcc, eraman<br>
<br>
Differential Revision: <a href="https://reviews.llvm.org/D51022" rel="noreferrer" target="_blank">https://reviews.llvm.org/D51022</a><br>
<br>
Added:<br>
    llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll<br>
Modified:<br>
    llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp<br>
<br>
Modified: llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp?rev=341082&r1=341081&r2=341082&view=diff" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp?rev=341082&r1=341081&r2=341082&view=diff</a><br>
==============================================================================<br>
--- llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp (original)<br>
+++ llvm/trunk/lib/Transforms/Instrumentation/SanitizerCoverage.cpp Thu Aug 30 08:54:44 2018<br>
@@ -273,9 +273,20 @@ Function *SanitizerCoverageModule::Creat<br>
   auto SecStart = SecStartEnd.first;<br>
   auto SecEnd = SecStartEnd.second;<br>
   Function *CtorFunc;<br>
+  Value *SecStartPtr = nullptr;<br>
+  // Account for the fact that on windows-msvc __start_* symbols actually<br>
+  // point to a uint64_t before the start of the array.<br>
+  if (TargetTriple.getObjectFormat() == Triple::COFF) {<br>
+    auto SecStartI8Ptr = IRB.CreatePointerCast(SecStart, Int8PtrTy);<br>
+    auto GEP = IRB.CreateGEP(SecStartI8Ptr,<br>
+                             ConstantInt::get(IntptrTy, sizeof(uint64_t)));<br>
+    SecStartPtr = IRB.CreatePointerCast(GEP, Ty);<br>
+  } else {<br>
+    SecStartPtr = IRB.CreatePointerCast(SecStart, Ty);<br>
+  }<br>
   std::tie(CtorFunc, std::ignore) = createSanitizerCtorAndInitFunctions(<br>
       M, SanCovModuleCtorName, InitFunctionName, {Ty, Ty},<br>
-      {IRB.CreatePointerCast(SecStart, Ty), IRB.CreatePointerCast(SecEnd, Ty)});<br>
+      {SecStartPtr, IRB.CreatePointerCast(SecEnd, Ty)});<br>
<br>
   if (TargetTriple.supportsCOMDAT()) {<br>
     // Use comdat to dedup CtorFunc.<br>
@@ -397,9 +408,20 @@ bool SanitizerCoverageModule::runOnModul<br>
     Function *InitFunction = declareSanitizerInitFunction(<br>
         M, SanCovPCsInitName, {IntptrPtrTy, IntptrPtrTy});<br>
     IRBuilder<> IRBCtor(Ctor->getEntryBlock().getTerminator());<br>
-    IRBCtor.CreateCall(InitFunction,<br>
-                       {IRB.CreatePointerCast(SecStartEnd.first, IntptrPtrTy),<br>
-                        IRB.CreatePointerCast(SecStartEnd.second, IntptrPtrTy)});<br>
+    Value *SecStartPtr = nullptr;<br>
+    // Account for the fact that on windows-msvc __start_pc_table actually<br>
+    // points to a uint64_t before the start of the PC table.<br>
+    if (TargetTriple.getObjectFormat() == Triple::COFF) {<br>
+      auto SecStartI8Ptr = IRB.CreatePointerCast(SecStartEnd.first, Int8PtrTy);<br>
+      auto GEP = IRB.CreateGEP(SecStartI8Ptr,<br>
+                               ConstantInt::get(IntptrTy, sizeof(uint64_t)));<br>
+      SecStartPtr = IRB.CreatePointerCast(GEP, IntptrPtrTy);<br>
+    } else {<br>
+      SecStartPtr = IRB.CreatePointerCast(SecStartEnd.first, IntptrPtrTy);<br>
+    }<br>
+    IRBCtor.CreateCall(<br>
+        InitFunction,<br>
+        {SecStartPtr, IRB.CreatePointerCast(SecStartEnd.second, IntptrPtrTy)});<br>
   }<br>
   // We don't reference these arrays directly in any of our runtime functions,<br>
   // so we need to prevent them from being dead stripped.<br>
@@ -809,8 +831,13 @@ void SanitizerCoverageModule::InjectCove<br>
<br>
 std::string<br>
 SanitizerCoverageModule::getSectionName(const std::string &Section) const {<br>
-  if (TargetTriple.getObjectFormat() == Triple::COFF)<br>
-    return ".SCOV$M";<br>
+  if (TargetTriple.getObjectFormat() == Triple::COFF) {<br>
+    if (Section == SanCovCountersSectionName)<br>
+      return ".SCOV$CM";<br>
+    if (Section == SanCovPCsSectionName)<br>
+      return ".SCOVP$M";<br>
+    return ".SCOV$GM"; // For SanCovGuardsSectionName.<br>
+  }<br>
   if (TargetTriple.isOSBinFormatMachO())<br>
     return "__DATA,__" + Section;<br>
   return "__" + Section;<br>
<br>
Added: llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll?rev=341082&view=auto" rel="noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll?rev=341082&view=auto</a><br>
==============================================================================<br>
--- llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll (added)<br>
+++ llvm/trunk/test/Instrumentation/SanitizerCoverage/coff-pc-table-inline-8bit-counters.ll Thu Aug 30 08:54:44 2018<br>
@@ -0,0 +1,12 @@<br>
+; Checks that the PC and 8-bit Counter Arrays are placed in their own sections in COFF binaries.<br>
+; RUN: opt < %s -sancov -sanitizer-coverage-level=1 -sanitizer-coverage-inline-8bit-counters=1 -sanitizer-coverage-pc-table=1 -S | FileCheck %s<br>
+target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"<br>
+target triple = "x86_64-pc-windows-msvc19.14.26433"<br>
+<br>
+define void @foo() {<br>
+entry:<br>
+  ret void<br>
+}<br>
+<br>
+; CHECK-DAG: section ".SCOV{{\$}}CM",<br>
+; CHECK-DAG: section ".SCOVP{{\$}}M",<br>
<br>
<br>
_______________________________________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits</a><br>
</blockquote></div>