zoecarver created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.
zoecarver added reviewers: kparzysz, eli.friedman.
zoecarver marked an inline comment as done.
zoecarver added inline comments.


================
Comment at: clang/test/Sema/builtin-stackaddress.c:10
+// expected-error@+1 {{argument to '__builtin_return_address' must be a 
constant integer}}
+return __builtin_return_address(x);
 }
----------------
For some reason `lit` was still erroring here. 


This patch checks that an argument passed to either of the builtins 
`__builtin_frame_address` or `__builtin_return_address` is at least 0. This 
patch resolves the issue where these builtins would cause an infinite loop ( 
25497 <https://bugs.llvm.org/show_bug.cgi?id=25497> ).


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D66839

Files:
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/test/Sema/builtin-stackaddress.c


Index: clang/test/Sema/builtin-stackaddress.c
===================================================================
--- clang/test/Sema/builtin-stackaddress.c
+++ clang/test/Sema/builtin-stackaddress.c
@@ -1,16 +1,30 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 %s
+
 void* a(unsigned x) {
 return __builtin_return_address(0);
 }
 
 void b(unsigned x) {
-return __builtin_return_address(x); // expected-error{{argument to 
'__builtin_return_address' must be a constant integer}}
+// TODO: lit doesn't catch this
+// expected-error@+1 {{argument to '__builtin_return_address' must be a 
constant integer}}
+return __builtin_return_address(x);
 }
 
 void* c(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_return_address' must be at least 
0}}
+return __builtin_return_address(-1);
+}
+
+void* d(unsigned x) {
 return __builtin_frame_address(0);
 }
 
-void d(unsigned x) {
-return __builtin_frame_address(x); // expected-error{{argument to 
'__builtin_frame_address' must be a constant integer}}
+void e(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_frame_address' must be a 
constant integer}}
+return __builtin_frame_address(x);
+}
+
+void* f(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_frame_address' must be at least 
0}}
+return __builtin_frame_address(-1);
 }
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -2568,6 +2568,14 @@
   case Builtin::BI__builtin_return_address: {
     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
                                                    getContext().UnsignedIntTy);
+    llvm::ConstantInt* DepthConstVal = dyn_cast<llvm::ConstantInt>(Depth);
+    if (!DepthConstVal)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_return_address' must be a 
constant integer");
+    if (DepthConstVal->getSExtValue() < 0)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_return_address' must be 
at least 0");
+
     Function *F = CGM.getIntrinsic(Intrinsic::returnaddress);
     return RValue::get(Builder.CreateCall(F, Depth));
   }
@@ -2578,6 +2586,14 @@
   case Builtin::BI__builtin_frame_address: {
     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
                                                    getContext().UnsignedIntTy);
+    llvm::ConstantInt* DepthConstVal = dyn_cast<llvm::ConstantInt>(Depth);
+    if (!DepthConstVal)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_frame_address' must be a 
constant integer");
+    if (DepthConstVal->getSExtValue() < 0)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_frame_address' must be at 
least 0");
+
     Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
     return RValue::get(Builder.CreateCall(F, Depth));
   }


Index: clang/test/Sema/builtin-stackaddress.c
===================================================================
--- clang/test/Sema/builtin-stackaddress.c
+++ clang/test/Sema/builtin-stackaddress.c
@@ -1,16 +1,30 @@
-// RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 %s
+
 void* a(unsigned x) {
 return __builtin_return_address(0);
 }
 
 void b(unsigned x) {
-return __builtin_return_address(x); // expected-error{{argument to '__builtin_return_address' must be a constant integer}}
+// TODO: lit doesn't catch this
+// expected-error@+1 {{argument to '__builtin_return_address' must be a constant integer}}
+return __builtin_return_address(x);
 }
 
 void* c(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_return_address' must be at least 0}}
+return __builtin_return_address(-1);
+}
+
+void* d(unsigned x) {
 return __builtin_frame_address(0);
 }
 
-void d(unsigned x) {
-return __builtin_frame_address(x); // expected-error{{argument to '__builtin_frame_address' must be a constant integer}}
+void e(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_frame_address' must be a constant integer}}
+return __builtin_frame_address(x);
+}
+
+void* f(unsigned x) {
+// expected-error@+1 {{argument to '__builtin_frame_address' must be at least 0}}
+return __builtin_frame_address(-1);
 }
Index: clang/lib/CodeGen/CGBuiltin.cpp
===================================================================
--- clang/lib/CodeGen/CGBuiltin.cpp
+++ clang/lib/CodeGen/CGBuiltin.cpp
@@ -2568,6 +2568,14 @@
   case Builtin::BI__builtin_return_address: {
     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
                                                    getContext().UnsignedIntTy);
+    llvm::ConstantInt* DepthConstVal = dyn_cast<llvm::ConstantInt>(Depth);
+    if (!DepthConstVal)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_return_address' must be a constant integer");
+    if (DepthConstVal->getSExtValue() < 0)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_return_address' must be at least 0");
+
     Function *F = CGM.getIntrinsic(Intrinsic::returnaddress);
     return RValue::get(Builder.CreateCall(F, Depth));
   }
@@ -2578,6 +2586,14 @@
   case Builtin::BI__builtin_frame_address: {
     Value *Depth = ConstantEmitter(*this).emitAbstract(E->getArg(0),
                                                    getContext().UnsignedIntTy);
+    llvm::ConstantInt* DepthConstVal = dyn_cast<llvm::ConstantInt>(Depth);
+    if (!DepthConstVal)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_frame_address' must be a constant integer");
+    if (DepthConstVal->getSExtValue() < 0)
+        CGM.ErrorUnsupported(E,
+                             "argument to '__builtin_frame_address' must be at least 0");
+
     Function *F = CGM.getIntrinsic(Intrinsic::frameaddress, AllocaInt8PtrTy);
     return RValue::get(Builder.CreateCall(F, Depth));
   }
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to