NO

Author Topic: crt exception Strange Error  (Read 1195 times)

Offline milen_prg

  • Member
  • *
  • Posts: 6
crt exception Strange Error
« on: June 20, 2023, 04:09:45 PM »
I try to use this simple library:
https://github.com/erkkah/tigr
It consists only of 2 files: tigr.c and tigr.h. Works just fine with MSVC 2022.

But in Pelles C, the exe crashes with message in console:
CRT: unhandled exception (main) -- terminating

The window opens, but with dialog "tigr1.exe has stopped working" and console with the upper message.
Is there some ideas to make work with Pelles C?

P.S. I attach the example project folder.

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 176
    • Bcx Basic to C/C++ Translator
Re: crt exception Strange Error
« Reply #1 on: June 20, 2023, 04:57:33 PM »
That's an interesting graphics library.

I can only get a working Pelles C build when compiling for 32-bit.

Here is my Pelles project file:

Code: [Select]
#
# PROJECT FILE generated by "Pelles C for Windows, version 12.00".
# WARNING! DO NOT EDIT THIS FILE.
#

POC_PROJECT_VERSION = 9.00#
POC_PROJECT_TYPE = 3#
POC_PROJECT_MODE = Release#
POC_PROJECT_RESULTDIR = tigr1#
POC_PROJECT_OUTPUTDIR = output#
!if "$(POC_PROJECT_MODE)" == "Release"
CCFLAGS = -Tx86-coff -std:C17 -Ot -Ob1 -fp:precise -W1 -Gd -Ze#
ASFLAGS = -AIA32 -Gd#
RCFLAGS = -r#
LINKFLAGS = -machine:x86 -subsystem:console -safeseh kernel32.lib advapi32.lib delayimp.lib#
SIGNFLAGS = -location:CU -store:MY -timeurl:http://timestamp.verisign.com/scripts/timstamp.dll -errkill#
CC = pocc.exe#
AS = poasm.exe#
RC = porc.exe#
LINK = polink.exe#
SIGN = posign.exe#
INCLUDE = $(PellesCDir)\Include\Win;$(PellesCDir)\Include#
LIB = $(PellesCDir)\Lib\Win;$(PellesCDir)\Lib#
POC_PROJECT_EXECUTOR = #
POC_PROJECT_ARGUMENTS = #
POC_PROJECT_WORKPATH = .#
POC_PROJECT_ZIPEXTRA = #
!elseif "$(POC_PROJECT_MODE)" == "Debug"
CCFLAGS = -Tx86-coff -Ot -W1 -std:C17 -Gd -Zi#
ASFLAGS = -AIA32 -Gd -Zi#
RCFLAGS = -r#
LINKFLAGS = -machine:x86 -subsystem:console -safeseh -debug -debugtype:po kernel32.lib advapi32.lib delayimp.lib#
SIGNFLAGS = -location:CU -store:MY -timeurl:http://timestamp.verisign.com/scripts/timstamp.dll -errkill#
CC = pocc.exe#
AS = poasm.exe#
RC = porc.exe#
LINK = polink.exe#
SIGN = posign.exe#
INCLUDE = $(PellesCDir)\Include\Win;$(PellesCDir)\Include#
LIB = $(PellesCDir)\Lib\Win;$(PellesCDir)\Lib#
POC_PROJECT_EXECUTOR = #
POC_PROJECT_ARGUMENTS = #
POC_PROJECT_WORKPATH = .#
POC_PROJECT_ZIPEXTRA = #
!else
!error "Unknown mode."
!endif

#
# Build tigr1.exe.
#
tigr1\tigr1.exe: \
output\main.obj \
output\tigr.obj
$(LINK) $(LINKFLAGS) -out:"$@" $**

#
# Build main.obj.
#
output\main.obj: \
main.c \
tigr.h
$(CC) $(CCFLAGS) "$!" -Fo"$@"

#
# Build tigr.obj.
#
output\tigr.obj: \
tigr.c \
tigr.h
$(CC) $(CCFLAGS) "$!" -Fo"$@"

.SILENT:

Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline milen_prg

  • Member
  • *
  • Posts: 6
Re: crt exception Strange Error
« Reply #2 on: June 20, 2023, 05:59:45 PM »
@MrBcx, interesting, indeed the 32 bit compilation with Pelles C is fully successful even without any additional linker parameters!

Therefore, I think, this is some bug in the 64 bit version of the Pelles C.

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: crt exception Strange Error
« Reply #3 on: June 22, 2023, 11:32:45 AM »
This is an alignment problem for setjmp() buffer.
Quote
EDIT:
In my opinion this is a compiler bug relative to __declspec(align(16)).
After some checks I concluded that the situation is some more complicate. The __declspec seems to work on static memory, can't say on automatic memory because it is always aligned on 16bytes boundary. On the other hand in a dynamic allocation the compiler doesn't know what will be the real address after allocation. Now the chosen strategy here seems to be: Assume that the memory block is aligned on a memory boundary that is suitable for any object (C standard, defined in C17, section 7.22.3, paragraph 1), then displace the aligned object, the jmp_buf in our case, by padding a multiple of alignment starting from beginning of the memory block.
The point is that PellesC, differently from MSVC, returns malloced blocks on 8bytes bounds (to be honest always with final '8' in the address). See this older topic.
Now if the strategy is compliant with MSVC for the alignment it is wrong for allocation.
I can't say where the bug is, but sure something strange happens...
And in any case when a jmp_buf, or any special object that requires an alignment bigger than 8bytes (i.e. 16bytes), is used inside a structure dynamically allocated will fail in PellesC, but will work for other compilers (i.e. MSVC, GCC, LLVM).
The following is the definition of jmp_buf in setjmp.h:
Code: [Select]
typedef struct __declspec(align(16)) {
    unsigned long long data[2];
} jmp_buf[16];
For some reason the calloc returns an address that is always aligned on 8bytes boundary, then the compiler pads the structure s to align the jump buffer not on a 16bytes boundary, but just 16bytes above in memory.
Look the code:
Code: [Select]
    State* s = (State*)calloc(1, sizeof(State));
    ...
    if (setjmp(s->jmp) == 1) {     //Here the program crashes when compiled for 64 bits.
        free(s);
        return 0;
    }
Now look the attached image, it shows that the calloc() returns an address aligned on 8 bytes boundary, then the padding added to R14, that holds address of structure s, is 16bytes (0x10), but it should have been:
Code: [Select]
    LEA  RCX[R14+0x10-1]
    AND RCX, 0xFFFFFFFFFFFFFFF0
When the setjmp() function tries to save XMM registers (xmm6-xmm15) the alignement exception trigger the CPU.

Try to allocate on the stack the structure s, on the stack it is aligned on 16bytes boundary, and the program will work ((obviously you have to remove the free()).
In 32bits mode there are no registers that require 16bits alignment, or calloc returns memory aligned on 16bytes boundary.

As a workaround you have to use memory aligned on 16bytes boundary. You can use _mm_malloc() and _mm_free() to correctly allocate memory on a 16bytes boundary.
Specifically replace the function tigrInflate() with following code:
Code: [Select]
int tigrInflate(void* out, unsigned outlen, const void* in, unsigned inlen) {
    int last;
//    State* s = (State*)calloc(1, sizeof(State));
    State* s = (State*)_mm_malloc(sizeof(State), 16); //Allocate memory on 16bytes boundary
memset(s, 0, sizeof(State)); //Clean memory

    // We assume we can buffer 2 extra bytes from off the end of 'in'.
    s->in = (unsigned char*)in;
    s->inend = s->in + inlen + 2;
    s->out = (unsigned char*)out;
    s->outend = s->out + outlen;
    s->bits = 0;
    s->count = 0;
    bits(s, 0);

    if (setjmp(s->jmp) == 1) {
        //free(s);
        _mm_free(s);
        return 0;
    }

    do {
        last = bits(s, 1);
        switch (bits(s, 2)) {
            case 0:
                stored(s);
                break;
            case 1:
                fixed(s);
                block(s);
                break;
            case 2:
                dynamic(s);
                block(s);
                break;
            case 3:
                FAIL();
        }
    } while (!last);

    //free(s);
    _mm_free(s);
    return 1;
}
« Last Edit: June 26, 2023, 10:58:19 AM by frankie »
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline MrBcx

  • Global Moderator
  • Member
  • *****
  • Posts: 176
    • Bcx Basic to C/C++ Translator
Re: crt exception Strange Error
« Reply #4 on: June 22, 2023, 05:59:22 PM »
Frankie ... impressive bit of sleuthing ...

You should consider changing your username to SHERLOCK.   ;)
Bcx Basic to C/C++ Translator
https://www.BcxBasicCoders.com

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: crt exception Strange Error
« Reply #5 on: June 22, 2023, 09:15:19 PM »
Frankie ... impressive bit of sleuthing ...

You should consider changing your username to SHERLOCK.   ;)

Elementary my dear Watson...  ;D :D ;D :D ;)

Thanks
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide

Offline John Z

  • Member
  • *
  • Posts: 796
Re: crt exception Strange Error
« Reply #6 on: June 22, 2023, 10:35:30 PM »
Wow Frankie!

I second MrBcx nomination.  Great work and superb explanation and workaround!

John Z

Offline frankie

  • Global Moderator
  • Member
  • *****
  • Posts: 2096
Re: crt exception Strange Error
« Reply #7 on: June 23, 2023, 10:46:48 AM »
Thanks John  :)
It is better to be hated for what you are than to be loved for what you are not. - Andre Gide