From ee9a81ac56f25903acbef8b270251e3025bbae6f Mon Sep 17 00:00:00 2001 From: Stanislav Zhuk Date: Wed, 2 Dec 2026 21:18:24 +0200 Subject: [PATCH] fix: free the plugin's buffers once at installer exit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ## 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 --- Src/envar.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/Src/envar.c b/Src/envar.c index 1992ee8..dfaba9b 100644 --- a/Src/envar.c +++ b/Src/envar.c @@ -46,11 +46,11 @@ PTCHAR StrAlloc(SIZE_T strlen) return (PTCHAR)GlobalAlloc(GPTR, strlen*sizeof(TCHAR)); } -/* Frees a string. */ -void StrFree(PTCHAR hVar) +/* Frees a string and clears the caller's pointer, so a second free is a no-op. */ +void StrFree(PTCHAR* phVar) { - GlobalFree(hVar); - hVar = NULL; + GlobalFree(*phVar); + *phVar = NULL; } /* Returns the string size. */ @@ -76,7 +76,7 @@ BOOL StrReAlloc(PTCHAR* phVar, int strlen) if (!temp) return 0; for (i = 0; i < StrSize(*phVar); i++) temp[i] = (*phVar)[i]; - StrFree(*phVar); + StrFree(phVar); *phVar = temp; return 1; @@ -93,9 +93,9 @@ void AllocStrs(void) /* Frees allocated buffers. */ void CleanUp(void) { - if (gptVarName) StrFree(gptVarName); - if (gptPathString) StrFree(gptPathString); - if (gptBuffer) StrFree(gptBuffer); + if (gptVarName) StrFree(&gptVarName); + if (gptPathString) StrFree(&gptPathString); + if (gptBuffer) StrFree(&gptBuffer); SendMessageTimeout(HWND_BROADCAST, WM_SETTINGCHANGE, (WPARAM)NULL, (LPARAM)HKCU_STR, 0, 100, 0); } @@ -557,13 +557,12 @@ __declspec(dllexport) void Update(HWND hwndParent, int string_size, TCHAR *varia pushstring(UpdateVar()); } -/* Our DLL entry point, this is called when we first load up our DLL. */ +/* Our DLL entry point, this is called when we first load up our DLL. + No CleanUp() on DLL_PROCESS_DETACH: NSISPluginCallback() already ran it at NSPIM_UNLOAD, a second run + freed the buffers twice and corrupted the installer's heap. */ BOOL WINAPI _DllMainCRTStartup(HINSTANCE hInst, DWORD ul_reason_for_call, LPVOID lpReserved) { hInstance = hInst; - if (ul_reason_for_call == DLL_PROCESS_DETACH) - CleanUp(); - return TRUE; }