Skip to content

Commit 2759e47

Browse files
authored
[clang-repl] We do not need to call new in the object allocation. (#78843)
This test demonstrates template instantiation via the interpreter code. In order to do that we can allocate the object on the stack and extend its lifetime by boxing it into a clang::Value. That avoids the subtle problem where we call the new operator on an object only known to the interpreter and we cannot destroy it from compiled code since there is not suitable facility in clang::Value yet. That should resolve the asan issues that was reported in #76218.
1 parent dedc7d4 commit 2759e47

File tree

1 file changed

+6
-9
lines changed

1 file changed

+6
-9
lines changed

clang/unittests/Interpreter/InterpreterTest.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,6 @@ using namespace clang;
3434
#define CLANG_INTERPRETER_NO_SUPPORT_EXEC
3535
#endif
3636

37-
#if LLVM_ADDRESS_SANITIZER_BUILD || LLVM_HWADDRESS_SANITIZER_BUILD
38-
#include <sanitizer/lsan_interface.h>
39-
#else
40-
extern "C" void __lsan_ignore_object(const void *p) {}
41-
#endif
42-
4337
int Global = 42;
4438
// JIT reports symbol not found on Windows without the visibility attribute.
4539
REPL_EXTERNAL_VISIBILITY int getGlobal() { return Global; }
@@ -257,7 +251,12 @@ TEST(IncrementalProcessing, FindMangledNameSymbol) {
257251
static Value AllocateObject(TypeDecl *TD, Interpreter &Interp) {
258252
std::string Name = TD->getQualifiedNameAsString();
259253
Value Addr;
260-
cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr));
254+
// FIXME: Consider providing an option in clang::Value to take ownership of
255+
// the memory created from the interpreter.
256+
// cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr));
257+
258+
// The lifetime of the temporary is extended by the clang::Value.
259+
cantFail(Interp.ParseAndExecute(Name + "()", &Addr));
261260
return Addr;
262261
}
263262

@@ -317,8 +316,6 @@ TEST(IncrementalProcessing, InstantiateTemplate) {
317316
auto fn =
318317
cantFail(Interp->getSymbolAddress(MangledName)).toPtr<TemplateSpecFn>();
319318
EXPECT_EQ(42, fn(NewA.getPtr()));
320-
// FIXME: release the memory.
321-
__lsan_ignore_object(NewA.getPtr());
322319
}
323320

324321
#ifdef CLANG_INTERPRETER_NO_SUPPORT_EXEC

0 commit comments

Comments
 (0)