Skip to content

Commit

Permalink
[codeview] Don't assert when the user violates the ODR
Browse files Browse the repository at this point in the history
If we have an array of a user-defined aggregates for which there was an
ODR violation, then the array size will not necessarily match the number
of elements times the size of the element.

Fixes PR32383

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@298750 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
rnk committed Mar 24, 2017
1 parent 6bf738a commit 4b2847e
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 28 deletions.
30 changes: 2 additions & 28 deletions lib/CodeGen/AsmPrinter/CodeViewDebug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1142,27 +1142,6 @@ TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {

uint64_t ElementSize = getBaseTypeSize(ElementTypeRef) / 8;


// We want to assert that the element type multiplied by the array lengths
// match the size of the overall array. However, if we don't have complete
// type information for the base type, we can't make this assertion. This
// happens if limited debug info is enabled in this case:
// struct VTableOptzn { VTableOptzn(); virtual ~VTableOptzn(); };
// VTableOptzn array[3];
// The DICompositeType of VTableOptzn will have size zero, and the array will
// have size 3 * sizeof(void*), and we should avoid asserting.
//
// There is a related bug in the front-end where an array of a structure,
// which was declared as incomplete structure first, ends up not getting a
// size assigned to it. (PR28303)
// Example:
// struct A(*p)[3];
// struct A { int f; } a[3];
bool PartiallyIncomplete = false;
if (Ty->getSizeInBits() == 0 || ElementSize == 0) {
PartiallyIncomplete = true;
}

// Add subranges to array type.
DINodeArray Elements = Ty->getElements();
for (int i = Elements.size() - 1; i >= 0; --i) {
Expand All @@ -1177,16 +1156,14 @@ TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
// Variable Length Array (VLA) has Count equal to '-1'.
// Replace with Count '1', assume it is the minimum VLA length.
// FIXME: Make front-end support VLA subrange and emit LF_DIMVARLU.
if (Count == -1) {
if (Count == -1)
Count = 1;
PartiallyIncomplete = true;
}

// Update the element size and element type index for subsequent subranges.
ElementSize *= Count;

// If this is the outermost array, use the size from the array. It will be
// more accurate if PartiallyIncomplete is true.
// more accurate if we had a VLA or an incomplete element type size.
uint64_t ArraySize =
(i == 0 && ElementSize == 0) ? Ty->getSizeInBits() / 8 : ElementSize;

Expand All @@ -1195,9 +1172,6 @@ TypeIndex CodeViewDebug::lowerTypeArray(const DICompositeType *Ty) {
ElementTypeIndex = TypeTable.writeKnownType(AR);
}

(void)PartiallyIncomplete;
assert(PartiallyIncomplete || ElementSize == (Ty->getSizeInBits() / 8));

return ElementTypeIndex;
}

Expand Down
100 changes: 100 additions & 0 deletions test/DebugInfo/COFF/array-odr-violation.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
; This tests that emitting CodeView arrays doesn't assert when an ODR violation
; makes our array dimension size calculations inaccurate. (PR32383)

; Here was the scenario:
; $ cat a.cpp
; typedef union YYSTYPE { int x; } YYSTYPE;
; YYSTYPE a;
; $ cat b.cpp
; typedef union YYSTYPE { char x; } YYSTYPE;
; void fn1() { YYSTYPE a[1]; }
; $ clang-cl -c -Zi -flto a.cpp b.cpp
; $ llvm-link a.obj b.obj -S -o t.ll # This is the test case IR.
; $ llc t.ll # Used to assert

; RUN: llc < %s | FileCheck %s

; FIXME: sizeof(a) in the user program is 1, but we claim it is 4 because
; sometimes the frontend lies to us. See array-types-advanced.ll for an example.
;
; CHECK: Array ({{.*}}) {
; CHECK: TypeLeafKind: LF_ARRAY (0x1503)
; CHECK: ElementType: YYSTYPE ({{.*}})
; CHECK: IndexType: unsigned __int64 (0x23)
; CHECK: SizeOf: 4
; CHECK: Name:
; CHECK: }

; sizeof(YYSTYPE) == 4
; CHECK: Union ({{.*}}) {
; CHECK: TypeLeafKind: LF_UNION (0x1506)
; CHECK: MemberCount: 1
; CHECK: Properties [ (0x600)
; CHECK: HasUniqueName (0x200)
; CHECK: Sealed (0x400)
; CHECK: ]
; CHECK: FieldList: <field list>
; CHECK: SizeOf: 4
; CHECK: Name: YYSTYPE
; CHECK: LinkageName: .?ATYYSTYPE@@
; CHECK: }

; ModuleID = 'llvm-link'
source_filename = "llvm-link"
target datalayout = "e-m:w-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-pc-windows-msvc19.10.24728"

%union.YYSTYPE = type { i32 }
%union.YYSTYPE.0 = type { i8 }

@"\01?a@@3TYYSTYPE@@A" = global %union.YYSTYPE zeroinitializer, align 4, !dbg !0

; Function Attrs: noinline nounwind sspstrong uwtable
define void @"\01?fn1@@YAXXZ"() #0 !dbg !21 {
entry:
%a = alloca [1 x %union.YYSTYPE.0], align 1
call void @llvm.dbg.declare(metadata [1 x %union.YYSTYPE.0]* %a, metadata !24, metadata !29), !dbg !30
ret void, !dbg !30
}

; Function Attrs: nounwind readnone
declare void @llvm.dbg.declare(metadata, metadata, metadata) #1

attributes #0 = { noinline nounwind sspstrong uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "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" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
attributes #1 = { nounwind readnone }

!llvm.dbg.cu = !{!2, !11}
!llvm.ident = !{!13, !13}
!llvm.module.flags = !{!14, !18, !19, !20}

!0 = !DIGlobalVariableExpression(var: !1)
!1 = distinct !DIGlobalVariable(name: "a", linkageName: "\01?a@@3TYYSTYPE@@A", scope: !2, file: !3, line: 2, type: !6, isLocal: false, isDefinition: true)
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "clang version 5.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4, globals: !5)
!3 = !DIFile(filename: "a.cpp", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "c0005139aa3df153c30d8c6953390a4b")
!4 = !{}
!5 = !{!0}
!6 = !DIDerivedType(tag: DW_TAG_typedef, name: "YYSTYPE", file: !3, line: 1, baseType: !7)
!7 = distinct !DICompositeType(tag: DW_TAG_union_type, name: "YYSTYPE", file: !3, line: 1, size: 32, elements: !8, identifier: ".?ATYYSTYPE@@")
!8 = !{!9}
!9 = !DIDerivedType(tag: DW_TAG_member, name: "x", scope: !7, file: !3, line: 1, baseType: !10, size: 32)
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!11 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !12, producer: "clang version 5.0.0 ", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, enums: !4)
!12 = !DIFile(filename: "b.cpp", directory: "C:\5Csrc\5Cllvm-project\5Cbuild", checksumkind: CSK_MD5, checksum: "9cfd390d8827beab36769147bb037abc")
!13 = !{!"clang version 5.0.0 "}
!14 = !{i32 6, !"Linker Options", !15}
!15 = !{!16, !17}
!16 = !{!"/DEFAULTLIB:libcmt.lib"}
!17 = !{!"/DEFAULTLIB:oldnames.lib"}
!18 = !{i32 2, !"CodeView", i32 1}
!19 = !{i32 2, !"Debug Info Version", i32 3}
!20 = !{i32 1, !"PIC Level", i32 2}
!21 = distinct !DISubprogram(name: "fn1", linkageName: "\01?fn1@@YAXXZ", scope: !12, file: !12, line: 2, type: !22, isLocal: false, isDefinition: true, scopeLine: 2, flags: DIFlagPrototyped, isOptimized: false, unit: !11, variables: !4)
!22 = !DISubroutineType(types: !23)
!23 = !{null}
!24 = !DILocalVariable(name: "a", scope: !21, file: !12, line: 2, type: !25)
!25 = !DICompositeType(tag: DW_TAG_array_type, baseType: !26, size: 8, elements: !27)
!26 = !DIDerivedType(tag: DW_TAG_typedef, name: "YYSTYPE", file: !12, line: 1, baseType: !7)
!27 = !{!28}
!28 = !DISubrange(count: 1)
!29 = !DIExpression()
!30 = !DILocation(line: 2, scope: !21)

0 comments on commit 4b2847e

Please sign in to comment.