173: fix double-free caused by usage of shared pointer r=Mic92 a=Mic92



Co-authored-by: Jörg Thalheim <joerg@thalheim.io>
This commit is contained in:
bors[bot] 2023-01-22 09:39:57 +00:00 committed by GitHub
commit 2e0855be9e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -249,7 +249,7 @@ std::string attrPathJoin(json input) {
}); });
} }
static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to, static void worker(ref<EvalState> state, Bindings &autoArgs, AutoCloseFD &to,
AutoCloseFD &from) { AutoCloseFD &from) {
std::optional<InstallableFlake> flake; std::optional<InstallableFlake> flake;
@ -258,8 +258,7 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
parseFlakeRefWithFragmentAndOutputsSpec(myArgs.releaseExpr, parseFlakeRefWithFragmentAndOutputsSpec(myArgs.releaseExpr,
absPath(".")); absPath("."));
flake.emplace(InstallableFlake({}, ref<EvalState>(&state), flake.emplace(InstallableFlake({}, state, std::move(flakeRef), fragment,
std::move(flakeRef), fragment,
outputSpec, {}, {}, outputSpec, {}, {},
flake::LockFlags{ flake::LockFlags{
.updateLockFile = false, .updateLockFile = false,
@ -268,7 +267,7 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
})); }));
}; };
auto vRoot = topLevelValue(state, autoArgs, flake); auto vRoot = topLevelValue(*state, autoArgs, flake);
while (true) { while (true) {
/* Wait for the collector to send us a job name. */ /* Wait for the collector to send us a job name. */
@ -288,14 +287,14 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
json reply = json{{"attr", attrPathS}, {"attrPath", path}}; json reply = json{{"attr", attrPathS}, {"attrPath", path}};
try { try {
auto vTmp = auto vTmp =
findAlongAttrPath(state, attrPathS, autoArgs, *vRoot).first; findAlongAttrPath(*state, attrPathS, autoArgs, *vRoot).first;
auto v = state.allocValue(); auto v = state->allocValue();
state.autoCallFunction(autoArgs, *vTmp, *v); state->autoCallFunction(autoArgs, *vTmp, *v);
if (v->type() == nAttrs) { if (v->type() == nAttrs) {
if (auto drvInfo = getDerivation(state, *v, false)) { if (auto drvInfo = getDerivation(*state, *v, false)) {
auto drv = Drv(state, *drvInfo); auto drv = Drv(*state, *drvInfo);
reply.update(drv); reply.update(drv);
/* Register the derivation as a GC root. !!! This /* Register the derivation as a GC root. !!! This
@ -306,7 +305,7 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
std::string(baseNameOf(drv.drvPath)); std::string(baseNameOf(drv.drvPath));
if (!pathExists(root)) { if (!pathExists(root)) {
auto localStore = auto localStore =
state.store state->store
.dynamic_pointer_cast<LocalFSStore>(); .dynamic_pointer_cast<LocalFSStore>();
auto storePath = auto storePath =
localStore->parseStorePath(drv.drvPath); localStore->parseStorePath(drv.drvPath);
@ -321,15 +320,15 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
// = true;` for top-level attrset // = true;` for top-level attrset
for (auto &i : for (auto &i :
v->attrs->lexicographicOrder(state.symbols)) { v->attrs->lexicographicOrder(state->symbols)) {
const std::string &name = state.symbols[i->name]; const std::string &name = state->symbols[i->name];
attrs.push_back(name); attrs.push_back(name);
if (name == "recurseForDerivations") { if (name == "recurseForDerivations") {
auto attrv = auto attrv =
v->attrs->get(state.sRecurseForDerivations); v->attrs->get(state->sRecurseForDerivations);
recurse = recurse =
state.forceBool(*attrv->value, attrv->pos); state->forceBool(*attrv->value, attrv->pos);
} }
} }
if (recurse) if (recurse)
@ -368,7 +367,7 @@ static void worker(EvalState &state, Bindings &autoArgs, AutoCloseFD &to,
writeLine(to.get(), "restart"); writeLine(to.get(), "restart");
} }
typedef std::function<void(EvalState &state, Bindings &autoArgs, typedef std::function<void(ref<EvalState> state, Bindings &autoArgs,
AutoCloseFD &to, AutoCloseFD &from)> AutoCloseFD &to, AutoCloseFD &from)>
Processor; Processor;
@ -388,10 +387,10 @@ struct Proc {
std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}]() { std::make_shared<AutoCloseFD>(std::move(toPipe.readSide))}]() {
debug("created worker process %d", getpid()); debug("created worker process %d", getpid());
try { try {
EvalState state(myArgs.searchPath, auto state = std::make_shared<EvalState>(
openStore(*myArgs.evalStoreUrl)); myArgs.searchPath, openStore(*myArgs.evalStoreUrl));
Bindings &autoArgs = *myArgs.getAutoArgs(state); Bindings &autoArgs = *myArgs.getAutoArgs(*state);
proc(state, autoArgs, *to, *from); proc(ref<EvalState>(state), autoArgs, *to, *from);
} catch (Error &e) { } catch (Error &e) {
nlohmann::json err; nlohmann::json err;
auto msg = e.msg(); auto msg = e.msg();