Importing rustc-1.56.0

Change-Id: I98941481270706fa55f8fb2cb91686ae3bd30f38
diff --git a/src/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp b/src/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp
index 4902be0..3809880 100644
--- a/src/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp
+++ b/src/llvm-project/compiler-rt/lib/sanitizer_common/symbolizer/sanitizer_symbolize.cpp
@@ -23,6 +23,16 @@
   return DefaultSymbolizer;
 }
 
+static llvm::symbolize::PrinterConfig getDefaultPrinterConfig() {
+  llvm::symbolize::PrinterConfig Config;
+  Config.Pretty = false;
+  Config.Verbose = false;
+  Config.PrintFunctions = true;
+  Config.PrintAddress = false;
+  Config.SourceContextLines = 0;
+  return Config;
+}
+
 namespace __sanitizer {
 int internal_snprintf(char *buffer, unsigned long length, const char *format,
                       ...);
@@ -38,19 +48,24 @@
   std::string Result;
   {
     llvm::raw_string_ostream OS(Result);
-    llvm::symbolize::DIPrinter Printer(OS);
+    llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig();
+    llvm::symbolize::Request Request{ModuleName, ModuleOffset};
+    auto Printer =
+        std::make_unique<llvm::symbolize::LLVMPrinter>(OS, OS, Config);
+
     // TODO: it is neccessary to set proper SectionIndex here.
     // object::SectionedAddress::UndefSection works for only absolute addresses.
     if (SymbolizeInlineFrames) {
       auto ResOrErr = getDefaultSymbolizer()->symbolizeInlinedCode(
           ModuleName,
           {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
-      Printer << (ResOrErr ? ResOrErr.get() : llvm::DIInliningInfo());
+      Printer->print(Request,
+                     ResOrErr ? ResOrErr.get() : llvm::DIInliningInfo());
     } else {
       auto ResOrErr = getDefaultSymbolizer()->symbolizeCode(
           ModuleName,
           {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
-      Printer << (ResOrErr ? ResOrErr.get() : llvm::DILineInfo());
+      Printer->print(Request, ResOrErr ? ResOrErr.get() : llvm::DILineInfo());
     }
   }
   return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
@@ -61,14 +76,18 @@
                                 char *Buffer, int MaxLength) {
   std::string Result;
   {
+    llvm::symbolize::PrinterConfig Config = getDefaultPrinterConfig();
     llvm::raw_string_ostream OS(Result);
-    llvm::symbolize::DIPrinter Printer(OS);
+    llvm::symbolize::Request Request{ModuleName, ModuleOffset};
+    auto Printer =
+        std::make_unique<llvm::symbolize::LLVMPrinter>(OS, OS, Config);
+
     // TODO: it is neccessary to set proper SectionIndex here.
     // object::SectionedAddress::UndefSection works for only absolute addresses.
     auto ResOrErr = getDefaultSymbolizer()->symbolizeData(
         ModuleName,
         {ModuleOffset, llvm::object::SectionedAddress::UndefSection});
-    Printer << (ResOrErr ? ResOrErr.get() : llvm::DIGlobal());
+    Printer->print(Request, ResOrErr ? ResOrErr.get() : llvm::DIGlobal());
   }
   return __sanitizer::internal_snprintf(Buffer, MaxLength, "%s",
                                         Result.c_str()) < MaxLength;
@@ -86,4 +105,32 @@
              : 0;
 }
 
+// Override __cxa_atexit and ignore callbacks.
+// This prevents crashes in a configuration when the symbolizer
+// is built into sanitizer runtime and consequently into the test process.
+// LLVM libraries have some global objects destroyed during exit,
+// so if the test process triggers any bugs after that, the symbolizer crashes.
+// An example stack trace of such crash:
+//
+// #1  __cxa_throw
+// #2  std::__u::__throw_system_error
+// #3  std::__u::recursive_mutex::lock
+// #4  __sanitizer_llvm::ManagedStaticBase::RegisterManagedStatic
+// #5  __sanitizer_llvm::errorToErrorCode
+// #6  __sanitizer_llvm::getFileAux
+// #7  __sanitizer_llvm::MemoryBuffer::getFileOrSTDIN
+// #10 __sanitizer_llvm::symbolize::LLVMSymbolizer::getOrCreateModuleInfo
+// #13 __sanitizer::Symbolizer::SymbolizeData
+// #14 __tsan::SymbolizeData
+// #16 __tsan::ReportRace
+// #18 __tsan_write4
+// #19 race() () at test/tsan/atexit4.cpp
+// #20 cxa_at_exit_wrapper
+// #21 __cxa_finalize
+// #22 __do_fini
+//
+// For the standalone llvm-symbolizer this does not hurt,
+// we just don't destroy few global objects on exit.
+int __cxa_atexit(void (*f)(void *a), void *arg, void *dso) { return 0; }
+
 }  // extern "C"