[llvm] DiagnosticInfo: Fix stack-use-after-scope in DiagnosticInfoStackSize (PR #190442)

Jinsong Ji via llvm-commits llvm-commits at lists.llvm.org
Fri Apr 3 20:44:19 PDT 2026


https://github.com/jsji created https://github.com/llvm/llvm-project/pull/190442

The string literal "stack frame size" passed to the base class
constructor created a temporary Twine that was destroyed after
the base constructor completed, leaving a dangling reference.

Fix by storing the Twine as a member variable in the derived class,
ensuring it lives as long as the diagnostic object itself.

Fixes ASAN stack-use-after-scope error in
  Clang :: Misc/backend-stack-frame-diagnostics-fallback.cpp
  LLVM :: CodeGen/X86/2007-04-24-Huge-Stack.ll
  LLVM :: CodeGen/X86/huge-stack-offset.ll
  LLVM :: CodeGen/X86/huge-stack-offset2.ll
  LLVM :: CodeGen/X86/huge-stack.ll
  LLVM :: CodeGen/X86/large-displacements.ll
  LLVM :: CodeGen/X86/stack-clash-extra-huge.ll
  LLVM :: CodeGen/X86/warn-stack.ll
  LLVM :: CodeGen/X86/win64-stackprobe-overflow.ll


>From b0b1627300a9c194be8fa29d13db3aacc1091a3c Mon Sep 17 00:00:00 2001
From: Jinsong Ji <jinsong.ji at intel.com>
Date: Sat, 4 Apr 2026 04:59:12 +0200
Subject: [PATCH] DiagnosticInfo: Fix stack-use-after-scope in
 DiagnosticInfoStackSize

The string literal "stack frame size" passed to the base class
constructor created a temporary Twine that was destroyed after
the base constructor completed, leaving a dangling reference.

Fix by storing the Twine as a member variable in the derived class,
ensuring it lives as long as the diagnostic object itself.

Fixes ASAN stack-use-after-scope error in
  Clang :: Misc/backend-stack-frame-diagnostics-fallback.cpp
  LLVM :: CodeGen/X86/2007-04-24-Huge-Stack.ll
  LLVM :: CodeGen/X86/huge-stack-offset.ll
  LLVM :: CodeGen/X86/huge-stack-offset2.ll
  LLVM :: CodeGen/X86/huge-stack.ll
  LLVM :: CodeGen/X86/large-displacements.ll
  LLVM :: CodeGen/X86/stack-clash-extra-huge.ll
  LLVM :: CodeGen/X86/warn-stack.ll
  LLVM :: CodeGen/X86/win64-stackprobe-overflow.ll
---
 llvm/include/llvm/IR/DiagnosticInfo.h | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/include/llvm/IR/DiagnosticInfo.h b/llvm/include/llvm/IR/DiagnosticInfo.h
index d72b1f42d84cf..189a8775a0468 100644
--- a/llvm/include/llvm/IR/DiagnosticInfo.h
+++ b/llvm/include/llvm/IR/DiagnosticInfo.h
@@ -501,12 +501,13 @@ class LLVM_ABI DiagnosticInfoResourceLimit
 
 class LLVM_ABI DiagnosticInfoStackSize : public DiagnosticInfoResourceLimit {
   void anchor() override;
+  const Twine ResourceName{"stack frame size"};
 
 public:
   DiagnosticInfoStackSize(const Function &Fn, uint64_t StackSize,
                           uint64_t StackLimit,
                           DiagnosticSeverity Severity = DS_Warning)
-      : DiagnosticInfoResourceLimit(Fn, "stack frame size", StackSize,
+      : DiagnosticInfoResourceLimit(Fn, ResourceName, StackSize,
                                     StackLimit, Severity, DK_StackSize) {}
 
   uint64_t getStackSize() const { return getResourceSize(); }



More information about the llvm-commits mailing list