[llvm] Fix crash when alloc functions are missing `alloc-family` (PR #138310)

via llvm-commits llvm-commits at lists.llvm.org
Fri May 2 14:32:04 PDT 2025


https://github.com/clubby789 updated https://github.com/llvm/llvm-project/pull/138310

>From 7beec2c754537da32b7661ccd9c7d4f763c89fae Mon Sep 17 00:00:00 2001
From: Jamie <jamie at osec.io>
Date: Fri, 2 May 2025 18:08:04 +0100
Subject: [PATCH] Fix crash when alloc functions are missing `alloc-family`

---
 .../InstCombine/InstructionCombining.cpp      |  6 ++----
 .../InstCombine/malloc-free-mismatched.ll     | 20 ++++++++++++++++++-
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index f807f5f4519fc..7494244d4b3c0 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3300,16 +3300,14 @@ static bool isAllocSiteRemovable(Instruction *AI,
           continue;
         }
 
-        if (getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
+        if (Family && getFreedOperand(cast<CallBase>(I), &TLI) == PI &&
             getAllocationFamily(I, &TLI) == Family) {
-          assert(Family);
           Users.emplace_back(I);
           continue;
         }
 
-        if (getReallocatedOperand(cast<CallBase>(I)) == PI &&
+        if (Family && getReallocatedOperand(cast<CallBase>(I)) == PI &&
             getAllocationFamily(I, &TLI) == Family) {
-          assert(Family);
           Users.emplace_back(I);
           Worklist.push_back(I);
           continue;
diff --git a/llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll b/llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll
index 658dbd662508c..5f31d787839e2 100644
--- a/llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll
+++ b/llvm/test/Transforms/InstCombine/malloc-free-mismatched.ll
@@ -3,7 +3,7 @@
 
 define dso_local i32 @_Z6answeri(i32 %0) {
 ; CHECK-LABEL: @_Z6answeri(
-; CHECK-NEXT:    [[TMP2:%.*]] = call noalias nonnull dereferenceable(80) ptr @_Znam(i64 80) #[[ATTR2:[0-9]+]]
+; CHECK-NEXT:    [[TMP2:%.*]] = call noalias nonnull dereferenceable(80) ptr @_Znam(i64 80) #[[ATTR4:[0-9]+]]
 ; CHECK-NEXT:    call void @free(ptr [[TMP2]])
 ; CHECK-NEXT:    ret i32 42
 ;
@@ -25,11 +25,29 @@ define void @test_alloca() {
   ret void
 }
 
+; Test that missing `alloc-family` attributes don't crash LLVM
+; https://github.com/llvm/llvm-project/issues/63749
+
+define void @no_family() {
+; CHECK-LABEL: @no_family(
+; CHECK-NEXT:       [[ALLOC:%.*]] = call ptr @customalloc(i64 64)
+; CHECK-NEXT:       call void @customfree(ptr [[ALLOC]])
+; CHECK-NEXT:       ret void
+;
+  %alloc = call ptr @customalloc(i64 64)
+  call void @customfree(ptr %alloc)
+  ret void
+}
+
+
 ; Function Attrs: nobuiltin allocsize(0)
 declare dso_local nonnull ptr @_Znam(i64) #1
 
 ; Function Attrs: nounwind
 declare dso_local void @free(ptr) allockind("free") "alloc-family"="malloc"
 
+declare ptr @customalloc(i64) allockind("alloc")
+declare void @customfree(ptr allocptr) allockind("free")
+
 attributes #0 = { builtin allocsize(0) }
 attributes #1 = { nobuiltin allocsize(0) allockind("alloc,uninitialized") "alloc-family"="_Znam" }



More information about the llvm-commits mailing list