Skip to content

Commit 72aa619

Browse files
committed
Warn of uninitialized variables on asm goto's indirect branch
Summary: Outputs from an asm goto block cannot be used on the indirect branch. It's not supported and may result in invalid code generation. Reviewers: jyknight, nickdesaulniers, hfinkel Reviewed By: nickdesaulniers Subscribers: martong, cfe-commits, rnk, craig.topper, hiraditya, rsmith Tags: #clang Differential Revision: https://reviews.llvm.org/D71314
1 parent 445195b commit 72aa619

File tree

2 files changed

+78
-3
lines changed

2 files changed

+78
-3
lines changed

clang/lib/Analysis/UninitializedValues.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,28 @@ class TransferFunctions : public StmtVisitor<TransferFunctions> {
576576
continue;
577577
}
578578

579+
if (AtPredExit == MayUninitialized) {
580+
// If the predecessor's terminator is an "asm goto" that initializes
581+
// the variable, then it won't be counted as "initialized" on the
582+
// non-fallthrough paths.
583+
CFGTerminator term = Pred->getTerminator();
584+
if (const auto *as = dyn_cast_or_null<GCCAsmStmt>(term.getStmt())) {
585+
const CFGBlock *fallthrough = *Pred->succ_begin();
586+
if (as->isAsmGoto() &&
587+
llvm::any_of(as->outputs(), [&](const Expr *output) {
588+
return vd == findVar(output).getDecl() &&
589+
llvm::any_of(as->labels(),
590+
[&](const AddrLabelExpr *label) {
591+
return label->getLabel()->getStmt() == B->Label &&
592+
B != fallthrough;
593+
});
594+
})) {
595+
Use.setUninitAfterDecl();
596+
continue;
597+
}
598+
}
599+
}
600+
579601
unsigned &SV = SuccsVisited[Pred->getBlockID()];
580602
if (!SV) {
581603
// When visiting the first successor of a block, mark all NULL
@@ -768,7 +790,11 @@ void TransferFunctions::VisitGCCAsmStmt(GCCAsmStmt *as) {
768790

769791
for (const Expr *o : as->outputs())
770792
if (const VarDecl *VD = findVar(o).getDecl())
771-
vals[VD] = Initialized;
793+
if (vals[VD] != Initialized)
794+
// If the variable isn't initialized by the time we get here, then we
795+
// mark it as potentially uninitialized for those cases where it's used
796+
// on an indirect path, where it's not guaranteed to be defined.
797+
vals[VD] = MayUninitialized;
772798
}
773799

774800
void TransferFunctions::VisitObjCMessageExpr(ObjCMessageExpr *ME) {
@@ -809,7 +835,7 @@ static bool runOnBlock(const CFGBlock *block, const CFG &cfg,
809835
tf.Visit(const_cast<Stmt *>(cs->getStmt()));
810836
}
811837
CFGTerminator terminator = block->getTerminator();
812-
if (GCCAsmStmt *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt()))
838+
if (auto *as = dyn_cast_or_null<GCCAsmStmt>(terminator.getStmt()))
813839
if (as->isAsmGoto())
814840
tf.Visit(as);
815841
return vals.updateValueVectorWithScratch(block);
Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,59 @@
11
// RUN: %clang_cc1 -std=c++11 -Wuninitialized -verify %s
2-
// expected-no-diagnostics
32

3+
// test1: Expect no diagnostics
44
int test1(int x) {
55
int y;
66
asm goto("nop" : "=r"(y) : "r"(x) : : err);
77
return y;
88
err:
99
return -1;
1010
}
11+
12+
int test2(int x) {
13+
int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}} \
14+
// expected-note {{initialize the variable}}
15+
if (x < 42)
16+
asm volatile goto("testl %0, %0; testl %1, %2; jne %l3" : "+S"(x), "+D"(y) : "r"(x) :: indirect_1, indirect_2);
17+
else
18+
asm volatile goto("testl %0, %1; testl %2, %3; jne %l5" : "+S"(x), "+D"(y) : "r"(x), "r"(y) :: indirect_1, indirect_2);
19+
return x + y;
20+
indirect_1:
21+
return -42;
22+
indirect_2:
23+
return y; // expected-note {{uninitialized use occurs here}}
24+
}
25+
26+
int test3(int x) {
27+
int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}} \
28+
// expected-note {{initialize the variable}}
29+
asm goto("xorl %1, %0; jmp %l2" : "=&r"(y) : "r"(x) : : fail);
30+
normal:
31+
y += x;
32+
return y;
33+
if (x) {
34+
fail:
35+
return y; // expected-note {{uninitialized use occurs here}}
36+
}
37+
return 0;
38+
}
39+
40+
int test4(int x) {
41+
int y; // expected-warning {{variable 'y' is used uninitialized whenever its declaration is reached}} \
42+
// expected-note {{initialize the variable}}
43+
goto forward;
44+
backward:
45+
return y; // expected-note {{uninitialized use occurs here}}
46+
forward:
47+
asm goto("# %0 %1 %2" : "=r"(y) : "r"(x) : : backward);
48+
return y;
49+
}
50+
51+
// test5: Expect no diagnostics
52+
int test5(int x) {
53+
int y;
54+
asm volatile goto("testl %0, %0; testl %1, %2; jne %l3" : "+S"(x), "+D"(y) : "r"(x) :: indirect, fallthrough);
55+
fallthrough:
56+
return y;
57+
indirect:
58+
return -2;
59+
}

0 commit comments

Comments
 (0)