Skip to content

fix: free the plugin's buffers once at installer exit - #2

Merged
stasadev merged 1 commit into
mainfrom
20261202_stasadev_fix_double_free
Dec 2, 2026
Merged

stasadev merged 1 commit into
mainfrom
20261202_stasadev_fix_double_free

Conversation

@stasadev

@stasadev stasadev commented Dec 2, 2026

Copy link
Copy Markdown
Member

Short Summary (TL;DR)

EnVar freed its three string buffers twice when the installer exited, which corrupted the installer's heap. The DDEV Windows installer then crashed at exit about one run in six. EnVar now frees them once.

The Issue

The DDEV Windows installer ddev_windows_amd64_installer.v1.25.6.exe sometimes exits with 0xC0000005 (an access violation in ntdll.dll) after its log already says "Installation completed successfully". Scripts that check the exit code then fail. Because of this, ddev's installer tests run the installer a second time, with the root cause unknown:

The installer also sometimes never exits after logging success:

That is the same bug. A dump of a hung installer shows its main thread inside the same OleUninitialize() free, holding the heap lock, while the thread pool threads wait for that lock.

CleanUp() runs twice at exit. NSISPluginCallback() runs it at NSPIM_UNLOAD, and _DllMainCRTStartup() runs it again at DLL_PROCESS_DETACH. StrFree() set only its own copy of the pointer to NULL, so the second run called GlobalFree() again on the same three buffers. The heap damage shows up later, usually when OleUninitialize() at NSIS exit frees COM's cached ShellLink class entry from CreateShortCut.

How This PR Solves The Issue

  • StrFree() takes the address of the pointer and sets the pointer itself to NULL, so a second CleanUp() frees nothing.
  • _DllMainCRTStartup() no longer calls CleanUp(), because NSISPluginCallback() already does it at NSPIM_UNLOAD. The WM_SETTINGCHANGE broadcast in CleanUp() then runs once, and not inside DllMain.

Manual Testing Instructions

On Windows, build a DDEV installer with this branch's x86-unicode EnVar.dll, as .ci-scripts/nsis_setup.sh does with the release zip. Then run it under full page heap, which stops a process at the first invalid free:

$exe = 'ddev_windows_amd64_installer.exe'
$k = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\$exe"
New-Item -Force $k | Out-Null
Set-ItemProperty $k GlobalFlag 0x02000000 -Type DWord
Set-ItemProperty $k PageHeapFlags 3 -Type DWord
(Start-Process -PassThru -Wait ".\$exe" -ArgumentList '/S /traditional').ExitCode
# Expect 0. With EnVar v0.3.1 it is -1073740767 (0xC0000421), the page heap stop.
Remove-Item -Recurse $k

Automated Testing Overview

No new tests, because the existing plugin test passes with the double free too. Claude Code tested the change on Windows 11 (build 26300) in a KVM VM. It built DDEV installers with NSIS 3.10-2 that differ only in EnVar.dll, and ran each one in turn:

EnVar.dll Full page heap, 1 run Normal runs
v0.3.1 0xC0000421 in DllMain at GlobalFree() 80 runs: 3 crashes (0xC0000005), 3 hangs, 74 exit 0
v0.3.1 with the CleanUp() call in DllMain patched out exit 0 40 runs: 40 exit 0
this change, built by this repository's CI exit 0 40 runs: 40 exit 0

The CI build of this change passes the plugin test for x86 ANSI, x86 Unicode and x64 Unicode.

Release/Deployment Notes

DDEV picks up the fix after a new EnVar release, because .ci-scripts/nsis_setup.sh in ddev/ddev downloads the latest EnVar-Plugin.zip. After that, ddev/ddev can drop the second installer run in runInstaller() in winpkg/installer_test.go.

🤖 Developed with assistance from Claude Code

## Short Summary (TL;DR)

EnVar freed its three string buffers twice when the installer exited, which corrupted the installer's heap. The DDEV Windows installer then crashed at exit about one run in six. EnVar now frees them once.

## The Issue

The DDEV Windows installer `ddev_windows_amd64_installer.v1.25.6.exe` sometimes exits with 0xC0000005 (an access violation in `ntdll.dll`) after its log already says "Installation completed successfully". Scripts that check the exit code then fail. Because of this, ddev's installer tests run the installer a second time, with the root cause unknown:

- ddev/ddev#9014

The installer also sometimes never exits after logging success:

- ddev/ddev#9014
- ddev/ddev#8337

That is the same bug. A dump of a hung installer shows its main thread inside the same `OleUninitialize()` free, holding the heap lock, while the thread pool threads wait for that lock.

`CleanUp()` runs twice at exit. `NSISPluginCallback()` runs it at `NSPIM_UNLOAD`, and `_DllMainCRTStartup()` runs it again at `DLL_PROCESS_DETACH`. `StrFree()` set only its own copy of the pointer to NULL, so the second run called `GlobalFree()` again on the same three buffers. The heap damage shows up later, usually when `OleUninitialize()` at NSIS exit frees COM's cached ShellLink class entry from `CreateShortCut`.

## How This PR Solves The Issue

- `StrFree()` takes the address of the pointer and sets the pointer itself to NULL, so a second `CleanUp()` frees nothing.
- `_DllMainCRTStartup()` no longer calls `CleanUp()`, because `NSISPluginCallback()` already does it at `NSPIM_UNLOAD`. The `WM_SETTINGCHANGE` broadcast in `CleanUp()` then runs once, and not inside `DllMain`.

## Manual Testing Instructions

On Windows, build a DDEV installer with this branch's `x86-unicode` EnVar.dll, as `.ci-scripts/nsis_setup.sh` does with the release zip. Then run it under full page heap, which stops a process at the first invalid free:

```powershell
$exe = 'ddev_windows_amd64_installer.exe'
$k = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\$exe"
New-Item -Force $k | Out-Null
Set-ItemProperty $k GlobalFlag 0x02000000 -Type DWord
Set-ItemProperty $k PageHeapFlags 3 -Type DWord
(Start-Process -PassThru -Wait ".\$exe" -ArgumentList '/S /traditional').ExitCode
# Expect 0. With EnVar v0.3.1 it is -1073740767 (0xC0000421), the page heap stop.
Remove-Item -Recurse $k
```

## Automated Testing Overview

No new tests, because the existing plugin test passes with the double free too. Claude Code tested the change on Windows 11 (build 26300) in a KVM VM. It built DDEV installers with NSIS 3.10-2 that differ only in EnVar.dll, and ran each one in turn:

| EnVar.dll | Full page heap, 1 run | Normal runs |
| --- | --- | --- |
| v0.3.1 | 0xC0000421 in `DllMain` at `GlobalFree()` | 80 runs: 3 crashes (0xC0000005), 3 hangs, 74 exit 0 |
| v0.3.1 with the `CleanUp()` call in `DllMain` patched out | exit 0 | 40 runs: 40 exit 0 |
| this change, built by this repository's CI | exit 0 | 40 runs: 40 exit 0 |

The CI build of this change passes the plugin test for x86 ANSI, x86 Unicode and x64 Unicode.

## Release/Deployment Notes

DDEV picks up the fix after a new EnVar release, because `.ci-scripts/nsis_setup.sh` in ddev/ddev downloads the latest `EnVar-Plugin.zip`. After that, ddev/ddev can drop the second installer run in `runInstaller()` in `winpkg/installer_test.go`.

🤖 Developed with assistance from [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@stasadev
stasadev merged commit 2796903 into main Dec 2, 2026
2 checks passed
@stasadev
stasadev deleted the 20261202_stasadev_fix_double_free branch December 2, 2026 20:13
stasadev added a commit to ddev/ddev that referenced this pull request Dec 2, 2026
… ddev-windows-installer] [skip github]

## Short Summary (TL;DR)

The Windows installer tests ran the installer a second time when the first run failed, because the installer sometimes crashed or hung at exit. EnVar v0.3.2 fixes that crash, so the tests now run the installer once.

## The Issue

The installer sometimes exited with 0xC0000005, or never exited, after its log said "Installation completed successfully". The cause was a double free in the EnVar NSIS plugin at installer exit. The retry and the 4-minute kill came in with these PRs:

- #8337
- #9014

ddev/EnVar#2 fixed the double free, and EnVar v0.3.2 includes the fix. With v0.3.1, 80 installer runs gave 3 crashes and 3 hangs. With the fix, 40 runs gave none.

## How This PR Solves The Issue

`runInstaller()` in `winpkg/installer_test.go` runs the installer once. It still kills an installer that runs longer than 4 minutes, so a hang fails the test instead of the whole job. It also still prints the installer debug log when a run fails.

Release builds need no change. `.github/workflows/linux-build-setup.sh` downloads the latest EnVar on each run, so the next release gets v0.3.2.

The Buildkite installer tests build the installer on the testbot with the NSIS in `C:\Program Files (x86)\NSIS`. The test jobs run unprivileged and can't update its plugins. So Claude Code updated the testbots over SSH on 2026-12-02:

| Testbot | NSIS | Agent | Action |
| --- | --- | --- | --- |
| tb-win11-06 | yes | `os=windows` | EnVar v0.3.1 replaced with v0.3.2 |
| tb-win11-10 | yes | `os=windows` | EnVar v0.3.1 replaced with v0.3.2 |
| tb-wsl-14 | yes | no `os=windows` | EnVar v0.3.1 replaced with v0.3.2 |
| tb-wsl-12 | no | no `os=windows` | none |
| tb-wsl-16 | no | no `os=windows` | none |

On each updated testbot, Claude Code did these steps:

1. Copied the old `Plugins\*\EnVar.dll` files to `%USERPROFILE%\EnVar-0.3.1-backup`. Their SHA-256 hashes match the v0.3.1 release zip.
2. Extracted the v0.3.2 `EnVar-Plugin.zip` over `C:\Program Files (x86)\NSIS`.
3. Checked that the SHA-256 of each new `EnVar.dll` matches the release zip.

The installer uses `Plugins\x86-unicode\EnVar.dll`, whose v0.3.2 SHA-256 is `3f51263eaad3c84d234d086d2f08f01214a8494d407579b698f4595a997df651`.

## Manual Testing Instructions

Run the `windows-installer` Buildkite pipeline on this branch a few times. Each `ddev-test-*` case and the traditional case must pass, and each log must show one "Running installer:" line per installer run.

To check a testbot's plugin, run this in PowerShell on it:

```powershell
Get-FileHash 'C:\Program Files (x86)\NSIS\Plugins\x86-unicode\EnVar.dll'
# Expect 3F51263EAAD3C84D234D086D2F08F01214A8494D407579B698F4595A997DF651
```

## Automated Testing Overview

No new tests. Claude Code ran `GOOS=windows go vet ./winpkg` and `GOOS=windows golangci-lint run ./winpkg/...` on Linux, with no issues. Nobody has run the installer tests with this change yet. The Buildkite pipeline runs them on Windows.

## Release/Deployment Notes

A new Windows testbot needs EnVar v0.3.2 or later. `docs/content/developers/scripts/windows_postinstall.sh` already downloads the latest release.

🤖 Developed with assistance from [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@rfay

rfay commented Dec 2, 2026

Copy link
Copy Markdown
Member

Amazing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants