Add a lint check for returning the address of stack memory.
authorDan Gohman <gohman@apple.com>
Fri, 28 May 2010 04:33:42 +0000 (04:33 +0000)
committerDan Gohman <gohman@apple.com>
Fri, 28 May 2010 04:33:42 +0000 (04:33 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@104936 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/Lint.cpp
test/Other/lint.ll

index a031cbc65b5bb6c3af3dac8f30548c37191ef1d5..8119debdaf1c62a3c1a337bef088097c994d9f50 100644 (file)
@@ -310,6 +310,12 @@ void Lint::visitReturnInst(ReturnInst &I) {
   Assert1(!F->doesNotReturn(),
           "Unusual: Return statement in function with noreturn attribute",
           &I);
+
+  if (Value *V = I.getReturnValue()) {
+    Value *Obj = V->getUnderlyingObject();
+    Assert1(!isa<AllocaInst>(Obj) && !isa<VAArgInst>(Obj),
+            "Unusual: Returning alloca or va_arg value", &I);
+  }
 }
 
 // TODO: Add a length argument and check that the reference is in bounds
index 1f9efe3ad9ad569784bf65dd62f3a31afa09fdef..d0d3c7e1863cd184717dfbb5e763a29fcccfc846 100644 (file)
@@ -97,3 +97,10 @@ define void @use_tail(i8* %valist) {
   tail call void @tailcallee(i8* %s)
   ret void
 }
+
+; CHECK: Unusual: Returning alloca or va_arg value
+define i8* @return_local(i32 %n, i32 %m) {
+  %t = alloca i8, i32 %n
+  %s = getelementptr i8* %t, i32 %m
+  ret i8* %s
+}