From 9d1fe9511140f4efea547ec0be3240cc549d33a7 Mon Sep 17 00:00:00 2001 From: Dan Blackwell Date: Tue, 18 Nov 2025 10:58:57 +0000 Subject: [PATCH] [Sanitizers] Add metadata to memsets created in zeroInit for LLDB LLDB memsets new allocas to zero. It does this in order to be able to display a friendly 'variable is uninitialized' message rather than garbage. Unfortunately this use of a variable before its lifetime.start disagrees with the memtag-stack tagging pass. This patch attaches a piece of metadata to these memsets, so that the memtag-stack tagging pass can recognize them and work around them appropriately. rdar://162206592 --- lib/IRGen/IRGenSIL.cpp | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/lib/IRGen/IRGenSIL.cpp b/lib/IRGen/IRGenSIL.cpp index c1dabec314a35..e58e77be09dc0 100644 --- a/lib/IRGen/IRGenSIL.cpp +++ b/lib/IRGen/IRGenSIL.cpp @@ -746,9 +746,19 @@ class IRGenSILFunction : getEarliestInsertionPoint()->getIterator()); // No debug location is how LLVM marks prologue instructions. ZeroInitBuilder.SetCurrentDebugLocation(nullptr); - ZeroInitBuilder.CreateMemSet( - AI, llvm::ConstantInt::get(IGM.Int8Ty, 0), - Size, llvm::MaybeAlign(AI->getAlign())); + // note that this memset is before lifetime.start which is Undefined + // Behaviour in LLVM + llvm::CallInst *Memset = + ZeroInitBuilder.CreateMemSet(AI, llvm::ConstantInt::get(IGM.Int8Ty, 0), + Size, llvm::MaybeAlign(AI->getAlign())); + + // memtag-stack tagging needs to use this metadata to determine whether to + // delay tagging until after the memset. This can be removed if the memset + // is removed or moved after the lifetime.start + llvm::LLVMContext *Ctx = &Memset->getContext(); + llvm::MDNode *Meta = + llvm::MDNode::get(*Ctx, llvm::MDString::get(*Ctx, "true")); + Memset->setMetadata("Swift.isSwiftLLDBpreinit", Meta); } /// Try to emit an inline assembly gadget which extends the lifetime of