8000 nix: the base env overflows by one slot, and the collector hides it · Issue #81 · nixidae/nixidae · GitHub
[go: up one dir, main page]

Skip to content

nix: the base env overflows by one slot, and the collector hides it #81

Closed

Description

@Lillecarl

What happens

The full suite against nanopynix-tests-nix_2_34-nogc aborts:

corrupted size vs. prev_size
Fatal Python error: Aborted
  File "tests/nanopynix/bindings/test_expr.py", line 73 in test_value_keeps_eval_state_alive

It aborts a second time in tests/nanopynix/test_examples.py::test_example_runs,
and both times the thread is inside the constructor of EvalState.

The cause, from the ASAN job of #76

nanopynix-tests-nix_2_34-asan is the same build with the sanitizer, and it
names the write:

ERROR: AddressSanitizer: heap-buffer-overflow
WRITE of size 8 at 0x7ae40324ff88
    #0 nix::EvalState::addPrimOp(nix::PrimOp&&)          ../eval.cc:550
    nixidae/nixidae#30 nix::EvalState::createBaseEnv(nix::EvalSettings const&) ../primops.cc:5427
    nixidae/nixidae#31 nix::EvalState::EvalState(...)                    ../eval.cc:349

0x7ae40324ff88 is located 0 bytes after 1032-byte region
allocated by thread T0 here:
    nixidae/nixidae#30 nix::EvalMemory::allocBytes(unsigned long) ../include/nix/expr/eval-inline.hh:22
    nixidae/nixidae#31 nix::EvalMemory::allocEnv(unsigned long)   ../include/nix/expr/eval-inline.hh:88
    nixidae/nixidae#32 nix::EvalState::EvalState(...)             ../eval.cc:311

The region is the base Env. eval.cc:228 fixes its size:

static constexpr size_t BASE_ENV_SIZE = 128;

1032 bytes is sizeof(Env) plus 128 slots of 8 bytes. addPrimOp and
addConstant then fill it with no bound check at all:

staticBaseEnv->vars.emplace_back(envName, baseEnvDispl);
baseEnv.values[baseEnvDispl++] = v;     // eval.cc:481 and eval.cc:550

Nix 2.34 publishes 119 names under builtins, and the base env holds those
plus the top-level constants. The count therefore sits at the ceiling, and one
name past it writes into the next heap block.

Why no build has ever reported this

EvalMemory::allocBytes is the collector under a normal build, and
GC_MALLOC rounds a request up to a size class. The write past slot 127 lands
in the slack of the block, so nothing observes it. -Dgc=disabled makes
allocBytes a plain calloc, which gives the exact size, so the same write
lands on the header of the next block and glibc aborts.

The collector hides the defect. It does not prevent it. The shipped
build makes the same out-of-bounds write, and it is undefined behaviour there
too.

An evaluator with no settings at all still fits, which is why a minimal script
runs and the suite does not.

What this blocks

Acceptance criterion 2 of #76 asks for the full suite against the non-GC
build. That suite cannot pass while this is open.

Options

  1. Report it upstream. The bound check is one line, and the size wants to
    come from the count of registered names rather than from a constant.
  2. Carry a patch on nix-expr in the -nogc and -asan scopes only, which
    raises BASE_ENV_SIZE or adds the bound check. This unblocks Support a non-GC build for RPC workers, so ASAN runs in a supported configuration #76 and ci: build the bindings with AddressSanitizer, beside the TSan matrix #64
    without touching the build that ships.

Option 2 unblocks the work. Option 1 is the correction.

Refs #76
Refs #64

Activity

  1. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    The count, exactly

    • Stock Nix 2.34 registers 119 names under builtins. addConstant and
      addPrimOp each take one base env slot and push one builtins attribute,
      and builtins is slot 0 through addConstant as well, so baseEnvDispl
      equals the attribute count.
    • _register_test_primops in tests/conftest.py calls
      nanopynix.register_primop 24 times.
    • 119 + 24 = 143 against BASE_ENV_SIZE = 128. The base env overflows by
      15 slots, which is 120 bytes.

    nanopynix.register_primop reaches nix::RegisterPrimOp
    (nanopynix-bindings/src/nix_expr.cpp:1354), which appends to a global
    registry. createBaseEnv then walks that registry with no bound:

    for (auto & primOp : RegisterPrimOp::primOps())
        if (experimentalFeatureSettings.isEnabled(primOp.experimentalFeature))
            addPrimOp(std::move(primOpAdjusted));
    for (auto & primOp : evalSettings.extraPrimOps)
        addPrimOp(std::move(primOpAdjusted));

    So stock Nix fits with 9 slots to spare, and this API is what exceeds the
    limit
    . Any consumer that registers 10 or more primops does the same.

    This is not limited to the non-GC build

    GC_MALLOC rounds the 1032-byte request up to a size class, so the 120 extra
    bytes land in the slack of the block. The GC build makes the same
    out-of-bounds write on every run of this suite and does not report it. The
    non-GC build only removes the rounding that hides it.

    Whether the slack absorbs all 15 slots depends on the size class, on the
    enabled experimental features, and on the number of registered primops. That
    is the profile of an intermittent, configuration-dependent corruption, so this
    is a candidate cause for the multi-EvalState corruption that has no
    explanation yet.

    BASE_ENV_SIZE = 128 is identical in 2.31, 2.34 and 2.35, and it is a
    static constexpr in eval.cc. It is not a meson option, not a setting, and
    not an environment variable, so no configuration avoids this.

  2. Lillecarl commented on Aug 3, 2026

    @Lillecarl
    ContributorAuthor

    Upstream issue filed against the fork, for the real fix: Lillecarl/nix#279

    It carries the analysis that this issue does not: why the size is a constant
    (the allocation is in the member-initialiser list, which runs before
    createBaseEnv can count), why an Env cannot grow (variable-length struct,
    and the parser compiles each reference to a fixed displacement), and why the
    size is not a performance parameter. It proposes sizing from
    RegisterPrimOp::primOps() plus EvalSettings::extraPrimOps, and a bound
    check at both writes.

    It also states the acceptance criterion that matters: a regression test under
    the collector proves nothing
    , because GC_MALLOC rounds the request up and
    hides the write. The test needs -Dgc=disabled or AddressSanitizer.

    This repository carries the stopgap in nix/patches/nix-base-env-size.patch,
    on every scope rather than only the ones with no collector.

  3. Lillecarl commented on Aug 5, 2026

    @Lillecarl
    ContributorAuthor

    Done. The patch landed in 2395efab, on develop, and it takes option 2 for
    every version rather than for the builds with no collector alone. The commit
    gives the reason: the collector build makes the same out-of-bounds write, and
    Boehm hides it in the slack of the size class.

    That commit closed with "The full suite on the non-GC build is not yet
    complete". It is complete now. Run
    31016955422
    ran test-nogc-* against all four versions:

    job result
    test-nogc-nix_2_31 success
    test-nogc-nix_2_34 success
    test-nogc-nix_2_35 success
    test-nogc-git success

    nanopynix-tests-nix_2_34-nogc is the build that aborted with "corrupted size
    vs. prev_size" in the report, and it runs the whole suite now.

    This also satisfies acceptance criterion 2 of #76.

    Option 1, the report upstream, stays undone. The patch header names the two
    containers and the missing bound check, so the material for it is written.

  4. added
    source: nanopynix
    on Oct 19, 2026
  5. added theissue type on Oct 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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