[PATCH] D71314: Emit a warning if a variable is uninitialized in indirect ASM goto destination.

Bill Wendling via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue Dec 10 16:18:08 PST 2019


void created this revision.
void added reviewers: jyknight, nickdesaulniers, hfinkel.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D71314

Files:
  clang/lib/Analysis/UninitializedValues.cpp
  clang/test/Analysis/uninit-asm-goto.cpp


Index: clang/test/Analysis/uninit-asm-goto.cpp
===================================================================
--- clang/test/Analysis/uninit-asm-goto.cpp
+++ clang/test/Analysis/uninit-asm-goto.cpp
@@ -1,6 +1,6 @@
 // RUN: %clang_cc1 -std=c++11 -Wuninitialized -verify %s
-// expected-no-diagnostics
 
+// test1: Expect no diagnostics
 int test1(int x) {
     int y;
     asm goto("# %0 %1 %2" : "=r"(y) : "r"(x) : : err);
@@ -8,3 +8,31 @@
   err:
     return -1;
 }
+
+int test2(int x) {
+  int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}} \
+         // expected-note {{initialize the variable}}
+  if (x < 42)
+    asm volatile goto("testl %0, %0; testl %1, %2; jne %l3" : "+S"(x), "+D"(y) : "r"(x) :: indirect_1, indirect_2);
+  else
+    asm volatile goto("testl %0, %1; testl %2, %3; jne %l5" : "+S"(x), "+D"(y) : "r"(x), "r"(y) :: indirect_1, indirect_2);
+  return x + y;
+indirect_1:
+  return -42;
+indirect_2:
+  return y; // expected-note {{uninitialized use occurs here}}
+}
+
+int foo(int x) {
+  int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}} \
+         // expected-note {{initialize the variable}}
+  asm goto("xorl %1, %0; jmp %l2" : "=&r"(y) : "r"(x) : : fail);
+normal:
+  y += x;
+  return y;
+  if (x) {
+fail:
+    return y; // expected-note {{uninitialized use occurs here}}
+  }
+  return 0;
+}
Index: clang/lib/Analysis/UninitializedValues.cpp
===================================================================
--- clang/lib/Analysis/UninitializedValues.cpp
+++ clang/lib/Analysis/UninitializedValues.cpp
@@ -637,6 +637,34 @@
           continue;
         }
 
+        if (AtPredExit == MayUninitialized) {
+          // If the predecessor's terminator is an "asm goto" that initializes
+          // the variable, then it won't be counted as "initialized" on the
+          // non-fallthrough paths.
+          CFGTerminator terminator = Pred->getTerminator();
+          if (GCCAsmStmt *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt()))
+            if (as->isAsmGoto()) {
+              bool uninitedAfterDecl = false;
+              for (const auto &o : as->outputs()) {
+                if (vd != findVar(o).getDecl())
+                  continue;
+                for (const auto &label : as->labels()) {
+                  const LabelStmt *ls = label->getLabel()->getStmt();
+                  if (ls == B->Label) {
+                    uninitedAfterDecl = true;
+                    break;
+                  }
+                }
+                if (uninitedAfterDecl)
+                  break;
+              }
+              if (uninitedAfterDecl) {
+                Use.setUninitAfterDecl();
+                continue;
+              }
+            }
+        }
+
         unsigned &SV = SuccsVisited[Pred->getBlockID()];
         if (!SV) {
           // When visiting the first successor of a block, mark all NULL
@@ -829,7 +857,8 @@
 
   for (const auto &o : as->outputs())
     if (const VarDecl *VD = findVar(o).getDecl())
-      vals[VD] = Initialized;
+      if (vals[VD] != Initialized)
+        vals[VD] = MayUninitialized;
 }
 
 void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D71314.233221.patch
Type: text/x-patch
Size: 3266 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20191211/473239c2/attachment.bin>


More information about the cfe-commits mailing list