[PATCH] D15996: Avoid undefined behavior in LinkAllPasses.h

Dimitry Andric via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 11 13:23:55 PST 2016


dim updated this revision to Diff 44544.
dim added a comment.

Trying to make both calls defined:

We can use `llvm::Function::Create()` to give us a nice, valid `Function` pointer.

For the `llvm::RegionPass` part, the problem is that there is only one descendant of this class in the tree, and it isn't exposed via a header.  So create a bogus descendant, and let it call `llvm::RegionPass::runOnRegion()`.


http://reviews.llvm.org/D15996

Files:
  include/llvm/LinkAllPasses.h

Index: include/llvm/LinkAllPasses.h
===================================================================
--- include/llvm/LinkAllPasses.h
+++ include/llvm/LinkAllPasses.h
@@ -46,6 +46,12 @@
 #include <cstdlib>
 
 namespace {
+  struct BogusRegionPass : public llvm::RegionPass {
+    explicit BogusRegionPass(char c) : RegionPass(c) {}
+    bool runOnRegion(llvm::Region *R, llvm::RGPassManager &RGM) override {
+      return RegionPass::runOnRegion(R, RGM);
+    }
+  };
   struct ForcePassLinking {
     ForcePassLinking() {
       // We must reference the passes in such a way that compilers will not
@@ -186,9 +192,9 @@
 
       (void)new llvm::IntervalPartition();
       (void)new llvm::ScalarEvolutionWrapperPass();
-      ((llvm::Function*)nullptr)->viewCFGOnly();
+      llvm::Function::Create(nullptr, llvm::GlobalValue::ExternalLinkage)->viewCFGOnly();
       llvm::RGPassManager RGM;
-      ((llvm::RegionPass*)nullptr)->runOnRegion((llvm::Region*)nullptr, RGM);
+      (new BogusRegionPass(0))->runOnRegion((llvm::Region*)nullptr, RGM);
       llvm::AliasSetTracker X(*(llvm::AliasAnalysis*)nullptr);
       X.add(nullptr, 0, llvm::AAMDNodes()); // for -print-alias-sets
       (void) llvm::AreStatisticsEnabled();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D15996.44544.patch
Type: text/x-patch
Size: 1227 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160111/92f0d4a5/attachment.bin>


More information about the llvm-commits mailing list