From e42d00c961ad948c80c27f49d58316a7eae5e728 Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Sat, 18 May 2024 04:09:56 -0700 Subject: [PATCH 1/5] Change `rlim_t` to `size_t` in `setStackSize` in preparation of Windows impl --- src/libutil/current-process.cc | 6 +++--- src/libutil/current-process.hh | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index c88013b3c42..b252bd4d035 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -60,14 +60,14 @@ unsigned int getMaxCPU() #ifndef _WIN32 -rlim_t savedStackSize = 0; +size_t savedStackSize = 0; -void setStackSize(rlim_t stackSize) +void setStackSize(size_t stackSize) { struct rlimit limit; if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { savedStackSize = limit.rlim_cur; - limit.rlim_cur = std::min(stackSize, limit.rlim_max); + limit.rlim_cur = std::min(static_cast(stackSize), limit.rlim_max); if (setrlimit(RLIMIT_STACK, &limit) != 0) { logger->log( lvlError, diff --git a/src/libutil/current-process.hh b/src/libutil/current-process.hh index a5adb70cfd5..8db4ada0d2e 100644 --- a/src/libutil/current-process.hh +++ b/src/libutil/current-process.hh @@ -21,7 +21,7 @@ unsigned int getMaxCPU(); /** * Change the stack size. */ -void setStackSize(rlim_t stackSize); +void setStackSize(size_t stackSize); #endif /** From 6a3f906382342f05cfe9254f8e0d1c4b01ad9ad5 Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Sat, 18 May 2024 04:27:09 -0700 Subject: [PATCH 2/5] Implement `setStackSize` for Windows --- src/libutil/current-process.cc | 23 +++++++++++++++++++++-- src/libutil/current-process.hh | 2 -- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index b252bd4d035..db311156061 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -59,11 +59,11 @@ unsigned int getMaxCPU() ////////////////////////////////////////////////////////////////////// -#ifndef _WIN32 size_t savedStackSize = 0; void setStackSize(size_t stackSize) { + #ifndef _WIN32 struct rlimit limit; if (getrlimit(RLIMIT_STACK, &limit) == 0 && limit.rlim_cur < stackSize) { savedStackSize = limit.rlim_cur; @@ -81,8 +81,27 @@ void setStackSize(size_t stackSize) ); } } + #else + ULONG currStackSize = 0; + // This retrieves the current promised stack size + SetThreadStackGuarantee(&currStackSize); + if (currStackSize < stackSize) { + savedStackSize = currStackSize; + ULONG newStackSize = stackSize; + if (SetThreadStackGuarantee(&newStackSize) == 0) { + logger->log( + lvlError, + HintFmt( + "Failed to increase stack size from %1% to %2%: %3%", + savedStackSize, + stackSize, + std::to_string(GetLastError()) + ).str() + ); + } + } + #endif } -#endif void restoreProcessContext(bool restoreMounts) { diff --git a/src/libutil/current-process.hh b/src/libutil/current-process.hh index 8db4ada0d2e..8286bf89d66 100644 --- a/src/libutil/current-process.hh +++ b/src/libutil/current-process.hh @@ -17,12 +17,10 @@ namespace nix { */ unsigned int getMaxCPU(); -#ifndef _WIN32 // TODO implement on Windows, if needed. /** * Change the stack size. */ void setStackSize(size_t stackSize); -#endif /** * Restore the original inherited Unix process context (such as signal From a41f4223de5123d90264f2eb9843f218153c59fb Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Sat, 18 May 2024 06:44:52 -0700 Subject: [PATCH 3/5] Use setStackSize on Windows --- src/nix/main.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/nix/main.cc b/src/nix/main.cc index bc13a4df5a6..d3f3324ac04 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -522,10 +522,13 @@ void mainWrapped(int argc, char * * argv) int main(int argc, char * * argv) { -#ifndef _WIN32 // TODO implement on Windows +#ifndef _WIN32 // Increase the default stack size for the evaluator and for // libstdc++'s std::regex. nix::setStackSize(64 * 1024 * 1024); +#else + // Windows' default stack reservation is 1 MB, going over will fail + nix::setStackSize(1 * 1024 * 1024); #endif return nix::handleExceptions(argv[0], [&]() { From 5f68e6d69fe734565cf64705d93a99546a88f97c Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Thu, 23 May 2024 03:54:35 -0700 Subject: [PATCH 4/5] Get max stack size in `setStackSize` to match Linux --- src/libutil/current-process.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/libutil/current-process.cc b/src/libutil/current-process.cc index db311156061..9efb68d47a2 100644 --- a/src/libutil/current-process.cc +++ b/src/libutil/current-process.cc @@ -82,19 +82,23 @@ void setStackSize(size_t stackSize) } } #else + ULONG_PTR stackLow, stackHigh; + GetCurrentThreadStackLimits(&stackLow, &stackHigh); + ULONG maxStackSize = stackHigh - stackLow; ULONG currStackSize = 0; // This retrieves the current promised stack size SetThreadStackGuarantee(&currStackSize); if (currStackSize < stackSize) { savedStackSize = currStackSize; - ULONG newStackSize = stackSize; + ULONG newStackSize = std::min(static_cast(stackSize), maxStackSize); if (SetThreadStackGuarantee(&newStackSize) == 0) { logger->log( lvlError, HintFmt( - "Failed to increase stack size from %1% to %2%: %3%", + "Failed to increase stack size from %1% to %2% (maximum allowed stack size: %3%): %4%", savedStackSize, stackSize, + maxStackSize, std::to_string(GetLastError()) ).str() ); From 0b7da099d112cf4bebc49844405ab5556c1a1ea4 Mon Sep 17 00:00:00 2001 From: PoweredByPie Date: Thu, 23 May 2024 03:55:25 -0700 Subject: [PATCH 5/5] Commit more stack size in some windows binaries This way we can commit the same amount of stack size (64 MB) without a conditional. Includes nix, libnixexpr-tests, libnixfetchers-tests, libnixstore-tests, libnixutil-tests. --- src/nix/local.mk | 5 +++++ src/nix/main.cc | 5 ----- tests/unit/libexpr/local.mk | 5 +++++ tests/unit/libfetchers/local.mk | 5 +++++ tests/unit/libstore/local.mk | 5 +++++ tests/unit/libutil/local.mk | 5 +++++ 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/nix/local.mk b/src/nix/local.mk index 305b0e9dfff..9883509fb9d 100644 --- a/src/nix/local.mk +++ b/src/nix/local.mk @@ -30,6 +30,11 @@ nix_LIBS = libexpr libmain libfetchers libstore libutil libcmd nix_LDFLAGS = $(THREAD_LDFLAGS) $(SODIUM_LIBS) $(EDITLINE_LIBS) $(BOOST_LDFLAGS) $(LOWDOWN_LIBS) +ifdef HOST_WINDOWS + # Increase the default reserved stack size to 65 MB so Nix doesn't run out of space + nix_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024))) +endif + $(foreach name, \ nix-build nix-channel nix-collect-garbage nix-copy-closure nix-daemon nix-env nix-hash nix-instantiate nix-prefetch-url nix-shell nix-store, \ $(eval $(call install-symlink, nix, $(bindir)/$(name)))) diff --git a/src/nix/main.cc b/src/nix/main.cc index d3f3324ac04..541d1a1b3ba 100644 --- a/src/nix/main.cc +++ b/src/nix/main.cc @@ -522,14 +522,9 @@ void mainWrapped(int argc, char * * argv) int main(int argc, char * * argv) { -#ifndef _WIN32 // Increase the default stack size for the evaluator and for // libstdc++'s std::regex. nix::setStackSize(64 * 1024 * 1024); -#else - // Windows' default stack reservation is 1 MB, going over will fail - nix::setStackSize(1 * 1024 * 1024); -#endif return nix::handleExceptions(argv[0], [&]() { nix::mainWrapped(argc, argv); diff --git a/tests/unit/libexpr/local.mk b/tests/unit/libexpr/local.mk index c59191db4d5..09a7dfca15a 100644 --- a/tests/unit/libexpr/local.mk +++ b/tests/unit/libexpr/local.mk @@ -38,3 +38,8 @@ libexpr-tests_LIBS = \ libexpr libexprc libfetchers libstore libstorec libutil libutilc libexpr-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) -lgmock + +ifdef HOST_WINDOWS + # Increase the default reserved stack size to 65 MB so Nix doesn't run out of space + libexpr-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024))) +endif diff --git a/tests/unit/libfetchers/local.mk b/tests/unit/libfetchers/local.mk index e9f659fd72e..d576d28f382 100644 --- a/tests/unit/libfetchers/local.mk +++ b/tests/unit/libfetchers/local.mk @@ -30,3 +30,8 @@ libfetchers-tests_LIBS = \ libfetchers libstore libutil libfetchers-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) + +ifdef HOST_WINDOWS + # Increase the default reserved stack size to 65 MB so Nix doesn't run out of space + libfetchers-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024))) +endif diff --git a/tests/unit/libstore/local.mk b/tests/unit/libstore/local.mk index b8f895fad32..0af1d262222 100644 --- a/tests/unit/libstore/local.mk +++ b/tests/unit/libstore/local.mk @@ -31,3 +31,8 @@ libstore-tests_LIBS = \ libstore libstorec libutil libutilc libstore-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) + +ifdef HOST_WINDOWS + # Increase the default reserved stack size to 65 MB so Nix doesn't run out of space + libstore-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024))) +endif diff --git a/tests/unit/libutil/local.mk b/tests/unit/libutil/local.mk index 39b4c0782c4..b9bddc24d20 100644 --- a/tests/unit/libutil/local.mk +++ b/tests/unit/libutil/local.mk @@ -27,6 +27,11 @@ libutil-tests_LIBS = libutil-test-support libutil libutilc libutil-tests_LDFLAGS := -lrapidcheck $(GTEST_LIBS) +ifdef HOST_WINDOWS + # Increase the default reserved stack size to 65 MB so Nix doesn't run out of space + libutil-tests_LDFLAGS += -Wl,--stack,$(shell echo $$((65 * 1024 * 1024))) +endif + check: $(d)/data/git/check-data.sh.test $(eval $(call run-test,$(d)/data/git/check-data.sh))