[PATCH] D58665: [analyzer] Handle comparison between non-default AS symbol and constant

David Stenberg via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Feb 26 01:25:47 PST 2019

dstenb created this revision.
dstenb added reviewers: NoQ, zaks.anna, george.karpenkov.
Herald added subscribers: cfe-commits, Charusso, jdoerfert, dkrupp, donat.nagy, Szelethus, mikhail.ramalho, a.sidorin, szepet, baloghadamsoftware, xazax.hun.
Herald added a project: clang.

When comparing a symbolic region and a constant, the constant would be
widened or truncated to the width of a void pointer, meaning that the
constant could be incorrectly truncated when handling symbols for
non-default address spaces. In the attached test case this resulted in a
false positive since the constant was truncated to zero. To fix this,
widen/truncate the constant to the width of the symbol expression's

This commit does not consider non-symbolic regions as I'm not sure how
to generalize getting the type there.

This fixes PR40814.

  rC Clang



Index: test/Analysis/ptr-cmp-const-trunc.cl
--- /dev/null
+++ test/Analysis/ptr-cmp-const-trunc.cl
@@ -0,0 +1,11 @@
+//RUN: %clang_analyze_cc1 -triple amdgcn-unknown-unknown -analyze -analyzer-checker=core -verify %s
+// expected-no-diagnostics
+#include <stdint.h>
+void bar(__global int *p) __attribute__((nonnull(1)));
+void foo(__global int *p) {
+  if ((uint64_t)p <= 1UL << 32)
+    bar(p);
Index: lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
--- lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
+++ lib/StaticAnalyzer/Core/SimpleSValBuilder.cpp
@@ -571,7 +571,11 @@
           // add 1 to a LocAsInteger, we'd better unpack the Loc and add to it,
           // then pack it back into a LocAsInteger.
           llvm::APSInt i = rhs.castAs<nonloc::ConcreteInt>().getValue();
-          BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i);
+          // FIXME: Handle non-default address spaces for non-symbolic regions.
+          if (SymbolRef lSym = lhs.getAsLocSymbol(true))
+            BasicVals.getAPSIntType(lSym->getType()).apply(i);
+          else
+            BasicVals.getAPSIntType(Context.VoidPtrTy).apply(i);
           return evalBinOpLL(state, op, lhsL, makeLoc(i), resultTy);

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58665.188320.patch
Type: text/x-patch
Size: 1372 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190226/034060e7/attachment.bin>

More information about the cfe-commits mailing list