8000 bindings: parse_flake_ref returns a FlakeRef that points at a destroyed fetchers::Settings · Issue #63 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

bindings: parse_flake_ref returns a FlakeRef that points at a destroyed fetchers::Settings #63

Closed

Description

@Lillecarl

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:714 and :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 a std::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

  1. No FlakeRef, LockedFlake or Input that nanopynix returns to Python
    points at a fetchers::Settings that has been destroyed.
  2. fetch_settings passed to Session.eval reaches lock_flake and
    get_flake on Nix 2.31, as it already does on 2.34 and 2.35.
  3. test_a_per_call_setting_beats_the_session_default passes on every
    supported version.
  4. A test locks a git+file:// flake and asserts that allow_dirty decides
    the answer. The test runs on every supported version, and it fails on 2.31
    before this change.
  5. test_every_settings_scope_reaches_the_door_that_owns_it states 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    source: nanopynix

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      0