test: Add case for libstdc++ bug on Windows #36620

Closed
hebasto wants to merge 1 commit from 261023-win-libstdcxx into master AGit
hebasto commented 2026-10-23 10:01:35 +00:00 (Migrated from github.com)

The code in BerkeleyRODatabase::Backup() relies on copy_file() with overwrite_existing working.

Upstream fixes:

Suggested in https://github.com/bitcoin/bitcoin/pull/36271#issuecomment-5794127014.

Here is an example of the failure when cross-compiling for Windows using GCC 14.2.0:

./test/fs_tests.cpp(145): Entering test case "copy_file_overwrite_existing"
unknown location(0): fatal error: in "fs_tests/copy_file_overwrite_existing": std::filesystem::__cxx11::filesystem_error: filesystem error: cannot copy file: File exists [C:\Users\RUNNER~1\AppData\Local\Temp\test_common bitcoin\fs_tests\copy_file_overwrite_existing\c72b38273c1f6fce4336\a] [C:\Users\RUNNER~1\AppData\Local\Temp\test_common bitcoin\fs_tests\copy_file_overwrite_existing\c72b38273c1f6fce4336\b]
./test/fs_tests.cpp(155): last checkpoint
The [code](https://github.com/bitcoin/bitcoin/blob/152a55397e451fed9c10041bddf01aa6839847d5/src/wallet/migrate.cpp#L745) in `BerkeleyRODatabase::Backup()` relies on `copy_file()` with `overwrite_existing` working. Upstream fixes: - https://github.com/gcc-mirror/gcc/commit/017e3f89b081e4828a588a3bd27b5feacea042b7 - https://github.com/gcc-mirror/gcc/commit/35c98149a5c7af9159fa7615d8d827b3aaa3cc97 - https://github.com/gcc-mirror/gcc/commit/8b3e47b4099f319e38d357f7c646f51040a13d6e Suggested in https://github.com/bitcoin/bitcoin/pull/36271#issuecomment-5794127014. [Here](https://github.com/hebasto/bitcoin/actions/runs/39924174253/job/120344755756#step:10:1943) is an example of the failure when cross-compiling for Windows using GCC 14.2.0: ``` ./test/fs_tests.cpp(145): Entering test case "copy_file_overwrite_existing" unknown location(0): fatal error: in "fs_tests/copy_file_overwrite_existing": std::filesystem::__cxx11::filesystem_error: filesystem error: cannot copy file: File exists [C:\Users\RUNNER~1\AppData\Local\Temp\test_common bitcoin\fs_tests\copy_file_overwrite_existing\c72b38273c1f6fce4336\a] [C:\Users\RUNNER~1\AppData\Local\Temp\test_common bitcoin\fs_tests\copy_file_overwrite_existing\c72b38273c1f6fce4336\b] ./test/fs_tests.cpp(155): last checkpoint ```
DrahtBot commented 2026-10-23 10:01:46 +00:00 (Migrated from github.com)

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

External sites

Reviews

See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36677 (util: Make files opened with fdbridge::fopen noninheritable by SirECMG)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/36620 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/36620.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/36620#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36677](https://github.com/bitcoin/bitcoin/pull/36677) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36677.svg"></sub> (util: Make files opened with fdbridge::fopen noninheritable by SirECMG) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
hebasto commented 2026-10-23 10:03:04 +00:00 (Migrated from github.com)

Here is an example of the failure when cross-compiling for Windows using GCC 14.2.0...

This can be done, for instance, with the following patch:

--- a/contrib/devtools/shell-win64-cross.nix
+++ b/contrib/devtools/shell-win64-cross.nix
@@ -16,9 +16,18 @@ let
   });
   toolchain = crossPkgs.stdenv.cc.targetPrefix;
   pthreads = crossPkgs.windows.pthreads;
+  gccVersion = "14.2.0";
   crossGcc = crossPkgs.buildPackages.gcc14;
-  gcc = crossGcc.override {
+  gcc = (crossGcc.override {
     cc = crossGcc.cc.overrideAttrs (old: {
+      version = gccVersion;
+      name = builtins.replaceStrings [ old.version ] [ gccVersion ] old.name;
+      postPatch = builtins.replaceStrings [ old.version ] [ gccVersion ] old.postPatch;
+      configureFlags = map (builtins.replaceStrings [ old.version ] [ gccVersion ]) old.configureFlags;
+      src = pkgs.fetchurl {
+        url = "mirror://gcc/releases/gcc-${gccVersion}/gcc-${gccVersion}.tar.xz";
+        sha256 = "a7b39bc69cbf9e25826c5a60ab26477001f7c08d85cec04bc0e29cabed6f3cc9";
+      };
       # Keep target pthread headers out of GCC's native build tools.
       env = old.env // {
         EXTRA_FLAGS_FOR_TARGET =
@@ -28,7 +37,9 @@ let
       };
     });
     extraPackages = [ pthreads ];
-  };
+  }).overrideAttrs (_: {
+    version = gccVersion;
+  });
 in
 
 pkgs.mkShellNoCC {
> [Here](https://github.com/hebasto/bitcoin/actions/runs/39924174253/job/120344755756#step:10:1943) is an example of the failure when cross-compiling for Windows using GCC 14.2.0... This can be done, for instance, with the following patch: ```diff --- a/contrib/devtools/shell-win64-cross.nix +++ b/contrib/devtools/shell-win64-cross.nix @@ -16,9 +16,18 @@ let }); toolchain = crossPkgs.stdenv.cc.targetPrefix; pthreads = crossPkgs.windows.pthreads; + gccVersion = "14.2.0"; crossGcc = crossPkgs.buildPackages.gcc14; - gcc = crossGcc.override { + gcc = (crossGcc.override { cc = crossGcc.cc.overrideAttrs (old: { + version = gccVersion; + name = builtins.replaceStrings [ old.version ] [ gccVersion ] old.name; + postPatch = builtins.replaceStrings [ old.version ] [ gccVersion ] old.postPatch; + configureFlags = map (builtins.replaceStrings [ old.version ] [ gccVersion ]) old.configureFlags; + src = pkgs.fetchurl { + url = "mirror://gcc/releases/gcc-${gccVersion}/gcc-${gccVersion}.tar.xz"; + sha256 = "a7b39bc69cbf9e25826c5a60ab26477001f7c08d85cec04bc0e29cabed6f3cc9"; + }; # Keep target pthread headers out of GCC's native build tools. env = old.env // { EXTRA_FLAGS_FOR_TARGET = @@ -28,7 +37,9 @@ let }; }); extraPackages = [ pthreads ]; - }; + }).overrideAttrs (_: { + version = gccVersion; + }); in pkgs.mkShellNoCC { ```
First-time contributor
Design and approach

The PR says Backup() relies on overwrite_existing but does not say which builds are affected or what happens at runtime. The answer is narrow: a migration retried within the same second stops safely. The test only reports the problem in CI and in developer test runs. If the goal is a CI guard that keeps the project's own Windows toolchain from shipping this bug, say so in the commit message. If the goal is to protect users of affected builds, the fix belongs in the migration code, not in a test. Checked against the checkout. BerkeleyRODatabase::Backup() calls copy_file with overwrite_existing (migrate.cpp:745). Its only caller is migration, because BERKELEY_RO databases can only be opened there (walletdb.cpp:1437-1442). The destination name is <prefix>_<GetTime()>.legacy.bak (wallet.cpp:4279), and the code comment says this name is meant to be unique (wallet.cpp:4267). If copy_file fails, Backup returns false and migration stops with "Unable to make a backup of your wallet" before the wallet is unlocked or converted (wallet.cpp:4281-4283). The destination already exists only when a backup with the same name was made in the same second. One way this happens is an immediate retry after a failed migration: RestoreWallet copies the backup with copy_options::none and leaves it in place (wallet.cpp:459). The test is correct. Each test case gets its own data directory, it writes two distinct files, and it reads back the result. It also runs in the Windows-native CI job (ci.yml:391-392), so it can guard the project's own toolchain. But it changes nothing at runtime for affected builds. The PR's only repro needed a manual override of the pinned toolchain, and the PR never says which builds are at risk or what users would see. That is a real gap in the stated motivation, so the needs_motivation objection is supported and actionable. The candidate's 'avoid overwrite_existing' option moved to the top-level alternatives with an extra cost found in the checkout. The candidate's goal, baseline and benefit were tightened to match the source. 1 2 3 4

Review coverage was incomplete.


Reviewed: 37c2ad655bc2 · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7ba49ca38b129bc35f71e566efbd136b17ae87b582376d9eebd04f18959e8274:9b5e0237a3fe9e1d05225291551198f07780ddb92e41e55ade9e98c2687fc127 --> <!-- ralph-base:07efc592927b869b106276b5ace5fe255b5cc2d2 --> <!-- ralph-head:37c2ad655bc2229451fe01801e6db1cd86ff1618 --> <!-- ralph-case:9f9ed856f2130e57480864967e69ffe79b684a34496ff146fc1a8cbfc3bb682f --> ##### Design and approach The PR says Backup() relies on overwrite_existing but does not say which builds are affected or what happens at runtime. The answer is narrow: a migration retried within the same second stops safely. The test only reports the problem in CI and in developer test runs. If the goal is a CI guard that keeps the project's own Windows toolchain from shipping this bug, say so in the commit message. If the goal is to protect users of affected builds, the fix belongs in the migration code, not in a test. Checked against the checkout. BerkeleyRODatabase::Backup() calls copy_file with overwrite_existing (migrate.cpp:745). Its only caller is migration, because BERKELEY_RO databases can only be opened there (walletdb.cpp:1437-1442). The destination name is `<prefix>_<GetTime()>.legacy.bak` (wallet.cpp:4279), and the code comment says this name is meant to be unique (wallet.cpp:4267). If copy_file fails, Backup returns false and migration stops with "Unable to make a backup of your wallet" before the wallet is unlocked or converted (wallet.cpp:4281-4283). The destination already exists only when a backup with the same name was made in the same second. One way this happens is an immediate retry after a failed migration: RestoreWallet copies the backup with copy_options::none and leaves it in place (wallet.cpp:459). The test is correct. Each test case gets its own data directory, it writes two distinct files, and it reads back the result. It also runs in the Windows-native CI job (ci.yml:391-392), so it can guard the project's own toolchain. But it changes nothing at runtime for affected builds. The PR's only repro needed a manual override of the pinned toolchain, and the PR never says which builds are at risk or what users would see. That is a real gap in the stated motivation, so the needs_motivation objection is supported and actionable. The candidate's 'avoid overwrite_existing' option moved to the top-level alternatives with an extra cost found in the checkout. The candidate's goal, baseline and benefit were tightened to match the source. [1](https://git.fish.foo/bitcoin/bitcoin/pulls/36620#issuecomment-612007) [2](https://git.fish.foo/bitcoin/bitcoin/pulls/36620#issuecomment-612099) [3](https://github.com/bitcoin/bitcoin/pull/36620#discussion_r4358942336) [4](https://github.com/bitcoin/bitcoin/pull/36620#issuecomment-6362887361) Review coverage was incomplete. <!-- Review tier: routine --> --- Reviewed: [`37c2ad655bc2`](<https://git.fish.foo/bitcoin/bitcoin/commit/37c2ad655bc2229451fe01801e6db1cd86ff1618>) · [Full review report](<https://review.fish.foo/traces/36620-37c2ad655bc2229451fe01801e6db1cd86ff1618-1802-7.html>) · [Previous reports](<https://review.fish.foo/traces/36620/>) [permalink](<https://review.fish.foo/traces/36620-37c2ad655bc2229451fe01801e6db1cd86ff1618-1802-7.html>)
maflcko commented 2026-10-23 10:17:01 +00:00 (Migrated from github.com)

Could address the Ralph comment?

Could address the Ralph comment?
fanquake commented 2026-10-23 10:20:12 +00:00 (Migrated from github.com)

Can you elaborate on the motivation here? Why this bug specifically? Which users (compiling in what currently used environments) do we expect to hit this?

Can you elaborate on the motivation here? Why this bug specifically? Which users (compiling in what currently used environments) do we expect to hit this?
hebasto commented 2026-10-23 10:29:15 +00:00 (Migrated from github.com)

Can you elaborate on the motivation here? Why this bug specifically? Which users (compiling in what currently used environments) do we expect to hit this?

Even after #36042, users can hit this bug in a build environment we don’t control (e.g. a distro’s g++-mingw-w64-ucrt64 package) that formally meets our minimum requirements but ships an affected libstdc++.

> Can you elaborate on the motivation here? Why this bug specifically? Which users (compiling in what currently used environments) do we expect to hit this? Even after #36042, users can hit this bug in a build environment we don’t control (e.g. a distro’s `g++-mingw-w64-ucrt64` package) that formally meets our minimum requirements but ships an affected libstdc++.
maflcko commented 2026-10-23 10:31:25 +00:00 (Migrated from github.com)

Seems harmless to add the test. An alternative could be to reject g++-mingw less than 14.3 during configure?

Seems harmless to add the test. An alternative could be to reject g++-mingw less than 14.3 during configure?
hebasto commented 2026-10-23 10:41:02 +00:00 (Migrated from github.com)

Could address the Ralph comment?

Done.

> Could address the Ralph comment? Done.
fanquake commented 2026-10-23 10:41:08 +00:00 (Migrated from github.com)

Even after https://github.com/bitcoin/bitcoin/pull/36042, users can hit this bug in a build environment we don’t control

Thanks:

Why this bug specifically?"
"Which users (compiling in what currently used environments) do we expect to hit this?" ?

It's not clear that adding a test here is that useful, unless you expect cross-compiling end-users are running the unit test binary on their systems before running bitcoind? Otherwise, as mentioned above, maybe it'd be better to reject the compiler outright.

It's also not clear what runtime failure / bug the user would be prevented from hitting (by seeing this test failure) if they happened to be using a broken compiler. If I understand correctly, does this mean that migration has likely never worked on windows with GCC-based cross-compiled binaries, but also nobody has ever reported it?

There are lots of compiler bugs, and we don't add tests for many of them, and the PR description / commit messages here don't really justify the changes, or explain how a user would be affected by this at runtime, if they used a broken compiler.

> Even after https://github.com/bitcoin/bitcoin/pull/36042, users can hit this bug in a build environment we don’t control Thanks: > Why this bug specifically?" > "Which users (compiling in what currently used environments) do we expect to hit this?" ? It's not clear that adding a test here is that useful, unless you expect cross-compiling end-users are running the unit test binary on their systems before running bitcoind? Otherwise, as mentioned above, maybe it'd be better to reject the compiler outright. It's also not clear what runtime failure / bug the user would be prevented from hitting (by seeing this test failure) if they happened to be using a broken compiler. If I understand correctly, does this mean that migration has likely never worked on windows with GCC-based cross-compiled binaries, but also nobody has ever reported it? There are lots of compiler bugs, and we don't add tests for many of them, and the PR description / commit messages here don't really justify the changes, or explain how a user would be affected by this at runtime, if they used a broken compiler.
fanquake (Migrated from github.com) reviewed 2026-10-23 11:17:33 +00:00
fanquake (Migrated from github.com) commented 2026-10-23 11:17:33 +00:00

(fixed in GCC 13.4 ... never in 12.x)

One other point here is that we have been shipping Windows release binaries (28.0 with 12.4.0 & 29.0/30.0 with 13.3.0), which should have this bug. 30.0 is still supported, so if this is serious enough to warrant a unit test now, shouldn't we be patching the compiler/libstdc++ in Guix and doing a point release with the fix?

> (fixed in GCC 13.4 ... never in 12.x) One other point here is that we have been shipping Windows release binaries (`28.0` with `12.4.0` & `29.0`/`30.0` with `13.3.0`), which should have this bug. `30.0` is still supported, so if this is serious enough to warrant a unit test now, shouldn't we be patching the compiler/libstdc++ in Guix and doing a point release with the fix?
hebasto commented 2026-10-23 13:16:52 +00:00 (Migrated from github.com)

I have to admit that I can’t see a real-life scenario in which the bug affects users.

The code is reachable only when migrating a legacy BDB wallet, either via the migratewallet RPC command or via the GUI.

The dest argument of BerkeleyRODatabase::Backup() is constructed in MigrateLegacyToDescriptor() and includes a timestamp. If a migration attempt fails for any reason, the next attempt is very likely to write a differently named file. However, the bug can only be hit if the backup file has the same name.

I have to admit that I can’t see a real-life scenario in which the bug affects users. The code is reachable only when migrating a legacy BDB wallet, either via the `migratewallet` RPC command or via the GUI. The `dest` argument of `BerkeleyRODatabase::Backup()` is constructed in `MigrateLegacyToDescriptor()` and includes a timestamp. If a migration attempt fails for any reason, the next attempt is very likely to write a differently named file. However, the bug can only be hit if the backup file has the same name.
theuni commented 2026-10-23 18:27:10 +00:00 (Migrated from github.com)

It seems to me this is either:

  • A compiler config not worth worrying about
  • A compiler config worth explicitly blocking as suggested by @maflcko
  • A compiler config we're worried about, in which case we could just remove the copy_options param from copy_file so that we can't hit this anywhere else.

Either way, adding a test that fails where we expect it to fail doesn't seem very helpful to me.

It seems to me this is either: - A compiler config not worth worrying about - A compiler config worth explicitly blocking as suggested by @maflcko - A compiler config we're worried about, in which case we could just remove the `copy_options` param from `copy_file` so that we can't hit this anywhere else. Either way, adding a test that fails where we expect it to fail doesn't seem very helpful to me.
hebasto commented 2026-10-24 08:10:58 +00:00 (Migrated from github.com)

... we could just remove the copy_options param from copy_file so that we can't hit this anywhere else.

@achow101

Could we?

> ... we could just remove the `copy_options` param from `copy_file` so that we can't hit this anywhere else. @achow101 Could we?
maflcko commented 2026-10-26 16:00:23 +00:00 (Migrated from github.com)

Could we?

It seems like a bugfix, because migration shouldn't overwrite?

> Could we? It seems like a bugfix, because migration shouldn't overwrite?
fanquake commented 2026-11-17 10:27:40 +00:00 (Migrated from github.com)

What's the status here? Looks like there's not agreement to add the test, and no interest in fixing the release binaries, so maybe a new pull can be opened for a wallet refactor? Or something can be done following #34193?

What's the status here? Looks like there's not agreement to add the test, and no interest in [fixing the release binaries](https://github.com/bitcoin/bitcoin/pull/36620#discussion_r4358942336), so maybe a new pull can be opened for a wallet refactor? Or something can be done following #34193?
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!36620
No description provided.