<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/70787>70787</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LLVM][DebugInfo] Strengthen DI metadata verifiers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            debuginfo
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Dinistro
      </td>
    </tr>
</table>

<pre>
    I recently ran into a crash caused by invalid debug metadata. Instead of throwing a verifier issue, the following reduced example explodes in the ASMPrinter. After building clang in Debug mode, I realized that the `baseType` field of the `DICompositeType` is mandatory (at least when it's an array type). While the debug mode triggers an assertion instead of running into a SegFault, it is not directly clear what pass produced the faulty debug metadata.

**I suggest that we try to strengthen the verifiers to catch more invalid DI metadata**, considering that this can be produced by many different frontends. In my case, there is a bug in MLIR, that only manifests this late in the pipeline.


Example (invalid due to lacking a `baseType` field on the `DICompositeType`): 
```
define i32 @func(ptr %0) #0 !dbg !3 {
  call void @llvm.dbg.value(metadata ptr %0, metadata !6, metadata !DIExpression()), !dbg !22
  ret i32 0
}

; Function Attrs: nocallback nofree nosync nounwind speculatable willreturn memory(none)
declare void @llvm.dbg.value(metadata, metadata, metadata) #1

attributes #0 = { "function-instrument"="xray-always" }
attributes #1 = { nocallback nofree nosync nounwind speculatable willreturn memory(none) }

!llvm.module.flags = !{!0}
!llvm.dbg.cu = !{!1}

!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C11, file: !2, producer: "clang", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
!2 = !DIFile(filename: "file.c", directory: "/")
!3 = distinct !DISubprogram(name: "func", scope: !2, file: !2, line: 46, type: !4, scopeLine: 48, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !1)
!4 = distinct !DISubroutineType(types: !5)
!5 = !{}
!6 = !DILocalVariable(name: "op", arg: 5, scope: !3, file: !2, line: 47, type: !19)
!19 = !DICompositeType(tag: DW_TAG_array_type, size: 2624, elements: !20)
!20 = !{!21}
!21 = !DISubrange(count: 41)
!22 = !DILocation(line: 0, scope: !3)
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVl1v47gO_TXKC9HAlpvEechDGk8Gwe3cO7idnXksZJt2tCNLhj7aen79grLz1e3uviwQuLUkkoeHh7SEc7LViBu2eGCLYiaCPxq7KaSWzlszK009bA5gsULt1QBWaJDaGxBQWeGOUIngsIZyAKlfhJI11FiGFjr0ohZezOGgnUdRg2nAH615lboFAS9oZSPRgnQuIOM78EeExig1nrBYhwprwDfR9QoB33planQgdTy5ffry1Urt0c5h23i0UAapajKtlNAtnStGJKaO_ikLoeQvrMEfhY9e2DIphcNvQ49smUAjUU1A415x2JmuN0768xHpoBO6Ft7YARjPhQeFwnl4PaIG6RlfORAahLViAE9WfD2HH0epMLqtz6DAW9m2aMfzzqH10hC9Z75s0FrGXCLjT9juRVCespGeoGjjoZYWK6pNpVBYeKXceuEc9NaMHEZmyXB4XxyWFCzZTk9OvwO40Lbo_EjSK6EcwBtw3qJuPWVJ_k71c7RXCV8doTMWzyooDucoo2MCXRntZI2WcppqIB1UQkOJF7jlQBQPUMumQYvaQ2ON9qhrR2qCboBKuJNmKKYDAZSX1PDl8fD_cUd4MFpFX7JB590YTQmPJxH1skclNd7yEJ-fJt0xnp-FHZCSVaL6OWr4Y_Xov1YP42uWbWGKs0ymX3ytsZEaQWYc2H3SBF0xnvfeAuMLMgTGswQYT-uypT8ZsNXDaApQCaXgxciabJV66eZ12c5fhKLWyk-FgIu73bk65Gv5fqE4fHrrLTonjWY8J9z0213F5_wU3KKPsKdE2Kq4oTN7gH3QVdT21nvriAJtCHIpqp-gTWMRQRs36Aq0CfpV6hpcj1VQwotSIbxKpSz6YDV02Bk7MJ5ro6m3TuxVSlj8Zw6uM739PzKcXkMX3ltZBo9uIj8riHRgnDd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