Files
clang-p2996/llvm/test/CodeGen/BPF/vla.ll
Yonghong Song 009f3a89d8 BPF: remove intrindics @llvm.stacksave() and @llvm.stackrestore()
Paul Chaignon reported a bpf verifier failure ([1]) due to using
non-ABI register R11. For the test case, llvm11 is okay while
llvm12 and later generates verifier unfriendly code.

The failure is related to variable length array size.
The following mimics the variable length array definition
in the test case:

struct t { char a[20]; };
void foo(void *);
int test() {
   const int a = 8;
   char tmp[AA + sizeof(struct t) + a];
   foo(tmp);
   ...
}

Paul helped bisect that the following llvm commit is
responsible:

552c6c2328 ("PR44406: Follow behavior of array bound constant
              folding in more recent versions of GCC.")

Basically, before the above commit, clang frontend did constant
folding for array size "AA + sizeof(struct t) + a" to be 68,
so used alloca for stack allocation. After the above commit,
clang frontend didn't do constant folding for array size
any more, which results in a VLA and llvm.stacksave/llvm.stackrestore
is generated.

BPF architecture API does not support stack pointer (sp) register.
The LLVM internally used R11 to indicate sp register but it should
not be in the final code. Otherwise, kernel verifier will reject it.

The early patch ([2]) tried to fix the issue in clang frontend.
But the upstream discussion considered frontend fix is really a
hack and the backend should properly undo llvm.stacksave/llvm.stackrestore.
This patch implemented a bpf IR phase to remove these intrinsics
unconditionally. If eventually the alloca can be resolved with
constant size, r11 will not be generated. If alloca cannot be
resolved with constant size, SelectionDag will complain, the same
as without this patch.

 [1] https://lore.kernel.org/bpf/20210809151202.GB1012999@Mem/
 [2] https://reviews.llvm.org/D107882

Differential Revision: https://reviews.llvm.org/D111897
2021-10-18 09:51:19 -07:00

116 lines
3.7 KiB
LLVM

; RUN: opt --bpf-ir-peephole -mtriple=bpf-pc-linux -S %s | FileCheck %s
; Source:
; #define AA 40
; struct t {
; char a[20];
; };
; void foo(void *);
;
; int test1() {
; const int a = 8;
; char tmp[AA + sizeof(struct t) + a];
; foo(tmp);
; return 0;
; }
;
; int test2(int b) {
; const int a = 8;
; char tmp[a + b];
; foo(tmp);
; return 0;
; }
; Compilation flag:
; clang -target bpf -O2 -S -emit-llvm t.c -Xclang -disable-llvm-passes
source_filename = "t.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf"
; Function Attrs: nounwind
define dso_local i32 @test1() #0 {
entry:
%a = alloca i32, align 4
%saved_stack = alloca i8*, align 8
%0 = bitcast i32* %a to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #4
store i32 8, i32* %a, align 4, !tbaa !3
%1 = call i8* @llvm.stacksave()
store i8* %1, i8** %saved_stack, align 8
%vla = alloca i8, i64 68, align 1
call void @foo(i8* %vla)
%2 = load i8*, i8** %saved_stack, align 8
call void @llvm.stackrestore(i8* %2)
%3 = bitcast i32* %a to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %3) #4
ret i32 0
}
; CHECK: define dso_local i32 @test1
; CHECK-NOT: %[[#]] = call i8* @llvm.stacksave()
; CHECK-NOT: store i8* %[[#]], i8** %saved_stack, align 8
; CHECK-NOT: %[[#]] = load i8*, i8** %saved_stack, align 8
; CHECK-NOT: call void @llvm.stackrestore(i8* %[[#]])
; Function Attrs: argmemonly nofree nosync nounwind willreturn
declare void @llvm.lifetime.start.p0i8(i64 immarg, i8* nocapture) #1
; Function Attrs: nofree nosync nounwind willreturn
declare i8* @llvm.stacksave() #2
declare dso_local void @foo(i8*) #3
; Function Attrs: nofree nosync nounwind willreturn
declare void @llvm.stackrestore(i8*) #2
; Function Attrs: argmemonly nofree nosync nounwind willreturn
declare void @llvm.lifetime.end.p0i8(i64 immarg, i8* nocapture) #1
; Function Attrs: nounwind
define dso_local i32 @test2(i32 %b) #0 {
entry:
%b.addr = alloca i32, align 4
%a = alloca i32, align 4
%saved_stack = alloca i8*, align 8
%__vla_expr0 = alloca i64, align 8
store i32 %b, i32* %b.addr, align 4, !tbaa !3
%0 = bitcast i32* %a to i8*
call void @llvm.lifetime.start.p0i8(i64 4, i8* %0) #4
store i32 8, i32* %a, align 4, !tbaa !3
%1 = load i32, i32* %b.addr, align 4, !tbaa !3
%add = add nsw i32 8, %1
%2 = zext i32 %add to i64
%3 = call i8* @llvm.stacksave()
store i8* %3, i8** %saved_stack, align 8
%vla = alloca i8, i64 %2, align 1
store i64 %2, i64* %__vla_expr0, align 8
call void @foo(i8* %vla)
%4 = load i8*, i8** %saved_stack, align 8
call void @llvm.stackrestore(i8* %4)
%5 = bitcast i32* %a to i8*
call void @llvm.lifetime.end.p0i8(i64 4, i8* %5) #4
ret i32 0
}
; CHECK: define dso_local i32 @test2
; CHECK-NOT: %[[#]] = call i8* @llvm.stacksave()
; CHECK-NOT: store i8* %[[#]], i8** %saved_stack, align 8
; CHECK-NOT: %[[#]] = load i8*, i8** %saved_stack, align 8
; CHECK-NOT: call void @llvm.stackrestore(i8* %[[#]])
attributes #0 = { nounwind "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #1 = { argmemonly nofree nosync nounwind willreturn }
attributes #2 = { nofree nosync nounwind willreturn }
attributes #3 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" }
attributes #4 = { nounwind }
!llvm.module.flags = !{!0, !1}
!llvm.ident = !{!2}
!0 = !{i32 1, !"wchar_size", i32 4}
!1 = !{i32 7, !"frame-pointer", i32 2}
!2 = !{!"clang version 14.0.0 (https://github.com/llvm/llvm-project.git 64c5d5c671fb5b5f25c464652a4eec2cf743af0d)"}
!3 = !{!4, !4, i64 0}
!4 = !{!"int", !5, i64 0}
!5 = !{!"omnipotent char", !6, i64 0}
!6 = !{!"Simple C/C++ TBAA"}