Skip to content

Commit

Permalink
Make the max-call-depth check thread-local
Browse files Browse the repository at this point in the history
  • Loading branch information
edolstra committed Jun 13, 2024
1 parent 3353f9a commit 9b814c4
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 14 deletions.
22 changes: 11 additions & 11 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1527,29 +1527,29 @@ void ExprLambda::eval(EvalState & state, Env & env, Value & v)
v.mkLambda(&env, this);
}

thread_local size_t EvalState::callDepth = 0;

namespace {
/** Increments a count on construction and decrements on destruction.
/**
* Increments a count on construction and decrements on destruction.
*/
class CallDepth {
size_t & count;
size_t & count;
public:
CallDepth(size_t & count) : count(count) {
++count;
}
~CallDepth() {
--count;
}
CallDepth(size_t & count) : count(count) {
++count;
}
~CallDepth() {
--count;
}
};
};

void EvalState::callFunction(Value & fun, size_t nrArgs, Value * * args, Value & vRes, const PosIdx pos)
{
debug("CALL %x %d", &vRes, vRes.internalType);
#if 0
if (callDepth > evalSettings.maxCallDepth)
error<EvalError>("stack overflow; max-call-depth exceeded").atPos(pos).debugThrow();
CallDepth _level(callDepth);
#endif

auto trace = evalSettings.traceFunctionCalls
? std::make_unique<FunctionCallTrace>(positions[pos])
Expand Down
6 changes: 4 additions & 2 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -610,9 +610,11 @@ private:
std::shared_ptr<StaticEnv> & staticEnv);

/**
* Current Nix call stack depth, used with `max-call-depth` setting to throw stack overflow hopefully before we run out of system stack.
* Current Nix call stack depth, used with `max-call-depth`
* setting to throw stack overflow hopefully before we run out of
* system stack.
*/
size_t callDepth = 0;
thread_local static size_t callDepth;

public:

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,38 @@
error: stack overflow (possible infinite recursion)
error:
… from call site
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:1:
1| (x: x x) (x: x x)
| ^
2|

… while calling anonymous lambda
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:2:
1| (x: x x) (x: x x)
| ^
2|

… from call site
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:5:
1| (x: x x) (x: x x)
| ^
2|

… while calling anonymous lambda
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:11:
1| (x: x x) (x: x x)
| ^
2|

… from call site
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:14:
1| (x: x x) (x: x x)
| ^
2|

(19997 duplicate frames omitted)

error: stack overflow; max-call-depth exceeded
at /pwd/lang/eval-fail-infinite-recursion-lambda.nix:1:14:
1| (x: x x) (x: x x)
| ^
2|

0 comments on commit 9b814c4

Please sign in to comment.