Skip to content

Commit b006756

Browse files
[InstCombine] Fix crash when alloc functions are missing alloc-family (#138310)
Fixes #63477 by bailing out instead of crashing. Co-authored-by: Jamie <[email protected]>
1 parent e92013c commit b006756

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3300,16 +3300,14 @@ static bool isAllocSiteRemovable(Instruction *AI,
33003300
continue;
33013301
}
33023302

3303-
if (getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
3303+
if (Family && getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
33043304
getAllocationFamily(I, &TLI) == Family) {
3305-
assert(Family);
33063305
Users.emplace_back(I);
33073306
continue;
33083307
}
33093308

3310-
if (getReallocatedOperand(cast<CallBase>(I)) == PI &&
3309+
if (Family && getReallocatedOperand(cast<CallBase>(I)) == PI &&
33113310
getAllocationFamily(I, &TLI) == Family) {
3312-
assert(Family);
33133311
Users.emplace_back(I);
33143312
Worklist.push_back(I);
33153313
continue;

llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
define dso_local i32 @_Z6answeri(i32 %0) {
55
; CHECK-LABEL: @_Z6answeri(
6-
; CHECK-NEXT: [[TMP2:%.*]] = call noalias nonnull dereferenceable(80) ptr @_Znam(i64 80) #[[ATTR2:[0-9]+]]
6+
; CHECK-NEXT: [[TMP2:%.*]] = call noalias nonnull dereferenceable(80) ptr @_Znam(i64 80) #[[ATTR4:[0-9]+]]
77
; CHECK-NEXT: call void @free(ptr [[TMP2]])
88
; CHECK-NEXT: ret i32 42
99
;
@@ -25,11 +25,29 @@ define void @test_alloca() {
2525
ret void
2626
}
2727

28+
; Test that missing `alloc-family` attributes don't crash LLVM
29+
; https://github.com/llvm/llvm-project/issues/63749
30+
31+
define void @no_family() {
32+
; CHECK-LABEL: @no_family(
33+
; CHECK-NEXT: [[ALLOC:%.*]] = call ptr @customalloc(i64 64)
34+
; CHECK-NEXT: call void @customfree(ptr [[ALLOC]])
35+
; CHECK-NEXT: ret void
36+
;
37+
%alloc = call ptr @customalloc(i64 64)
38+
call void @customfree(ptr %alloc)
39+
ret void
40+
}
41+
42+
2843
; Function Attrs: nobuiltin allocsize(0)
2944
declare dso_local nonnull ptr @_Znam(i64) #1
3045

3146
; Function Attrs: nounwind
3247
declare dso_local void @free(ptr) allockind("free") "alloc-family"="malloc"
3348

49+
declare ptr @customalloc(i64) allockind("alloc")
50+
declare void @customfree(ptr allocptr) allockind("free")
51+
3452
attributes #0 = { builtin allocsize(0) }
3553
attributes #1 = { nobuiltin allocsize(0) allockind("alloc,uninitialized") "alloc-family"="_Znam" }

0 commit comments

Comments
 (0)