Skip to content

Commit 05de2e4

Browse files
committed
[bpf] error when BPF stack size exceeds 512 bytes
Signed-off-by: Alexei Starovoitov <[email protected]> llvm-svn: 292180
1 parent 36d382b commit 05de2e4

File tree

2 files changed

+102
-2
lines changed

2 files changed

+102
-2
lines changed

llvm/lib/Target/BPF/BPFRegisterInfo.cpp

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/Support/ErrorHandling.h"
2222
#include "llvm/Target/TargetFrameLowering.h"
2323
#include "llvm/Target/TargetInstrInfo.h"
24+
#include "llvm/IR/DiagnosticInfo.h"
2425

2526
#define GET_REGINFO_TARGET_DESC
2627
#include "BPFGenRegisterInfo.inc"
@@ -41,16 +42,37 @@ BitVector BPFRegisterInfo::getReservedRegs(const MachineFunction &MF) const {
4142
return Reserved;
4243
}
4344

45+
static void WarnSize(int Offset, MachineFunction &MF, DebugLoc& DL)
46+
{
47+
if (Offset <= -512) {
48+
auto F = MF.getFunction();
49+
DiagnosticInfoUnsupported DiagStackSize(*F,
50+
"Looks like the BPF stack limit of 512 bytes is exceeded. "
51+
"Please move large on stack variables into BPF per-cpu array map.\n",
52+
DL);
53+
F->getContext().diagnose(DiagStackSize);
54+
}
55+
}
56+
4457
void BPFRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
4558
int SPAdj, unsigned FIOperandNum,
4659
RegScavenger *RS) const {
4760
assert(SPAdj == 0 && "Unexpected");
4861

4962
unsigned i = 0;
5063
MachineInstr &MI = *II;
51-
MachineFunction &MF = *MI.getParent()->getParent();
64+
MachineBasicBlock &MBB = *MI.getParent();
65+
MachineFunction &MF = *MBB.getParent();
5266
DebugLoc DL = MI.getDebugLoc();
5367

68+
if (!DL)
69+
/* try harder to get some debug loc */
70+
for (auto &I : MBB)
71+
if (I.getDebugLoc()) {
72+
DL = I.getDebugLoc();
73+
break;
74+
}
75+
5476
while (!MI.getOperand(i).isFI()) {
5577
++i;
5678
assert(i < MI.getNumOperands() && "Instr doesn't have FrameIndex operand!");
@@ -59,11 +81,11 @@ void BPFRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
5981
unsigned FrameReg = getFrameRegister(MF);
6082
int FrameIndex = MI.getOperand(i).getIndex();
6183
const TargetInstrInfo &TII = *MF.getSubtarget().getInstrInfo();
62-
MachineBasicBlock &MBB = *MI.getParent();
6384

6485
if (MI.getOpcode() == BPF::MOV_rr) {
6586
int Offset = MF.getFrameInfo().getObjectOffset(FrameIndex);
6687

88+
WarnSize(Offset, MF, DL);
6789
MI.getOperand(i).ChangeToRegister(FrameReg, false);
6890
unsigned reg = MI.getOperand(i - 1).getReg();
6991
BuildMI(MBB, ++II, DL, TII.get(BPF::ADD_ri), reg)
@@ -78,6 +100,8 @@ void BPFRegisterInfo::eliminateFrameIndex(MachineBasicBlock::iterator II,
78100
if (!isInt<32>(Offset))
79101
llvm_unreachable("bug in frame offset");
80102

103+
WarnSize(Offset, MF, DL);
104+
81105
if (MI.getOpcode() == BPF::FI_ri) {
82106
// architecture does not really support FI_ri, replace it with
83107
// MOV_rr <target_reg>, frame_reg

llvm/test/CodeGen/BPF/warn-stack.ll

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
; RUN: not llc -march=bpfel < %s 2>&1 >/dev/null | FileCheck %s
2+
3+
;; CHECK-NOT: nowarn
4+
define void @nowarn() local_unnamed_addr #0 !dbg !6 {
5+
%1 = alloca [504 x i8], align 1
6+
%2 = getelementptr inbounds [504 x i8], [504 x i8]* %1, i64 0, i64 0, !dbg !15
7+
call void @llvm.lifetime.start(i64 504, i8* nonnull %2) #4, !dbg !15
8+
tail call void @llvm.dbg.declare(metadata [504 x i8]* %1, metadata !10, metadata !16), !dbg !17
9+
call void @doit(i8* nonnull %2) #4, !dbg !18
10+
call void @llvm.lifetime.end(i64 504, i8* nonnull %2) #4, !dbg !19
11+
ret void, !dbg !19
12+
}
13+
14+
; Function Attrs: argmemonly nounwind
15+
declare void @llvm.lifetime.start(i64, i8* nocapture) #1
16+
17+
; Function Attrs: nounwind readnone
18+
declare void @llvm.dbg.declare(metadata, metadata, metadata) #2
19+
20+
declare void @doit(i8*) local_unnamed_addr #3
21+
22+
; Function Attrs: argmemonly nounwind
23+
declare void @llvm.lifetime.end(i64, i8* nocapture) #1
24+
25+
; CHECK: error: warn_stack.c
26+
; CHECK: BPF stack limit
27+
define void @warn() local_unnamed_addr #0 !dbg !20 {
28+
%1 = alloca [512 x i8], align 1
29+
%2 = getelementptr inbounds [512 x i8], [512 x i8]* %1, i64 0, i64 0, !dbg !26
30+
call void @llvm.lifetime.start(i64 512, i8* nonnull %2) #4, !dbg !26
31+
tail call void @llvm.dbg.declare(metadata [512 x i8]* %1, metadata !22, metadata !16), !dbg !27
32+
call void @doit(i8* nonnull %2) #4, !dbg !28
33+
call void @llvm.lifetime.end(i64 512, i8* nonnull %2) #4, !dbg !29
34+
ret void, !dbg !29
35+
}
36+
37+
attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
38+
attributes #1 = { argmemonly nounwind }
39+
attributes #2 = { nounwind readnone }
40+
attributes #3 = { "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }
41+
attributes #4 = { nounwind }
42+
43+
!llvm.dbg.cu = !{!0}
44+
!llvm.module.flags = !{!3, !4}
45+
!llvm.ident = !{!5}
46+
47+
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 5.0.0 (trunk 292141) (llvm/trunk 292156)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2)
48+
!1 = !DIFile(filename: "warn_stack.c", directory: "/w/llvm/bld")
49+
!2 = !{}
50+
!3 = !{i32 2, !"Dwarf Version", i32 4}
51+
!4 = !{i32 2, !"Debug Info Version", i32 3}
52+
!5 = !{!"clang version 5.0.0 (trunk 292141) (llvm/trunk 292156)"}
53+
!6 = distinct !DISubprogram(name: "nowarn", scope: !1, file: !1, line: 2, type: !7, isLocal: false, isDefinition: true, scopeLine: 3, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !9)
54+
!7 = !DISubroutineType(types: !8)
55+
!8 = !{null}
56+
!9 = !{!10}
57+
!10 = !DILocalVariable(name: "buf", scope: !6, file: !1, line: 4, type: !11)
58+
!11 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, size: 4088, elements: !13)
59+
!12 = !DIBasicType(name: "char", size: 8, encoding: DW_ATE_signed_char)
60+
!13 = !{!14}
61+
!14 = !DISubrange(count: 504)
62+
!15 = !DILocation(line: 4, column: 2, scope: !6)
63+
!16 = !DIExpression()
64+
!17 = !DILocation(line: 4, column: 7, scope: !6)
65+
!18 = !DILocation(line: 5, column: 2, scope: !6)
66+
!19 = !DILocation(line: 6, column: 1, scope: !6)
67+
!20 = distinct !DISubprogram(name: "warn", scope: !1, file: !1, line: 7, type: !7, isLocal: false, isDefinition: true, scopeLine: 8, flags: DIFlagPrototyped, isOptimized: true, unit: !0, variables: !21)
68+
!21 = !{!22}
69+
!22 = !DILocalVariable(name: "buf", scope: !20, file: !1, line: 9, type: !23)
70+
!23 = !DICompositeType(tag: DW_TAG_array_type, baseType: !12, size: 4096, elements: !24)
71+
!24 = !{!25}
72+
!25 = !DISubrange(count: 512)
73+
!26 = !DILocation(line: 9, column: 2, scope: !20)
74+
!27 = !DILocation(line: 9, column: 7, scope: !20)
75+
!28 = !DILocation(line: 10, column: 2, scope: !20)
76+
!29 = !DILocation(line: 11, column: 1, scope: !20)

0 commit comments

Comments
 (0)