This is an archive of the discontinued LLVM Phabricator instance.

[BPF] Don't crash on missing line info
ClosedPublic

Authored by tamird on Jul 27 2023, 9:00 AM.

Details

Summary

When compiling Rust code we may end up with calls to functions provided
by other code units. Presently this code crashes on a null pointer
dereference - this patch avoids that crash and adds a test.

Diff Detail

Event Timeline

tamird created this revision.Jul 27 2023, 9:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 9:00 AM
tamird requested review of this revision.Jul 27 2023, 9:00 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 27 2023, 9:00 AM
tamird updated this revision to Diff 544991.Jul 27 2023, 6:01 PM

Avoid the crash

tamird retitled this revision from [BPF] Emit better error on missing line info to [BPF] Avoid crashing on missing callee DI.Jul 27 2023, 6:02 PM
tamird edited the summary of this revision. (Show Details)
tamird updated this revision to Diff 544998.Jul 27 2023, 7:09 PM

Revert back to better error

tamird retitled this revision from [BPF] Avoid crashing on missing callee DI to [BPF] Emit better error on missing line info.Jul 27 2023, 7:10 PM
tamird edited the summary of this revision. (Show Details)

This error could be detected using the following test case:

$ cat test2.c
; ModuleID = 'test2.c'
source_filename = "test2.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf"

; Function Attrs: noinline nounwind optnone
define dso_local i64 @foo() {
entry:
  ret i64 42
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3, !4, !5}

!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "foo", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "file.c", directory: "/some/dir", checksumkind: CSK_MD5, checksum: "f58a7abbd9253986370acb1013fc9e55")
!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 = !{!"clang version 18.0.0 (/home/eddy/work/llvm-project/clang 61cd4a1d35aa7c0f7b312b8f43ed3bff33f20d55)"}

$ llc test2.ll -o /dev/null
/home/eddy/work/llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp:1640:42: runtime error: member call on null pointer of type 'llvm::DISubprogram'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/eddy/work/llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp:1640:42 in 

Necessary conditions are: module with debug attributes (llvm.dbg.cu -> DICompileUnit) and a function and instruction w/o debug metadata. I think such test case should be added to the suite.
Also, I don't see a point in reporting this (either as warning or as error). Absence of DISubprogram is ignored in BTFDebug::processFuncPrototypes and it two other places getSubprogram() can't return null. I think the following patch should suffice:

diff --git a/llvm/lib/Target/BPF/BTFDebug.cpp b/llvm/lib/Target/BPF/BTFDebug.cpp
index d886edaff555..6df421934b70 100644
--- a/llvm/lib/Target/BPF/BTFDebug.cpp
+++ b/llvm/lib/Target/BPF/BTFDebug.cpp
@@ -1634,8 +1635,8 @@ void BTFDebug::beginInstruction(const MachineInstr *MI) {
   if (!DL || PrevInstLoc == DL) {
     // This instruction will be skipped, no LineInfo has
     // been generated, construct one based on function signature.
-    if (LineInfoGenerated == false) {
-      auto *S = MI->getMF()->getFunction().getSubprogram();
+    DISubprogram *S = MI->getMF()->getFunction().getSubprogram();
+    if (!LineInfoGenerated && S) {
       MCSymbol *FuncLabel = Asm->getFunctionBegin();
       constructLineInfo(S, FuncLabel, S->getLine(), 0);
       LineInfoGenerated = true;
tamird added a comment.Aug 2 2023, 9:00 AM

This error could be detected using the following test case:

$ cat test2.c
; ModuleID = 'test2.c'
source_filename = "test2.c"
target datalayout = "e-m:e-p:64:64-i64:64-i128:128-n32:64-S128"
target triple = "bpf"

; Function Attrs: noinline nounwind optnone
define dso_local i64 @foo() {
entry:
  ret i64 42
}

!llvm.dbg.cu = !{!0}
!llvm.module.flags = !{!2, !3, !4, !5}

!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "foo", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "file.c", directory: "/some/dir", checksumkind: CSK_MD5, checksum: "f58a7abbd9253986370acb1013fc9e55")
!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 = !{!"clang version 18.0.0 (/home/eddy/work/llvm-project/clang 61cd4a1d35aa7c0f7b312b8f43ed3bff33f20d55)"}

$ llc test2.ll -o /dev/null
/home/eddy/work/llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp:1640:42: runtime error: member call on null pointer of type 'llvm::DISubprogram'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /home/eddy/work/llvm-project/llvm/lib/Target/BPF/BTFDebug.cpp:1640:42 in 

Necessary conditions are: module with debug attributes (llvm.dbg.cu -> DICompileUnit) and a function and instruction w/o debug metadata. I think such test case should be added to the suite.
Also, I don't see a point in reporting this (either as warning or as error). Absence of DISubprogram is ignored in BTFDebug::processFuncPrototypes and it two other places getSubprogram() can't return null. I think the following patch should suffice:

diff --git a/llvm/lib/Target/BPF/BTFDebug.cpp b/llvm/lib/Target/BPF/BTFDebug.cpp
index d886edaff555..6df421934b70 100644
--- a/llvm/lib/Target/BPF/BTFDebug.cpp
+++ b/llvm/lib/Target/BPF/BTFDebug.cpp
@@ -1634,8 +1635,8 @@ void BTFDebug::beginInstruction(const MachineInstr *MI) {
   if (!DL || PrevInstLoc == DL) {
     // This instruction will be skipped, no LineInfo has
     // been generated, construct one based on function signature.
-    if (LineInfoGenerated == false) {
-      auto *S = MI->getMF()->getFunction().getSubprogram();
+    DISubprogram *S = MI->getMF()->getFunction().getSubprogram();
+    if (!LineInfoGenerated && S) {
       MCSymbol *FuncLabel = Asm->getFunctionBegin();
       constructLineInfo(S, FuncLabel, S->getLine(), 0);
       LineInfoGenerated = true;

Sounds good! I'll do that. Can you share the C source code of the program as well please?

Sounds good! I'll do that. Can you share the C source code of the program as well please?

// clang -g --target=bpf -S -emit-llvm test2.c -o -
unsigned long foo(void) {
  return 42;
}

Modified to remove !dbg from function and ret instructions.

tamird updated this revision to Diff 546565.Aug 2 2023, 11:47 AM

Avoid crash, add test.

tamird edited the summary of this revision. (Show Details)Aug 2 2023, 11:47 AM
tamird retitled this revision from [BPF] Emit better error on missing line info to [BPF] Don't crash on missing line info.Aug 2 2023, 12:45 PM
tamird added subscribers: dave-tucker, ajwerner.
ast requested changes to this revision.Aug 2 2023, 4:38 PM
ast added inline comments.
llvm/lib/Target/BPF/BTFDebug.cpp
1378

parens

This revision now requires changes to proceed.Aug 2 2023, 4:38 PM
tamird updated this revision to Diff 546666.Aug 2 2023, 6:16 PM

Remove braces

tamird marked an inline comment as done.Aug 2 2023, 6:16 PM
tamird added inline comments.
llvm/lib/Target/BPF/BTFDebug.cpp
1378

Done.

tamird updated this revision to Diff 546667.Aug 2 2023, 6:19 PM
tamird marked an inline comment as done.

Less const

ast accepted this revision.Aug 2 2023, 6:35 PM

lgtm assuming all tests are green.

This revision is now accepted and ready to land.Aug 2 2023, 6:35 PM
tamird updated this revision to Diff 546740.Aug 3 2023, 1:08 AM

Remove one CHECK line.

This revision was automatically updated to reflect the committed changes.