-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Ensure that files are parsed/evaluated only once #13938
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
Changes from all commits
7f9b522
8fbf4b9
47c16fc
ad6eb22
4b63ff6
8d8f49c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -38,6 +38,7 @@ | |||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #include <nlohmann/json.hpp> | ||||||||||||||||||||||||||||||||||
| #include <boost/container/small_vector.hpp> | ||||||||||||||||||||||||||||||||||
| #include <boost/unordered/concurrent_flat_map.hpp> | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| #include "nix/util/strings-inline.hh" | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
|
|
@@ -264,6 +265,9 @@ EvalState::EvalState( | |||||||||||||||||||||||||||||||||
| , debugRepl(nullptr) | ||||||||||||||||||||||||||||||||||
| , debugStop(false) | ||||||||||||||||||||||||||||||||||
| , trylevel(0) | ||||||||||||||||||||||||||||||||||
| , srcToStore(make_ref<decltype(srcToStore)::element_type>()) | ||||||||||||||||||||||||||||||||||
| , importResolutionCache(make_ref<decltype(importResolutionCache)::element_type>()) | ||||||||||||||||||||||||||||||||||
| , fileEvalCache(make_ref<decltype(fileEvalCache)::element_type>()) | ||||||||||||||||||||||||||||||||||
| , regexCache(makeRegexCache()) | ||||||||||||||||||||||||||||||||||
| #if NIX_USE_BOEHMGC | ||||||||||||||||||||||||||||||||||
| , valueAllocCache(std::allocate_shared<void *>(traceable_allocator<void *>(), nullptr)) | ||||||||||||||||||||||||||||||||||
|
|
@@ -1031,63 +1035,85 @@ Value * ExprPath::maybeThunk(EvalState & state, Env & env) | |||||||||||||||||||||||||||||||||
| return &v; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial) | ||||||||||||||||||||||||||||||||||
| /** | ||||||||||||||||||||||||||||||||||
| * A helper `Expr` class to lets us parse and evaluate Nix expressions | ||||||||||||||||||||||||||||||||||
| * from a thunk, ensuring that every file is parsed/evaluated only | ||||||||||||||||||||||||||||||||||
| * once (via the thunk stored in `EvalState::fileEvalCache`). | ||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||
| struct ExprParseFile : Expr | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| FileEvalCache::iterator i; | ||||||||||||||||||||||||||||||||||
| if ((i = fileEvalCache.find(path)) != fileEvalCache.end()) { | ||||||||||||||||||||||||||||||||||
| v = i->second; | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| SourcePath & path; | ||||||||||||||||||||||||||||||||||
| bool mustBeTrivial; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| auto resolvedPath = resolveExprPath(path); | ||||||||||||||||||||||||||||||||||
| if ((i = fileEvalCache.find(resolvedPath)) != fileEvalCache.end()) { | ||||||||||||||||||||||||||||||||||
| v = i->second; | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| ExprParseFile(SourcePath & path, bool mustBeTrivial) | ||||||||||||||||||||||||||||||||||
| : path(path) | ||||||||||||||||||||||||||||||||||
| , mustBeTrivial(mustBeTrivial) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| printTalkative("evaluating file '%1%'", resolvedPath); | ||||||||||||||||||||||||||||||||||
| Expr * e = nullptr; | ||||||||||||||||||||||||||||||||||
| void eval(EvalState & state, Env & env, Value & v) override | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| printTalkative("evaluating file '%s'", path); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| auto j = fileParseCache.find(resolvedPath); | ||||||||||||||||||||||||||||||||||
| if (j != fileParseCache.end()) | ||||||||||||||||||||||||||||||||||
| e = j->second; | ||||||||||||||||||||||||||||||||||
| auto e = state.parseExprFromFile(path); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (!e) | ||||||||||||||||||||||||||||||||||
| e = parseExprFromFile(resolvedPath); | ||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| auto dts = | ||||||||||||||||||||||||||||||||||
| state.debugRepl | ||||||||||||||||||||||||||||||||||
| ? makeDebugTraceStacker( | ||||||||||||||||||||||||||||||||||
| state, *e, state.baseEnv, e->getPos(), "while evaluating the file '%s':", path.to_string()) | ||||||||||||||||||||||||||||||||||
| : nullptr; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Enforce that 'flake.nix' is a direct attrset, not a | ||||||||||||||||||||||||||||||||||
| // computation. | ||||||||||||||||||||||||||||||||||
| if (mustBeTrivial && !(dynamic_cast<ExprAttrs *>(e))) | ||||||||||||||||||||||||||||||||||
| state.error<EvalError>("file '%s' must be an attribute set", path).debugThrow(); | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| state.eval(e, v); | ||||||||||||||||||||||||||||||||||
| } catch (Error & e) { | ||||||||||||||||||||||||||||||||||
| state.addErrorTrace(e, "while evaluating the file '%s':", path.to_string()); | ||||||||||||||||||||||||||||||||||
| throw; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
| }; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fileParseCache.emplace(resolvedPath, e); | ||||||||||||||||||||||||||||||||||
| void EvalState::evalFile(const SourcePath & path, Value & v, bool mustBeTrivial) | ||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||
| auto resolvedPath = getConcurrent(*importResolutionCache, path); | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This leads to a dangling reference, because this variable is allocated on the stack, but it's later saved to the heap allocated in |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||
| auto dts = debugRepl ? makeDebugTraceStacker( | ||||||||||||||||||||||||||||||||||
| *this, | ||||||||||||||||||||||||||||||||||
| *e, | ||||||||||||||||||||||||||||||||||
| this->baseEnv, | ||||||||||||||||||||||||||||||||||
| e->getPos(), | ||||||||||||||||||||||||||||||||||
| "while evaluating the file '%1%':", | ||||||||||||||||||||||||||||||||||
| resolvedPath.to_string()) | ||||||||||||||||||||||||||||||||||
| : nullptr; | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| // Enforce that 'flake.nix' is a direct attrset, not a | ||||||||||||||||||||||||||||||||||
| // computation. | ||||||||||||||||||||||||||||||||||
| if (mustBeTrivial && !(dynamic_cast<ExprAttrs *>(e))) | ||||||||||||||||||||||||||||||||||
| error<EvalError>("file '%s' must be an attribute set", path).debugThrow(); | ||||||||||||||||||||||||||||||||||
| eval(e, v); | ||||||||||||||||||||||||||||||||||
| } catch (Error & e) { | ||||||||||||||||||||||||||||||||||
| addErrorTrace(e, "while evaluating the file '%1%':", resolvedPath.to_string()); | ||||||||||||||||||||||||||||||||||
| throw; | ||||||||||||||||||||||||||||||||||
| if (!resolvedPath) { | ||||||||||||||||||||||||||||||||||
| resolvedPath = resolveExprPath(path); | ||||||||||||||||||||||||||||||||||
| importResolutionCache->emplace(path, *resolvedPath); | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| if (auto v2 = getConcurrent(*fileEvalCache, *resolvedPath)) { | ||||||||||||||||||||||||||||||||||
| forceValue(**v2, noPos); | ||||||||||||||||||||||||||||||||||
| v = **v2; | ||||||||||||||||||||||||||||||||||
| return; | ||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fileEvalCache.emplace(resolvedPath, v); | ||||||||||||||||||||||||||||||||||
| if (path != resolvedPath) | ||||||||||||||||||||||||||||||||||
| fileEvalCache.emplace(path, v); | ||||||||||||||||||||||||||||||||||
| Value * vExpr; | ||||||||||||||||||||||||||||||||||
| ExprParseFile expr{*resolvedPath, mustBeTrivial}; | ||||||||||||||||||||||||||||||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is a bug. This expression is allocated on the stack and it the results in a dangling pointer. E.g this leads to a segfault in:
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, maybe there's something else at play here. |
||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||
| fileEvalCache->try_emplace_and_cvisit( | ||||||||||||||||||||||||||||||||||
| *resolvedPath, | ||||||||||||||||||||||||||||||||||
| nullptr, | ||||||||||||||||||||||||||||||||||
| [&](auto & i) { | ||||||||||||||||||||||||||||||||||
| vExpr = allocValue(); | ||||||||||||||||||||||||||||||||||
| vExpr->mkThunk(&baseEnv, &expr); | ||||||||||||||||||||||||||||||||||
|
Comment on lines
+1102
to
+1103
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any particular reason this has to be in a callback? Can't we just do this: Value * vExpr = allocValue();
ExprParseFile expr{*resolvedPath, mustBeTrivial};
vExpr->mkThunk(&baseEnv, &expr);
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It avoids an allocation in the case where the
Comment on lines
+1096
to
+1103
|
||||||||||||||||||||||||||||||||||
| ExprParseFile expr{*resolvedPath, mustBeTrivial}; | |
| fileEvalCache->try_emplace_and_cvisit( | |
| *resolvedPath, | |
| nullptr, | |
| [&](auto & i) { | |
| vExpr = allocValue(); | |
| vExpr->mkThunk(&baseEnv, &expr); | |
| auto expr = new ExprParseFile{*resolvedPath, mustBeTrivial}; | |
| fileEvalCache->try_emplace_and_cvisit( | |
| *resolvedPath, | |
| nullptr, | |
| [&](auto & i) { | |
| vExpr = allocValue(); | |
| vExpr->mkThunk(&baseEnv, expr); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Storing a reference to
SourcePathinExprParseFileis unsafe when the referenced object's lifetime is not guaranteed. Consider storing by value instead to avoid potential dangling references.