Repository navigation
Description
parse_flake_ref builds the FlakeRef against a nix::fetchers::Settings
that lives on the stack of that one call. On Nix 2.31 a fetchers::Input
stores a pointer to its Settings, so the returned FlakeRef points at a
destroyed object. Every lock_flake and every get_flake in nanopynix goes
through that function, so every flake operation on 2.31 reads freed memory.
This is the cause of #60. It is the only failing job on develop today:
test-local-nix_2_31 and test-daemon-nix_2_31, two failures each,
test_a_per_call_setting_beats_the_session_default.
The defect
nanopynix-bindings/src/nix_flake.cpp:98
static PyFlakeRef parse_flake_ref(const std::string &url,
const std::map<std::string, std::string> &fetch_settings = {}) {
nix::fetchers::Settings fetchSettings; // stack
apply_settings_overrides(fetchSettings, fetch_settings);
...
ref.emplace(nix::parseFlakeRef(fetchSettings, url));
return PyFlakeRef(std::move(*ref)); // outlives fetchSettings
}Nix 2.31 keeps the pointer, and its git fetcher reads through it:
src/libfetchers/include/nix/fetchers/fetchers.hh:39—const Settings * settings;src/libfetchers/git.cc:714and:838—getRevCount(*input.settings, ...)src/libfetchers/git.cc:843—repoInfo.warnDirty(*input.settings)src/libfetchers/cache.cc:153—Settings::getCache()locks_cache, a
Sync<>that owns astd::mutex
pthread_mutex_lock on a destroyed mutex answers EINVAL, libstdc++ turns
that into std::system_error, and nanobind turns that into
RuntimeError: Invalid argument. The message names no Nix concept, because
nothing in Nix raised it.
Nix 2.34 and Nix 2.35 removed the stored pointer. Each call takes the settings
as an argument, and flake.cc passes state.fetchSettings, which the
EvalState owns:
- 2.31
src/libfetchers/input-cache.cc:9—
getAccessor(ref<Store>, const Input &, UseRegistries) - 2.34
src/libfetchers/input-cache.cc:8—
getAccessor(const Settings &, Store &, const Input &, UseRegistries)
So 2.31 is the only supported version that reads the dead object, and it reads
it on every flake operation.
Two symptoms, one cause
1. The evaluator's fetch settings do not reach a flake operation on 2.31.
parse_flake_ref is called with no overrides, so the Input carries a
default-constructed Settings. On 2.31 that is the only Settings the git
fetcher sees. NixFetchSettings(allow_dirty=True) on the call, and the session
default under it, both have no effect. This is what
test_a_per_call_setting_beats_the_session_default reports.
test_every_settings_scope_reaches_the_door_that_owns_it passes on 2.31 for
the wrong reason: it expects a refusal, and it gets one.
2. The result is not deterministic, because the memory is dead. Two runs of
the same probe on the same repository, same version, gave different answers.
Reproduce
nix develop --file . nanopynixVersions.nix_2_31.shell \
--command pytest tests/nanopynix/test_config_flow.py::test_a_per_call_setting_beats_the_session_default
Two failures, AssertionError: fetch_settings must beat the session default.
Below the test, on a clean git+file:// flake:
RuntimeError: Invalid argument
The C++ frames, from gdb with a breakpoint on __cxa_throw:
std::__throw_system_error(int)
nix::fetchers::Settings::getCache() const
nix::fetchers::GitInputScheme::getRevCount(nix::fetchers::Settings const&, ...)
nix::fetchers::GitInputScheme::getAccessor(nix::ref<nix::Store>, nix::fetchers::Input const&)
nix::fetchers::InputCache::getAccessor(...)
nix::flake::getFlake(...)
nix::flake::lockFlake(...)
The proof that the lifetime is the cause
eval_flake (nix_flake.cpp:201) parses the same reference against
es.fetchSettings, which the EvalState owns and which outlives the call.
lock_flake parses it against the stack local. One repository, one version,
one call each:
| tree | allow-dirty on the evaluator |
eval_flake |
lock_flake |
|---|---|---|---|
| clean | true | ok | ok |
| clean | false | ok | ok |
| dirty | true | ok | ok |
| dirty | false | Error: Git tree ... is dirty |
RuntimeError: Invalid argument |
eval_flake answers correctly in all four cases, including the refusal that
Nix authors. lock_flake answers from freed memory.
The same pattern elsewhere
nanopynix-bindings/src/nix_fetchers.cpp:56 and :68 build a PyInput
against a stack-local Settings in the same way. No test reaches the
dereference today, so this is latent rather than failing, and the fix must
cover it.
list_fetch_settings_metadata_json also builds a local Settings, but it only
calls toJSON() before the object dies. That one is correct.
Acceptance criteria
- No
FlakeRef,LockedFlakeorInputthat nanopynix returns to Python
points at afetchers::Settingsthat has been destroyed. fetch_settingspassed toSession.evalreacheslock_flakeand
get_flakeon Nix 2.31, as it already does on 2.34 and 2.35.test_a_per_call_setting_beats_the_session_defaultpasses on every
supported version.- A test locks a
git+file://flake and asserts thatallow_dirtydecides
the answer. The test runs on every supported version, and it fails on 2.31
before this change. test_every_settings_scope_reaches_the_door_that_owns_itstates why its
fetch-scope refusal is the refusal Nix authors, so it cannot pass again for
the wrong reason.
Closes the question that #60 asks. 2.31 can route a per-call fetch setting, so
a version gate on the test would have hidden a use-after-free.
Activity
ci(sanitizers): build and run an ASan+UBSan variant beside TSan
fix(nix): make the ASan build run, and stop pytest from hiding its re…
test(asan): restore the #34 defect to prove the ASAN job catches it
test(asan): restore the #34 defect again, over the boost fix
fix(ci): tell ASAN that Nix switches to a coroutine stack
ci(asan): run the ASAN jobs on every commit, now that the number is in
fix(bindings): let a flake reference own the fetch settings it points at
ci(sanitizers): build and run an ASan+UBSan variant beside TSan
fix(nix): make the ASan build run, and stop pytest from hiding its re…
fix(ci): tell ASAN that Nix switches to a coroutine stack
ci(asan): run the ASAN jobs on every commit, now that the number is in