Skip to content

Commit c93112d

Browse files
committed
Validate argument passed to __builtin_frame_address and __builtin_return_address
Verifies that the argument passed to __builtin_frame_address and __builtin_return_address is within the range [0, 0xFFFF].
1 parent df8dda6 commit c93112d

File tree

2 files changed

+30
-3
lines changed

2 files changed

+30
-3
lines changed

clang/lib/Sema/SemaChecking.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,11 @@ Sema::CheckBuiltinFunctionCall(FunctionDecl *FDecl, unsigned BuiltinID,
18471847
if (SemaBuiltinOSLogFormat(TheCall))
18481848
return ExprError();
18491849
break;
1850+
case Builtin::BI__builtin_frame_address:
1851+
case Builtin::BI__builtin_return_address:
1852+
if (!SemaBuiltinConstantArgRange(TheCall, 0, 0, 0xFFFF))
1853+
return ExprError();
1854+
break;
18501855
}
18511856

18521857
// Since the target specific builtins for each arch overlap, only check those
Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// RUN: %clang_cc1 -fsyntax-only -verify %s
2-
void* a(unsigned x) {
2+
3+
void a(unsigned x) {
34
return __builtin_return_address(0);
45
}
56

@@ -8,9 +9,30 @@ return __builtin_return_address(x); // expected-error{{argument to '__builtin_re
89
}
910

1011
void* c(unsigned x) {
12+
// expected-error@+1 {{argument value 4294967295 is outside the valid range [0, 65535]}}
13+
return __builtin_return_address(-1);
14+
}
15+
16+
void* d(unsigned x) {
17+
// expected-error@+1 {{argument value 1048575 is outside the valid range [0, 65535]}}
18+
return __builtin_return_address(0xFFFFF);
19+
}
20+
21+
void* e(unsigned x) {
1122
return __builtin_frame_address(0);
1223
}
1324

14-
void d(unsigned x) {
15-
return __builtin_frame_address(x); // expected-error{{argument to '__builtin_frame_address' must be a constant integer}}
25+
void f(unsigned x) {
26+
// expected-error@+1 {{argument to '__builtin_frame_address' must be a constant integer}}
27+
return __builtin_frame_address(x);
28+
}
29+
30+
void* g(unsigned x) {
31+
// expected-error@+1 {{argument value 4294967295 is outside the valid range [0, 65535]}}
32+
return __builtin_frame_address(-1);
33+
}
34+
35+
void* h(unsigned x) {
36+
// expected-error@+1 {{argument value 1048575 is outside the valid range [0, 65535]}}
37+
return __builtin_frame_address(0xFFFFF);
1638
}

0 commit comments

Comments
 (0)