Skip to content

Commit

Permalink
[clang-repl] We do not need to call new in the object allocation. (#7…
Browse files Browse the repository at this point in the history
…8843)

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.
  • Loading branch information
vgvassilev committed Jan 21, 2024
1 parent dedc7d4 commit 2759e47
Showing 1 changed file with 6 additions and 9 deletions.
15 changes: 6 additions & 9 deletions clang/unittests/Interpreter/InterpreterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ using namespace clang;
#define CLANG_INTERPRETER_NO_SUPPORT_EXEC
#endif

#if LLVM_ADDRESS_SANITIZER_BUILD || LLVM_HWADDRESS_SANITIZER_BUILD
#include <sanitizer/lsan_interface.h>
#else
extern "C" void __lsan_ignore_object(const void *p) {}
#endif

int Global = 42;
// JIT reports symbol not found on Windows without the visibility attribute.
REPL_EXTERNAL_VISIBILITY int getGlobal() { return Global; }
Expand Down Expand Up @@ -257,7 +251,12 @@ TEST(IncrementalProcessing, FindMangledNameSymbol) {
static Value AllocateObject(TypeDecl *TD, Interpreter &Interp) {
std::string Name = TD->getQualifiedNameAsString();
Value Addr;
cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr));
// FIXME: Consider providing an option in clang::Value to take ownership of
// the memory created from the interpreter.
// cantFail(Interp.ParseAndExecute("new " + Name + "()", &Addr));

// The lifetime of the temporary is extended by the clang::Value.
cantFail(Interp.ParseAndExecute(Name + "()", &Addr));
return Addr;
}

Expand Down Expand Up @@ -317,8 +316,6 @@ TEST(IncrementalProcessing, InstantiateTemplate) {
auto fn =
cantFail(Interp->getSymbolAddress(MangledName)).toPtr<TemplateSpecFn>();
EXPECT_EQ(42, fn(NewA.getPtr()));
// FIXME: release the memory.
__lsan_ignore_object(NewA.getPtr());
}

#ifdef CLANG_INTERPRETER_NO_SUPPORT_EXEC
Expand Down

0 comments on commit 2759e47

Please sign in to comment.