Skip to content

Commit 9d045a5

Browse files
committed
[Sema] add -Walloca to flag uses of alloca
This CL adds an optional warning to diagnose uses of the `__builtin_alloca` family of functions. The use of these functions is discouraged by many, so it seems like a good idea to allow clang to warn about it. Patch by Elaina Guan! Differential Revision: https://reviews.llvm.org/D64883 llvm-svn: 367067
1 parent 29af3b4 commit 9d045a5

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,11 @@ def err_no_accessor_for_property : Error<
27792779
def err_cannot_find_suitable_accessor : Error<
27802780
"cannot find suitable %select{getter|setter}0 for property %1">;
27812781

2782+
def warn_alloca : Warning<
2783+
"use of function %0 is discouraged; there is no way to check for failure but "
2784+
"failure may still occur, resulting in a possibly exploitable security vulnerability">,
2785+
InGroup<DiagGroup<"alloca">>, DefaultIgnore;
2786+
27822787
def warn_alloca_align_alignof : Warning<
27832788
"second argument to __builtin_alloca_with_align is supposed to be in bits">,
27842789
InGroup<DiagGroup<"alloca-with-align-alignof">>;

clang/lib/Sema/SemaChecking.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,6 +1179,10 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
11791179
case Builtin::BI__builtin_alloca_with_align:
11801180
if (SemaBuiltinAllocaWithAlign(TheCall))
11811181
return ExprError();
1182+
LLVM_FALLTHROUGH;
1183+
case Builtin::BI__builtin_alloca:
1184+
Diag(TheCall->getBeginLoc(), diag::warn_alloca)
1185+
<< TheCall->getDirectCallee();
11821186
break;
11831187
case Builtin::BI__assume:
11841188
case Builtin::BI__builtin_assume:

clang/test/Sema/warn-alloca.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -DSILENCE -fsyntax-only -verify -Wall %s
2+
// RUN: %clang_cc1 -fsyntax-only -verify -Walloca %s
3+
4+
#ifdef SILENCE
5+
// expected-no-diagnostics
6+
#endif
7+
8+
void test1(int a) {
9+
__builtin_alloca(a);
10+
#ifndef SILENCE
11+
// expected-warning@-2 {{use of function '__builtin_alloca' is discouraged; there is no way to check for failure but failure may still occur, resulting in a possibly exploitable security vulnerability}}
12+
#endif
13+
}
14+
15+
void test2(int a) {
16+
__builtin_alloca_with_align(a, 32);
17+
#ifndef SILENCE
18+
// expected-warning@-2 {{use of function '__builtin_alloca_with_align' is discouraged; there is no way to check for failure but failure may still occur, resulting in a possibly exploitable security vulnerability}}
19+
#endif
20+
}

0 commit comments

Comments
 (0)