C language > Expert questions

setjmp x64 crash

(1/1)

TimoVJL:
setjmp x64 crash if jmpenv isn't aligned.
problem came with malloc a struct.
should it just fail in that situation?
is it a bug?

frankie:
Hello Timo,
Hard to say.
in setjmp.h for an X64 target is is explicitly indicated that the buffer must be aligned on 16bits boundary:

--- Code: ---#elif __POCC_TARGET__ == 3

/* macros */
#define setjmp(env)  _setjmp((env),0)

/* type definitions */
typedef struct __declspec(align(16)) {
    unsigned long long data[2];
} jmp_buf[16];

...

--- End code ---

Maybe the problem is malloc family, that should supply memory on a boundary suitable, even if in the help Pelle specify that:

--- Quote ---The allocated space is guaranteed to be suitable aligned for all basic data types.

--- End quote ---
The workaround is to allocate almost 15 bytes more than required and supply to setjmp an adjusted pointer that is on a 16bits boundary.

TimoVJL:
This C11 function can be used instead:

--- Code: ---void * aligned_alloc(size_t alignment, size_t size);
--- End code ---
That problem was with TinyC libtcc.c code.
TinyC source works with gcc, msvc, ...

Navigation

[0] Message Index

Go to full version