Skip to content

builtins.sort crashes when nix is build with -D_GLIBCXX_DEBUG: Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)). #12106

Open
@trofi

Description

@trofi

Describe the bug

In an attempt to reproduce an unrelated I tried to build nix with -D_GLIBCXX_DEBUG. This exposed the invariant violation around the comparator to stable_sort:

nix-env -qa --json --arg config 'import <nixpkgs/pkgs/top-level/packages-config.nix>' -I nixpkgs=~/n -f ~/n
/nix/store/L89IQC7AM6I60Y8VK507ZWRZXF0WCD3V-gcc-14-20241116/include/c++/14-20241116/bits/stl_algo.h:5027:
In function:
    void std::stable_sort(_RAIter, _RAIter, _Compare) [with _RAIter =
    nix::Value**; _Compare = nix::prim_sort(EvalState&, PosIdx, Value**,
    Value&)::<lambda(nix::Value*, nix::Value*)>]

Error: comparison doesn't meet irreflexive requirements, assert(!(a < a)).

Objects involved in the operation:
    instance "functor" @ 0x7ffcb5307980 {
      type = nix::prim_sort(nix::EvalState&, nix::PosIdx, nix::Value**, nix::Value&)::{lambda(nix::Value*, nix::Value*)#1};
    }
    iterator::value_type "ordered type"  {
      type = nix::Value*;
    }
Aborted (core dumped)

Steps To Reproduce

Build nix with -D_GLIBCXX_DEBUG. I did it as:

  nix.package = ((pkgs.nixVersions.latest or pkgs.nixVersions.unstable).override ({
    # propagate -D_GLIBCXX_DEBUG to dependencies where is affects ABI
    withAWS = false;
    gtest = pkgs.gtest.overrideAttrs (oa: {
      env = (oa.env or {}) // {
        NIX_CFLAGS_COMPILE = (oa.env.NIX_CFLAGS_COMPILE or "") + " -D_GLIBCXX_DEBUG";
      };
    });
  })).overrideAttrs (oa: {
    env = (oa.env or {}) // {
      NIX_CFLAGS_COMPILE = (oa.env.NIX_CFLAGS_COMPILE or "") + " -D_GLIBCXX_DEBUG";
    };
  });

Expected behavior

nix-env should not crash.

Metadata

nix-env (Nix) 2.25.3

Additional context

It looks like the predicate problem is already known at

/* FIXME: std::sort can segfault if the comparator is not a strict

    /* FIXME: std::sort can segfault if the comparator is not a strict
       weak ordering. What to do? std::stable_sort() seems more
       resilient, but no guarantees... */
    std::stable_sort(list.begin(), list.end(), comparator);

The backtrace:

$ gdb --args nix-env -qa --json --arg config 'import <nixpkgs/pkgs/top-level/packages-config.nix>' -I nixpkgs=~/n -f ~/n
(gdb) run
...
(gdb) bt
#0  0x00007ffff66988ec in __pthread_kill_implementation () from /nix/store/65h17wjrrlsj2rj540igylrx7fqcd6vq-glibc-2.40-36/lib/libc.so.6
#1  0x00007ffff66409c6 in raise () from /nix/store/65h17wjrrlsj2rj540igylrx7fqcd6vq-glibc-2.40-36/lib/libc.so.6
#2  0x00007ffff6628938 in abort () from /nix/store/65h17wjrrlsj2rj540igylrx7fqcd6vq-glibc-2.40-36/lib/libc.so.6
#3  0x00007ffff6ab0203 in __gnu_debug::_Error_formatter::_M_error() const [clone .cold] () from /nix/store/bpq1s72cw9qb2fs8mnmlw6hn2c7iy0ss-gcc-14-20241116-lib/lib/libstdc++.so.6
#4  0x00007ffff7bab7fe in nix::prim_sort(nix::EvalState&, nix::PosIdx, nix::Value**, nix::Value&) [clone .lto_priv.0] () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#5  0x00007ffff7aed1bf in nix::EvalState::callFunction(nix::Value&, unsigned long, nix::Value**, nix::Value&, nix::PosIdx) () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#6  0x00007ffff7baac4c in nix::prim_length(nix::EvalState&, nix::PosIdx, nix::Value**, nix::Value&) [clone .lto_priv.0] () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#7  0x00007ffff7aece69 in nix::EvalState::callFunction(nix::Value&, unsigned long, nix::Value**, nix::Value&, nix::PosIdx) () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#8  0x00007ffff7af1150 in nix::ExprCall::eval(nix::EvalState&, nix::Env&, nix::Value&) () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#9  0x00007ffff7badace in nix::prim_lessThan(nix::EvalState&, nix::PosIdx, nix::Value**, nix::Value&) [clone .lto_priv.0] () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#10 0x00007ffff7aece69 in nix::EvalState::callFunction(nix::Value&, unsigned long, nix::Value**, nix::Value&, nix::PosIdx) () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#11 0x00007ffff7af1150 in nix::ExprCall::eval(nix::EvalState&, nix::Env&, nix::Value&) () from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
#12 0x00007ffff7ae8bc4 in nix::EvalState::evalBool(nix::Env&, nix::Expr*, nix::PosIdx, std::basic_string_view<char, std::char_traits<char> >) ()
   from /nix/store/nmf85jbch9v9a9mq516wrlm0qxq32vfb-nix-2.25.3/lib/libnixexpr.so
...

Checklist


Add 👍 to issues you find important.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions