Skip to content
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

[llvm-gsymutil] Fix assert failure on FileEntry.Dir empty #79926

Merged
merged 2 commits into from
Jan 30, 2024

Conversation

kusmour
Copy link
Contributor

@kusmour kusmour commented Jan 30, 2024

Summary:
FileEntry.Dir can be empty if debug info only contains relative path. This caused an assertion failure when gsym segmentation is trying to copy a file entry with empty dir. As the fitst entry of StringTable is always empty (and is preserved), StringOffsetMap doesn't have key 0. Hence, find(0) returns End and operator->() fails the assertion

Test Plan:
./bin/llvm-lit -sv llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml

Summary:
FileEntry.Dir can be empty if debug info only contains relative path.
This caused an assertion failure when gsym segmentation is trying to copy a file entry with empty dir.
As the fitst entry of StringTable is always empty (and is preserved), `StringOffsetMap` doesn't have key 0.
Hence, `find(0)` returns `End` and `operator->()` fails the assertion

Test Plan:
./bin/llvm-lit -sv llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml
@llvmbot
Copy link
Collaborator

llvmbot commented Jan 30, 2024

@llvm/pr-subscribers-debuginfo

Author: kusmour (kusmour)

Changes

Summary:
FileEntry.Dir can be empty if debug info only contains relative path. This caused an assertion failure when gsym segmentation is trying to copy a file entry with empty dir. As the fitst entry of StringTable is always empty (and is preserved), StringOffsetMap doesn't have key 0. Hence, find(0) returns End and operator->() fails the assertion

Test Plan:
./bin/llvm-lit -sv llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml


Full diff: https://github.com/llvm/llvm-project/pull/79926.diff

2 Files Affected:

  • (modified) llvm/lib/DebugInfo/GSYM/GsymCreator.cpp (+4-1)
  • (added) llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml (+108)
diff --git a/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp b/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
index ee7b0efba5ea47..74138755090a45 100644
--- a/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
+++ b/llvm/lib/DebugInfo/GSYM/GsymCreator.cpp
@@ -55,7 +55,10 @@ uint32_t GsymCreator::copyFile(const GsymCreator &SrcGC, uint32_t FileIdx) {
     return 0;
   const FileEntry SrcFE = SrcGC.Files[FileIdx];
   // Copy the strings for the file and then add the newly converted file entry.
-  uint32_t Dir = StrTab.add(SrcGC.StringOffsetMap.find(SrcFE.Dir)->second);
+  uint32_t Dir =
+      SrcFE.Dir == 0
+          ? 0
+          : StrTab.add(SrcGC.StringOffsetMap.find(SrcFE.Dir)->second);
   uint32_t Base = StrTab.add(SrcGC.StringOffsetMap.find(SrcFE.Base)->second);
   FileEntry DstFE(Dir, Base);
   return insertFileEntry(DstFE);
diff --git a/llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml b/llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml
new file mode 100644
index 00000000000000..6f6acf88004884
--- /dev/null
+++ b/llvm/test/tools/llvm-gsymutil/X86/elf-empty-dir.yaml
@@ -0,0 +1,108 @@
+## Test converting DWARF using relative path
+
+# RUN: yaml2obj %s -o %t
+# RUN: llvm-gsymutil --convert %t -o %t.gsym --segment-size=100000 --num-threads=80 --quiet 2>&1 | FileCheck %s --check-prefix=CONVERT
+
+# CONVERT: Input file: {{.*\.yaml\.tmp}}
+# CONVERT: Output file (x86_64): {{.*\.yaml\.tmp\.gsym}}
+# CONVERT: Pruned 0 functions, ended with 1 total
+
+--- !ELF
+FileHeader:
+  Class:   ELFCLASS64
+  Data:    ELFDATA2LSB
+  Type:    ET_EXEC
+  Machine: EM_X86_64
+DWARF:
+  debug_str:
+    - ''
+    - main.cpp
+    - foo
+  debug_abbrev:
+    - ID:              0
+      Table:
+        - Code:            0x1
+          Tag:             DW_TAG_compile_unit
+          Children:        DW_CHILDREN_yes
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_language
+              Form:            DW_FORM_udata
+            - Attribute:       DW_AT_stmt_list
+              Form:            DW_FORM_sec_offset
+        - Code:            0x2
+          Tag:             DW_TAG_subprogram
+          Children:        DW_CHILDREN_no
+          Attributes:
+            - Attribute:       DW_AT_name
+              Form:            DW_FORM_strp
+            - Attribute:       DW_AT_low_pc
+              Form:            DW_FORM_addr
+            - Attribute:       DW_AT_high_pc
+              Form:            DW_FORM_addr
+  debug_info:
+    - Length:          0x27
+      Version:         4
+      AbbrevTableID:   0
+      AbbrOffset:      0x0
+      AddrSize:        8
+      Entries:
+        - AbbrCode:        0x1
+          Values:
+            - Value:           0x1
+            - Value:           0x2
+            - Value:           0x0
+        - AbbrCode:        0x2
+          Values:
+            - Value:           0xA
+            - Value:           0x1000
+            - Value:           0x1050
+        - AbbrCode:        0x0
+  debug_line:
+    - Length:          66
+      Version:         2
+      PrologueLength:  31
+      MinInstLength:   1
+      DefaultIsStmt:   1
+      LineBase:        251
+      LineRange:       14
+      OpcodeBase:      13
+      StandardOpcodeLengths: [ 0, 1, 1, 1, 1, 0, 0, 0, 1, 0, 0, 1 ]
+      Files:
+        - Name:            main.cpp
+          DirIdx:          0
+          ModTime:         0
+          Length:          0
+      Opcodes:
+        - Opcode:          DW_LNS_extended_op
+          ExtLen:          9
+          SubOpcode:       DW_LNE_set_address
+          Data:            4096
+        - Opcode:          DW_LNS_advance_line
+          SData:           9
+          Data:            0
+        - Opcode:          DW_LNS_copy
+          Data:            0
+        - Opcode:          DW_LNS_advance_pc
+          Data:            16
+        - Opcode:          DW_LNS_advance_line
+          SData:           1
+          Data:            0
+        - Opcode:          DW_LNS_copy
+          Data:            0
+        - Opcode:          DW_LNS_advance_line
+          SData:           1
+          Data:            0
+        - Opcode:          DW_LNS_copy
+          Data:            0
+        - Opcode:          DW_LNS_advance_pc
+          Data:            64
+        - Opcode:          DW_LNS_advance_line
+          SData:           1
+          Data:            0
+        - Opcode:          DW_LNS_extended_op
+          ExtLen:          1
+          SubOpcode:       DW_LNE_end_sequence
+          Data:            0
+...

@kusmour kusmour merged commit 59c9a48 into llvm:main Jan 30, 2024
3 of 4 checks passed
@kusmour kusmour deleted the llvm-gsymutil-bugfix branch January 30, 2024 01:41
Copy link
Collaborator

@clayborg clayborg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just remove the "--num-threads" option from the test yaml file and this is good to go.

## Test converting DWARF using relative path

# RUN: yaml2obj %s -o %t
# RUN: llvm-gsymutil --convert %t -o %t.gsym --segment-size=100000 --num-threads=80 --quiet 2>&1 | FileCheck %s --check-prefix=CONVERT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't specify the number of threads, they will automatically be set to a good value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants