llvmbot wrote:

<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang

Author: None (nataliakokoromyti)

<details>
<summary>Changes</summary>

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 &amp;a = *new A;
const std::type_info &amp;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.


---
Full diff: https://github.com/llvm/llvm-project/pull/174082.diff


2 Files Affected:

- (modified) clang/lib/AST/ByteCode/Compiler.cpp (+4-2) 
- (added) clang/test/AST/ByteCode/gh173950.cpp (+21) 


``````````diff
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);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/174082
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to