Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6bc5c7c
[ubsan] Add more -fsanitize-annotate-debug-info checks
thurstond May 28, 2025
41735cb
Refactor to use explicit CheckOrdinal variable
thurstond May 29, 2025
3e53462
Mention why void* is used instead of ApplyDebugLocation*
thurstond May 29, 2025
fbadcee
!ApplyTrapDI assertion
thurstond May 29, 2025
6f82a48
Cleanup
thurstond May 29, 2025
58d76a6
ApplyTrapDI and stdin cleanup
thurstond May 29, 2025
4f9a4de
Add missing SanitizerOrdinal to SanitizerScope
thurstond May 29, 2025
4041270
Rename to __ubsan_check_debug_info_anchor
thurstond May 30, 2025
93ef358
Unnecessary braces
thurstond May 30, 2025
5da37cd
Update tests
thurstond May 30, 2025
9dee8ea
Use SanitizerHandler in name
thurstond May 31, 2025
05d047b
Simplify no-handler case
thurstond Jun 2, 2025
827dcbd
Add note about SO_Vptr ambiguity
thurstond Jun 2, 2025
2a816fe
Florian1 feedback
thurstond Jun 2, 2025
ed2c9b5
Use std::make_unique
thurstond Jun 2, 2025
16d6bee
Remove useless assert
thurstond Jun 2, 2025
8176e88
Merge remote-tracking branch 'origin/main' into ubsan_annotate
vitalybuka Jun 3, 2025
2eee786
SanitizerDebugLocation
vitalybuka Jun 3, 2025
637c4a2
Merge remote-tracking branch 'origin/main' into ubsan_annotate
vitalybuka Jun 3, 2025
ff5ffbf
Replace manual ApplyDebugLocation annotation with SanitizerDebugLocation
thurstond Jun 3, 2025
1df8fe4
Revert "Replace manual ApplyDebugLocation annotation with SanitizerDe…
thurstond Jun 3, 2025
aead894
Replace manual ApplyDebugLocation with SanitizerDebugLocation, and also
thurstond Jun 3, 2025
11a74d1
Refactor EmitTypeCheck into three more precise SanitizerDebugLocations
thurstond Jun 3, 2025
be32bd7
assert(IsSanitizerScope); in EmitVTablePtrCheck
thurstond Jun 3, 2025
81ecf62
Re-run CI
thurstond Jun 3, 2025
5a510a3
Use ordinal for label if unique
thurstond Jun 4, 2025
31291d0
Undo test change
thurstond Jun 4, 2025
3dedb83
Remove newline
thurstond Jun 4, 2025
0ded77d
Merge remote-tracking branch 'upstream' into ubsan_annotate (and
thurstond Jun 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 38 additions & 6 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6421,9 +6421,7 @@ CodeGenFunction::LexicalScope::~LexicalScope() {
}
}

llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
SanitizerHandler Handler) {
static std::string SanitizerHandlerToCheckLabel(SanitizerHandler Handler) {
std::string Label;
switch (Handler) {
#define SANITIZER_CHECK(Enum, Name, Version) \
Expand All @@ -6435,10 +6433,44 @@ llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
#undef SANITIZER_CHECK
};

// Label doesn't require sanitization

return Label;
}

static std::string
SanitizerOrdinalToCheckLabel(SanitizerKind::SanitizerOrdinal Ordinal) {
std::string Label;
switch (Ordinal) {
#define SANITIZER(NAME, ID) \
case SanitizerKind::SO_##ID: \
Label = "__ubsan_check_" NAME; \
break;
#include "clang/Basic/Sanitizers.def"
default:
llvm_unreachable("unexpected sanitizer kind");
}

// Sanitize label (convert hyphens to underscores; also futureproof against
// non-alpha)
for (unsigned int i = 0; i < Label.length(); i++)
if (!std::isalpha(Label[i]))
Label[i] = '_';

return Label;
}

llvm::DILocation *CodeGenFunction::SanitizerAnnotateDebugInfo(
ArrayRef<SanitizerKind::SanitizerOrdinal> Ordinals,
SanitizerHandler Handler) {
std::string Label;
if (Ordinals.size() == 1)
Label = SanitizerOrdinalToCheckLabel(Ordinals[0]);
else
Label = SanitizerHandlerToCheckLabel(Handler);

llvm::DILocation *CheckDI = Builder.getCurrentDebugLocation();

// TODO: the annotation could be more precise e.g.,
// use the ordinal name if there is only one ordinal
for (auto Ord : Ordinals) {
// TODO: deprecate ClArrayBoundsPseudoFn
if (((ClArrayBoundsPseudoFn && Ord == SanitizerKind::SO_ArrayBounds) ||
Expand All @@ -6463,4 +6495,4 @@ SanitizerDebugLocation::SanitizerDebugLocation(
SanitizerDebugLocation::~SanitizerDebugLocation() {
assert(CGF->IsSanitizerScope);
CGF->IsSanitizerScope = false;
}
}
8 changes: 4 additions & 4 deletions clang/test/CodeGen/bounds-checking-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ double f1(int b, int i) {

//.
// CHECK-TRAP: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
// CHECK-TRAP: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// CHECK-TRAP: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// CHECK-TRAP: [[DBG4]] = distinct !DISubprogram(name: "f1", scope: [[META5:![0-9]+]], file: [[META5]], line: 63, type: [[META6:![0-9]+]], scopeLine: 63, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META10]])
// CHECK-TRAP: [[META5]] = !DIFile(filename: "{{.*}}bounds-checking-debuginfo.c", directory: {{.*}})
// CHECK-TRAP: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
Expand All @@ -89,14 +89,14 @@ double f1(int b, int i) {
// CHECK-TRAP: [[DBG21]] = !DILocation(line: 65, column: 3, scope: [[DBG4]])
// CHECK-TRAP: [[DBG22]] = !DILocation(line: 66, column: 12, scope: [[DBG4]])
// CHECK-TRAP: [[DBG23]] = !DILocation(line: 0, scope: [[META24:![0-9]+]], inlinedAt: [[DBG26]])
// CHECK-TRAP: [[META24]] = distinct !DISubprogram(name: "__ubsan_check_out_of_bounds", scope: [[META5]], file: [[META5]], type: [[META25:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK-TRAP: [[META24]] = distinct !DISubprogram(name: "__ubsan_check_array_bounds", scope: [[META5]], file: [[META5]], type: [[META25:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK-TRAP: [[META25]] = !DISubroutineType(types: null)
// CHECK-TRAP: [[DBG26]] = !DILocation(line: 66, column: 10, scope: [[DBG4]])
// CHECK-TRAP: [[PROF27]] = !{!"branch_weights", i32 1048575, i32 1}
// CHECK-TRAP: [[DBG28]] = !DILocation(line: 66, column: 3, scope: [[DBG4]])
//.
// CHECK-NOTRAP: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
// CHECK-NOTRAP: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// CHECK-NOTRAP: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// CHECK-NOTRAP: [[DBG4]] = distinct !DISubprogram(name: "f1", scope: [[META5:![0-9]+]], file: [[META5]], line: 63, type: [[META6:![0-9]+]], scopeLine: 63, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: [[META0]], retainedNodes: [[META10]])
// CHECK-NOTRAP: [[META5]] = !DIFile(filename: "{{.*}}bounds-checking-debuginfo.c", directory: {{.*}})
// CHECK-NOTRAP: [[META6]] = !DISubroutineType(types: [[META7:![0-9]+]])
Expand All @@ -117,7 +117,7 @@ double f1(int b, int i) {
// CHECK-NOTRAP: [[DBG21]] = !DILocation(line: 65, column: 3, scope: [[DBG4]])
// CHECK-NOTRAP: [[DBG22]] = !DILocation(line: 66, column: 12, scope: [[DBG4]])
// CHECK-NOTRAP: [[DBG23]] = !DILocation(line: 0, scope: [[META24:![0-9]+]], inlinedAt: [[DBG26]])
// CHECK-NOTRAP: [[META24]] = distinct !DISubprogram(name: "__ubsan_check_out_of_bounds", scope: [[META5]], file: [[META5]], type: [[META25:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK-NOTRAP: [[META24]] = distinct !DISubprogram(name: "__ubsan_check_array_bounds", scope: [[META5]], file: [[META5]], type: [[META25:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK-NOTRAP: [[META25]] = !DISubroutineType(types: null)
// CHECK-NOTRAP: [[DBG26]] = !DILocation(line: 66, column: 10, scope: [[DBG4]])
// CHECK-NOTRAP: [[PROF27]] = !{!"branch_weights", i32 1048575, i32 1}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/cfi-check-fail-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void caller(void (*f)(void)) {
}
//.
// CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
// CHECK: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// CHECK: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// CHECK: [[DBG7]] = distinct !DISubprogram(name: "caller", scope: [[META8:![0-9]+]], file: [[META8]], line: 22, type: [[META9:![0-9]+]], scopeLine: 22, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META14:![0-9]+]])
// CHECK: [[META8]] = !DIFile(filename: "{{.*}}cfi-check-fail-debuginfo.c", directory: {{.*}})
// CHECK: [[META9]] = !DISubroutineType(types: [[META10:![0-9]+]])
Expand All @@ -39,7 +39,7 @@ void caller(void (*f)(void)) {
// CHECK: [[META18]] = !{i64 0, i64 2451761621477796417}
// CHECK: [[META19]] = !DILocation(line: 0, scope: [[DBG7]])
// CHECK: [[DBG20]] = !DILocation(line: 0, scope: [[META21:![0-9]+]], inlinedAt: [[DBG23]])
// CHECK: [[META21]] = distinct !DISubprogram(name: "__ubsan_check_cfi_check_fail", scope: [[META8]], file: [[META8]], type: [[META22:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META21]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META8]], file: [[META8]], type: [[META22:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META22]] = !DISubroutineType(types: null)
// CHECK: [[DBG23]] = !DILocation(line: 23, column: 3, scope: [[DBG7]])
// CHECK: [[META24]] = !{}
Expand Down
8 changes: 4 additions & 4 deletions clang/test/CodeGen/cfi-icall-generalize-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void g(int** (*fp)(const char *, const char **)) {

//.
// UNGENERALIZED: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: [[META2:![0-9]+]], splitDebugInlining: false, nameTableKind: None)
// UNGENERALIZED: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// UNGENERALIZED: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// UNGENERALIZED: [[META2]] = !{[[META3:![0-9]+]]}
// UNGENERALIZED: [[META3]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META4:![0-9]+]], size: 64)
// UNGENERALIZED: [[META4]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META5:![0-9]+]], size: 64)
Expand Down Expand Up @@ -85,15 +85,15 @@ void g(int** (*fp)(const char *, const char **)) {
// UNGENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
// UNGENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
// UNGENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
// UNGENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_check_fail", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// UNGENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// UNGENERALIZED: [[META36]] = !DISubroutineType(types: null)
// UNGENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// UNGENERALIZED: [[META38]] = !{}
// UNGENERALIZED: [[PROF39]] = !{!"branch_weights", i32 1048575, i32 1}
// UNGENERALIZED: [[DBG40]] = !DILocation(line: 54, column: 1, scope: [[DBG25]])
//.
// GENERALIZED: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, retainedTypes: [[META2:![0-9]+]], splitDebugInlining: false, nameTableKind: None)
// GENERALIZED: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// GENERALIZED: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// GENERALIZED: [[META2]] = !{[[META3:![0-9]+]]}
// GENERALIZED: [[META3]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META4:![0-9]+]], size: 64)
// GENERALIZED: [[META4]] = !DIDerivedType(tag: DW_TAG_pointer_type, baseType: [[META5:![0-9]+]], size: 64)
Expand Down Expand Up @@ -123,7 +123,7 @@ void g(int** (*fp)(const char *, const char **)) {
// GENERALIZED: [[META32]] = !{i64 0, !"_ZTSFvPvE.generalized"}
// GENERALIZED: [[META33]] = !DILocation(line: 0, scope: [[DBG25]])
// GENERALIZED: [[DBG34]] = !DILocation(line: 0, scope: [[META35:![0-9]+]], inlinedAt: [[DBG37]])
// GENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_check_fail", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// GENERALIZED: [[META35]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META11]], file: [[META11]], type: [[META36:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// GENERALIZED: [[META36]] = !DISubroutineType(types: null)
// GENERALIZED: [[DBG37]] = !DILocation(line: 53, column: 3, scope: [[DBG25]])
// GENERALIZED: [[META38]] = !{}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/cfi-icall-normalize2-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void baz(void (*fn)(int, int, int), int arg1, int arg2, int arg3) {

//.
// CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
// CHECK: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// CHECK: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// CHECK: [[DBG7]] = distinct !DISubprogram(name: "foo", scope: [[META8:![0-9]+]], file: [[META8]], line: 24, type: [[META9:![0-9]+]], scopeLine: 24, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META15:![0-9]+]])
// CHECK: [[META8]] = !DIFile(filename: "{{.*}}cfi-icall-normalize2-debuginfo.c", directory: {{.*}})
// CHECK: [[META9]] = !DISubroutineType(types: [[META10:![0-9]+]])
Expand All @@ -82,7 +82,7 @@ void baz(void (*fn)(int, int, int), int arg1, int arg2, int arg3) {
// CHECK: [[META19]] = !{i64 0, !"_ZTSFvPvu3i32E.normalized.generalized"}
// CHECK: [[META20]] = !DILocation(line: 0, scope: [[DBG7]])
// CHECK: [[DBG21]] = !DILocation(line: 0, scope: [[META22:![0-9]+]], inlinedAt: [[DBG24]])
// CHECK: [[META22]] = distinct !DISubprogram(name: "__ubsan_check_cfi_check_fail", scope: [[META8]], file: [[META8]], type: [[META23:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META22]] = distinct !DISubprogram(name: "__ubsan_check_cfi_icall", scope: [[META8]], file: [[META8]], type: [[META23:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META23]] = !DISubroutineType(types: null)
// CHECK: [[DBG24]] = !DILocation(line: 25, column: 5, scope: [[DBG7]])
// CHECK: [[META25]] = !{}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/CodeGen/ubsan-function-debuginfo.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void call_no_prototype(void (*f)()) { f(); }
void call_prototype(void (*f)(void)) { f(); }
//.
// CHECK: [[META0:![0-9]+]] = distinct !DICompileUnit(language: DW_LANG_C11, file: [[META1:![0-9]+]], isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
// CHECK: [[META1]] = !DIFile(filename: "<stdin>", directory: {{.*}})
// CHECK: [[META1]] = !DIFile(filename: "{{.*}}<stdin>", directory: {{.*}})
// CHECK: [[DBG5]] = distinct !DISubprogram(name: "call_no_prototype", scope: [[META6:![0-9]+]], file: [[META6]], line: 14, type: [[META7:![0-9]+]], scopeLine: 14, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: [[META0]], retainedNodes: [[META12:![0-9]+]])
// CHECK: [[META6]] = !DIFile(filename: "{{.*}}ubsan-function-debuginfo.c", directory: {{.*}})
// CHECK: [[META7]] = !DISubroutineType(types: [[META8:![0-9]+]])
Expand All @@ -62,7 +62,7 @@ void call_prototype(void (*f)(void)) { f(); }
// CHECK: [[META26]] = !{i32 -1056584962, i32 -747727454}
// CHECK: [[META27]] = !DILocation(line: 0, scope: [[DBG18]])
// CHECK: [[DBG28]] = !DILocation(line: 0, scope: [[META29:![0-9]+]], inlinedAt: [[DBG31]])
// CHECK: [[META29]] = distinct !DISubprogram(name: "__ubsan_check_function_type_mismatch", scope: [[META6]], file: [[META6]], type: [[META30:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META29]] = distinct !DISubprogram(name: "__ubsan_check_function", scope: [[META6]], file: [[META6]], type: [[META30:![0-9]+]], flags: DIFlagArtificial, spFlags: DISPFlagDefinition, unit: [[META0]])
// CHECK: [[META30]] = !DISubroutineType(types: null)
// CHECK: [[DBG31]] = !DILocation(line: 37, column: 40, scope: [[DBG18]])
// CHECK: [[META32]] = !{}
Expand Down