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

Reply via email to