[PATCH] D38702: [Analyzer] Do not segfault on unexpected call_once implementation

George Karpenkov via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Oct 9 13:03:18 PDT 2017


george.karpenkov created this revision.
Herald added subscribers: szepet, xazax.hun, javed.absar.

Fixes https://bugs.llvm.org/show_bug.cgi?id=30565

@dcoughlin Any advice on how to handle different stdlib implementations?
Can we conjure a separate symbol instead of relying on a particular struct layout?
For now this implementation will simply not go inside a differently implemented `call_once`.


https://reviews.llvm.org/D38702

Files:
  lib/Analysis/BodyFarm.cpp
  test/Analysis/call_once.cpp


Index: test/Analysis/call_once.cpp
===================================================================
--- test/Analysis/call_once.cpp
+++ test/Analysis/call_once.cpp
@@ -231,3 +231,12 @@
   int x = call_once();
   clang_analyzer_eval(x == 5); // expected-warning{{TRUE}}
 }
+
+namespace std {
+template <typename d, typename e>
+void call_once(d, e);
+}
+void g();
+void test_no_segfault_on_different_impl() {
+  std::call_once(g, false); // no-warning
+}
Index: lib/Analysis/BodyFarm.cpp
===================================================================
--- lib/Analysis/BodyFarm.cpp
+++ lib/Analysis/BodyFarm.cpp
@@ -362,6 +362,12 @@
                         /* GetNonReferenceType=*/true);
 
   CXXRecordDecl *FlagCXXDecl = FlagType->getAsCXXRecordDecl();
+  if (FlagCXXDecl == nullptr) {
+    DEBUG(llvm::dbgs() << "Flag field is not a CXX record: "
+                       << "unknown std::call_once implementation."
+                       << "Ignoring the call.\n");
+    return nullptr;
+  }
 
   // Note: here we are assuming libc++ implementation of call_once,
   // which has a struct with a field `__state_`.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38702.118258.patch
Type: text/x-patch
Size: 1124 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171009/782f10fd/attachment.bin>


More information about the cfe-commits mailing list