[llvm] r286647 - Fix static initialization order fiasco in MCTests

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 11 14:18:43 PST 2016


Author: mehdi_amini
Date: Fri Nov 11 16:18:42 2016
New Revision: 286647

URL: http://llvm.org/viewvc/llvm-project?rev=286647&view=rev
Log:
Fix static initialization order fiasco in MCTests

Reported by Kostya on llvm-dev, uncovered by an ASAN bot

Modified:
    llvm/trunk/unittests/MC/DwarfLineTables.cpp

Modified: llvm/trunk/unittests/MC/DwarfLineTables.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/MC/DwarfLineTables.cpp?rev=286647&r1=286646&r2=286647&view=diff
==============================================================================
--- llvm/trunk/unittests/MC/DwarfLineTables.cpp (original)
+++ llvm/trunk/unittests/MC/DwarfLineTables.cpp Fri Nov 11 16:18:42 2016
@@ -46,21 +46,25 @@ struct Context {
   operator MCContext &() { return *Ctx; };
 };
 
-Context Ctxt;
+Context &getContext() {
+  static Context Ctxt;
+  return Ctxt;
+}
 }
 
 void verifyEncoding(MCDwarfLineTableParams Params, int LineDelta, int AddrDelta,
                     ArrayRef<uint8_t> ExpectedEncoding) {
   SmallString<16> Buffer;
   raw_svector_ostream EncodingOS(Buffer);
-  MCDwarfLineAddr::Encode(Ctxt, Params, LineDelta, AddrDelta, EncodingOS);
+  MCDwarfLineAddr::Encode(getContext(), Params, LineDelta, AddrDelta,
+                          EncodingOS);
   ArrayRef<uint8_t> Encoding(reinterpret_cast<uint8_t *>(Buffer.data()),
                              Buffer.size());
   EXPECT_EQ(ExpectedEncoding, Encoding);
 }
 
 TEST(DwarfLineTables, TestDefaultParams) {
-  if (!Ctxt)
+  if (!getContext())
     return;
 
   MCDwarfLineTableParams Params;
@@ -110,7 +114,7 @@ TEST(DwarfLineTables, TestDefaultParams)
 }
 
 TEST(DwarfLineTables, TestCustomParams) {
-  if (!Ctxt)
+  if (!getContext())
     return;
 
   // Some tests against the example values given in the standard.
@@ -164,7 +168,7 @@ TEST(DwarfLineTables, TestCustomParams)
 }
 
 TEST(DwarfLineTables, TestCustomParams2) {
-  if (!Ctxt)
+  if (!getContext())
     return;
 
   // Corner case param values.




More information about the llvm-commits mailing list