Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

eval: add NIX_VALIDATE_EVAL_NONDETERMINISM know for attrset comparison #8711

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/libexpr/eval.cc
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,8 @@ EvalState::EvalState(
{
countCalls = getEnv("NIX_COUNT_CALLS").value_or("0") != "0";

validateNondeterminism = getEnv("NIX_VALIDATE_EVAL_NONDETERMINISM").value_or("0") != "0";

assert(gcInitialised);

static_assert(sizeof(Env) <= 16, "environment must be <= 16 bytes");
Expand Down Expand Up @@ -2464,11 +2466,24 @@ bool EvalState::eqValues(Value & v1, Value & v2, const PosIdx pos, std::string_v

/* Otherwise, compare the attributes one by one. */
Bindings::iterator i, j;
for (i = v1.attrs->begin(), j = v2.attrs->begin(); i != v1.attrs->end(); ++i, ++j)
bool result = true;
for (i = v1.attrs->begin(), j = v2.attrs->begin(); i != v1.attrs->end(); ++i, ++j) {
if (i->name != j->name || !eqValues(*i->value, *j->value, pos, errorCtx))
return false;
result = false;
/* Do not short-cut comparison to catch attrset
* non-determinism caused by thrown exceptions
* when attribute sets are compared. Example:
* > { a = 1; b = assert false; 1; } == { a = 2; b = 1; }
* false
* > { b = 1; a = assert false; 1; } == { b = 2; a = 1; }
* error: assertion 'false' failed
*
* Exhaustive check should catch such non-determinism.
*/
if (!result && !validateNondeterminism) return result;
}

return true;
return result;
}

/* Functions are incomparable. */
Expand Down
2 changes: 2 additions & 0 deletions src/libexpr/eval.hh
Original file line number Diff line number Diff line change
Expand Up @@ -727,6 +727,8 @@ private:

bool countCalls;

bool validateNondeterminism;

typedef std::map<std::string, size_t> PrimOpCalls;
PrimOpCalls primOpCalls;

Expand Down
4 changes: 4 additions & 0 deletions tests/misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,7 @@ expectStderr 1 nix-instantiate --eval -E '[]' -A 'x' | grepQuiet "should be a se
expectStderr 1 nix-instantiate --eval -E '{}' -A '1' | grepQuiet "should be a list"
expectStderr 1 nix-instantiate --eval -E '{}' -A '.' | grepQuiet "empty attribute name"
expectStderr 1 nix-instantiate --eval -E '[]' -A '1' | grepQuiet "out of range"

# Validate deterministic attrset comparison:
NIX_VALIDATE_EVAL_NONDETERMINISM=1 expectStderr 1 nix-instantiate --eval -E '{ a = 1; b = assert false; 1; } == { a = 2; b = 1; }' | grepQuiet "assertion 'false' failed"
NIX_VALIDATE_EVAL_NONDETERMINISM=1 expectStderr 1 nix-instantiate --eval -E '{ b = 1; a = assert false; 1; } == { b = 2; a = 1; }' | grepQuiet "assertion 'false' failed"
Loading