NO

Author Topic: Compiler bugs with PuTTY 0.79  (Read 5033 times)

Offline severach

  • Member
  • *
  • Posts: 3
Compiler bugs with PuTTY 0.79
« on: November 27, 2023, 06:26:50 PM »
I'm working on PuTTY 0.79. The large code base has revealed some compiler bugs in 12.00.2 and previous versions. Download and unpack the Unix sources into a Project folder 'src'. The single file examples run on unpatched sources. The project must be patched and renamed with the included shell script to compile.

Bug: IDE Project Manager
When adding .c files of the same name in different folders, the extra source files are considered a duplicate and not added to the project. An extra .obj file is added to the PPJ which is not visible in the IDE tree so can only be removed manually.
  Example: Add src\config.c and and src\windows\config.c to a project.

Either block duplicates in the OBJ listing or fix the IDE so it can take same names in different folders.

Bug: Compiler
PuTTY uses tertiary ?: in constant initializers. If the 3 items are constants it should evaluate at compile time as a constant but instead it outputs an error.
Code: [Select]
  aes.h(119)
#define AES_GCM_VTABLE(impl_c, impl_display, bits)                      \
        .ssh2_id = bits==192 ? NULL : "aes" #bits "-gcm@openssh.com",   \
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx86-coff -std:C17 -Zi -MT -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\crypto\aes-sw.c
src\crypto\aes-sw.c(1133): error #2069: Initializer for 'ssh_aes128_gcm_sw' must be constant.
src\crypto\aes-sw.c(1133): error #2069: Initializer for 'ssh_aes192_gcm_sw' must be constant.
src\crypto\aes-sw.c(1133): error #2069: Initializer for 'ssh_aes256_gcm_sw' must be constant.                           

  defs.h(218)
#define TYPECHECK(to_check, to_return)                  \
    (sizeof(to_check) ? (to_return) : (to_return))
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx86-coff -std:C17 -Zi -MT -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\utils\cert-expr.c
src\utils\cert-expr.c(217): error #2069: Initializer for 'operators' must be constant.

Bug: Windows Headers
Include sequence doesn't does not define CRYPTPROTECTMEMORY_BLOCK_SIZE found in dpapi.h
Code: [Select]
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx86-coff -std:C17 -Zi -MT -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\windows\utils\cryptoapi.c
src\windows\utils\cryptoapi.c(38): error #2048: Undeclared identifier 'CRYPTPROTECTMEMORY_BLOCK_SIZE' (did you mean: ?).
src\windows\utils\cryptoapi.c(66): error #2048: Undeclared identifier 'CRYPTPROTECTMEMORY_CROSS_PROCESS' (did you mean: ?).
Adding #include <dpapi.h> generates conflicts with wincrypt.h
Code: [Select]
c:\Program Files\PellesC\Include\Win\dpapi.h(48): error #2119: Redeclaration of '_CRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3400).
c:\Program Files\PellesC\Include\Win\dpapi.h(53): error #2119: Redeclaration of 'CRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3400).
c:\Program Files\PellesC\Include\Win\dpapi.h(53): error #2119: Redeclaration of 'PCRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3405).
c:\Program Files\PellesC\Include\Win\dpapi.h(74): error #2119: Redeclaration of 'CryptProtectData', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3795).
c:\Program Files\PellesC\Include\Win\dpapi.h(75): error #2119: Redeclaration of 'CryptUnprotectData', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3796).

Bug: Compiler
Optimize for speed and full debugging produces bad compile.
Code: [Select]
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx86-coff -std:C17 -Zi -Ot -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\windows\storage.c
src\windows\storage.c(846): error #3120: [asm] Symbol '@1854' is undefined.
src\windows\storage.c(846): error #3120: [asm] Symbol '@1853' is undefined.
src\windows\storage.c(846): error #3120: [asm] Symbol '@1852' is undefined.
src\windows\storage.c(846): error #3120: [asm] Symbol '@1851' is undefined.
Compiles fine with any of the following: Remove optimization -Ot. Remove debugging -Zi.

Bug: Compiler
x64+AVX2 does not provide intrinsic _addcarry_u64. Similar to _addcarryx_u64. Sources using _addcarry_u64 would require AVX2 if available. _addcarry_u64 wouldn't be needed if Pelles provided an __int128 and __SIZEOF_INT128__ type.

Bug:
x64 compiler halts with internal error. Compiler is running on an i7-2620M which does not support AVX2.
Code: [Select]
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx64-coff -arch:AVX2 -std:C17 -Zi -MT -Ot -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\ssh\transport2.c
fatal error: Internal error: get_rule_number().

Compiles fine with AVX or SSE2.

Bug: IDE Debugger
Unable to debug PuTTY project in 12.00.2. One debug format crashes on startup and the other won't leave functional breakpoints.
Linker Debug Information: "Pelles C format" or both crashes on debugger startup. Debugging works on sample project Hello so maybe the PuTTY project is too large.
Linker Debug Information: "COFF" enters the debugger after disabling all breakpoints. Breakpoints in breakpoint window don't work until the breakpoint is turned on in the debugger.

In 11.00.2 the debugger works with "Pelles C format". "COFF" works poorly as described above.

Bug: Compiler
Inline optimization for strcpy in 11.00.2 applies a single offset multiple times leading to wrong memory being written to. The same inline code in 10.00.6 works. The inline code in 12.00.2 is completely different, slower, and works. The bug may persist in 12.00.2 or the advanced code was disabled in 12.00.2 because the 11.00.2 code was so badly broken.
Enclosed is a project Demo-strcpy-inline that demonstrates an inline optimization bug.

Bug: IDE Editor
Find Next is grayed out when Find what is non Blank from dropdown.

Edit, Replace
Note that Find what is blank and Find Next is greyed out.
Select something in the Find what dropdown.
Note that the Find Next is still grayed out.
Type something in Find what and now Find Next is higlighted.
« Last Edit: November 27, 2023, 06:33:31 PM by severach »

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Compiler bugs with PuTTY 0.79
« Reply #1 on: November 28, 2023, 11:41:32 AM »
Probably the least of your concerns but this issue is one I previously reported:

Bug: IDE Editor
Find Next is grayed out when Find what is non Blank from dropdown.

Edit, Replace
Note that Find what is blank and Find Next is greyed out.
Select something in the Find what dropdown.
Note that the Find Next is still grayed out.
Type something in Find what and now Find Next is higlighted.

is from version 11 and is fixed in version 12.  If you are seeing it in version 12 you might need to reinstall.
I confirmed situation in both version 11 and version 12 today just to be sure.

John Z

https://forum.pellesc.de/index.php?topic=10486.0

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Compiler bugs with PuTTY 0.79
« Reply #2 on: November 28, 2023, 01:52:50 PM »
This looks to me to be an issue with the putty windows sources not Pelles C.  According to Micro$oft, CRYPTPROTECTMEMORY_BLOCK_SIZE is to be defined in dpapi.h.  Pelles C has it defined there.  No where in the putty windows sources is dpapi.h included.

Bug: Windows Headers
Include sequence doesn't does not define CRYPTPROTECTMEMORY_BLOCK_SIZE found in dpapi.h
Code: [Select]
"c:\Program Files\PellesC\Bin\pocc.exe" -Tx86-coff -std:C17 -Zi -MT -Ob1 -fp:fast -W1 -Gd -Ze -Zx -DHAVE_STRTOUMAX=1 -DHAVE_GCP_RESULTSW=1 -DAPPNAME=PuTTY -DSSH=1 -DSERIAL=1 -DOTHERBACKENDS=1 -I"c:\Program Files\PellesC\Include\Win" -I"c:\Program Files\PellesC\Include" -Isrc -Isrc\windows -Isrc\terminal src\windows\utils\cryptoapi.c
src\windows\utils\cryptoapi.c(38): error #2048: Undeclared identifier 'CRYPTPROTECTMEMORY_BLOCK_SIZE' (did you mean: ?).
src\windows\utils\cryptoapi.c(66): error #2048: Undeclared identifier 'CRYPTPROTECTMEMORY_CROSS_PROCESS' (did you mean: ?).
Adding #include <dpapi.h> generates conflicts with wincrypt.h
Code: [Select]
c:\Program Files\PellesC\Include\Win\dpapi.h(48): error #2119: Redeclaration of '_CRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3400).
c:\Program Files\PellesC\Include\Win\dpapi.h(53): error #2119: Redeclaration of 'CRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3400).
c:\Program Files\PellesC\Include\Win\dpapi.h(53): error #2119: Redeclaration of 'PCRYPTPROTECT_PROMPTSTRUCT', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3405).
c:\Program Files\PellesC\Include\Win\dpapi.h(74): error #2119: Redeclaration of 'CryptProtectData', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3795).
c:\Program Files\PellesC\Include\Win\dpapi.h(75): error #2119: Redeclaration of 'CryptUnprotectData', previously declared at c:\Program Files\PellesC\Include\Win\wincrypt.h(3796).

What "Windows header" are you expecting it to be in?

As a possible workaround to see what else is wrong try adding
#define CRYPTPROTECTMEMORY_BLOCK_SIZE  16
to cryptoapi.c which is the only place this constant is referenced.

John Z

Offline severach

  • Member
  • *
  • Posts: 3
Re: Compiler bugs with PuTTY 0.79
« Reply #3 on: November 28, 2023, 06:48:17 PM »
I'm doing all development in 11 until the debugger is fixed. Later bugs may be 11 only.

PuTTY compiles cryptoapi.c without modification for me MinGW CYGWin and MinGW MSYS2. Tatham compiles in MSVC, clang, LCC and probably others. I looked in CYGWin include/w32api and the only place that has that constant is dpapi.h. It is in the include sequence somewhere. In Pelles it's not. Include dpapi.h doesn't work so I must add the constants at the top to compile.

Code: (putty.0.79.0004.cryptoapi.CRYPTPROTECTMEMORY_BLOCK_SIZE.patch) [Select]
diff -pNaru5 src.20230826_034018/windows/utils/cryptoapi.c src/windows/utils/cryptoapi.c
--- src.20230826_034018/windows/utils/cryptoapi.c 2023-08-26 03:40:17.000000000 -0400
+++ src/windows/utils/cryptoapi.c 2023-11-23 00:39:13.312861000 -0500
@@ -2,15 +2,22 @@
  * windows/utils/cryptoapi.c: implementation of cryptoapi.h.
  */
 
 #include "putty.h"
 
-#include "putty.h"
 #include "ssh.h"
 
 #include "cryptoapi.h"
 
+#ifndef CRYPTPROTECTMEMORY_BLOCK_SIZE
+#define CRYPTPROTECTMEMORY_BLOCK_SIZE  16
+
+#define CRYPTPROTECTMEMORY_SAME_PROCESS  0x00
+#define CRYPTPROTECTMEMORY_CROSS_PROCESS  0x01
+#define CRYPTPROTECTMEMORY_SAME_LOGON  0x02
+#endif
+
 DEF_WINDOWS_FUNCTION(CryptProtectMemory);
 
 bool got_crypt(void)
 {
     static bool attempted = false;

Many includes are not for including directly but are embedded in other includes you should be using. In CYGWin include/w32api it is found at the bottom of wincrypt.h
Code: (wincrypt.h) [Select]
#ifndef _WINCRYPT_NO_DPAPI
#include <dpapi.h>
#endif

In Pelles include it isn't found anywhere.

Offline Vortex

  • Member
  • *
  • Posts: 864
    • http://www.vortex.masmcode.com
Re: Compiler bugs with PuTTY 0.79
« Reply #4 on: November 28, 2023, 07:50:09 PM »
Hi severach,

Did you try Frankie's updated SDK for PellesC?

https://github.com/Frankie-PellesC/fsdk
Code it... That's all...

Offline severach

  • Member
  • *
  • Posts: 3
Re: Compiler bugs with PuTTY 0.79
« Reply #5 on: November 29, 2023, 06:08:45 AM »
fSDK only got a few files in before errors.

Offline John Z

  • Member
  • *
  • Posts: 860
Re: Compiler bugs with PuTTY 0.79
« Reply #6 on: November 29, 2023, 10:58:14 AM »
OK thanks for finding the 'where'

I'm doing all development in 11 until the debugger is fixed. Later bugs may be 11 only.

You can still use the Find & Replace dropdowns and search fields int Pelles C 11 without re-typing the
whole entry.  If needed pick from the list then add and delete a space from the end of the search term.
This activates Find and Find Next without needing to reytpe everything.  Saves a bit of time and typing.

John Z