libexpr: Add missing GC root for baseEnv

This missing GC root wasn't much of a problem before, because the
heap would end up with a reference to the `baseEnv` pretty soon,
but when unit testing, the construction of `EvalState` doesn't
necessarily happen well before GC runs for the first time.

Found while unit testing the Rust bindings that currently reside
at https://github.com/nixops4/nixops4/tree/main/rust
This commit is contained in:
Robert Hensing 2024-05-01 18:47:19 +02:00
parent e17aad23d6
commit f34b52b521
2 changed files with 12 additions and 1 deletions

View file

@ -50,6 +50,7 @@
#include <gc/gc.h> #include <gc/gc.h>
#include <gc/gc_cpp.h> #include <gc/gc_cpp.h>
#include <gc/gc_allocator.h>
#include <boost/coroutine2/coroutine.hpp> #include <boost/coroutine2/coroutine.hpp>
#include <boost/coroutine2/protected_fixedsize_stack.hpp> #include <boost/coroutine2/protected_fixedsize_stack.hpp>
@ -342,6 +343,8 @@ void initGC()
gcInitialised = true; gcInitialised = true;
} }
static constexpr size_t BASE_ENV_SIZE = 128;
EvalState::EvalState( EvalState::EvalState(
const LookupPath & _lookupPath, const LookupPath & _lookupPath,
ref<Store> store, ref<Store> store,
@ -424,8 +427,11 @@ EvalState::EvalState(
#if HAVE_BOEHMGC #if HAVE_BOEHMGC
, valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr)) , valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
, env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr)) , env1AllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr))
, baseEnvP(std::allocate_shared<Env *>(traceable_allocator<Env *>(), &allocEnv(BASE_ENV_SIZE)))
, baseEnv(**baseEnvP)
#else
, baseEnv(allocEnv(BASE_ENV_SIZE))
#endif #endif
, baseEnv(allocEnv(128))
, staticBaseEnv{std::make_shared<StaticEnv>(nullptr, nullptr)} , staticBaseEnv{std::make_shared<StaticEnv>(nullptr, nullptr)}
{ {
corepkgsFS->setPathDisplay("<nix", ">"); corepkgsFS->setPathDisplay("<nix", ">");

View file

@ -547,6 +547,11 @@ public:
*/ */
SingleDerivedPath coerceToSingleDerivedPath(const PosIdx pos, Value & v, std::string_view errorCtx); SingleDerivedPath coerceToSingleDerivedPath(const PosIdx pos, Value & v, std::string_view errorCtx);
#if HAVE_BOEHMGC
/** A GC root for the baseEnv reference. */
std::shared_ptr<Env *> baseEnvP;
#endif
public: public:
/** /**