Skip to content

unit1664: add workaround for VS2019 16.3 - VS2022 17.4 x64 miscompile - #23337

Closed
vszakats wants to merge 8 commits into
curl:masterfrom
vszakats:vs2019-x64-release-unit1664-miscompile
Closed

vszakats wants to merge 8 commits into
curl:masterfrom
vszakats:vs2019-x64-release-unit1664-miscompile

Conversation

@vszakats

@vszakats vszakats commented Oct 9, 2026 •

Copy link
Copy Markdown
Member

It affects _MSC_VER >= 1923 && _MSC_VER <= 1934, combined with -O1
or -O2, and inlining reduced via -Ob0 or -Ob1 (CMake
RelWithDebInfo config). Also with -O2 -Ob2 (CMake Release config), in
curl units.exe context.

Introduced in: Visual Studio 2019 v16.3 (~2019-09-23)
Fixed in: Visual Studio 2022 v17.5 (~2023-02-21)

Godbolt reproducer: https://godbolt.org/z/T78GWxzeT

As seen in AppVeyor CI:

  • CM VS2019, Debug , x64, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: PASS
  • CM VS2019, Release, x64, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: FAIL test1664
  • CM VS2019, Release, x86, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: PASS
  • VS2017/VS2022, Debug/Release: PASS

Fixing:

# Visual Studio Community 2019 v16.11.50
-- Building for: Visual Studio 16 2019
-- Using CMake version 4.1.0
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.17763.
-- The C compiler identification is MSVC 19.29.30159.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- CMake platform flags: WIN32 MSVC-1929
[...]
-- Found _WIN32_WINNT=0x0a00
-- Picky compiler options: -W4 -Wall -wd4061 -wd4191 -wd4255 -wd4464 -wd4548 -wd4574 -wd4668 -wd4710 -wd4711 -wd4746 -wd4820 -wd5045
[...]
test 1664 [unit tests for strparse.c string parsing functions]

   1664: stdout FAILED:
  --- log/check-expected
  +++ log/check-generated
  @@ -154,9 +154,9 @@
   3: (%62) 6, line 0[LF]
   4: (%5c) 6, line 0[LF]
   5: (%20) 6, line 0[LF]
  -6: (%0a) 0, line 1[LF]
  -7: (%0d) 0, line 1[LF]
  -8: (%0d) 0, line 1[LF]
  +6: (%00) 0, line 1[LF]
  +7: (%00) 0, line 1[LF]
  +8: (%0a) 0, line 1[LF]
   9: (%0c) 6, line 0[LF]
   10: (%00) 6, line 0[LF]
   curlx_str_hex[LF]
[...]
TESTFAIL: These test cases failed: 1664

Ref: https://ci.appveyor.com/project/curlorg/curl/builds/54864988/job/m823tifoodo7tt1a

Bug: #23306 (comment)
Bug: #23306 (comment)
Bug: #23306 (comment)
Ref: #23347 (seeking a minimal reproducer)
Cherry-picked from #23306


Reproduced in this PR:
https://ci.appveyor.com/project/curlorg/curl/builds/54868446/job/q8575pmwne0av023 (!verbose-strings)
https://ci.appveyor.com/project/curlorg/curl/builds/54868446/job/os8tdx8dgqrhbb3b (verbose-strings; default)

Code snipped causing the issue extracted here (not reproducing the
issue in this isolated case):
https://godbolt.org/z/n5jYPKWsM

Seen in VS2019 Release x64 build in AppVeyor CI.

- CM VS2019, Debug  , x64, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: PASS
- CM VS2019, Release, x64, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: FAIL test1664
- CM VS2019, Release, x86, OpenSSL 3.0 + Schannel, Shared, !verbose, Build-tests: PASS

Visual Studio Community 2019 v16.11.50
```
-- Building for: Visual Studio 16 2019
-- Using CMake version 4.1.0
-- Selecting Windows SDK version 10.0.26100.0 to target Windows 10.0.17763.
-- The C compiler identification is MSVC 19.29.30159.0
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio/2019/Community/VC/Tools/MSVC/14.29.30133/bin/Hostx64/x64/cl.exe - skipped
-- CMake platform flags: WIN32 MSVC-1929
[...]
-- Found _WIN32_WINNT=0x0a00
-- Picky compiler options: -W4 -Wall -wd4061 -wd4191 -wd4255 -wd4464 -wd4548 -wd4574 -wd4668 -wd4710 -wd4711 -wd4746 -wd4820 -wd5045
[...]
test 1664 [unit tests for strparse.c string parsing functions]

   1664: stdout FAILED:
  --- log/check-expected    2026-10-09 08:50:52.895380000 +0000
  +++ log/check-generated   2026-10-09 08:50:52.895380000 +0000
  @@ -154,9 +154,9 @@
   3: (%62) 6, line 0[LF]
   4: (%5c) 6, line 0[LF]
   5: (%20) 6, line 0[LF]
  -6: (%0a) 0, line 1[LF]
  -7: (%0d) 0, line 1[LF]
  -8: (%0d) 0, line 1[LF]
  +6: (%00) 0, line 1[LF]
  +7: (%00) 0, line 1[LF]
  +8: (%0a) 0, line 1[LF]
   9: (%0c) 6, line 0[LF]
   10: (%00) 6, line 0[LF]
   curlx_str_hex[LF]
```
Ref: https://ci.appveyor.com/project/curlorg/curl/builds/54864988/job/m823tifoodo7tt1a

Does not reproduce in this isolated example:
https://godbolt.org/z/n5jYPKWsM

Bug: curl#23306 (comment)
Bug: curl#23306 (comment)
Bug: curl#23306 (comment)
Cherry-picked from curl#23306
@vszakats
vszakats marked this pull request as draft October 9, 2026 19:17
@github-actions github-actions Bot added cmdline tool tests CI Continuous Integration labels Oct 9, 2026
@vszakats vszakats added build Windows Windows-specific tests and removed cmdline tool tests CI Continuous Integration labels Oct 9, 2026
@github-actions github-actions Bot added cmdline tool CI Continuous Integration labels Oct 9, 2026
@vszakats vszakats changed the title unit1664: add workaround for VS2019 miscompile unit1664: add workaround for VS2019-x64-Release miscompile Oct 9, 2026
@vszakats
vszakats marked this pull request as ready for review October 9, 2026 19:33
@jay

jay commented Oct 10, 2026

Copy link
Copy Markdown
Member

I thought from the other bug that you traced this to dual CRTs mixing their output? Also would it be better to exclude this test rather than add the workaround? If we don't know the cause of this then we can't say for certain it's the compiler. It looks likely but it can't be reproduced in a self contained example without curl. Maybe there is some undefined behavior causing it.

@vszakats

vszakats commented Oct 10, 2026 •

Copy link
Copy Markdown
Member Author

I thought from the other bug that you traced this to dual CRTs mixing their output? Also would it be better to exclude this test rather than add the workaround? If we don't know the cause of this then we can't say for certain it's the compiler. It looks likely but it can't be reproduced in a self contained example without curl. Maybe there is some undefined behavior causing it.

This is not related to the CRT issue. Would it be a better workaround to disable this test? I'm not so sure, this workaround seems to explain this better, and where the issue happens, than a comment somewhere else. To exclude it dynamically, we'd pbly need clumsy and verbose detection code somewhere in runtests.

I did not make a great effort with the minimal example so perhaps I just remove it and leave it to someone expert in this (or somebody with this compiler version installed locally). To me this seems not specifically curl related but rather related to the larger generated code context. (The actual tested function and the test itself are generic and simple.)

One more thing to be done if someone feels like digging: downloading the object/binary as an artifact and peeking into the disassembly.

@vszakats

This comment was marked as outdated.

@vszakats

Copy link
Copy Markdown
Member Author

Diff of a reduced-units.c assembly output, master (failing) vs this patch (passing):
https://github.com/curl/curl/blob/142a1bc12936b678afb3b56f898383f00abec9b8/tests/unit/unit1664.c
(also switched to static libcurl, and dropped any surplus code from first.c/h,
also made the single called curlx function local.)

--- units.cod
+++ units-workaround.cod
--- units.cod
+++ units-patched.cod
@@ -130,13 +130,13 @@
 ;	COMDAT pdata
 pdata	SEGMENT
 $pdata$1$test_unit1664 DD imagerel test_unit1664+20
-	DD	imagerel test_unit1664+124
+	DD	imagerel test_unit1664+121
 	DD	imagerel $chain$1$test_unit1664
 pdata	ENDS
 ;	COMDAT pdata
 pdata	SEGMENT
-$pdata$2$test_unit1664 DD imagerel test_unit1664+124
-	DD	imagerel test_unit1664+132
+$pdata$2$test_unit1664 DD imagerel test_unit1664+121
+	DD	imagerel test_unit1664+129
 	DD	imagerel $chain$2$test_unit1664
 pdata	ENDS
 ;	COMDAT pdata
@@ -495,7 +495,7 @@
 	00 00		 mov	 r8, QWORD PTR ?newl@?1??test_unit1664@@9@9
   0000d	33 db		 xor	 ebx, ebx
   0000f	4d 85 c0	 test	 r8, r8
-  00012	74 68		 je	 SHORT $LN10@test_unit1
+  00012	74 65		 je	 SHORT $LN10@test_unit1
   00014	48 89 74 24 40	 mov	 QWORD PTR [rsp+64], rsi
   00019	48 8d 35 00 00
 	00 00		 lea	 rsi, OFFSET FLAT:?newl@?1??test_unit1664@@9@9
@@ -515,21 +515,20 @@
 
 ; 62   :     printf("%d: (%%%02x) %d, line %d\n",
 
-  0003f	48 8b 4c 24 48	 mov	 rcx, QWORD PTR line$1[rsp]
-  00044	44 8b c8	 mov	 r9d, eax
-  00047	8b d1		 mov	 edx, ecx
-  00049	41 2b d0	 sub	 edx, r8d
-  0004c	89 54 24 20	 mov	 DWORD PTR [rsp+32], edx
-  00050	8b d3		 mov	 edx, ebx
-  00052	44 0f be 01	 movsx	 r8d, BYTE PTR [rcx]
-  00056	48 8d 0d 00 00
+  0003f	8b 4c 24 48	 mov	 ecx, DWORD PTR line$1[rsp]
+  00043	44 8b c8	 mov	 r9d, eax
+  00046	41 2b c8	 sub	 ecx, r8d
+  00049	8b d3		 mov	 edx, ebx
+  0004b	45 0f be 00	 movsx	 r8d, BYTE PTR [r8]
+  0004f	89 4c 24 20	 mov	 DWORD PTR [rsp+32], ecx
+  00053	48 8d 0d 00 00
 	00 00		 lea	 rcx, OFFSET FLAT:??_C@_0BK@DKNMMGEI@?$CFd?3?5?$CI?$CF?$CF?$CF02x?$CJ?5?$CFd?0?5line?5?$CFd?6@
-  0005d	e8 00 00 00 00	 call	 printf
-  00062	4c 8b 44 fe 08	 mov	 r8, QWORD PTR [rsi+rdi*8+8]
-  00067	48 8d 7f 01	 lea	 rdi, QWORD PTR [rdi+1]
-  0006b	ff c3		 inc	 ebx
-  0006d	4d 85 c0	 test	 r8, r8
-  00070	75 be		 jne	 SHORT $LL4@test_unit1
+  0005a	e8 00 00 00 00	 call	 printf
+  0005f	4c 8b 44 fe 08	 mov	 r8, QWORD PTR [rsi+rdi*8+8]
+  00064	48 8d 7f 01	 lea	 rdi, QWORD PTR [rdi+1]
+  00068	ff c3		 inc	 ebx
+  0006a	4d 85 c0	 test	 r8, r8
+  0006d	75 c1		 jne	 SHORT $LL4@test_unit1
 
 ; 63   : /* Workaround for the expression erroneously returning the byte from the next
 ; 64   :    address for inputs \n, \r and \r\n. Thus returning NUL, NUL, \n instead of
@@ -544,16 +543,16 @@
 ; 73   : 
 ; 74   :   return (CURLcode)0;
 
-  00072	48 8b 7c 24 50	 mov	 rdi, QWORD PTR [rsp+80]
-  00077	48 8b 74 24 40	 mov	 rsi, QWORD PTR [rsp+64]
+  0006f	48 8b 7c 24 50	 mov	 rdi, QWORD PTR [rsp+80]
+  00074	48 8b 74 24 40	 mov	 rsi, QWORD PTR [rsp+64]
 $LN10@test_unit1:
 
 ; 75   : }
 
-  0007c	33 c0		 xor	 eax, eax
-  0007e	48 83 c4 30	 add	 rsp, 48			; 00000030H
-  00082	5b		 pop	 rbx
-  00083	c3		 ret	 0
+  00079	33 c0		 xor	 eax, eax
+  0007b	48 83 c4 30	 add	 rsp, 48			; 00000030H
+  0007f	5b		 pop	 rbx
+  00080	c3		 ret	 0
 test_unit1664 ENDP
 _TEXT	ENDS
 ; Function compile flags: /Ogtpy

via:
https://ci.appveyor.com/project/curlorg/curl/builds/54875742/job/7tlq8hktuv0bn9d2/artifacts
https://ci.appveyor.com/project/curlorg/curl/builds/54875742/job/ur4fode42d2j6fp6/artifacts

@vszakats

Copy link
Copy Markdown
Member Author

Godbolt reproducer:
https://godbolt.org/z/T78GWxzeT

It required /O1 (or /O2) combined with /Ob0 on godbolt.

(In curl it seems to happen with /O2 /Ob2. This still needs to be cleared up.)

@vszakats vszakats changed the title unit1664: add workaround for VS2019-x64-Release miscompile unit1664: add workaround for VS2019/VS2022 miscompile Oct 11, 2026
@vszakats vszakats changed the title unit1664: add workaround for VS2019/VS2022 miscompile unit1664: add workaround for VS2019 16.3 - VS2022 17.4 miscompile Oct 11, 2026
@vszakats vszakats changed the title unit1664: add workaround for VS2019 16.3 - VS2022 17.4 miscompile unit1664: add workaround for VS2019 16.3 - VS2022 17.4 x64 miscompile Oct 11, 2026
@vszakats vszakats closed this in 49f003f Oct 11, 2026
@vszakats
vszakats deleted the vs2019-x64-release-unit1664-miscompile branch October 11, 2026 17:43
@jay

jay commented Oct 11, 2026

Copy link
Copy Markdown
Member

Introduced in: Visual Studio 2019 v16.3 (~2019-09-23)
Fixed in: Visual Studio 2022 v17.5 (~2023-02-21)

Thanks for figuring this out. I played around with it in a VM image that has a not-up-to-date VS 2022 17.1 and I saw the same thing. In the debug profile I disabled runtime checks and enabled maximum optimization /O1 and that was enough to do it. The inline function expansion optimization setting (/Ob0, /Ob1 etc) had no effect. I found that if the for loop is removed or condition omitted (like i = 6 for ;; followed by break before the end of the block) then the bug does not occur. However if the for loop has even one condition/iteration like i = 6; i == 6; ++i then the bug occurs. This is definitely a compiler issue.

@vszakats

vszakats commented Oct 11, 2026 •

Copy link
Copy Markdown
Member Author

Introduced in: Visual Studio 2019 v16.3 (~2019-09-23)
Fixed in: Visual Studio 2022 v17.5 (~2023-02-21)

Thanks for figuring this out. I played around with it in a VM image that has a not-up-to-date VS 2022 17.1 and I saw the same thing. In the debug profile I disabled runtime checks and enabled maximum optimization /O1 and that was enough to do it. The inline function expansion optimization setting (/Ob0, /Ob1 etc) had no effect. I found that if the for loop is removed or condition omitted (like i = 6 for ;; followed by break before the end of the block) then the bug does not occur. However if the for loop has even one condition/iteration like i = 6; i == 6; ++i then the bug occurs. This is definitely a compiler issue.

The exact option combo is kind of murky, yes. In CI, Release builds
reproduced it (/O2 /Ob2), then I switched to RelWithDebInfo (/O2 /Ob1)
while reducing the code, and it kept reproducing, but no longer in
Release mode. Then on godbolt I noticed that removing the /Ob* from
/O1 /Ob1 option no longer reproduces it. I guess there may be many
variants depending on how the exact details are hitting the bug or not.

CMake (as of 4.4.3) config options for MSVC, FTR:

./Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_DEBUG_INIT "${_MDd}${_Zi} /Ob0 /Od${_RTC1_local}")
./Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_RELEASE_INIT "${_MD} /O2 /Ob2 /DNDEBUG")
./Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_RELWITHDEBINFO_INIT "${_MD}${_Zi} /O2 /Ob1 /DNDEBUG")
./Platform/Windows-MSVC.cmake:      string(APPEND CMAKE_${lang}_FLAGS_MINSIZEREL_INIT "${_MD} /O1 /Ob1 /DNDEBUG")

@vszakats vszakats mentioned this pull request Oct 16, 2026
9 of 10 tasks
vszakats added a commit that referenced this pull request Oct 18, 2026
- in jobs building tests, also run them.
- to maintain CI cycle times, only run unit tests by default, and run
  all (supported) tests in just one job, carefully selected to be the
  one running them the fastest.
  Per job, it takes ~15 seconds to run ~80 unit tests.
  It takes 5 minutes to run all (~1600) tests. Total job time 5.5-6
  minutes.
  If the full test run turns out to be annoying or grow slow, we disable
  or limit it to a subset of tests.
- run tests with `-j10` parallelism. It results in the fastest runs,
  based on tests.
- to avoid the MSBuild test breaker heuristics, and to improve
  performance, run tests by directly invoking `runtests.pl` (not via
  CMake -> MSBuild -> `runtests.pl`).
  It saves 10-40 seconds per full test run.
- when running directly via `runtests.pl`, exclude test 1119 and 1167.
  It's not compatible with Visual Studio generator builds.
- run the artifact upload in parallel with running tests.

Running Windows tests in multiple environments makes it more likely they
can run on local machines, too, also making them easier to debug.
AppVeyor also supports remote access via RDP, which may help debugging
notorious Windows issues live, with some luck of reproducing it in a run
with RDP access explicitly enabled beforehand.

Fixes/improvements via this PR, merged separately:

dc99e12 #23467 processhelp.pm: improve task info in trace output (Windows)
70f3f85 #23471 test1488: drop unused C preprocessor logic
af9d2dd #23336 cmake: fix preconditions for option `CURL_STATIC_CRT=ON`
adf8b78 #23428 setopt: fix setting `CURLWS_NOAUTOPONG` with compilers lacking C99 bool type
780405b #23416 tests: add `--dump-module-paths` debug option (Windows)
bc22977 #23400 msvc: replace debug CRT GUI popups with stderr output
87a1dc7 #23396 appveyor: minor improvements
67f3510 #23381 runtests: add `-S` option to show success logs
e9af987 #23378 tests/server: fix crashing with VS2013 and earlier
8cdee8a #23377 lib557: exclude three floating point test cases for VS2013 and earlier
1aac64a #23375 test1119, 1167: disable MSYS2 argument conversion narrowly
49f003f #23337 unit1664: add workaround for VS2019 16.3 - VS2022 17.4 x64 miscompile
b8f71d6 #23345 test428: skip when running on older MSYS2 versions
2f3f99d #23339 test1119, 1167: disable MSYS2 argument conversion
098d3a0 #23332 runtests: unset `CURL_MEMDEBUG` for curl requests made to the CI API
d3e7caf #23335 GHA/windows: drop unused `-ac` runtests options
2874cc6 #23317 mqttd: work around bogus VS2019 32-bit compiler warning
e8adc72 #23315 schannel: fix guard for old MSVC workaround

Follow-up to 41198e0 #20390

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

Labels

build CI Continuous Integration cmdline tool tests Windows Windows-specific

Development

Successfully merging this pull request may close these issues.

2 participants