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

Dimitry Andric via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 8 08:28:34 PST 2016


dim created this revision.
dim added reviewers: chandlerc, craig.topper, rnk.
dim added subscribers: llvm-commits, emaste.

The LinkAllPasses.h file is included in several main programs, to force
a large number of passes to be linked in.  However, the ForcePassLinking
constructor uses undefined behavior, since it calls member functions on
`nullptr`, e.g.:

      ((llvm::Function*)nullptr)->viewCFGOnly();
      llvm::RGPassManager RGM;
      ((llvm::RegionPass*)nullptr)->runOnRegion((llvm::Region*)nullptr, RGM);

When the optimization level is -O2 or higher, the code below the first
nullptr dereference is optimized away, and replaced by `ud2` (on x86).

Therefore, the calls after that first dereference are never emitted.  In
my case, I noticed there was no call to `llvm::sys::RunningOnValgrind()`!

Replace these two instances of `nullptr` by `1`, which is ugly, but at
least defined, and accomplishes the goal of emitting the desired
references.

http://reviews.llvm.org/D15996

Files:
  include/llvm/LinkAllPasses.h

Index: include/llvm/LinkAllPasses.h
===================================================================
--- include/llvm/LinkAllPasses.h
+++ include/llvm/LinkAllPasses.h
@@ -185,9 +185,9 @@
 
       (void)new llvm::IntervalPartition();
       (void)new llvm::ScalarEvolutionWrapperPass();
-      ((llvm::Function*)nullptr)->viewCFGOnly();
+      ((llvm::Function*)1)->viewCFGOnly();
       llvm::RGPassManager RGM;
-      ((llvm::RegionPass*)nullptr)->runOnRegion((llvm::Region*)nullptr, RGM);
+      ((llvm::RegionPass*)1)->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.44330.patch
Type: text/x-patch
Size: 745 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20160108/762d77c3/attachment.bin>


More information about the llvm-commits mailing list