Files
clang-p2996/llvm/test/CodeGen/BPF/CORE/offset-reloc-basic.ll
yonghong-song 4c701577cd BPF: Use DebugLoc to find Filename for BTF line info (#90302)
Andrii found an issue where the BTF line info may have empty source
which seems wrong. The program is a Meta internal bpf program. I can
reproduce with latest upstream compiler as well. Let the bpf program
built without this patch and then with the following veristat check
where veristat is a bpf verifier tool to do kernel verification for bpf
programs:

  $ veristat -vl2 yhs.bpf.o --log-size=150000000 >& log
  $ rg '^;' log | sort | uniq -c | sort -nr | head -n10
   4206 ; } else if (action->dry_run) { @ src_mitigations.h:57
   3907 ; if (now < start_allow_time) { @ ban.h:17
   3674 ;  @ src_mitigations.h:0
3223 ; if (action->vip_id != ALL_VIPS_ID && action->vip_id != vip_id) {
@ src_mitigations.h:85
1737 ; pkt_info->is_dry_run_drop = action->dry_run; @
src_mitigations.h:26
   1737 ; if (mitigation == ALLOW) { @ src_mitigations.h:28
1737 ; enum match_action mitigation = action->action; @
src_mitigations.h:25
1727 ; void* res = bpf_map_lookup_elem(bpf_map, key); @
filter_helpers.h:498
1691 ; bpf_map_lookup_elem(&rate_limit_config_map, rule_id); @
rate_limit.h:76
   1688 ; if (throttle_cfg) { @ rate_limit.h:85

You can see

   3674 ;  @ src_mitigations.h:0

where we do not have proper line information and line number.

In LLVM Machine IR, some instructions may carry DebugLoc information
to specify where the corresponding source is for this instruction.
The information includes file_name, line_num and col_num.
Each instruction may also attribute to a function in debuginfo.
So there are two ways to find file_name for a particular insn:
  (1) find the corresponding function in debuginfo
      (MI->getMF()->getFunction().getSubprogram()) and then
      find the file_name from DISubprogram.
  (2) find the corresponding file_name from DebugLoc.

The option (1) is used in current implementation. This mostly works.
But if one instruction is somehow generated from multiple functions,
the compiler has to pick just one. This may cause a mismatch between
file_name and line_num/col_num.

Besides potential incorrect mismatch of file_name vs. line_num/col_num,
There is another issue where some DebugLoc has line number 0. For
example,
I dumped the dwarf line table for the above bpf program:
    
Address Line Column File ISA Discriminator OpIndex Flags
------------------ ------ ------ ------ --- ------------- -------
-------------
0x0000000000000000 96 0 17 0 0 0 is_stmt
0x0000000000000010 100 12 17 0 0 0 is_stmt prologue_end
      0x0000000000000020      0     12     17   0             0       0
0x0000000000000058 37 7 17 0 0 0 is_stmt
      0x0000000000000060      0      0     17   0             0       0
      0x0000000000000088     37      7     17   0             0       0
0x0000000000000090 42 75 17 0 0 0 is_stmt
      0x00000000000000a8     42     52     17   0             0       0
0x00000000000000c0 120 9 17 0 0 0 is_stmt
      0x00000000000000c8      0      9     17   0             0       0
0x00000000000000d0 106 21 17 0 0 0 is_stmt
      0x00000000000000d8    106      3     17   0             0       0
0x00000000000000e0 110 25 17 0 0 0 is_stmt
      0x00000000000000f8    110     36     17   0             0       0
      0x0000000000000100      0     36     17   0             0       0
      ...
    
These DebugLoc with line number 0 needs to be skipped since we cannot
map them to the correct source code. Note that selftest
offset-reloc-basic.ll
has this issue as well which is adjusted by this patch.

With the above two fixes, empty lines for source annotation are removed.

  $ veristat -vl2 yhs.bpf.o --log-size=150000000 >& log
  $ rg '^;' log.latest | sort | uniq -c | sort -nr | head -n10
   4206 ; } else if (action->dry_run) { @ src_mitigations.h:57
   3907 ; if (now < start_allow_time) { @ ban.h:17
3223 ; if (action->vip_id != ALL_VIPS_ID && action->vip_id != vip_id) {
@ src_mitigations.h:85
1737 ; pkt_info->is_dry_run_drop = action->dry_run; @
src_mitigations.h:26
   1737 ; if (mitigation == ALLOW) { @ src_mitigations.h:28
1737 ; enum match_action mitigation = action->action; @
src_mitigations.h:25
1727 ; void* res = bpf_map_lookup_elem(bpf_map, key); @
filter_helpers.h:498
1691 ; bpf_map_lookup_elem(&rate_limit_config_map, rule_id); @
rate_limit.h:76
   1688 ; if (throttle_cfg) { @ rate_limit.h:85
   1670 ; if (rl_cfg) { @ rate_limit.h:77

You can see that we do not have empty line any more.

3223 ; if (action->vip_id != ALL_VIPS_ID && action->vip_id != vip_id) {
@ src_mitigations.h:85

Signed-off-by: Yonghong Song <yonghong.song@linux.dev>
2024-04-29 09:20:56 -07:00

187 lines
9.2 KiB
LLVM

; RUN: opt -O2 %s | llvm-dis > %t1
; RUN: llc -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
; RUN: llc -mattr=+alu32 -filetype=asm -o - %t1 | FileCheck -check-prefixes=CHECK %s
; Source code:
; struct sk_buff {
; int i;
; struct net_device *dev;
; };
; #define _(x) (__builtin_preserve_access_index(x))
; static int (*bpf_probe_read)(ptr dst, int size, ptr unsafe_ptr)
; = (ptr) 4;
;
; int bpf_prog(struct sk_buff *ctx) {
; struct net_device *dev = 0;
; bpf_probe_read(&dev, sizeof(dev), _(&ctx->dev));
; return dev != 0;
; }
; Compilation flag:
; clang -target bpf -O2 -g -S -emit-llvm -Xclang -disable-llvm-passes test.c
target triple = "bpf"
%struct.sk_buff = type { i32, ptr }
%struct.net_device = type opaque
; Function Attrs: nounwind
define dso_local i32 @bpf_prog(ptr) local_unnamed_addr #0 !dbg !15 {
%2 = alloca ptr, align 8
call void @llvm.dbg.value(metadata ptr %0, metadata !26, metadata !DIExpression()), !dbg !28
call void @llvm.lifetime.start.p0(i64 8, ptr nonnull %2) #4, !dbg !29
call void @llvm.dbg.value(metadata ptr null, metadata !27, metadata !DIExpression()), !dbg !28
store ptr null, ptr %2, align 8, !dbg !30, !tbaa !31
%3 = tail call ptr @llvm.preserve.struct.access.index.p0.net_devices.p0.sk_buffs(ptr elementtype(%struct.sk_buff) %0, i32 1, i32 1), !dbg !35, !llvm.preserve.access.index !19
%4 = call i32 inttoptr (i64 4 to ptr)(ptr nonnull %2, i32 8, ptr %3) #4, !dbg !36
%5 = load ptr, ptr %2, align 8, !dbg !37, !tbaa !31
call void @llvm.dbg.value(metadata ptr %5, metadata !27, metadata !DIExpression()), !dbg !28
%6 = icmp ne ptr %5, null, !dbg !38
%7 = zext i1 %6 to i32, !dbg !38
call void @llvm.lifetime.end.p0(i64 8, ptr nonnull %2) #4, !dbg !39
ret i32 %7, !dbg !40
}
; CHECK: .section .BTF,"",@progbits
; CHECK-NEXT: .short 60319 # 0xeb9f
; CHECK-NEXT: .byte 1
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .long 24
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 120
; CHECK-NEXT: .long 120
; CHECK-NEXT: .long 90
; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 1)
; CHECK-NEXT: .long 33554432 # 0x2000000
; CHECK-NEXT: .long 2
; CHECK-NEXT: .long 1 # BTF_KIND_STRUCT(id = 2)
; CHECK-NEXT: .long 67108866 # 0x4000002
; CHECK-NEXT: .long 16
; CHECK-NEXT: .long 9
; CHECK-NEXT: .long 3
; CHECK-NEXT: .long 0 # 0x0
; CHECK-NEXT: .long 11
; CHECK-NEXT: .long 4
; CHECK-NEXT: .long 64 # 0x40
; CHECK-NEXT: .long 15 # BTF_KIND_INT(id = 3)
; CHECK-NEXT: .long 16777216 # 0x1000000
; CHECK-NEXT: .long 4
; CHECK-NEXT: .long 16777248 # 0x1000020
; CHECK-NEXT: .long 0 # BTF_KIND_PTR(id = 4)
; CHECK-NEXT: .long 33554432 # 0x2000000
; CHECK-NEXT: .long 5
; CHECK-NEXT: .long 19 # BTF_KIND_FWD(id = 5)
; CHECK-NEXT: .long 117440512 # 0x7000000
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 0 # BTF_KIND_FUNC_PROTO(id = 6)
; CHECK-NEXT: .long 218103809 # 0xd000001
; CHECK-NEXT: .long 3
; CHECK-NEXT: .long 30
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long 34 # BTF_KIND_FUNC(id = 7)
; CHECK-NEXT: .long 201326593 # 0xc000001
; CHECK-NEXT: .long 6
; CHECK-NEXT: .byte 0 # string offset=0
; CHECK-NEXT: .ascii "sk_buff" # string offset=1
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .byte 105 # string offset=9
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "dev" # string offset=11
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "int" # string offset=15
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "net_device" # string offset=19
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "ctx" # string offset=30
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "bpf_prog" # string offset=34
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii ".text" # string offset=43
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "/tmp/home/yhs/work/tests/llvm/test.c" # string offset=49
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .ascii "0:1" # string offset=86
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .section .BTF.ext,"",@progbits
; CHECK-NEXT: .short 60319 # 0xeb9f
; CHECK-NEXT: .byte 1
; CHECK-NEXT: .byte 0
; CHECK-NEXT: .long 32
; CHECK-NEXT: .long 0
; CHECK-NEXT: .long 20
; CHECK-NEXT: .long 20
; CHECK-NEXT: .long 108
; CHECK-NEXT: .long 128
; CHECK-NEXT: .long 28
; CHECK-NEXT: .long 8 # FuncInfo
; CHECK: .long 16 # FieldReloc
; CHECK-NEXT: .long 43 # Field reloc section string offset=43
; CHECK-NEXT: .long 1
; CHECK-NEXT: .long .Ltmp{{[0-9]+}}
; CHECK-NEXT: .long 2
; CHECK-NEXT: .long 86
; CHECK-NEXT: .long 0
; Function Attrs: argmemonly nounwind
declare void @llvm.lifetime.start.p0(i64 immarg, ptr nocapture) #1
; Function Attrs: nounwind readnone
declare ptr @llvm.preserve.struct.access.index.p0.net_devices.p0.sk_buffs(ptr, i32 immarg, i32 immarg) #2
; Function Attrs: argmemonly nounwind
declare void @llvm.lifetime.end.p0(i64 immarg, ptr nocapture) #1
; Function Attrs: nounwind readnone speculatable
declare void @llvm.dbg.value(metadata, metadata, metadata) #3
attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "frame-pointer"="all" "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" }
attributes #1 = { argmemonly nounwind }
attributes #2 = { nounwind readnone }
attributes #3 = { nounwind readnone speculatable }
attributes #4 = { nounwind }
!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!11, !12, !13}
!llvm.ident = !{!14}
!0 = distinct !DICompileUnit(language: DW_LANG_C99, file: !1, producer: "clang version 9.0.0 (trunk 360739) (llvm/trunk 360747)", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, enums: !2, globals: !3, nameTableKind: None)
!1 = !DIFile(filename: "test.c", directory: "/tmp/home/yhs/work/tests/llvm")
!2 = !{}
!3 = !{!4}
!4 = !DIGlobalVariableExpression(var: !5, expr: !DIExpression())
!5 = distinct !DIGlobalVariable(name: "bpf_probe_read", scope: !0, file: !1, line: 6, type: !6, isLocal: true, isDefinition: true)
!6 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !7, size: 64)
!7 = !DISubroutineType(types: !8)
!8 = !{!9, !10, !9, !10}
!9 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!10 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: null, size: 64)
!11 = !{i32 2, !"Dwarf Version", i32 4}
!12 = !{i32 2, !"Debug Info Version", i32 3}
!13 = !{i32 1, !"wchar_size", i32 4}
!14 = !{!"clang version 9.0.0 (trunk 360739) (llvm/trunk 360747)"}
!15 = distinct !DISubprogram(name: "bpf_prog", scope: !1, file: !1, line: 9, type: !16, scopeLine: 9, flags: DIFlagPrototyped, isLocal: false, isDefinition: true, isOptimized: true, unit: !0, retainedNodes: !25)
!16 = !DISubroutineType(types: !17)
!17 = !{!9, !18}
!18 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !19, size: 64)
!19 = distinct !DICompositeType(tag: DW_TAG_structure_type, name: "sk_buff", file: !1, line: 1, size: 128, elements: !20)
!20 = !{!21, !22}
!21 = !DIDerivedType(tag: DW_TAG_member, name: "i", scope: !19, file: !1, line: 2, baseType: !9, size: 32)
!22 = !DIDerivedType(tag: DW_TAG_member, name: "dev", scope: !19, file: !1, line: 3, baseType: !23, size: 64, offset: 64)
!23 = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: !24, size: 64)
!24 = !DICompositeType(tag: DW_TAG_structure_type, name: "net_device", file: !1, line: 3, flags: DIFlagFwdDecl)
!25 = !{!26, !27}
!26 = !DILocalVariable(name: "ctx", arg: 1, scope: !15, file: !1, line: 9, type: !18)
!27 = !DILocalVariable(name: "dev", scope: !15, file: !1, line: 10, type: !23)
!28 = !DILocation(line: 0, scope: !15)
!29 = !DILocation(line: 10, column: 3, scope: !15)
!30 = !DILocation(line: 10, column: 22, scope: !15)
!31 = !{!32, !32, i64 0}
!32 = !{!"any pointer", !33, i64 0}
!33 = !{!"omnipotent char", !34, i64 0}
!34 = !{!"Simple C/C++ TBAA"}
!35 = !DILocation(line: 11, column: 37, scope: !15)
!36 = !DILocation(line: 11, column: 3, scope: !15)
!37 = !DILocation(line: 12, column: 10, scope: !15)
!38 = !DILocation(line: 12, column: 14, scope: !15)
!39 = !DILocation(line: 13, column: 1, scope: !15)
!40 = !DILocation(line: 12, column: 3, scope: !15)