lkml.org 
[lkml]   [2021]   [Feb]   [22]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Subjectproblems with memory allocation and the alignment check
From
Date
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
\
 
 \ /
  Last update: 2021-02-22 10:34    [W:0.058 / U:17.624 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site