[llvm] 2417618 - [Verifier] Reject dllexport with non-default visibility

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 5 10:53:49 PDT 2022


Author: Fangrui Song
Date: 2022-09-05T10:53:41-07:00
New Revision: 2417618d5ca4b151908df09d8e3a00a49f029222

URL: https://github.com/llvm/llvm-project/commit/2417618d5ca4b151908df09d8e3a00a49f029222
DIFF: https://github.com/llvm/llvm-project/commit/2417618d5ca4b151908df09d8e3a00a49f029222.diff

LOG: [Verifier] Reject dllexport with non-default visibility

Add a visibility check for dllimport and dllexport. Note: dllimport with a
non-default visibility (implicit dso_local) is already rejected, but with a less
clear dso_local error.

The MC level visibility `MCSA_Exported` (D123951) is mapped from IR level
default visibility when dllexport is specified. The D123951 error is now very
difficult to trigger (needs to disable the IR verifier).

Reviewed By: mstorsjo

Differential Revision: https://reviews.llvm.org/D133267

Added: 
    llvm/test/Verifier/dllstorage.ll

Modified: 
    llvm/lib/IR/Verifier.cpp
    llvm/test/CodeGen/PowerPC/aix-xcoff-exported-nondefault.ll
    llvm/test/Feature/globalvars.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 004d2641a893..da19afa9d1bb 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -663,7 +663,13 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
   if (GV.isDeclarationForLinker())
     Check(!GV.hasComdat(), "Declaration may not be in a Comdat!", &GV);
 
+  if (GV.hasDLLExportStorageClass()) {
+    Check(GV.hasDefaultVisibility(),
+          "dllexport GlobalValue must have default visibility", &GV);
+  }
   if (GV.hasDLLImportStorageClass()) {
+    Check(GV.hasDefaultVisibility(),
+          "dllimport GlobalValue must have default visibility", &GV);
     Check(!GV.isDSOLocal(), "GlobalValue with DLLImport Storage is dso_local!",
           &GV);
 

diff  --git a/llvm/test/CodeGen/PowerPC/aix-xcoff-exported-nondefault.ll b/llvm/test/CodeGen/PowerPC/aix-xcoff-exported-nondefault.ll
index 7b1dc5c2500a..9fac28868edd 100644
--- a/llvm/test/CodeGen/PowerPC/aix-xcoff-exported-nondefault.ll
+++ b/llvm/test/CodeGen/PowerPC/aix-xcoff-exported-nondefault.ll
@@ -1,15 +1,5 @@
-; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff 2>&1 < %s | \
-; RUN:   FileCheck %s
-; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff 2>&1 < %s |\
-; RUN:   FileCheck %s
+; RUN: not llc -filetype=null -mtriple powerpc-ibm-aix-xcoff 2>&1 %s | FileCheck %s
+; RUN: not llc -filetype=null -mtriple powerpc64-ibm-aix-xcoff 2>&1 %s | FileCheck %s
 
-; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff \
-; RUN:     -filetype=obj -o %t.o < %s 2>&1 | \
-; RUN:   FileCheck %s
-
-; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff \
-; RUN:     -filetype=obj -o %t.o 2>&1 < %s 2>&1 | \
-; RUN:   FileCheck %s
-
-; CHECK: LLVM ERROR: Cannot not be both dllexport and non-default visibility
+; CHECK: dllexport GlobalValue must have default visibility
 @b_e = hidden dllexport global i32 0, align 4

diff  --git a/llvm/test/Feature/globalvars.ll b/llvm/test/Feature/globalvars.ll
index 99bb6071cde3..3e47650df7d0 100644
--- a/llvm/test/Feature/globalvars.ll
+++ b/llvm/test/Feature/globalvars.ll
@@ -16,5 +16,5 @@ define i32 @foo(i32 %blah) {
         ret i32 %blah
 }
 
- at 1 = hidden dllexport global i32 42
+ at 1 = default dllexport global i32 42
 @2 = dllexport global i32 42

diff  --git a/llvm/test/Verifier/dllstorage.ll b/llvm/test/Verifier/dllstorage.ll
new file mode 100644
index 000000000000..4791691c5214
--- /dev/null
+++ b/llvm/test/Verifier/dllstorage.ll
@@ -0,0 +1,18 @@
+; RUN: not opt -verify %s 2>&1 | FileCheck %s
+
+target datalayout = "e-m:w-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-pc-windows-gnu"
+
+; CHECK:      dllexport GlobalValue must have default visibility
+; CHECK-NEXT: ptr @dllexport_hidden
+declare hidden dllexport i32 @dllexport_hidden()
+; CHECK:      dllexport GlobalValue must have default visibility
+; CHECK-NEXT: ptr @dllexport_protected
+declare protected dllexport i32 @dllexport_protected()
+
+; CHECK:      dllimport GlobalValue must have default visibility
+; CHECK-NEXT: ptr @dllimport_hidden
+declare hidden dllimport i32 @dllimport_hidden()
+; CHECK:      dllimport GlobalValue must have default visibility
+; CHECK-NEXT: ptr @dllimport_protected
+declare protected dllimport i32 @dllimport_protected()


        


More information about the llvm-commits mailing list