problems with memory allocation and the alignment check
From: Michael J. Baars
Date: Mon Feb 22 2021 - 04:33:34 EST
Hi,
I just wrote this little program to demonstrate a possible flaw in both malloc and calloc.
If I allocate a the simplest memory region from main(), one out of three optimization flags fail.
If I allocate the same region from a function, three out of three optimization flags fail.
Does someone know if this really is a flaw, and if so, is it a gcc or a kernel flaw?
Regards,
Mischa.
#include <stdlib.h>
#include <stdint.h>
#define RFLAGS_REGISTER_GET(y) \
{ \
asm volatile \
( \
" pushfq \n" \
" pop %0 \n" \
\
: "=m" (* y) \
); \
};
#define RFLAGS_REGISTER_SET(x) \
{ \
asm volatile \
( \
" push %0 \n" \
" popfq \n" \
\
: \
: "r" (* x) \
); \
};
struct storage
{
uint8_t* c;
};
int function(struct storage* s)
{
s->c = calloc (sizeof(uint8_t), 8);
free (s->c);
};
int main()
{
struct storage s;
uint64_t rflags;
RFLAGS_REGISTER_GET(&rflags); rflags ^= 0x0000000000040000;
RFLAGS_REGISTER_SET(&rflags);
// function(&s);
s.c = calloc (sizeof(uint8_t), 8);
free (s.c);
RFLAGS_REGISTER_GET(&rflags); rflags ^= 0x0000000000040000;
RFLAGS_REGISTER_SET(&rflags);
}
all:
gcc -o main main.c
gcc -O2 -o mainO2 main.c
gcc -Ofast -o mainOfast main.c