- 
                Notifications
    
You must be signed in to change notification settings  - Fork 15.1k
 
[BPF] Handle traps with kfunc call __bpf_trap #131731
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            yonghong-song
  merged 2 commits into
  llvm:main
from
yonghong-song:handle-undef-unreachable-call
  
      
      
   
  May 27, 2025 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            2 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      
    File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| ; RUN: llc -mtriple=bpfel -mcpu=v3 -filetype=obj -o %t1 %s | ||
| ; RUN: llvm-objcopy --dump-section='.BTF'=%t2 %t1 | ||
| ; RUN: %python %p/print_btf.py %t2 | FileCheck -check-prefixes=CHECK-BTF %s | ||
| ; RUN: llc -mtriple=bpfel -mcpu=v3 < %s | FileCheck -check-prefixes=CHECK %s | ||
| 
     | 
||
| ; BPFTargetMachine Options.NoTrapAfterNoreturn has been set to true, | ||
| ; so in below code, 'unreachable' will become a noop and | ||
| ; llvm.trap() will become 'call __bpf_trap' after selectiondag. | ||
| define dso_local void @foo(i32 noundef %0) { | ||
| tail call void @llvm.trap() | ||
| unreachable | ||
| } | ||
| 
     | 
||
| ; CHECK: .Lfunc_begin0: | ||
| ; CHECK-NEXT: .cfi_startproc | ||
| ; CHECK-NEXT: # %bb.0: | ||
| ; CHECK-NEXT: call __bpf_trap | ||
| ; CHECK-NEXT: exit | ||
| ; CHECK-NEXT: .Lfunc_end0: | ||
| 
     | 
||
| ; CHECK-BTF: [1] FUNC_PROTO '(anon)' ret_type_id=0 vlen=0 | ||
| ; CHECK-BTF: [2] FUNC '__bpf_trap' type_id=1 linkage=extern | ||
| ; CHECK-BTF: [3] DATASEC '.ksyms' size=0 vlen=1 | ||
| ; CHECK-BTF: type_id=2 offset=0 size=0 | ||
| 
     | 
||
| declare void @llvm.trap() #1 | ||
| 
     | 
||
| attributes #1 = {noreturn} | ||
| 
     | 
||
| !llvm.dbg.cu = !{!0} | ||
| !llvm.module.flags = !{!2, !3, !4, !5, !6} | ||
| 
     | 
||
| !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, emissionKind: FullDebug) | ||
| !1 = !DIFile(filename: "test_trap.c", directory: "/some/dir") | ||
| !2 = !{i32 7, !"Dwarf Version", i32 5} | ||
| !3 = !{i32 2, !"Debug Info Version", i32 3} | ||
| !4 = !{i32 1, !"wchar_size", i32 4} | ||
| !5 = !{i32 7, !"frame-pointer", i32 2} | ||
| !6 = !{i32 7, !"debug-info-assignment-tracking", i1 true} | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| ; RUN: llc -mtriple=bpfel -mcpu=v3 -filetype=obj -o %t1 %s | ||
| ; RUN: llvm-objcopy --dump-section='.BTF'=%t2 %t1 | ||
| ; RUN: %python %p/print_btf.py %t2 | FileCheck -check-prefixes=CHECK-BTF %s | ||
| ; RUN: llc -mtriple=bpfel -mcpu=v3 < %s | FileCheck -check-prefixes=CHECK %s | ||
| 
     | 
||
| define void @foo() { | ||
| entry: | ||
| tail call void @bar() | ||
| unreachable | ||
| } | ||
| 
     | 
||
| ; CHECK: foo: | ||
| ; CHECK-NEXT: .Lfunc_begin0: | ||
| ; CHECK-NEXT: .cfi_startproc | ||
| ; CHECK-NEXT: # %bb.0: | ||
| ; CHECK-NEXT: call bar | ||
| ; CHECK-NEXT: call __bpf_trap | ||
| ; CHECK-NEXT: exit | ||
| ; CHECK-NEXT: .Lfunc_end0: | ||
| 
     | 
||
| define void @buz() #0 { | ||
| entry: | ||
| tail call void asm sideeffect "r0 = r1; exit;", ""() | ||
| unreachable | ||
| } | ||
| 
     | 
||
| ; CHECK: buz: | ||
| ; CHECK-NEXT: .Lfunc_begin1: | ||
| ; CHECK-NEXT: .cfi_startproc | ||
| ; CHECK-NEXT: # %bb.0: | ||
| ; CHECK-NEXT: #APP | ||
| ; CHECK-NEXT: r0 = r1 | ||
| ; CHECK-NEXT: exit | ||
| ; CHECK-EMPTY: | ||
| ; CHECK-NEXT: #NO_APP | ||
| ; CHECK-NEXT: .Lfunc_end1: | ||
| 
     | 
||
| ; CHECK-BTF: [1] FUNC_PROTO '(anon)' ret_type_id=0 vlen=0 | ||
| ; CHECK-BTF: [2] FUNC '__bpf_trap' type_id=1 linkage=extern | ||
| ; CHECK-BTF: [3] DATASEC '.ksyms' size=0 vlen=1 | ||
| ; CHECK-BTF: type_id=2 offset=0 size=0 | ||
| 
     | 
||
| declare dso_local void @bar() | ||
| 
     | 
||
| attributes #0 = { naked } | ||
| 
     | 
||
| !llvm.dbg.cu = !{!0} | ||
| !llvm.module.flags = !{!2, !3} | ||
| 
     | 
||
| !0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, emissionKind: FullDebug) | ||
| !1 = !DIFile(filename: "test.c", directory: "/some/dir") | ||
| !2 = !{i32 7, !"Dwarf Version", i32 5} | ||
| !3 = !{i32 2, !"Debug Info Version", i32 3} | 
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yonghong-song I'm seeing a lot of gcc warnings:
Any chance you can change this to:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@RKSimon I could do the change. But can you let me know how to reproduce the warning? Maybe I missed some compilation flags?