Skip to content

Commit

Permalink
move EvalState into worker to avoid stack-use-after-return
Browse files Browse the repository at this point in the history
  • Loading branch information
Mic92 committed Nov 10, 2024
1 parent eb8a3fe commit f97b4be
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
15 changes: 3 additions & 12 deletions src/nix-eval-jobs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,8 @@ namespace {
MyArgs myArgs; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
}

using Processor = std::function<void(
nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
nix::AutoCloseFD &to, nix::AutoCloseFD &from, MyArgs &args)>;
using Processor = std::function<void(MyArgs &myArgs, nix::AutoCloseFD &to,
nix::AutoCloseFD &from)>;

/* Auto-cleanup of fork's process and fds. */
struct Proc {
Expand All @@ -89,15 +88,7 @@ struct Proc {
nix::lvlDebug,
nix::fmt("created worker process %d", getpid()));
try {
auto evalStore = myArgs.evalStoreUrl
? nix::openStore(*myArgs.evalStoreUrl)
: nix::openStore();
auto state = std::make_shared<nix::EvalState>(
myArgs.lookupPath, evalStore, nix::fetchSettings,
nix::evalSettings);
nix::Bindings &autoArgs = *myArgs.getAutoArgs(*state);
proc(nix::ref<nix::EvalState>(state), autoArgs, *to, *from,
myArgs);
proc(myArgs, *to, *from);
} catch (nix::Error &e) {
nlohmann::json err;
const auto &msg = e.msg();
Expand Down
11 changes: 9 additions & 2 deletions src/worker.cc
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,16 @@ auto attrPathJoin(nlohmann::json input) -> std::string {
} // namespace

void worker(
nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
MyArgs &args,
nix::AutoCloseFD &to, // NOLINT(bugprone-easily-swappable-parameters)
nix::AutoCloseFD &from, MyArgs &args) {
nix::AutoCloseFD &from) {

auto evalStore = args.evalStoreUrl ? nix::openStore(*args.evalStoreUrl)
: nix::openStore();
auto state = nix::make_ref<nix::EvalState>(
args.lookupPath, evalStore, nix::fetchSettings, nix::evalSettings);
nix::Bindings &autoArgs = *args.getAutoArgs(*state);

nix::Value *vRoot = [&]() {
if (args.flake) {
auto [flakeRef, fragment, outputSpec] =
Expand Down
3 changes: 1 addition & 2 deletions src/worker.hh
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ class EvalState;
template <typename T> class ref;
} // namespace nix

void worker(nix::ref<nix::EvalState> state, nix::Bindings &autoArgs,
nix::AutoCloseFD &to, nix::AutoCloseFD &from, MyArgs &args);
void worker(MyArgs &args, nix::AutoCloseFD &to, nix::AutoCloseFD &from);

0 comments on commit f97b4be

Please sign in to comment.