1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-09 03:56:01 +01:00

treewide: Remove toView() because it leads to segfaults when compiled with newer nixpkgs

Firstly, this is now available on darwin where the default in llvm 19.
Secondly, this leads to very weird segfaults when building with newer nixpkgs for some reason.
(It's UB after all).

This appears when building with the following:

mesonComponentOverrides = finalAttrs: prevAttrs: {
mesonBuildType = "debugoptimized";
dontStrip = true;
doCheck = false;
separateDebugInfo = false;
preConfigure = (prevAttrs.preConfigure or "") + ''
  case "$mesonBuildType" in
  release|minsize|debugoptimized) appendToVar mesonFlags "-Db_lto=true"  ;;
  *)                              appendToVar mesonFlags "-Db_lto=false" ;;
  esac
'';
};

And with the following nixpkgs input:

nix build ".#nix-cli" -L --override-input nixpkgs "https://releases.nixos.org/nixos/unstable/nixos-25.11pre870157.7df7ff7d8e00/nixexprs.tar.xz"

Stacktrace:

 #0  0x00000000006afdc0 in ?? ()
 #1  0x00007ffff71cebb6 in _Unwind_ForcedUnwind_Phase2 () from /nix/store/41ym1jm1b7j3rhglk82gwg9jml26z1km-gcc-14.3.0-lib/lib/libgcc_s.so.1
 #2  0x00007ffff71cf5b5 in _Unwind_Resume () from /nix/store/41ym1jm1b7j3rhglk82gwg9jml26z1km-gcc-14.3.0-lib/lib/libgcc_s.so.1
 #3  0x00007ffff7eac7d8 in std::basic_ios<char, std::char_traits<char> >::~basic_ios (this=<optimized out>, this=<optimized out>)
     at /nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include/c++/14.3.0/bits/basic_ios.h:286
 #4  std::__cxx11::basic_ostringstream<char, std::char_traits<char>, std::allocator<char> >::basic_ostringstream (this=<optimized out>, this=<optimized out>)
     at /nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include/c++/14.3.0/sstream:806
 #5  nix::SimpleLogger::logEI (this=<optimized out>, ei=...) at ../logging.cc:121
 #6  0x00007ffff7515794 in nix::Logger::logEI (this=0x675450, lvl=nix::lvlError, ei=...) at /nix/store/bkshji3nnxmrmgwa4n2kaxadajkwvn65-nix-util-2.32.0pre-dev/include/nix/util/logging.hh:144
 #7  nix::handleExceptions (programName=..., fun=...) at ../shared.cc:336
 #8  0x000000000047b76b in main (argc=<optimized out>, argv=<optimized out>) at /nix/store/82kmz7r96navanrc2fgckh2bamiqrgsw-gcc-14.3.0/include/c++/14.3.0/bits/new_allocator.h:88
This commit is contained in:
Sergei Zimmerman 2025-10-05 02:30:21 +03:00
parent 35d7719f46
commit dce1a893d0
No known key found for this signature in database
13 changed files with 16 additions and 38 deletions

View file

@ -669,7 +669,7 @@ ProcessLineResult NixRepl::processLine(std::string line)
ss << "No documentation found.\n\n"; ss << "No documentation found.\n\n";
} }
auto markdown = toView(ss); auto markdown = ss.view();
logger->cout(trim(renderMarkdownToTerminal(markdown))); logger->cout(trim(renderMarkdownToTerminal(markdown)));
} else } else

View file

@ -591,7 +591,7 @@ std::optional<EvalState::Doc> EvalState::getDoc(Value & v)
.name = name, .name = name,
.arity = 0, // FIXME: figure out how deep by syntax only? It's not semantically useful though... .arity = 0, // FIXME: figure out how deep by syntax only? It's not semantically useful though...
.args = {}, .args = {},
.doc = makeImmutableString(toView(s)), // NOTE: memory leak when compiled without GC .doc = makeImmutableString(s.view()), // NOTE: memory leak when compiled without GC
}; };
} }
if (isFunctor(v)) { if (isFunctor(v)) {
@ -1811,7 +1811,7 @@ void ExprAssert::eval(EvalState & state, Env & env, Value & v)
if (!state.evalBool(env, cond, pos, "in the condition of the assert statement")) { if (!state.evalBool(env, cond, pos, "in the condition of the assert statement")) {
std::ostringstream out; std::ostringstream out;
cond->show(state.symbols, out); cond->show(state.symbols, out);
auto exprStr = toView(out); auto exprStr = out.view();
if (auto eq = dynamic_cast<ExprOpEq *>(cond)) { if (auto eq = dynamic_cast<ExprOpEq *>(cond)) {
try { try {

View file

@ -2412,7 +2412,7 @@ static void prim_toXML(EvalState & state, const PosIdx pos, Value ** args, Value
std::ostringstream out; std::ostringstream out;
NixStringContext context; NixStringContext context;
printValueAsXML(state, true, false, *args[0], out, context, pos); printValueAsXML(state, true, false, *args[0], out, context, pos);
v.mkString(toView(out), context); v.mkString(out.view(), context);
} }
static RegisterPrimOp primop_toXML({ static RegisterPrimOp primop_toXML({
@ -2520,7 +2520,7 @@ static void prim_toJSON(EvalState & state, const PosIdx pos, Value ** args, Valu
std::ostringstream out; std::ostringstream out;
NixStringContext context; NixStringContext context;
printValueAsJSON(state, true, *args[0], pos, out, context); printValueAsJSON(state, true, *args[0], pos, out, context);
v.mkString(toView(out), context); v.mkString(out.view(), context);
} }
static RegisterPrimOp primop_toJSON({ static RegisterPrimOp primop_toJSON({

View file

@ -139,7 +139,7 @@ static void prim_fromTOML(EvalState & state, const PosIdx pos, Value ** args, Va
attrs.alloc("_type").mkStringNoCopy("timestamp"); attrs.alloc("_type").mkStringNoCopy("timestamp");
std::ostringstream s; std::ostringstream s;
s << t; s << t;
auto str = toView(s); auto str = s.view();
forceNoNullByte(str); forceNoNullByte(str);
attrs.alloc("value").mkString(str); attrs.alloc("value").mkString(str);
v.mkAttrs(attrs); v.mkAttrs(attrs);

View file

@ -461,7 +461,7 @@ private:
std::ostringstream s; std::ostringstream s;
s << state.positions[v.lambda().fun->pos]; s << state.positions[v.lambda().fun->pos];
output << " @ " << filterANSIEscapes(toView(s)); output << " @ " << filterANSIEscapes(s.view());
} }
} else if (v.isPrimOp()) { } else if (v.isPrimOp()) {
if (v.primOp()) if (v.primOp())

View file

@ -183,7 +183,7 @@ public:
std::ostringstream oss; std::ostringstream oss;
showErrorInfo(oss, ei, loggerSettings.showTrace.get()); showErrorInfo(oss, ei, loggerSettings.showTrace.get());
log(*state, ei.level, toView(oss)); log(*state, ei.level, oss.view());
} }
void log(State & state, Verbosity lvl, std::string_view s) void log(State & state, Verbosity lvl, std::string_view s)

View file

@ -102,7 +102,7 @@ struct TunnelLogger : public Logger
showErrorInfo(oss, ei, false); showErrorInfo(oss, ei, false);
StringSink buf; StringSink buf;
buf << STDERR_NEXT << toView(oss); buf << STDERR_NEXT << oss.view();
enqueueMsg(buf.s); enqueueMsg(buf.s);
} }

View file

@ -12,11 +12,6 @@
namespace nix { namespace nix {
/*
* workaround for unavailable view() method (C++20) of std::ostringstream under MacOS with clang-16
*/
std::string_view toView(const std::ostringstream & os);
/** /**
* String tokenizer. * String tokenizer.
* *

View file

@ -121,7 +121,7 @@ public:
std::ostringstream oss; std::ostringstream oss;
showErrorInfo(oss, ei, loggerSettings.showTrace.get()); showErrorInfo(oss, ei, loggerSettings.showTrace.get());
log(ei.level, toView(oss)); log(ei.level, oss.view());
} }
void startActivity( void startActivity(

View file

@ -8,23 +8,6 @@
namespace nix { namespace nix {
struct view_stringbuf : public std::stringbuf
{
inline std::string_view toView()
{
auto begin = pbase();
return {begin, begin + pubseekoff(0, std::ios_base::cur, std::ios_base::out)};
}
};
__attribute__((no_sanitize("undefined"))) std::string_view toView(const std::ostringstream & os)
{
/* Downcasting like this is very much undefined behavior, so we disable
UBSAN for this function. */
auto buf = static_cast<view_stringbuf *>(os.rdbuf());
return buf->toView();
}
template std::list<std::string> tokenizeString(std::string_view s, std::string_view separators); template std::list<std::string> tokenizeString(std::string_view s, std::string_view separators);
template StringSet tokenizeString(std::string_view s, std::string_view separators); template StringSet tokenizeString(std::string_view s, std::string_view separators);
template std::vector<std::string> tokenizeString(std::string_view s, std::string_view separators); template std::vector<std::string> tokenizeString(std::string_view s, std::string_view separators);

View file

@ -100,7 +100,7 @@ struct CmdConfigCheck : StoreCommand
ss << "Multiple versions of nix found in PATH:\n"; ss << "Multiple versions of nix found in PATH:\n";
for (auto & dir : dirs) for (auto & dir : dirs)
ss << " " << dir << "\n"; ss << " " << dir << "\n";
return checkFail(toView(ss)); return checkFail(ss.view());
} }
return checkPass("PATH contains only one nix version."); return checkPass("PATH contains only one nix version.");
@ -143,7 +143,7 @@ struct CmdConfigCheck : StoreCommand
for (auto & dir : dirs) for (auto & dir : dirs)
ss << " " << dir << "\n"; ss << " " << dir << "\n";
ss << "\n"; ss << "\n";
return checkFail(toView(ss)); return checkFail(ss.view());
} }
return checkPass("All profiles are gcroots."); return checkPass("All profiles are gcroots.");
@ -162,7 +162,7 @@ struct CmdConfigCheck : StoreCommand
<< "sync with the daemon.\n\n" << "sync with the daemon.\n\n"
<< "Client protocol: " << formatProtocol(clientProto) << "\n" << "Client protocol: " << formatProtocol(clientProto) << "\n"
<< "Store protocol: " << formatProtocol(storeProto) << "\n\n"; << "Store protocol: " << formatProtocol(storeProto) << "\n\n";
return checkFail(toView(ss)); return checkFail(ss.view());
} }
return checkPass("Client protocol matches store protocol."); return checkPass("Client protocol matches store protocol.");

View file

@ -285,10 +285,10 @@ static void main_nix_build(int argc, char ** argv)
execArgs, execArgs,
interpreter, interpreter,
escapeShellArgAlways(script), escapeShellArgAlways(script),
toView(joined)); joined.view());
} else { } else {
envCommand = envCommand =
fmt("exec %1% %2% %3% %4%", execArgs, interpreter, escapeShellArgAlways(script), toView(joined)); fmt("exec %1% %2% %3% %4%", execArgs, interpreter, escapeShellArgAlways(script), joined.view());
} }
} }

View file

@ -108,7 +108,7 @@ bool createUserEnv(
auto manifestFile = ({ auto manifestFile = ({
std::ostringstream str; std::ostringstream str;
printAmbiguous(manifest, state.symbols, str, nullptr, std::numeric_limits<int>::max()); printAmbiguous(manifest, state.symbols, str, nullptr, std::numeric_limits<int>::max());
StringSource source{toView(str)}; StringSource source{str.view()};
state.store->addToStoreFromDump( state.store->addToStoreFromDump(
source, source,
"env-manifest.nix", "env-manifest.nix",