public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* problems with memory allocation and the alignment check
@ 2021-02-22  9:08 Michael J. Baars
  2021-02-22  9:29 ` Andrew Pinski
  0 siblings, 1 reply; 9+ messages in thread
From: Michael J. Baars @ 2021-02-22  9:08 UTC (permalink / raw)
  To: gcc, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 390 bytes --]

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.

[-- Attachment #2: main.c --]
[-- Type: text/x-csrc, Size: 862 bytes --]

#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);
}


[-- Attachment #3: makefile --]
[-- Type: text/x-makefile, Size: 102 bytes --]

all:

	gcc        -o main      main.c
	gcc -O2    -o mainO2    main.c
	gcc -Ofast -o mainOfast main.c

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2021-02-24  7:05 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-22  9:08 problems with memory allocation and the alignment check Michael J. Baars
2021-02-22  9:29 ` Andrew Pinski
2021-02-22  9:37   ` Michael J. Baars
2021-02-22  9:41     ` Andrew Pinski
2021-02-22 10:04       ` Michael J. Baars
2021-02-24  7:05       ` Michael J. Baars
2021-02-22  9:50     ` Gabriel Ravier
2021-02-22 10:05       ` Michael J. Baars
2021-02-23 11:46     ` David Laight

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).