r315750 - [analyzer] pr28449: Fix support for various array initializers.

Artem Dergachev via cfe-commits cfe-commits at lists.llvm.org
Fri Oct 13 13:54:56 PDT 2017


Author: dergachev
Date: Fri Oct 13 13:54:56 2017
New Revision: 315750

URL: http://llvm.org/viewvc/llvm-project?rev=315750&view=rev
Log:
[analyzer] pr28449: Fix support for various array initializers.

In some cases the analyzer didn't expect an array-type variable to be
initialized with anything other than a string literal. The patch essentially
removes the assertion, and ensures relatively sane behavior.

There is a bigger problem with these initializers. Currently our memory model
(RegionStore) is being ordered to initialize the array with a region that
is assumed to be storing the initializer rvalue, and it guesses to copy
the contents of that region to the array variable. However, it would make
more sense for RegionStore to receive the correct initializer in the first
place. This problem isn't addressed with this patch.

rdar://problem/27248428
Differential Revision: https://reviews.llvm.org/D23963

Added:
    cfe/trunk/test/Analysis/compound-literals.c
    cfe/trunk/test/Analysis/objc-encode.m
Modified:
    cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp

Modified: cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp?rev=315750&r1=315749&r2=315750&view=diff
==============================================================================
--- cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp (original)
+++ cfe/trunk/lib/StaticAnalyzer/Core/RegionStore.cpp Fri Oct 13 13:54:56 2017
@@ -2088,15 +2088,12 @@ RegionStoreManager::bindArray(RegionBind
   if (const ConstantArrayType* CAT = dyn_cast<ConstantArrayType>(AT))
     Size = CAT->getSize().getZExtValue();
 
-  // Check if the init expr is a string literal.
+  // Check if the init expr is a literal. If so, bind the rvalue instead.
+  // FIXME: It's not responsibility of the Store to transform this lvalue
+  // to rvalue. ExprEngine or maybe even CFG should do this before binding.
   if (Optional<loc::MemRegionVal> MRV = Init.getAs<loc::MemRegionVal>()) {
-    const StringRegion *S = cast<StringRegion>(MRV->getRegion());
-
-    // Treat the string as a lazy compound value.
-    StoreRef store(B.asStore(), *this);
-    nonloc::LazyCompoundVal LCV = svalBuilder.makeLazyCompoundVal(store, S)
-        .castAs<nonloc::LazyCompoundVal>();
-    return bindAggregate(B, R, LCV);
+    SVal V = getBinding(B.asStore(), *MRV, R->getValueType());
+    return bindAggregate(B, R, V);
   }
 
   // Handle lazy compound values.

Added: cfe/trunk/test/Analysis/compound-literals.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/compound-literals.c?rev=315750&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/compound-literals.c (added)
+++ cfe/trunk/test/Analysis/compound-literals.c Fri Oct 13 13:54:56 2017
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple=i386-apple-darwin10 -analyze -analyzer-checker=debug.ExprInspection -verify %s
+void clang_analyzer_eval(int);
+
+// pr28449: Used to crash.
+void foo(void) {
+  static const unsigned short array[] = (const unsigned short[]){0x0F00};
+  // FIXME: Should be true.
+  clang_analyzer_eval(array[0] == 0x0F00); // expected-warning{{UNKNOWN}}
+}

Added: cfe/trunk/test/Analysis/objc-encode.m
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Analysis/objc-encode.m?rev=315750&view=auto
==============================================================================
--- cfe/trunk/test/Analysis/objc-encode.m (added)
+++ cfe/trunk/test/Analysis/objc-encode.m Fri Oct 13 13:54:56 2017
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -analyze -analyzer-checker=debug.ExprInspection -verify %s
+// expected-no-diagnostics
+
+void clang_analyzer_eval(int);
+
+// rdar://problem/34831581: Used to crash.
+void foo(void) {
+  char buf1[] = @encode(int **);
+}




More information about the cfe-commits mailing list