[clang] [clang][bytecode] Check if block is initialized before invoking destructor (PR #174082)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Dec 31 03:14:23 PST 2025
https://github.com/nataliakokoromyti created https://github.com/llvm/llvm-project/pull/174082
Fixes #173950.
The bytecode interpreter was crashing when evaluating typeid() on references to dynamically allocated objects. For example, this would cause an assertion failure:
static A &a = *new A;
const std::type_info &a_ti = typeid(a);
The problem was that when initialization failed, the code tried to call invokeDtor() on blocks that were never marked as initialized. This caused the assertion "IsInitialized" to fail. With this fix, we first check if the block is actually initialized before trying to invoke its destructor.
The test case I added reproduces the original crash and with the fix, it now passes.
>From 23ae304d290469973f718622f2dd73808489970d Mon Sep 17 00:00:00 2001
From: Natalia Kokoromyti <nataliakokoromyti at gmail.com>
Date: Wed, 31 Dec 2025 03:04:19 -0800
Subject: [PATCH] [clang][bytecode] Check if block is initialized before
invoking destructor. Fixes #173950
---
clang/lib/AST/ByteCode/Compiler.cpp | 6 ++++--
clang/test/AST/ByteCode/gh173950.cpp | 21 +++++++++++++++++++++
2 files changed, 25 insertions(+), 2 deletions(-)
create mode 100644 clang/test/AST/ByteCode/gh173950.cpp
diff --git a/clang/lib/AST/ByteCode/Compiler.cpp b/clang/lib/AST/ByteCode/Compiler.cpp
index 4daab0702f147..0398f267c1be6 100644
--- a/clang/lib/AST/ByteCode/Compiler.cpp
+++ b/clang/lib/AST/ByteCode/Compiler.cpp
@@ -4801,7 +4801,8 @@ VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD,
auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
GD.InitState = GlobalInitState::InitializerFailed;
- GlobalBlock->invokeDtor();
+ if (GlobalBlock->isInitialized())
+ GlobalBlock->invokeDtor();
}
}
@@ -4862,7 +4863,8 @@ bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD, const Expr *Init,
auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
GD.InitState = GlobalInitState::InitializerFailed;
- GlobalBlock->invokeDtor();
+ if (GlobalBlock->isInitialized())
+ GlobalBlock->invokeDtor();
}
return false;
}
diff --git a/clang/test/AST/ByteCode/gh173950.cpp b/clang/test/AST/ByteCode/gh173950.cpp
new file mode 100644
index 0000000000000..260523eddc22b
--- /dev/null
+++ b/clang/test/AST/ByteCode/gh173950.cpp
@@ -0,0 +1,21 @@
+// RUN: %clang_cc1 -std=c++20 -fsyntax-only -fexperimental-new-constant-interpreter -verify %s
+// expected-no-diagnostics
+
+// Test for issue #173950: Assertion `IsInitialized' failed when evaluating
+// typeid on a reference to a dynamically allocated object.
+
+namespace std {
+class type_info {};
+}
+
+namespace GH173950 {
+struct A {
+ virtual void f();
+};
+
+static A &a = *new A;
+extern A &a;
+
+// This used to crash with: Assertion `IsInitialized' failed in invokeDtor()
+const std::type_info &a_ti = typeid(a);
+}
More information about the cfe-commits
mailing list