Skip to content

Commit 56b038f

Browse files
eddyz87yonghong-song
authored andcommitted
[BPF][clang] Ignore stack protector options for BPF target
Stack protector builtin functions are not implemented for BPF target, thus compiling programs with one of the following options would result in an error: -fstack-protector -fstack-protector-all -fstack-protector-strong This commit adds logic to ignore these options for BPF target. Searching through DiagnosticDriverKinds.td shows that all messages for such kind of behavior are implemented as warnings, this commit follows the suit. Here is an example of the diagnostic message: clang-16: warning: ignoring '-fstack-protector' option as it is not currently supported for target 'bpf' [-Woption-ignored] Differential Revision: https://reviews.llvm.org/D142046
1 parent c52f948 commit 56b038f

File tree

2 files changed

+40
-0
lines changed

2 files changed

+40
-0
lines changed

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3239,6 +3239,12 @@ static void RenderSSPOptions(const Driver &D, const ToolChain &TC,
32393239
StackProtectorLevel = LangOptions::SSPStrong;
32403240
else if (A->getOption().matches(options::OPT_fstack_protector_all))
32413241
StackProtectorLevel = LangOptions::SSPReq;
3242+
3243+
if (EffectiveTriple.isBPF() && StackProtectorLevel != LangOptions::SSPOff) {
3244+
D.Diag(diag::warn_drv_unsupported_option_for_target)
3245+
<< A->getSpelling() << EffectiveTriple.getTriple();
3246+
StackProtectorLevel = DefaultStackProtectorLevel;
3247+
}
32423248
} else {
32433249
StackProtectorLevel = DefaultStackProtectorLevel;
32443250
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// REQUIRES: bpf-registered-target
2+
3+
// RUN %clang -target bpf -S -emit-llvm -o - %s -fno-stack-protector 2>&1 \
4+
// RUN | FileCheck -check-prefix=OFF -check-prefix=COMMON %s
5+
6+
// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector 2>&1 \
7+
// RUN: | FileCheck -check-prefix=ON -check-prefix=COMMON %s
8+
9+
// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-all 2>&1 \
10+
// RUN: | FileCheck -check-prefix=ALL -check-prefix=COMMON %s
11+
12+
// RUN: %clang -target bpf -S -emit-llvm -o - %s -fstack-protector-strong 2>&1 \
13+
// RUN: | FileCheck -check-prefix=STRONG -check-prefix=COMMON %s
14+
15+
typedef __SIZE_TYPE__ size_t;
16+
17+
int printf(const char * _Format, ...);
18+
size_t strlen(const char *s);
19+
char *strcpy(char *s1, const char *s2);
20+
21+
// OFF-NOT: warning
22+
// ON: warning: ignoring '-fstack-protector'
23+
// ALL: warning: ignoring '-fstack-protector-all'
24+
// STRONG: warning: ignoring '-fstack-protector-strong'
25+
// COMMON-SAME: option as it is not currently supported for target 'bpf'
26+
27+
// COMMON: define {{.*}}void @test1(ptr noundef %msg) #[[A:.*]] {
28+
void test1(const char *msg) {
29+
char a[strlen(msg) + 1];
30+
strcpy(a, msg);
31+
printf("%s\n", a);
32+
}
33+
34+
// COMMON-NOT: attributes #[[A]] = {{.*}} ssp

0 commit comments

Comments
 (0)